Chabao.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. Db::startTrans();
  126. try {
  127. $userModel = new UserModel();
  128. $order_model = new ProductOrder();
  129. $data = $this->params['data'];
  130. $mobile_list = array_column($data, 'mobile');
  131. $order_id_arr = array_column($data, 'order_id');
  132. $order_id_arr = array_map(function($item) {
  133. return 'CFB_' . $item; // 自动将数字转为字符串
  134. }, $order_id_arr);
  135. $user = $userModel->whereIn('phone', $mobile_list)->field('phone,id')->select();
  136. $product_arr = $order_model->whereIn('order_id', $order_id_arr)->column('order_id');
  137. $add_data=[];
  138. $ret_data=[];
  139. foreach ($data as &$item) {
  140. $item_order_id='CFB_'.$item['order_id'];
  141. if(!in_array($item_order_id,$product_arr)){
  142. foreach($user as &$item_1){
  143. if($item['mobile']==$item_1['phone']){
  144. $ret = $order_model->setGiverwagoods('CFB_'.$item['order_id'], 0, $item['product_id'], $item_1['id'], $order_model::Airdrop);
  145. $add_data[]=$ret;
  146. // if ($ret) {
  147. // $item['gift_record_type'] = 1;
  148. // $item['reason'] = '赠送RWA茶成功';
  149. // } else {
  150. // $item['gift_record_type'] = 0;
  151. // $item['reason'] = '赠送RWA茶失败';
  152. // }
  153. $item['gift_record_type'] = 1;
  154. $item['reason'] = '赠送RWA茶成功';
  155. break;
  156. }else{
  157. $item['gift_record_type'] = 2;
  158. $item['reason'] = '手机号未注册';
  159. }
  160. }
  161. $item['order_id']=(int)$item['order_id'];
  162. $ret_data[]=$item;
  163. }else{
  164. // $item['order_id']=(int)$item['order_id'];
  165. // $item['gift_record_type'] = 3;
  166. // $item['reason'] = '已经赠送';
  167. }
  168. }
  169. $order_model->saveAll($add_data);
  170. unset($mobile_list);
  171. unset($user);
  172. $this->success('赠送RWA茶', $ret_data);
  173. Db::commit();
  174. } catch (Exception $e) {
  175. // 回滚事务
  176. Db::rollback();
  177. Log::error($e->getMessage(),'赠送RWA失败');
  178. }
  179. }
  180. }