You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.3 KiB
117 lines
3.3 KiB
<?php
|
|
|
|
namespace app\base\controller\mpweixin\api;
|
|
|
|
use app\base\model\mpweixin\MpweixinUser;
|
|
use tencent\wechat\mpweixin\User;
|
|
use think\App;
|
|
use think\facade\Db;
|
|
|
|
class Auth extends Base
|
|
{
|
|
|
|
/**
|
|
* 微信小程序 wx.login
|
|
* @date 2020-11-30
|
|
*/
|
|
public function getOpenid()
|
|
{
|
|
$code = input('post.code');
|
|
$mp_weixin_config = get_mp_weixin_config();
|
|
|
|
$mpweixin_user_model = new MpweixinUser();
|
|
$user_class = new User($mp_weixin_config);
|
|
$jwt_class = new \jwt\Jwt();
|
|
|
|
// 获取微信小程序用户信息
|
|
$res = $user_class->getOpenid($code);
|
|
|
|
// 更新小程序用户信息表
|
|
$user_info = [
|
|
'uid' => UID,
|
|
'openid' => isset($res['openid']) ? $res['openid'] : '',
|
|
'unionid' => isset($res['unionid']) ? $res['unionid'] : '',
|
|
'session_key' => isset($res['session_key']) ? $res['session_key'] : '',
|
|
'session_key_time' => time(),
|
|
];
|
|
if (!$user_info['openid']) {
|
|
return sendErrorMessage($res['errcode'], $res['errmsg']);
|
|
}
|
|
|
|
$res = $mpweixin_user_model->updateUser($user_info);
|
|
|
|
// 获取用户信息
|
|
$mpweixin_user = $mpweixin_user_model->getOneData([
|
|
['openid', '=', $user_info['openid']]
|
|
], 'id,user_id,openid');
|
|
|
|
// 获取token
|
|
$info = [
|
|
'uid' => UID,
|
|
'openid' => $mpweixin_user['openid'],
|
|
'mpweixin_user_id' => $mpweixin_user['id'],
|
|
'user_id' => empty($mpweixin_user['user_id']) ? '' : $mpweixin_user['user_id'],
|
|
];
|
|
$res_token = $jwt_class->signToken($info);
|
|
|
|
$r_data = $info;
|
|
$r_data['access_token'] = $res_token['token'];
|
|
$r_data['access_token_expire_time'] = $res_token['exp'];
|
|
|
|
return sendSuccessMessage($r_data);
|
|
}
|
|
|
|
/**
|
|
* 微信小程序 授权获取手机号
|
|
* @date 2021-12-30
|
|
*/
|
|
public function getPhoneNumber()
|
|
{
|
|
$param = input('post.');
|
|
$param['last_user_id'] = input('post.last_user_id', '');
|
|
$mp_weixin_param = get_mp_weixin_config();
|
|
|
|
$user_logic = new \app\base\logic\User();
|
|
$jwt_class = new \jwt\Jwt();
|
|
$user_class = new User($mp_weixin_param);
|
|
|
|
//换取用户手机号
|
|
$result = $user_class->getPhoneNumber($param['code']);
|
|
|
|
if ($result['errcode'] != 0) {
|
|
return sendErrorMessage($result['errcode'], $result['errmsg']);
|
|
}
|
|
|
|
Db::startTrans();
|
|
|
|
//根据不同平台组合info参数
|
|
$info = TOKEN_DATA;
|
|
$info['user_agent'] = USER_AGENT;
|
|
|
|
//登录并更新相关信息(用户表和平台用户表)
|
|
$res = $user_logic->login($result['phone_info']['phoneNumber'], $info, $param['last_user_id']);
|
|
if ($res['code'] != 0) {
|
|
Db::rollback();
|
|
return json($res);
|
|
}
|
|
$user_id = $res['data']['user_id'];
|
|
Db::commit();
|
|
|
|
|
|
//绑定三方平台
|
|
// 签发token
|
|
$arr = TOKEN_DATA;
|
|
$arr['user_id'] = $user_id;
|
|
|
|
$res_token = $jwt_class->signToken($arr);
|
|
|
|
$r_data = [
|
|
'user_id' => $user_id,
|
|
'access_token' => $res_token['token'],
|
|
'access_token_expire_time' => $res_token['exp']
|
|
];
|
|
|
|
return sendSuccessMessage($r_data);
|
|
}
|
|
}
|