...
|
...
|
@@ -20,78 +20,111 @@ class User extends Api |
|
|
protected $noNeedRight = ['*'];
|
|
|
|
|
|
/**
|
|
|
*手机号授权登录
|
|
|
*
|
|
|
* @ApiTitle (小程序登录)
|
|
|
* @ApiSummary (微信小程序登录)
|
|
|
* @ApiTitle (手机号授权登录)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiParams (name="code", type="string", required=true, description="code")
|
|
|
* @ApiParams (name="phone", type="string", required=true, description="phone")
|
|
|
* @ApiRoute (/api/myinfo/login)
|
|
|
* @ApiParams (name="code", type="string", required=true, description="code-wx.login的code")
|
|
|
* @ApiParams (name="encryptedData", type="string", required=true, description="encryptedData")
|
|
|
* @ApiParams (name="iv", type="string", required=true, description="iv")
|
|
|
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
|
|
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
|
|
* @ApiReturn ({
|
|
|
'code':'1',
|
|
|
'msg':'返回成功'
|
|
|
'data':[
|
|
|
'id':'用户id'
|
|
|
'nickname':'用户昵称'
|
|
|
'mobile':'手机号'
|
|
|
'avatar':'头像'
|
|
|
'token':'token']
|
|
|
})
|
|
|
*/
|
|
|
public function login()
|
|
|
{
|
|
|
$config = Config::get('wxapp');
|
|
|
$code = $this->request->param('code');
|
|
|
$phone = $this->request->param('phone');
|
|
|
if (empty($code)) {
|
|
|
public function login(){
|
|
|
//接收传递信息
|
|
|
$config = Config::get('wechat');
|
|
|
$encryptedData = $this->request->post('encryptedData');
|
|
|
$iv = $this->request->post('iv');
|
|
|
$code = $this->request->post('code');
|
|
|
if(empty($code)){
|
|
|
$this->error('参数错误');
|
|
|
}
|
|
|
$params = [
|
|
|
'appid' => $config['app_id'], //小程序appid
|
|
|
'secret' => $config['secret'], //小程序appid的秘钥
|
|
|
'js_code' => $code, //前端传来的code
|
|
|
'appid'=>$config['app_id'], //小程序appid
|
|
|
'secret'=>$config['secret'], //小程序appid的秘钥
|
|
|
'js_code'=>$code, //前端传来的code
|
|
|
'grant_type' => 'authorization_code' //authorization_code — 授权码模式(即先登录获取code,再获取token)
|
|
|
];
|
|
|
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');//接口
|
|
|
if ($result['ret']) {
|
|
|
$json = json_decode($result['msg'], true); //json_decode()接收一个json格式数据将他变为php变量
|
|
|
if (isset($json['openid'])) {
|
|
|
if (isset($phone)) {
|
|
|
$userId = Db::name('third')->where(['apptype' => 'wxapp', 'openid' => $json['openid']])->value('user_id');
|
|
|
if($result['ret']){
|
|
|
$json = json_decode($result['msg'],true); //json_decode()接收一个json格式数据将他变为php变量
|
|
|
if(isset($json['openid'])){
|
|
|
$options = [
|
|
|
'debug'=>true,
|
|
|
'app_id'=>$config['app_id'],
|
|
|
'secret'=>$config['secret'],
|
|
|
'token'=>$this->auth->getToken(), //获取token
|
|
|
'log' => [
|
|
|
'level' => 'debug',
|
|
|
'file' =>'/tmp/easywechat.log',
|
|
|
],
|
|
|
];
|
|
|
$sessionKey = $json['session_key'];
|
|
|
$app = Factory::miniProgram($options);
|
|
|
try {
|
|
|
$phone = $app->encryptor->decryptData($sessionKey,$iv,$encryptedData);
|
|
|
}catch (\Exception $e){
|
|
|
$this->error('网络不好,重新操作');
|
|
|
}
|
|
|
if (isset($phone['phoneNumber'])){
|
|
|
$userId = Db::name('third')->where(['apptype'=>'wxapp','openid'=>$json['openid']])->value('user_id');
|
|
|
$auth = Auth::instance();
|
|
|
$ret = $auth->direct($userId);
|
|
|
if ($ret) { //假如登录上后获取信息
|
|
|
if ($ret){ //假如登录上后获取信息
|
|
|
$data = $auth->getUserinfo();
|
|
|
$aut = \db('user')->where('id', $userId)->find();
|
|
|
$aut = \db('user')->where('id',$userId)->find();
|
|
|
$data['avatar'] = cdnurl($data['avatar']); //cdnurl第三方存储
|
|
|
$data['mobile'] = $aut['mobile'];
|
|
|
$this->success('登录成功', $data);
|
|
|
} elseif (empty($ret)) {
|
|
|
$this->success('登录成功',$data);
|
|
|
}elseif(empty($ret)){
|
|
|
/*注册一个用户*/
|
|
|
$data = [
|
|
|
'nickname' => '微信用户',
|
|
|
'username' => '微信用户',
|
|
|
'avatar' => '/assets/img/avatar.png',
|
|
|
'status' => 'normal',
|
|
|
'mobile' => $phone,
|
|
|
'jointime' => time()
|
|
|
'mobile' => $phone['phoneNumber'],
|
|
|
'jointime'=>time()
|
|
|
];
|
|
|
//插入user
|
|
|
$id = \db('user')->insertGetId($data);
|
|
|
//插入third
|
|
|
\db('third')->insert(['user_id' => $id, 'apptype' => 'wxapp', 'openname' => '微信用户',
|
|
|
'access_token' => $json['session_key'], 'openid' => $json['openid']]);
|
|
|
$userId = Db::name('third')->where(['apptype' => 'wxapp', 'openid' => $json['openid']])->value('user_id');
|
|
|
$userId = Db::name('third')->where(['apptype'=> 'wxapp', 'openid'=>$json['openid']])->value('user_id');
|
|
|
$auth = Auth::instance();
|
|
|
$ret = $auth->direct($userId);
|
|
|
if ($ret) {
|
|
|
if ($ret){
|
|
|
$data = $auth->getUserinfo();
|
|
|
$aut = \db('user')->where('id', $userId)->find();
|
|
|
$aut = \db('user')->where('id',$userId)->find();
|
|
|
$data['avatar'] = cdnurl($data['avatar']);
|
|
|
$data['mobile'] = $aut['mobile'];
|
|
|
$this->success('登录成功', $data);
|
|
|
} else {
|
|
|
$this->success('登录成功',$data);
|
|
|
}else{
|
|
|
$this->error('连接失败');
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
}else{
|
|
|
$this->error('手机号未找到,请重新输入');
|
|
|
}
|
|
|
} else {
|
|
|
}else{
|
|
|
$this->error('获取openid失败');
|
|
|
}
|
|
|
} else {
|
|
|
}else{
|
|
|
$this->error('请求失败');
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
|