Chabao.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\LedgerTeacAngelChangeModel;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\LedgerWalletModel;
  9. use think\Log;
  10. use fast\Asset;
  11. use Exception;
  12. use app\common\model\UserModel;
  13. use think\Db;
  14. use app\common\model\LedgerTeacEcolyChangeModel;
  15. use app\common\library\Token;
  16. use think\Env;
  17. use app\common\model\ProductOrder;
  18. use function PHPSTORM_META\type;
  19. //茶宝Api
  20. class Chabao extends Api
  21. {
  22. protected array $noNeedLogin = ['*'];
  23. protected $params = [];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->params = $this->request->post();
  28. if (empty($this->params['rwa_set'])) {
  29. if (empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
  30. $sign = '';
  31. foreach ($this->params as $key => $value) {
  32. if ($key != 'sign') $sign .= $value;
  33. }
  34. $sign .= Env::get('rwa.AppID');
  35. $sign = md5($sign);
  36. if ($this->params['sign'] != $sign) $this->error('签名验签错误');
  37. }
  38. }
  39. /*
  40. * 判断登录
  41. */
  42. public function login(UserModel $userModel)
  43. {
  44. $user = $userModel::where('phone', $this->params['mobile'])->find();
  45. if (!$user) $this->error('账号不存在', [], 4000);
  46. Token::marshal($user['id'], $user['address']);
  47. $user['token'] = Token::getEncryptedToken($user['id']);
  48. $this->success('ok', $user);
  49. }
  50. /*
  51. * 注册
  52. */
  53. public function register(UserModel $userModel)
  54. {
  55. //手机号存在
  56. if ($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
  57. $newUserID = 0;
  58. Db::startTrans();
  59. try {
  60. //注册
  61. $time = time();
  62. $data = [
  63. 'phone' => $this->params['mobile'],
  64. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99), //188开头,时间戳去掉开头两位,再后面再加两位随机数
  65. 'avatar' => $this->request->domain() . '/assets/img/logo.png',
  66. 'create_time' => $time,
  67. 'team_level_id' => 0,
  68. ];
  69. // 创建用户
  70. $newUserID = $userModel->insertGetId($data);
  71. // 创建钱包
  72. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  73. Token::marshal($newUserID);
  74. // 提交事务
  75. Db::commit();
  76. } catch (Exception $e) {
  77. // 回滚事务
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. }
  81. if ($newUserID == 0) $this->error('注册失败');
  82. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  83. }
  84. //绑定地址
  85. public function bindAddress(UserModel $userModel)
  86. {
  87. $user = $userModel->getByAddress($this->params['address']);
  88. if (!$user) $this->error('钱包地址不存在');
  89. if ($user->phone) $this->error('钱包地址已被绑定');
  90. //修改手机号
  91. $user->phone = $this->params['mobile'];
  92. $user->save();
  93. $this->success('ok');
  94. }
  95. //转让茶宝
  96. public function chalink(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  97. {
  98. $user = $userModel->getByPhone($this->params['mobile']);
  99. if (!$user) $this->error('钱包地址不存在');
  100. //添加茶宝
  101. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $this->params['chalink'], LedgerTokenChangeModel::ChaLink);
  102. $this->success('ok');
  103. }
  104. //转入Teac
  105. public function teac(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  106. {
  107. $user = $userModel->getByPhone($this->params['mobile']);
  108. if (!$user) $this->error('钱包地址不存在');
  109. //添加Teac
  110. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $this->params['teac'], LedgerTeacAngelChangeModel::TransferIn);
  111. $this->success('ok');
  112. }
  113. //转入TeaC生态发展
  114. public function teacecology(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  115. {
  116. $user = $userModel->getByPhone($this->params['mobile']);
  117. if (!$user) $this->error('钱包地址不存在');
  118. //添加Teac
  119. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $this->params['teacecology'], LedgerTeacEcolyChangeModel::TransferIn);
  120. $this->success('ok');
  121. }
  122. //茶付宝消费金额达到指定区间,赠送商品
  123. public function give_rwa_goods_list()
  124. {
  125. $userModel = new UserModel();
  126. $order_model = new ProductOrder();
  127. $data = $this->params['data'];
  128. $mobile_list = array_column($data, 'mobile');
  129. $user = $userModel->whereIn('phone', $mobile_list)->field('phone,id')->select();
  130. foreach ($data as &$item) {
  131. $item['gift_record_type'] = 2;
  132. $item['reason'] = '手机号未注册';
  133. foreach($user as &$item_1){
  134. if($item['mobile']==$item_1['phone']){
  135. $ret = $order_model->setGiverwagoods(0, 0, $item['product_id'], $item_1['id'], $order_model::Giveaway);
  136. if ($ret) {
  137. $item['gift_record_type'] = 1;
  138. $item['reason'] = '赠送RWA茶成功';
  139. } else {
  140. $item['gift_record_type'] = 0;
  141. $item['reason'] = '赠送RWA茶失败';
  142. }
  143. break;
  144. }
  145. }
  146. }
  147. unset($mobile_list);
  148. unset($user);
  149. $this->success('赠送RWA茶', $data);
  150. }
  151. }