QQ小程序授权登入和微信小程序登入除了请求接口和APPid不同外其他的都是一样的,我把微信小程序的API登入接口改成QQ的后就可以直接登入了,请问怎么在增加个登入接口,让微信小程序登入和QQ小程序同时能用
下面这个是我把微信的请求接口和改成QQ了
// 小程序登录
public function xcx() {
if (IS_POST) {
$json = json_decode($_POST['json'], true);
if (!$json) {
$this->_json(0, 'POST数据解析失败');
} elseif (!$_POST['js_code']) {
$this->_json(0, 'js_code数据获取失败');
}
$url = "https://api.q.qq.com/sns/jscode2session?appid=" . $this->weixin['xcx']['appid'] . "&secret=".$this->weixin['xcx']['appsecret']."&js_code=". $_POST['js_code']."&grant_type=authorization_code";
$token = json_decode(dr_catcher_data($url), true);
if (!$token) {
$this->_json(0, 'jscode2session获取失败');
} elseif (isset($token['errcode']) && $token['errcode']) {
$this->_json(0, 'jscode2session错误代码('.$token['errcode'].'):'.$token['errmsg'].' appid='.$this->weixin['xcx']['appid'].'&secret='.$this->weixin['xcx']['appsecret'].'');
} elseif (!$token['openid']) {
$this->_json(0, 'jscode2session:openid获取失败');
}
$rt = \Phpcmf\Service::M('member')->insert_oauth(0, 'login', [
'oid' => $token['openid'],
'oauth' => 'wxxcx',
'avatar' => $json['avatarUrl'],
'unionid' => (string)$token['unionid'],
'nickname' => dr_emoji2html($json['nickName']),
'expire_at' => SYS_TIME,
'access_token' => $token['session_key'],
'refresh_token' => '',
]);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
$oauth = \Phpcmf\Service::M()->table('member_oauth')->get($rt['code']);
if (!$oauth) {
$this->_json(0, '服务端储存用户失败');
} elseif ($oauth['uid']) {
$rt = \Phpcmf\Service::M('member')->login_uid($oauth, $oauth['uid']);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
$this->_json(1, 'login', $rt['data']);
}
if ($this->weixin['xcx']['login']) {
// 直接登录
$rt = \Phpcmf\Service::M('member')->register_oauth(0, $oauth);
if ($rt['code']) {
// 登录成功
$this->_json(1, 'login', $rt['data']);
} else {
$this->_json(0, $rt['msg']);
}
} else {
// 提示注册
$oauth['nickname'] = dr_html2emoji($oauth['nickname']);
$this->_json(1, 'register', $oauth);
}
} else {
$this->_json(0, '非POST提交');
}
exit;
}
// 小程序绑定账号
public function xcx_bang() {
if (IS_POST) {
$oid = (int)\Phpcmf\Service::L('Input')->post('oid');
$post = \Phpcmf\Service::L('Input')->post('data', true);
if (!$post) {
$this->_json(0, 'POST数据解析失败');
} elseif (!$oid) {
$this->_json(0, '小程序OpenId为空');
}
$oauth = \Phpcmf\Service::M()->table('member_oauth')->get($oid);
if (!$oauth) {
$this->_json(0, '服务端用户不存在');
} elseif ($oauth['uid']) {
$rt = \Phpcmf\Service::M('member')->login_uid($oauth, $oauth['uid']);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
$this->_json(1, 'ok', $rt['data']);
}
// 登录绑定
if (empty($post['username']) || empty($post['password'])) {
$this->_json(0, dr_lang('账号或密码必须填写'));
} else {
$rt = \Phpcmf\Service::M('member')->login($post['username'], $post['password']);
if ($rt['code']) {
// 登录成功
\Phpcmf\Service::M()->db->table('member_oauth')->where('id', $oid)->update(['uid' => $rt['data']['member']['id']]);
$this->_json(1, 'ok', $rt['data']);
} else {
$this->_json(0, $rt['msg']);
}
}
} else {
$this->_json(0, '非POST提交');
}
}
// 小程序注册账号
public function xcx_reg() {
if (IS_POST) {
$oid = (int)\Phpcmf\Service::L('Input')->post('oid');
$post = \Phpcmf\Service::L('Input')->post('data', true);
if (!$post) {
$this->_json(0, 'POST数据解析失败');
} elseif (!$oid) {
$this->_json(0, '小程序OpenId为空');
}
$oauth = \Phpcmf\Service::M()->table('member_oauth')->get($oid);
if (!$oauth) {
$this->_json(0, '服务端用户不存在');
} elseif ($oauth['uid']) {
$rt = \Phpcmf\Service::M('member')->login_uid($oauth, $oauth['uid']);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
}
$this->_json(1, 'ok', $rt['data']);
}
// 注册
if (empty($post['password'])) {
$this->_json(0, dr_lang('密码必须填写'), ['field' => 'password']);
} elseif ($post['password'] != $post['password2']) {
$this->_json(0, dr_lang('确认密码不一致'), ['field' => 'password2']);
} else {
$rt = \Phpcmf\Service::M('member')->register_oauth_bang($oauth, 0, [
'username' => (string)$post['username'],
'phone' => (string)$post['phone'],
'email' => (string)$post['email'],
'password' => dr_safe_password($post['password']),
'name' => dr_safe_replace($post['name']),
]);
if ($rt['code']) {
// 注册绑定成功
$this->_json(1, 'ok', $rt['data']);
} else {
$this->_json(0, $rt['msg'], ['field' => $rt['data']['field']]);
}
}
} else {
$this->_json(0, '非POST提交');
}
}