Chabao.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\CommonLogic;
  4. use app\common\controller\Api;
  5. use app\api\logic\TeacLogin;
  6. use app\common\model\LedgerTeacChangeModel;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\UserTeac;
  9. use app\common\model\LedgerWalletModel;
  10. use think\Log;
  11. use fast\Asset;
  12. use Exception;
  13. use app\common\model\UserModel;
  14. use think\Db;
  15. use app\api\validate\Chabao as ChabaoValidate;
  16. use app\common\library\Token;
  17. use think\Env;
  18. //茶宝Api
  19. class Chabao extends Api
  20. {
  21. protected array $noNeedLogin = ['*'];
  22. protected $params = [];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->params = $this->request->post();
  27. if(empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
  28. $sign = '';
  29. foreach ($this->params as $key => $value) {
  30. if($key != 'sign') $sign .= $value;
  31. }
  32. $sign .= Env::get('rwa.AppID');
  33. $sign = md5($sign);
  34. if($this->params['sign'] != $sign) $this->error('签名验签错误');
  35. }
  36. /*
  37. * 判断登录
  38. */
  39. public function login(UserModel $userModel)
  40. {
  41. $user = $userModel::where('mobile', $this->params['mobile'])->find();
  42. if(!$user) $this->error('账号不存在', [], 4000);
  43. Token::marshal($user['id'], $user['address']);
  44. $user['token'] = Token::getEncryptedToken($user['id']);
  45. $this->success('ok', $user);
  46. }
  47. /*
  48. * 注册
  49. */
  50. public function register(UserModel $userModel)
  51. {
  52. //手机号存在
  53. if($userModel::where('mobile', $this->params['mobile'])->find()) $this->error('账号已存在');
  54. $newUserID = 0;
  55. Db::startTrans();
  56. try {
  57. //注册
  58. $time = time();
  59. $data = [
  60. 'mobile' => $this->params['mobile'],
  61. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
  62. 'avatar' => $this->request->domain().'/assets/img/logo.png',
  63. 'create_time' => $time,
  64. 'team_level_id' => 0,
  65. ];
  66. // 创建用户
  67. $newUserID = $userModel->insertGetId($data);
  68. // 创建钱包
  69. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  70. Token::marshal($newUserID);
  71. // 提交事务
  72. Db::commit();
  73. } catch (Exception $e) {
  74. // 回滚事务
  75. Db::rollback();
  76. $this->error($e->getMessage());
  77. }
  78. if($newUserID == 0) $this->error('注册失败');
  79. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  80. }
  81. //绑定地址
  82. public function bindAddress(UserModel $userModel)
  83. {
  84. $user = $userModel->getByAddress($this->params['address']);
  85. if(!$user) $this->error('钱包地址不存在');
  86. if($user->mobile) $this->error('钱包地址已被绑定');
  87. //修改手机号
  88. $user->mobile = $this->params['mobile'];
  89. $user->save();
  90. $this->success('ok');
  91. }
  92. //取消交易
  93. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  94. {
  95. $id = $this->request->post('teac_id/d', 0);
  96. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  97. if(empty($row)) $this->error('订单不存在');
  98. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  99. Db::startTrans();
  100. try {
  101. //返回相应茶宝Teac
  102. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  103. $row->frozen = 0;
  104. $row->status = ProductTeac::Closure;
  105. $row->save();
  106. // 提交事务
  107. Db::commit();
  108. } catch (Exception $e) {
  109. // 回滚事务
  110. Db::rollback();
  111. return $e->getMessage();
  112. }
  113. $this->success('ok');
  114. }
  115. }