Chabao.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. protected $type = 'goods';
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->params = $this->request->post();
  29. if (empty($this->params['rwa_set'])) {
  30. if (empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
  31. $sign = '';
  32. foreach ($this->params as $key => $value) {
  33. if ($key != 'sign') $sign .= $value;
  34. }
  35. $sign .= Env::get('rwa.AppID');
  36. $sign = md5($sign);
  37. // if ($this->params['sign'] != $sign) $this->error('签名验签错误');
  38. }
  39. }
  40. /*
  41. * 判断登录
  42. */
  43. public function login(UserModel $userModel)
  44. {
  45. $user = $userModel::where('phone', $this->params['mobile'])->find();
  46. if (!$user) $this->error('账号不存在', [], 4000);
  47. Token::marshal($user['id'], $user['address']);
  48. $user['token'] = Token::getEncryptedToken($user['id']);
  49. $this->success('ok', $user);
  50. }
  51. /*
  52. * 注册
  53. */
  54. public function register(UserModel $userModel)
  55. {
  56. //手机号存在
  57. if ($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
  58. $newUserID = 0;
  59. Db::startTrans();
  60. try {
  61. //注册
  62. $time = time();
  63. $data = [
  64. 'phone' => $this->params['mobile'],
  65. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99), //188开头,时间戳去掉开头两位,再后面再加两位随机数
  66. 'avatar' => $this->request->domain() . '/assets/img/logo.png',
  67. 'create_time' => $time,
  68. 'team_level_id' => 0,
  69. ];
  70. // 创建用户
  71. $newUserID = $userModel->insertGetId($data);
  72. // 创建钱包
  73. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  74. Token::marshal($newUserID);
  75. // 提交事务
  76. Db::commit();
  77. } catch (Exception $e) {
  78. // 回滚事务
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. }
  82. if ($newUserID == 0) $this->error('注册失败');
  83. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  84. }
  85. //绑定地址
  86. public function bindAddress(UserModel $userModel)
  87. {
  88. $user = $userModel->getByAddress($this->params['address']);
  89. if (!$user) $this->error('钱包地址不存在');
  90. if ($user->phone) $this->error('钱包地址已被绑定');
  91. //修改手机号
  92. $user->phone = $this->params['mobile'];
  93. $user->save();
  94. $this->success('ok');
  95. }
  96. //转让茶宝
  97. public function chalink(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  98. {
  99. $user = $userModel->getByPhone($this->params['mobile']);
  100. if (!$user) $this->error('钱包地址不存在');
  101. //添加茶宝
  102. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $this->params['chalink'], LedgerTokenChangeModel::ChaLink);
  103. $this->success('ok');
  104. }
  105. //转入Teac
  106. public function teac(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  107. {
  108. $user = $userModel->getByPhone($this->params['mobile']);
  109. if (!$user) $this->error('钱包地址不存在');
  110. //添加Teac
  111. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $this->params['teac'], LedgerTeacAngelChangeModel::TransferIn);
  112. $this->success('ok');
  113. }
  114. //转入TeaC生态发展
  115. public function teacecology(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  116. {
  117. $user = $userModel->getByPhone($this->params['mobile']);
  118. if (!$user) $this->error('钱包地址不存在');
  119. //添加Teac
  120. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $this->params['teacecology'], LedgerTeacEcolyChangeModel::TransferIn);
  121. $this->success('ok');
  122. }
  123. //茶付宝消费金额达到指定区间,赠送商品
  124. public function give_rwa_goods_list()
  125. {
  126. $userModel = new UserModel();
  127. $order_model = new ProductOrder();
  128. $data = $this->params['data'];
  129. $this->type = $this->params['type'];
  130. switch ($this->type) {
  131. case 'local':
  132. $this->type='CFB_LOCAL_';
  133. break;
  134. default:
  135. $this->type='CFB_';
  136. break;
  137. }
  138. $mobile_list = array_column($data, 'mobile');
  139. $order_id_arr = array_column($data, 'order_id');
  140. $order_id_arr = array_map(function($item) {
  141. return $this->type.$item; // 自动将数字转为字符串
  142. }, $order_id_arr);
  143. $user = $userModel->whereIn('phone', $mobile_list)->field('phone,id')->select();
  144. $product_arr = $order_model->whereIn('order_id', $order_id_arr)->column('order_id');
  145. $add_data=[];
  146. $ret_data=[];
  147. foreach ($data as &$item) {
  148. $item_order_id=$this->type.$item['order_id'];
  149. if(!in_array($item_order_id,$product_arr)){
  150. foreach($user as &$item_1){
  151. if($item['mobile']==$item_1['phone']){
  152. $ret = $order_model->setGiverwagoods($this->type.$item['order_id'], 0, $item['product_id'], $item_1['id'], $order_model::Airdrop);
  153. $add_data[]=$ret;
  154. // if ($ret) {
  155. // $item['gift_record_type'] = 1;
  156. // $item['reason'] = '赠送RWA茶成功';
  157. // } else {
  158. // $item['gift_record_type'] = 0;
  159. // $item['reason'] = '赠送RWA茶失败';
  160. // }
  161. $item['gift_record_type'] = 1;
  162. $item['reason'] = '赠送RWA茶成功';
  163. break;
  164. }else{
  165. $item['gift_record_type'] = 2;
  166. $item['reason'] = '手机号未注册';
  167. }
  168. }
  169. $item['order_id']=(int)$item['order_id'];
  170. $ret_data[]=$item;
  171. }else{
  172. // $item['order_id']=(int)$item['order_id'];
  173. // $item['gift_record_type'] = 3;
  174. // $item['reason'] = '已经赠送';
  175. }
  176. }
  177. if(empty($user)&&!empty($ret_data)){
  178. foreach ($ret_data as &$item) {
  179. $item['gift_record_type'] = 2;
  180. $item['reason'] = '手机号未注册';
  181. }
  182. }
  183. try {
  184. $order_model->saveAll($add_data);
  185. } catch (Exception $e) {
  186. Log::write('赠送RWA茶失败:'.$e->getMessage(),'info');
  187. }
  188. unset($mobile_list);
  189. unset($user);
  190. $this->success('赠送RWA茶', $ret_data);
  191. }
  192. }