LoginController.php
3.8 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
<?php
/**
* Created by PhpStorm.
* User: ruidiudiu
* Date: 2018/11/26
* Time: 14:49
*/
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use wxapp\aes\WXBizDataCrypt;
/**
* @title 登录相关接口
* @description 登录相关接口
* @group 登录相关接口
*/
class LoginController extends HomeBaseController{
private $appId="wxeb2a189f2d90f623";
private $appSecret="a0abb5ab77ebcfbca7f9cdff4253de44";
/**
* @title 登录凭证校验
* @description 接口说明
* @author 董瑞恩
* @url /portal/login/login
* @method post
*
* @param name:code type:String require:1 default:null desc:登录凭证
*
* @return users_id:用户id
* @return type:是否注册(0、未注册,1、已注册)
* @return is_use:是否在使用设备(0:空闲,1:在用)
*/
public function login(){
$code=$this->request->param('code');
//登录凭证校验接口
$loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appId."&secret=".$this->appSecret."&js_code=".$code."&grant_type=authorization_code";
$loginData=json_decode(file_get_contents($loginUrl),true);
if(isset($loginData['openid'])){
$users = Db::name('users')->where('open_id', $loginData['openid'])->find();
if (empty($users)){
$users_id=Db::name('users')->insertGetId(['open_id'=>$loginData['openid'],'create_time'=>time()]);
$this->apiResponse(200,'success',['users_id'=>$users_id,'type'=>0,'is_use'=>0]);
}else{
if (empty($users['phone'])){
$this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>0,'is_use'=>$users['is_use']]);
}else{
$this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>1,'is_use'=>$users['is_use']]);
}
}
}else{
$this->apiResponse(301,'获取登录凭证失败',$loginData);
}
}
/**
* @title 获取手机号
* @description 获取手机号
* @author 董瑞恩
* @url /portal/login/getPhone
* @method GET
*
* @param name:code type:String require:1 default:无 other: desc:code
* @param name:encrypted_data type:String require:1 default:无 other: desc:encrypted_data
* @param name:iv type:String require:1 default:无 other: desc:iv
*
* @return users_id:用户id
* @return type:是否注册(0、未注册,1、已注册)
* @return is_use:是否在使用设备(0:空闲,1:在用)
*/
public function getPhone(){
$param = $this->request->param();
$loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appId."&secret=".$this->appSecret."&js_code=".$param['code']."&grant_type=authorization_code";
$response=cmf_curl_get($loginUrl);
$response=json_decode($response,true);
if (!empty($response['errcode'])) {
$this->apiResponse(303,'获取session_key失败');
}
$openid = $response['openid'];
$sessionKey = $response['session_key'];
$pc = new WXBizDataCrypt($this->appId, $sessionKey);
$errCode = $pc->decryptData($param['encrypted_data'], $param['iv'], $wxUserData);
if ($errCode != 0) {
$this->apiResponse(301,'操作失败');
}
$phone=$wxUserData['phoneNumber'];
$users=Db::name('users')->where('open_id',$openid)->find();
try{
Db::name('users')->where('open_id',$openid)->update(['phone'=>$phone]);
}catch (\Exception $exception){
$this->apiResponse(302,'error:'.$exception->getMessage());
}
$this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>1,'is_use'=>$users['is_use']]);
}
}