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.
135 lines
3.6 KiB
135 lines
3.6 KiB
<?php
|
|
/**
|
|
*
|
|
*/
|
|
include_once(SERVER_ROOT . "/model/mBase.php");
|
|
|
|
|
|
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)) {
|
|
$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;
|
|
$data['openid'] = $userinfo['openid'];
|
|
$data['nickname'] = $userinfo['nickname'];
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function getJwtToken($jwtdata) {
|
|
$jwtobj = new mJwt();
|
|
$jwtobj->expire = 30 * 24 * 60 * 60;
|
|
$jwttoken = $jwtobj->getJwtEncode($jwtdata);
|
|
if (empty($jwttoken)) {
|
|
$this->setError("登录失败|jwt");
|
|
return false;
|
|
}
|
|
return $jwttoken;
|
|
}
|
|
|
|
public function weixinBind($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;
|
|
}
|
|
|
|
$aid = explode('-', $state)[1];
|
|
|
|
$uobj = new mUser();
|
|
$admin_user_info = $uobj->getAdminUserByOpenid($res['openid']);
|
|
if (!empty($admin_user_info) && $admin_user_info['aid'] == $aid) {
|
|
$this->setError("账号已存在");
|
|
return false;
|
|
}
|
|
|
|
if (!empty($admin_user_info)) {
|
|
$uobj->updateAdminUser($admin_user_info['id'], array('aid' => $aid));
|
|
return true;
|
|
}
|
|
|
|
$data = array(
|
|
'aid' => $aid,
|
|
'nickname' => $userinfo['nickname'],
|
|
'openid' => $userinfo['openid'],
|
|
'is_super_admin' => ADMIN_USER_NOT_SUPER,
|
|
'status' => ADMIN_USER_OPEN,
|
|
);
|
|
|
|
$id = $uobj->addAdminUser($data);
|
|
if (!$id) {
|
|
$this->setError("绑定失败");
|
|
return false;
|
|
}
|
|
|
|
return $id;
|
|
}
|
|
}
|