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.

86 lines
2.4 KiB

2 months ago
<?php
/**
*
*/
2 months ago
include_once(SERVER_ROOT . "/model/mBase.php");
2 months ago
class mManage extends mBase {
/**
* 微信扫码登录
* @param unknown $code
* @param unknown $state
* @return boolean|string[]|number[]
*/
public function weixinLogin($code, $state) {
if (empty($code)) {
$this->setError("缺少回调参数code");
return false;
}
if (empty($state)) {
$this->setError("缺少回调参数state");
return false;
}
// 获取access token
$wxopenobj = new mWeixinOpen();
$res = $wxopenobj->getAccessToken($code);
if (!$res) {
$this->setError($wxopenobj->getError());
return false;
}
// 获取扫码登录用户信息
$userinfo = $wxopenobj->getUserInfo($res['access_token'], $res['openid']);
if (!$userinfo) {
$this->setError($wxopenobj->getError());
return false;
}
$uobj = new mUser();
$agent_login_info = $uobj->getAdminUserByOpenid($res['openid']);
if (empty($agent_login_info)) {
$login_user_info = $uobj->getLoginUserByOpenid($res['openid']);
2 months ago
if (empty($login_user_info)) $login_user_info = $uobj->addLoginUser(array('openid' => $userinfo['openid'], 'nickname' => $userinfo['nickname']));
2 months ago
2 months ago
if (empty($login_user_info)) {
2 months ago
$this->setError("注册失败");
return false;
}
$this->setError("请联系管理员开启账号");
return false;
}
if ($agent_login_info['status'] == 0) {
$this->setError("账号已被禁用");
return false;
}
$jwttoken = $this->getJwtToken(array('openid' => $res['openid']));
if (empty($jwttoken)) {
$this->setError($this->getError());
return false;
}
$data = array();
$data['jwttoken'] = $jwttoken;
2 months ago
$data['openid'] = $userinfo['openid'];
$data['nickname'] = $userinfo['nickname'];
2 months ago
return $data;
}
private function getJwtToken($jwtdata) {
$jwtobj = new mJwt();
2 months ago
$jwtobj->expire = 30 * 24 * 60 * 60;
2 months ago
$jwttoken = $jwtobj->getJwtEncode($jwtdata);
if (empty($jwttoken)) {
$this->setError("登录失败|jwt");
return false;
}
return $jwttoken;
}
}