Login.php
5.9 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
152
<?php
/**
* Created by PhpStorm.
* Login: Kevin
* Date: 2020/6/28
* Time: 09:14
*/
namespace app\api\controller\v1;
use lib\WXBizDataCrypt;
use think\Db;
use think\Request;
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT');
class Login extends Base
{
/**
* 1.小程序授权注册用户、返回用户信息(用户需要验证手机号)
*/
public function get_user_by_shouquan()
{
$appid = $this->AppID;
$AppSecret = $this->AppSecret;
$post = $this->request->post();
$code = $post['code'];// I('post.code');
$encryptedData = $post['encryptedData'];//I('post.encryptedData');
$iv = $post['iv'];//I('post.iv');
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
$res = json_decode(httpRequest($url), true);
if (!$res) {
$res = json_decode(send_post($url), true);
}
$sessionKey = $res['session_key'];
$openid = $res['openid'];//获取用户openid
$unionid = $res['unionid'];//获取用户openid
file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . json_encode($res) . PHP_EOL, FILE_APPEND);
require_once '../extend/lib/WXBizDataCrypt.class.php';
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
$data = json_decode($data, true);
file_put_contents("hzz01.txt", "授权2:" . date("Y-m-d H:i:s") . $errCode . PHP_EOL, FILE_APPEND);
file_put_contents("hzz01.txt", "授权3:" . date("Y-m-d H:i:s") . json_encode($data) . PHP_EOL, FILE_APPEND);
if ($errCode != 0) {
$ajax['code'] = 0;
$ajax['info'] = $errCode;
$errCode = $errCode == "-41001" || $errCode == "-41003" ? "授权失败,请尝试重新授权" : $errCode;
$this->error($errCode);
}
if ($openid) {
$openid_info = [
"openid" => $openid,
"unionid" => $unionid,
"from" => "wx",
];
if (!empty($data['nickName'])) {
$openid_info['nickName'] = $data['nickName'];
}
if (!empty($data['gender'])) {
$openid_info['gender'] = $data['gender'];
}
if (!empty($data['avatarUrl'])) {
$openid_info['avatarUrl'] = $data['avatarUrl'];
}
if (!empty($data['country'])) {
$openid_info['country'] = $data['country'];
}
if (!empty($data['province'])) {
$openid_info['province'] = $data['province'];
}
if (!empty($data['city'])) {
$openid_info['city'] = $data['city'];
}
if (!empty($data['phoneNumber'])) {
$openid_info['phoneNumber'] = $data['phoneNumber'];
}
insert_openid_info($openid_info);//更新下微信用户信息到数据库
}
//直接通过unionid 查找用户信息
$userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find();
if (empty($userres)) {
//如果通过小程序unionid找不到会员
// if ($errCode != 0) {
// $ajax['code'] = 0;
// $ajax['info'] = $errCode;
// $errCode = $errCode == "-41001" ? "授权失败,请尝试重新授权。" : $errCode;
// $this->error($errCode);
// }
//注册处理
$extend_data = [
"nickname" => $openid_info['nickName'],
"avatar" => $openid_info["avatarUrl"],
"wx_xcx_openid" => $openid,
"unionid" => $unionid,
];
$username = $openid_info['nickName'];
$ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);
if ($ret) {
$return_data = $this->auth->getUserinfo();
if (empty($return_data['mobile'])) {
$this->success("授权成功,请继续认证手机号后才能正常使用", $return_data, 2);
}
$this->success("登录成功", $return_data);
} else {
$this->error($this->auth->getError());
}
} else {
//小程序unionid找到了会员
$update_data = [];
$update_data['logintime'] = time();
if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) {
$update_data['mobile'] = $openid_info['phoneNumber'];
}
if (empty($userres['username']) && !empty($openid_info['phoneNumber'])) {
$update_data['username'] = $openid_info['phoneNumber'];
}
if (!empty($openid_info['nickName'])) {
$update_data['nickname'] = $openid_info['nickName'];
}
$update_data['wx_xcx_openid'] = $openid;
Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data);
$this->auth->direct($userres['id']);
$user = $this->auth->getUserinfo();
$this->success(__('登录成功'), $user);
}
}
/**
* 给后台添加用户时自动注册用
*/
public function auto_regist_user($username, $password = "a123456", $email = "", $mobile, $extend_data)
{
$ret = $this->auth->register($username, $password, '', $mobile, $extend_data);
if ($ret) {
$return_data = $this->auth->getUserinfo();
return array_callback(true, "用户添加成功", $return_data['id']);
} else {
return array_callback(false, $this->auth->getError());
}
}
}