| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\api\controller;
- use app\common\model\ApiUser;
- use app\common\model\Orders;
- use app\common\model\OrdersRequestLog;
- use think\facade\Db;
- use think\Exception;
- use think\facade\Request;
- use think\Log;
- class Transaction extends Base
- {
- protected $contract_address = '0x55d398326f99059ff775485246999027b3197955';//交易币种的合约地址,默认为usdt的:0x55d398326f99059ff775485246999027b3197955,测试合约:0xcf3271b1be72f834e4dd35b9d558d583894473f1
- /**
- *
- *
- *
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function withdraw(){
- // $data = [
- // 'apiId' => '$api_key',
- // 'toAddress' => '$address',
- // 'coinName' => 'usdt',
- // 'coinType' => 'bsc',
- // 'amount' => '$mum',//提币数量
- // 'orderNo' => '$order_sn',//本平台的唯一订单号
- // 'timeStamp' => '$order_sn',//本平台的唯一订单号
- // 'sign' => '$order_sn'//本平台的唯一订单号
- // ];
- //获取post参数
- $params = Request::post();
- $api_user = (new ApiUser())
- ->where('api_id', $params['apiId'])
- ->find();
- if(empty($api_user)){
- $this->error('apiId有误');
- }
- $sign = $params['sign'];
- // "param" => "amount=2&apiId=1001&coinName=usdt&coinType=BEP20&orderNo=T2023&timeStamp=1700584041&toAddress=ox4545454545-&apiKey=123456"
- if($sign != md5(getSignKey($params, $api_user['api_key']))){
- dump($sign);
- dump(md5(getSignKey($params, $api_user['api_key'])));
- $this->error('签名错误');
- }
- $check_order = (new Orders())
- ->where('api_id', $params['apiId'])
- ->where('user_order_no', $params['orderNo'])
- ->count();
- if($check_order){
- $this->error('该订单号已存在,请勿重复提交');
- }
- // 启动事务
- Db::startTrans();
- try {
- // 生成订单
- $order_id = (new Orders())
- ->insertGetId([
- 'api_id' => $params['apiId'],
- 'order_no' => $params['apiId'] . '-' . $params['orderNo'],
- 'user_order_no' => $params['orderNo'],
- 'to_address' => $params['toAddress'],
- 'amount' => $params['amount'],
- 'coin_type' => 'BEP20',
- 'coin_name' => 'usdt',
- 'create_time' => date('Y-m-d H:i:s')
- ]);
- $rs_id = (new OrdersRequestLog())
- ->save([
- 'order_id' => $order_id,
- 'ip' => $_SERVER['REMOTE_ADDR'],
- 'url' => $this->request->host(),
- 'params' => json_encode($params),
- ]);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('提交成功');
- }
- public function getTxhashDetail(){
- $tx_hash = Request::post('tx_hash');
- $api_url = 'http://127.0.0.1:3389/api/txDetail';
- if(empty($tx_hash)){
- $this->error('参数有误');
- }
- $data = [
- 'tx_hash' => $tx_hash,
- ];
- $body = $this->doCurlPostRequest($api_url, $data);
- //dump($body);
- $info = json_decode($body, true);
- //dump($body);
- //$body = $this->getInfoByTransactionHash($txhash);
- if($info['code'] == 200){
- $this->success('', $info['data']);
- }
- $this->error($info['message']);
- }
- public function createAddress(){
- $tx_hash = Request::post('tx_hash');
- $api_url = 'http://127.0.0.1:3389/api/txDetail';
- if(empty($tx_hash)){
- $this->error('参数有误');
- }
- $data = [
- 'api_id' => 1,
- 'coin_type' => 'BEP20',
- 'user_no' => 1,
- 'time_stamp' => 1122,
- 'reset' => true
- ];
- $body = $this->doCurlPostRequest($api_url, $data);
- //dump($body);
- $info = json_decode($body, true);
- //dump($body);
- //$body = $this->getInfoByTransactionHash($txhash);
- if($info['code'] == 200){
- $this->success('', $info['data']);
- }
- $this->error($info['message']);
- }
- function doCurlPostRequest($url = '',Array $data = array())
- {
- $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
- // $data_string = $data;
- $curl_con = curl_init();
- curl_setopt($curl_con, CURLOPT_URL,$url);
- curl_setopt($curl_con, CURLOPT_HEADER, false);
- curl_setopt($curl_con, CURLOPT_POST, true);
- curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($curl_con, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data_string))
- );
- curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string);
- $res = curl_exec($curl_con);
- $status = curl_getinfo($curl_con);
- curl_close($curl_con);
- if (isset($status['http_code']) && $status['http_code'] == 200) {
- return $res;
- } else {
- return FALSE;
- }
- }
- }
|