| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Token;
- use app\common\model\LedgerWalletModel;
- use app\common\model\UserModel;
- use app\common\model\UserPathModel;
- use fast\Asset;
- use think\Config;
- use think\Db;
- use think\Exception;
- use think\Hook;
- use think\Validate;
- use function fast\e;
- /**
- * 会员接口
- */
- class Member extends Api
- {
- protected $model = null;
- protected array $noNeedLogin = ['register', 'auth'];
- public function _initialize()
- {
- parent::_initialize();
- $this->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['address']);
- $user['token'] = Token::getEncryptedToken($user['id']);
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error( $e->getMessage());
- }
- $this->success($msg, $user);
- }
- }
|