model = new UserModel(); } /** * 注册或更新用户 * @param string $address * @param int $parentID * @return void * @throws Exception */ private function insertOrUpdateData(string $address, int $parentID = 0) { if ($parentID > 0) { $parent = $this->model->getById($parentID); if (empty($parent)) throw new Exception('邀请人不存在'); } $user = $this->model->getByAddress($address); $ledgerWalletModel = new LedgerWalletModel(); if (!empty($user)) { // 已存在 if ($user['parent_id'] == 0) { // 绑定上级 $this->model->save(['parent_id' => $parentID], ['id' => $user['id']]); if ($parentID > 0) { // 创建网体 (new UserPathModel())->createPath($user['id'], $parentID); //添加推荐人奖励 //$ledgerWalletModel->changeWalletAccount($parentID, Asset::TOKEN, getConfig('direct_income'), $ledgerWalletModel::Share, $user['id']); } } } else { // 不存在 $time = time(); $data = [ 'address' => $address, 'parent_id' => $parentID, 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数 'avatar' => $this->request->domain().'/assets/img/logo.png', 'create_time' => $time, 'team_level_id' => 0, ]; // 创建用户 $newUserID = $this->model->insertGetId($data); // 创建钱包 (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]); if ($parentID > 0) { // 创建网体 (new UserPathModel())->createPath($newUserID, $parentID); //推荐人奖励 //$ledgerWalletModel->changeWalletAccount($parentID, Asset::TOKEN, getConfig('direct_income'), $ledgerWalletModel::Share, $newUserID); } } } /** * 用户信息/登录 * @return void */ public function auth() { $address = $this->request->post('address'); // 地址 //$sign = $this->request->post('sign'); // 签名信 $inviteCode = $this->request->post('inviteCode'); // 邀请码 $sign = 'register'; if (empty($address) || empty($sign)) { $this->error(__('Invalid parameters')); } $address = strtolower($address); // 转小写 $inviteCode = strtolower($inviteCode); // 转小写 $msg = ''; Db::startTrans(); try { // 不存在时注册 $user = (new UserModel())->getByAddress($address); if (empty($user)) { $this->insertOrUpdateData($address); // 半注册,上级ID为0 $user = (new UserModel())->getByAddress($address); } // 尝试绑定上级 if (empty($user['parent_id'])) { // 未绑定时 if (empty($inviteCode)) { $msg = __('Please bind your superior first'); } else if ($inviteCode == $address) { $msg = __('The recommender cannot be yourself please re-bind to your superior'); } else { $parent = (new UserModel())->getByAddress($inviteCode); if (empty($parent)) { // 邀请码有效时 $msg = __('The invitation code is invalid, please manually bind your superior'); } else { $this->insertOrUpdateData($address, $parent['id']); $user = (new UserModel())->getByAddress($address); } } } if($user['is_login']){ throw new Exception(__('Your account has been locked please contact customer service')); } Token::marshal($user['id']); $user['token'] = Token::getEncryptedToken($user['id']); Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage()); } $this->success($msg, $user); } /** * 绑定上级 * @return void */ public function bind() { $mobile = $this->request->post('mobile'); // 手机号 $inviteCode = $this->request->post('inviteCode'); // 邀请码 if (empty($mobile) || empty($inviteCode)) $this->error(__('Invalid parameters')); $inviteCode = strtolower($inviteCode); // 转小写 $msg = ''; Db::startTrans(); try { // 不存在时注册 $user = (new UserModel())->getByMobile($mobile); if (empty($user)) throw new Exception(__('账号不存在')); // 尝试绑定上级 if (empty($user['parent_id'])) { $parent = (new UserModel())->getByAddress($inviteCode); if (empty($parent)) throw new Exception(__('The invitation code is invalid, please manually bind your superior')) ; // 创建网体 (new UserPathModel())->createPath($user['id'], $parent->id); //添加推荐人奖励 $user->parent_id = $parent->id; $user->save(); } Token::marshal($user['id']); $user['token'] = Token::getEncryptedToken($user['id']); Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage()); } $this->success('ok', $user); } }