PublicController.php
5.4 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\wxapp\controller;
use think\Db;
use cmf\controller\RestBaseController;
use wxapp\aes\WXBizDataCrypt;
use think\Validate;
class PublicController extends RestBaseController
{
// 微信小程序用户登录 TODO 增加最后登录信息记录,如 ip
public function login()
{
$validate = new Validate([
'code' => 'require',
'encrypted_data' => 'require',
'iv' => 'require',
'raw_data' => 'require',
'signature' => 'require',
]);
$validate->message([
'code.require' => '缺少参数code!',
'encrypted_data.require' => '缺少参数encrypted_data!',
'iv.require' => '缺少参数iv!',
'raw_data.require' => '缺少参数raw_data!',
'signature.require' => '缺少参数signature!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$code = $data['code'];
$wxappSettings = cmf_get_option('wxapp_settings');
$appId = $this->request->header('XX-Wxapp-AppId');
if (empty($appId)) {
if (empty($wxappSettings['default'])) {
$this->error('没有设置默认小程序!');
} else {
$defaultWxapp = $wxappSettings['default'];
$appId = $defaultWxapp['app_id'];
$appSecret = $defaultWxapp['app_secret'];
}
} else {
if (empty($wxappSettings['wxapps'][$appId])) {
$this->error('小程序设置不存在!');
} else {
$appId = $wxappSettings['wxapps'][$appId]['app_id'];
$appSecret = $wxappSettings['wxapps'][$appId]['app_secret'];
}
}
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
$response = json_decode($response, true);
if (!empty($response['errcode'])) {
$this->error('操作失败!');
}
$openid = $response['openid'];
$sessionKey = $response['session_key'];
$pc = new WXBizDataCrypt($appId, $sessionKey);
$errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
if ($errCode != 0) {
$this->error('操作失败!');
}
$findThirdPartyUser = Db::name("third_party_user")
->where('openid', $openid)
->where('app_id', $appId)
->find();
$currentTime = time();
$ip = $this->request->ip(0, true);
$wxUserData['sessionKey'] = $sessionKey;
unset($wxUserData['watermark']);
if ($findThirdPartyUser) {
$userId = $findThirdPartyUser['user_id'];
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'wxapp');
$userData = [
'last_login_ip' => $ip,
'last_login_time' => $currentTime,
'login_times' => Db::raw('login_times+1'),
'more' => json_encode($wxUserData)
];
if (isset($wxUserData['unionId'])) {
$userData['union_id'] = $wxUserData['unionId'];
}
Db::name("third_party_user")
->where('openid', $openid)
->where('app_id', $appId)
->update($userData);
} else {
//TODO 使用事务做用户注册
$userId = Db::name("user")->insertGetId([
'create_time' => $currentTime,
'user_status' => 1,
'user_type' => 2,
'sex' => $wxUserData['gender'],
'user_nickname' => $wxUserData['nickName'],
'avatar' => $wxUserData['avatarUrl'],
'last_login_ip' => $ip,
'last_login_time' => $currentTime,
]);
Db::name("third_party_user")->insert([
'openid' => $openid,
'user_id' => $userId,
'third_party' => 'wxapp',
'app_id' => $appId,
'last_login_ip' => $ip,
'union_id' => isset($wxUserData['unionId']) ? $wxUserData['unionId'] : '',
'last_login_time' => $currentTime,
'create_time' => $currentTime,
'login_times' => 1,
'status' => 1,
'more' => json_encode($wxUserData)
]);
$token = cmf_generate_user_token($userId, 'wxapp');
}
$user = Db::name('user')->where('id', $userId)->find();
$this->success("登录成功!", ['token' => $token, 'user' => $user]);
}
}