Chabao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //茶宝Api
  18. class Chabao extends Api
  19. {
  20. protected array $noNeedLogin = ['*'];
  21. protected $params = [];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->params = $this->request->post();
  26. if(empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
  27. $sign = '';
  28. foreach ($this->params as $key => $value) {
  29. if($key != 'sign') $sign .= $value;
  30. }
  31. $sign .= Env::get('rwa.AppID');
  32. $sign = md5($sign);
  33. if($this->params['sign'] != $sign) $this->error('签名验签错误');
  34. }
  35. /*
  36. * 判断登录
  37. */
  38. public function login(UserModel $userModel)
  39. {
  40. $user = $userModel::where('phone', $this->params['mobile'])->find();
  41. if(!$user) $this->error('账号不存在', [], 4000);
  42. Token::marshal($user['id'], $user['address']);
  43. $user['token'] = Token::getEncryptedToken($user['id']);
  44. $this->success('ok', $user);
  45. }
  46. /*
  47. * 注册
  48. */
  49. public function register(UserModel $userModel)
  50. {
  51. //手机号存在
  52. if($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
  53. $newUserID = 0;
  54. Db::startTrans();
  55. try {
  56. //注册
  57. $time = time();
  58. $data = [
  59. 'phone' => $this->params['mobile'],
  60. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
  61. 'avatar' => $this->request->domain().'/assets/img/logo.png',
  62. 'create_time' => $time,
  63. 'team_level_id' => 0,
  64. ];
  65. // 创建用户
  66. $newUserID = $userModel->insertGetId($data);
  67. // 创建钱包
  68. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  69. Token::marshal($newUserID);
  70. // 提交事务
  71. Db::commit();
  72. } catch (Exception $e) {
  73. // 回滚事务
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. if($newUserID == 0) $this->error('注册失败');
  78. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  79. }
  80. //绑定地址
  81. public function bindAddress(UserModel $userModel)
  82. {
  83. $user = $userModel->getByAddress($this->params['address']);
  84. if(!$user) $this->error('钱包地址不存在');
  85. if($user->phone) $this->error('钱包地址已被绑定');
  86. //修改手机号
  87. $user->phone = $this->params['mobile'];
  88. $user->save();
  89. $this->success('ok');
  90. }
  91. //转让茶宝
  92. public function chalink(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  93. {
  94. $user = $userModel->getByPhone($this->params['mobile']);
  95. if(!$user) $this->error('钱包地址不存在');
  96. //添加茶宝
  97. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $this->params['chalink'], LedgerTokenChangeModel::ChaLink);
  98. $this->success('ok');
  99. }
  100. //转入Teac
  101. public function teac(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  102. {
  103. $user = $userModel->getByPhone($this->params['mobile']);
  104. if(!$user) $this->error('钱包地址不存在');
  105. //添加Teac
  106. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $this->params['teac'], LedgerTeacAngelChangeModel::TransferIn);
  107. $this->success('ok');
  108. }
  109. //转入TeaC生态发展
  110. public function teacecology(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  111. {
  112. $user = $userModel->getByPhone($this->params['mobile']);
  113. if(!$user) $this->error('钱包地址不存在');
  114. //添加Teac
  115. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $this->params['teacecology'], LedgerTeacEcolyChangeModel::TransferIn);
  116. $this->success('ok');
  117. }
  118. }