Member.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Token;
  5. use app\common\model\LedgerWalletModel;
  6. use app\common\model\UserModel;
  7. use app\common\model\UserPathModel;
  8. use fast\Asset;
  9. use think\Config;
  10. use think\Db;
  11. use think\Exception;
  12. use think\Hook;
  13. use think\Validate;
  14. use function fast\e;
  15. /**
  16. * 会员接口
  17. */
  18. class Member extends Api
  19. {
  20. protected $model = null;
  21. protected array $noNeedLogin = ['register', 'auth'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new UserModel();
  26. }
  27. /**
  28. * 注册或更新用户
  29. * @param string $address
  30. * @param int $parentID
  31. * @return void
  32. * @throws Exception
  33. */
  34. private function insertOrUpdateData(string $address, int $parentID = 0)
  35. {
  36. if ($parentID > 0) {
  37. $parent = $this->model->getById($parentID);
  38. if (empty($parent)) throw new Exception('邀请人不存在');
  39. }
  40. $user = $this->model->getByAddress($address);
  41. $ledgerWalletModel = new LedgerWalletModel();
  42. if (!empty($user)) { // 已存在
  43. if ($user['parent_id'] == 0) {
  44. // 绑定上级
  45. $this->model->save(['parent_id' => $parentID], ['id' => $user['id']]);
  46. if ($parentID > 0) {
  47. // 创建网体
  48. (new UserPathModel())->createPath($user['id'], $parentID);
  49. //添加推荐人奖励
  50. //$ledgerWalletModel->changeWalletAccount($parentID, Asset::TOKEN, getConfig('direct_income'), $ledgerWalletModel::Share, $user['id']);
  51. }
  52. }
  53. } else { // 不存在
  54. $time = time();
  55. $data = [
  56. 'address' => $address,
  57. 'parent_id' => $parentID,
  58. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
  59. 'avatar' => $this->request->domain().'/assets/img/logo.png',
  60. 'create_time' => $time,
  61. 'team_level_id' => 0,
  62. ];
  63. // 创建用户
  64. $newUserID = $this->model->insertGetId($data);
  65. // 创建钱包
  66. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  67. if ($parentID > 0) {
  68. // 创建网体
  69. (new UserPathModel())->createPath($newUserID, $parentID);
  70. //推荐人奖励
  71. //$ledgerWalletModel->changeWalletAccount($parentID, Asset::TOKEN, getConfig('direct_income'), $ledgerWalletModel::Share, $newUserID);
  72. }
  73. }
  74. }
  75. /**
  76. * 用户信息/登录
  77. * @return void
  78. */
  79. public function auth()
  80. {
  81. $address = $this->request->post('address'); // 地址
  82. //$sign = $this->request->post('sign'); // 签名信
  83. $inviteCode = $this->request->post('inviteCode'); // 邀请码
  84. $sign = 'register';
  85. if (empty($address) || empty($sign)) {
  86. $this->error(__('Invalid parameters'));
  87. }
  88. $address = strtolower($address); // 转小写
  89. $inviteCode = strtolower($inviteCode); // 转小写
  90. $msg = '';
  91. Db::startTrans();
  92. try {
  93. // 不存在时注册
  94. $user = (new UserModel())->getByAddress($address);
  95. if (empty($user)) {
  96. $this->insertOrUpdateData($address); // 半注册,上级ID为0
  97. $user = (new UserModel())->getByAddress($address);
  98. }
  99. // 尝试绑定上级
  100. if (empty($user['parent_id'])) { // 未绑定时
  101. if (empty($inviteCode)) {
  102. $msg = __('Please bind your superior first');
  103. } else if ($inviteCode == $address) {
  104. $msg = __('The recommender cannot be yourself please re-bind to your superior');
  105. } else {
  106. $parent = (new UserModel())->getByAddress($inviteCode);
  107. if (empty($parent)) { // 邀请码有效时
  108. $msg = __('The invitation code is invalid, please manually bind your superior');
  109. } else {
  110. $this->insertOrUpdateData($address, $parent['id']);
  111. $user = (new UserModel())->getByAddress($address);
  112. }
  113. }
  114. }
  115. if($user['is_login']){
  116. throw new Exception(__('Your account has been locked please contact customer service'));
  117. }
  118. Token::marshal($user['id'], $user['address']);
  119. $user['token'] = Token::getEncryptedToken($user['id']);
  120. Db::commit();
  121. } catch (Exception $e) {
  122. // 回滚事务
  123. Db::rollback();
  124. $this->error( $e->getMessage());
  125. }
  126. $this->success($msg, $user);
  127. }
  128. }