第三方微信登錄
阿新 • • 發佈:2018-10-26
nbsp 存在 androi http UNC 平臺 values userinfo .html
https://mp.weixin.qq.com/s/cTaIv28GyFZvc-Gj15TYnA
1、首先引入
2、使用auth方法
3、配置
3 - 1 首頁需要到微信開放平臺去申請審核 https://open.weixin.qq.com/
註:如果需要ios 和 安卓 使用,請註意上圖 將ios和android應用都勾選上
4、auth方法成功之後,使用getToken方法
5、在使用getToken方法成功後,使用getUserInfo方法
將獲取到的用戶信息按需要邏輯處理
附上小編的項目代碼
小編的代碼有處理是否綁定的邏輯以及登錄等,大家只需參考wx登錄部分即可
html部分
<div class="aui-col-xs-2" style="text-align: center;" tapmode onclick="wxauth()"> <i class="iconfont icon-liaotian icon_bg2"></i> <div class="aui-grid-label" style="display:block;margin-top:10px;">微信</div></div>
js部分
//微信登錄 function wxauth() { api.showProgress({ style: ‘default‘, animationType: ‘fade‘, title: ‘‘, text: ‘啟動中...‘, modal: false }); var wx = api.require(‘wx‘); wx.auth({ apiKey: ‘wx***********‘ }, function(ret, err) { api.hideProgress(); if (ret) { if (ret.status) { var code = ret.code; getToken(code); } else { api.toast({ msg: ‘錯誤‘, duration: 2000, location: ‘middle‘ }); } } else { switch(err.code) { case -1: api.toast({ msg: ‘未知錯誤‘, duration: 2000, location: ‘middle‘ }); break; case 1: api.toast({ msg: ‘用戶取消‘, duration: 2000, location: ‘middle‘ }); break; case 2: api.toast({ msg: ‘用戶拒絕授權‘, duration: 2000, location: ‘middle‘ }); break; case 3: api.toast({ msg: ‘當前設備未安裝微信客戶端‘, duration: 2000, location: ‘middle‘ }); break; } } }); }
/** * [getToken 獲取用戶token] * @param {[type]} code [授權成功後返回code 參數] * @return {[type]} [description] */ function getToken(code) { var wx = api.require(‘wx‘); wx.getToken({ apiKey: ‘‘, apiSecret: ‘‘, code: code }, function(ret, err) { if (ret) { if (ret.status) { var accessToken=ret.accessToken; var dynamicToken=ret.dynamicToken; var openId=ret.openId; getUserInfo(accessToken,openId); } else { api.toast({ msg: ‘錯誤‘, duration: 2000, location: ‘middle‘ }); } } else { switch(err.code) { case -1: api.toast({ msg: ‘未知錯誤‘, duration: 2000, location: ‘middle‘ }); break; case 1: api.toast({ msg: ‘apiKey值為空或非法‘, duration: 2000, location: ‘middle‘ }); break; case 2: api.toast({ msg: ‘apiSecret值為空或非法‘, duration: 2000, location: ‘middle‘ }); break; case 3: api.toast({ msg: ‘code值為空或非法‘, duration: 2000, location: ‘middle‘ }); break; case 4: api.toast({ msg: ‘網絡超時‘, duration: 2000, location: ‘middle‘ }); break; } } }); }
/** * [getUserInfo 獲取用戶信息] * @param {[type]} accessToken [getToken 接口或 refreshToken 接口成功獲取的 accessToken 值] * @param {[type]} openId [getToken 接口或 refreshToken 接口成功獲取的 openId 值] */ function getUserInfo(accessToken,openId) { var wx = api.require(‘wx‘); wx.getUserInfo({ accessToken: accessToken, openId: openId }, function(ret, err) { if (ret) { if (ret.status) { var openid=ret.openid, unionid = ret.unionid, nickname=ret.nickname, head_pic=ret.headimgurl, source=‘weixin‘; api.ajax({ url: domainName + ‘/index.php?g=User&m=Api&a=thirdLogin‘, method: ‘post‘, data: { values: { openid: openid, unionid: unionid, nickname: nickname, head_pic: head_pic, source: source } } }, function(ret, err) { if (ret) { if (ret.msg == ‘0‘) { //已經存在該用戶信息 if (ret.binding_status == ‘1‘) { /*1:未綁定 2已綁定*/ //未綁定,到綁定頁面 api.openWin({ name: ‘user_binding_win‘, url: ‘./user_binding_win.html‘, pageParam: { openid: openid, source: source } }); } else { //已綁定,到登錄頁面 userInfoAll(openid,source); api.openWin({ name: ‘index‘, url: ‘../main.html‘ }); } } else if (ret.msg == ‘1‘){ //添加用戶信息成功 api.openWin({ name: ‘user_binding_win‘, url: ‘./user_binding_win.html‘, pageParam: { openid: openid } }); } else if (ret.msg ==‘2‘){ api.toast({ msg: ‘添加用戶失敗‘, duration: 2000, location: ‘bottom‘ }); } else { api.toast({ msg: ‘未知錯誤‘, duration: 2000, location: ‘bottom‘ }); } } else { api.toast({ msg: ‘未知錯誤‘, duration: 2000, location: ‘middle‘ }); } }); } else { api.toast({ msg: ‘錯誤‘, duration: 2000, location: ‘middle‘ }); } } else { switch(err.code) { case -1: api.toast({ msg: ‘未知錯誤‘, duration: 2000, location: ‘middle‘ }); break; case 1: api.toast({ msg: ‘accessToken 過期‘, duration: 2000, location: ‘middle‘ }); break; case 2: api.toast({ msg: ‘openId非法‘, duration: 2000, location: ‘middle‘ }); break; case 3: api.toast({ msg: ‘openId值為空‘, duration: 2000, location: ‘middle‘ }); break; case 4: api.toast({ msg: ‘accessToken值為空‘, duration: 2000, location: ‘middle‘ }); break; case 5: api.toast({ msg: ‘accessToken非法‘, duration: 2000, location: ‘middle‘ }); break; case 6: api.toast({ msg: ‘網絡超時‘, duration: 2000, location: ‘middle‘ }); break; } } }); }
第三方微信登錄