User.php
4.5 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
<?php
namespace addons\litestore\controller\api;
use app\common\controller\Api;
use addons\litestore\library\Service;
use addons\third\model\Third;
use fast\Http;
use think\Config;
class User extends Api
{
protected $noNeedLogin = ['login_hawk','Updata_user_hawk'];
protected $token = '';
public function _initialize()
{
$this->token = $this->request->post('token');
if ($this->request->action() == 'login' && $this->token) {
$this->request->post(['token' => '']);
}
parent::_initialize();
if (!Config::get('fastadmin.usercenter')) {
$this->error(__('User center already closed'));
}
$ucenter = get_addon_info('ucenter');
if ($ucenter && $ucenter['state']) {
include ADDON_PATH . 'ucenter' . DS . 'uc.php';
}
}
public function Updata_user_hawk(){
$userInfo = $this->request->post("userInfo", '', 'trim');
$mobile = $this->request->post("mobile");
if (!$userInfo||!$this->token) {
$this->error("参数不正确");
}
$this->auth->init($this->token);
//检测是否登录
if ($this->auth->isLogin()) {
$user = $this->auth->getUser();
$fields = [];
$userInfo = json_decode($userInfo,true);
$fields['avatar'] = $userInfo['avatarUrl'];
$fields['nickname'] = $userInfo['nickName'];
$fields['mobile'] = $mobile;
$user->save($fields);
$this->success("已经登录", ['userInfo' => $this->auth->getUserinfo()] );
}else{
$this->error("未登录状态");
}
}
public function login_hawk()
{
$config = get_addon_config('litestore');
$code = $this->request->post("code");
if (!$code) {
$this->error("参数不正确");
}
$params = [
'appid' => $config['AppID'],
'secret' => $config['AppSecret'],
'js_code' => $code,
'grant_type' => 'authorization_code'
];
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
if ($result['ret']) {
$json = (array)json_decode($result['msg'], true);
if (isset($json['openid'])) {
//如果有传Token
if ($this->token) {
$this->auth->init($this->token);
//检测是否登录
if ($this->auth->isLogin()) {
$third = Third::where(['openid' => $json['openid'], 'platform' => 'wxapp'])->find();
if ($third && $third['user_id'] == $this->auth->id) {
//把最新的 session_key 保存到 第三方表的 access_token 里
$third['access_token'] = $json['session_key'];
$third->save();
$this->success("登录成功", $this->Format_avatarUrl($this->auth->getUserinfo()));
}
}
}
$platform = 'wxapp';
$result = [
'openid' => $json['openid'],
'userinfo' => [
'nickname' => '游客未登录',
],
'access_token' => $json['session_key'],
'refresh_token' => '',
'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
];
$extend = ['mobile'=>'NoLoginData' ,'gender' => '0', 'nickname' => '游客未登录', 'avatar' =>'/assets/img/avatar.png'];
$ret = Service::connect_hawk($platform, $result, $extend);
if ($ret) {
$this->success("登录成功", $this->Format_avatarUrl($this->auth->getUserinfo()));
} else {
$this->error("连接失败");
}
} else {
$this->error("登录失败",$json);
}
}
return;
}
private function startsWith($str, $prefix)
{
for ($i = 0; $i < strlen($prefix); ++$i) {
if ($prefix[$i] !== $str[$i]) {
return false;
}
}
return true;
}
private function Format_avatarUrl($userInfo){
$avatar = $userInfo['avatar'];
if($this->startsWith($avatar,"/uploads/")){
$userInfo['avatar'] = cdnurl($avatar, true);
}
return ['userInfo' => $userInfo];
}
}