liyaozhi
2025-11-27 d729c91f610c8902fcec2444df6cac7b1c2e1002
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
<?php
 
namespace app\api\model\user;
 
use app\common\model\user\Grade as GradeModel;
use app\common\model\user\Sms as SmsModel;
use think\facade\Cache;
use app\common\model\user\User as UserModel;
 
/**
 * 用户模型类
 */
class UserWeb extends UserModel
{
    private $token;
 
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'open_id',
        'is_delete',
        'app_id',
        'create_time',
        'update_time'
    ];
 
    /**
     * 用户登录
     */
    public function login($data)
    {
        if(!$this->check($data)){
            return false;
        }
        $user = $this->where('mobile', '=', $data['mobile'])->find();
        if(!$user){
            $this->save([
                'mobile' => $data['mobile'],
                'reg_source' => 'h5',
                //默认等级
                'grade_id' => GradeModel::getDefaultGradeId(),
                'app_id' => self::$app_id
            ]);
            $user_id = $this['user_id'];
            $mobile = $data['mobile'];
        }else{
            $user_id = $user['user_id'];
            $mobile = $user['mobile'];
        }
        // 生成token (session3rd)
        $this->token = $this->token($mobile);
        // 记录缓存, 30天
        Cache::tag('cache')->set($this->token, $user_id, 86400 * 30);
        return $user_id;
    }
 
    /**
     * 验证
     */
    private function check($data)
    {
        //判断验证码是否过期、是否正确
        $sms_model = new SmsModel();
        $sms_record_list = $sms_model
            ->where('mobile', '=', $data['mobile'])
            ->order(['create_time' => 'desc'])
            ->limit(1)->select();
 
        if(count($sms_record_list) == 0){
            $this->error = '未查到短信发送记录';
            return false;
        }
        $sms_model = $sms_record_list[0];
        if((time() - strtotime($sms_model['create_time']))/60 > 30){
            $this->error = '短信验证码超时';
            return false;
        }
        if($sms_model['code'] != $data['code']){
            $this->error = '验证码不正确';
            return false;
        }
        return true;
    }
 
    /**
     * 获取token
     */
    public function getToken()
    {
        return $this->token;
    }
 
    /**
     * 绑定手机
     */
    public function bindMobile($user, $data){
        if(!$this->check($data)){
            return false;
        }
        return $user->save([
            'mobile' => $data['mobile']
        ]);
    }
 
    /**
     * 生成用户认证的token
     */
    private function token($openid)
    {
        $app_id = self::$app_id;
        // 生成一个不会重复的随机字符串
        $guid = \getGuidV4();
        // 当前时间戳 (精确到毫秒)
        $timeStamp = microtime(true);
        // 自定义一个盐
        $salt = 'token_salt';
        return md5("{$app_id}_{$timeStamp}_{$openid}_{$guid}_{$salt}");
    }
 
}