quanwei
2025-12-13 30563323a53b0d0260c97d08a9e8bd4cc8227a95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
 
namespace app\api\model\user;
 
use app\api\model\plus\agent\Referee as RefereeModel;
use app\common\model\user\Grade as GradeModel;
use think\facade\Cache;
use app\common\exception\BaseException;
use app\common\model\user\User as UserModel;
 
/**
 * 公众号用户模型类
 */
class UserMp extends UserModel
{
    private $token;
 
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'open_id',
        'is_delete',
        'app_id',
        'create_time',
        'update_time'
    ];
 
    /**
     * 用户登录
     */
    public function login($userInfo, $referee_id = null)
    {
        $userInfo = $userInfo['original'];
        // 自动注册用户
        $user_id = $this->register($userInfo, $referee_id);
        // 生成token (session3rd)
        $this->token = $this->token($userInfo['openid']);
        // 记录缓存, 7天
        Cache::set($this->token, $user_id, 86400 * 7);
        return $user_id;
    }
 
    /**
     * 获取token
     */
    public function getToken()
    {
        return $this->token;
    }
 
    /**
     * 生成用户认证的token
     */
    private function token($openid)
    {
        return md5($openid . 'token_salt');
    }
 
    /**
     * 自动注册用户
     */
    private function register($userInfo, $referee_id = null)
    {
        $data = [];
        //通过unionid查询用户是否存在
        $user = null;
        $data['union_id'] = '';
        if(isset($userInfo['unionid']) && !empty($userInfo['unionid'])){
            $data['union_id'] = $userInfo['unionid'];
            $user = self::detailByUnionid($userInfo['unionid']);
        }
        // 查询用户是否已存在
        if(!$user){
            $user = self::detail(['mpopen_id' => $userInfo['openid']]);
        }
        if($user){
            $model = $user;
            // 只修改union_id
            $data = [
                'union_id' => $data['union_id'],
            ];
        }else{
            $model = $this;
            $data['referee_id'] = $referee_id;
            $data['reg_source'] = 'mp';
            // 用户数据
            $data['mpopen_id'] = $userInfo['openid'];
            $data['nickName'] =  $userInfo['nickname'];
            $data['avatarUrl'] = $userInfo['headimgurl'];
            $data['gender'] = $userInfo['sex'];
            $data['province'] = $userInfo['province'];
            $data['country'] = $userInfo['country'];
            $data['city'] = $userInfo['city'];
            //默认等级
            $data['grade_id'] = GradeModel::getDefaultGradeId();
        }
 
        try {
            $this->startTrans();
            // 保存/更新用户记录
            if (!$model->save(array_merge($data, [
                'app_id' => self::$app_id
            ]))
            ) {
                throw new BaseException(['msg' => '用户注册失败']);
            }
            if (!$user && $referee_id > 0) {
                // 记录推荐人关系,
                RefereeModel::createRelation($model['user_id'], $referee_id);
                //更新用户邀请数量
                (new UserModel())->setIncInvite($referee_id);
            }
            $this->commit();
        } catch (\Exception $e) {
            $this->rollback();
            throw new BaseException(['msg' => $e->getMessage()]);
        }
        return $model['user_id'];
    }
 
}