Chabao.php 8.0 KB

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