QQ小程序授權(quán)登入和微信小程序登入除了請求接口和APPid不同外其他的都是一樣的,我把微信小程序的API登入接口改成QQ的后就可以直接登入了,請問怎么在增加個登入接口,讓微信小程序登入和QQ小程序同時能用
下面這個是我把微信的請求接口和改成QQ了
// 小程序登錄
public function xcx() {
if (IS_POST) {
$json = json_decode($_POST['json'], true);
if (!$json) {
$this->_json(0, 'POST數(shù)據(jù)解析失敗');
} elseif (!$_POST['js_code']) {
$this->_json(0, 'js_code數(shù)據(jù)獲取失敗');
}
$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, '服務(wù)端儲存用戶失敗');
} 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數(shù)據(jù)解析失敗');
} elseif (!$oid) {
$this->_json(0, '小程序OpenId為空');
}
$oauth = \Phpcmf\Service::M()->table('member_oauth')->get($oid);
if (!$oauth) {
$this->_json(0, '服務(wù)端用戶不存在');
} 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數(shù)據(jù)解析失敗');
} elseif (!$oid) {
$this->_json(0, '小程序OpenId為空');
}
$oauth = \Phpcmf\Service::M()->table('member_oauth')->get($oid);
if (!$oauth) {
$this->_json(0, '服務(wù)端用戶不存在');
} 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提交');
}
}
新建新建一個php文件就不會沖突了,比如以前叫Xx.php,你復(fù)制一個改成Bb.php
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復(fù)@官方插件技術(shù)-實習(xí)
這個是微信插件里面的小程序配置文件呀!嘗試直接在這個php里復(fù)制份一樣的代碼后是不行的
復(fù)制后,還需要修改文件里面的類名,訪問時地址也要發(fā)生變化
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復(fù)@官方插件技術(shù)-實習(xí) 這個賬號設(shè)置頁面也有增加一個,感覺好難,官方能不能加個QQ小程序的配置接口呀
目前微信插件沒有這種功能,需要你自己二次開發(fā)
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!