1.在微信小程序实现微信登录相关功能 2.从 app 直接调用小程序登录页面,获取 openid或者登录凭证 3.将小程序获取的 opendi 或者登录凭证调期 app,将相关的凭证回传 app 之后,app 使用相关的凭证进行登录。
接口返回数据: 参数 类型 说明 userInfo OBJECT 用户信息对象 rawData String 不包括敏感信息的原始数据字符串,用于计算签名。 signature String 使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息。 encryptedData String 包括敏感数据在内的完整用户信息的加密数据,详细见加密数据解密算法。 iv String 加密算法的初始向量,详细见加密数据解密算法。 cloudID String 敏感数据对应的云 ID,开通云开发的小程序才会返回,可通过云调用直接获取开放数据,详细见云调用直接获取开放数据 errMsg String 描述信息
<!-- #ifdef APP-PLUS||MP --> <view class="fh-wechat-login-main"> <slot> <view class="fh-wechat-login-button" @click="dowechatLogin"> <view class="fh-wechat-icon"></view> 使用微信账号登录 </view> </slot> </view> <!-- #endif -->
dowechatLogin() { // 展示加载框 uni.showLoading({ title: '加载中', }); let url = this.$baseUrl + 'api/third-login/wechat-login/'; uni.getUserProfile({ desc: '登录后可同步数据', success: async (obj) => { console.log('obj', obj); // 调用 action ,请求登录接口 // await this.login(obj); uni.login({ provider: 'weixin', success: (res) => { console.log('res-login', res); this.code = res.code; console.log('code', res.code); if (res.errMsg == 'login:ok') { uni.request({ url: url, data: { code: this.code }, method: "POST", header: {}, success: (res) => { // console.log(res.data); }, fail: (e) => { console.log('failed:', e); uni.showToast({ title: '登录失败,请稍候再试', icon: 'none' }); this.isLoginProcess = false; } }); console.log('res', res); } }, }); }, fail: err => { console.log(err) uni.showToast({ title: '授权已取消', icon: 'error', mask: true, }); }, complete: () => { // 隐藏loading uni.hideLoading(); }, }); },
服务端部分:
def wechat_login(self, request): try: # user = request.user # user = User.objects.get(pk=1) try: code = request.data['code'] resp = requests.get( 'https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secrct&js_code=' + code + '&grant_type=authorization_code ') resp_json = json.loads(resp.text) if 'errcode' in resp_json: return ErrorResponse(_('code 已经被使用')) # return DetailResponse(resp_json) # print(resp_json) user_info = resp_json # print(user_info) open_id = user_info['openid'] # auth_realname = user_info['auth_realname'] # 创建用户账号逻辑省略 msg = {'token': token.key, 'user': {} } return DetailResponse(msg) except Exception as e: print("Exception=", e) return ErrorResponse(_('数据错误')) except: return ErrorResponse(_('错误的数据格式'))
appToMp() { let that = this let url = '/pages/page/wechatLogin' //跳转小程序的路径 plus.share.getServices(function(res) { var sweixin = null; for (var i = 0; i < res.length; i++) { var t = res[i]; if (t.id == 'weixin') { sweixin = t; } } if (sweixin) { that.wxPay = true sweixin.launchMiniProgram({ id: 'gh_8**********', //这里写你的小程序原始id(以gh开头) type: 0, //微信小程序版本类型可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。 path: url, success(res) { // return true console.log('success=', res); }, fail(res) { uni.showToast({ title: '跳转小程序失败', icon: 'none', duration: 3000 }) console.log('跳转小程序失败', res) return false; }, }) } }, function(err) { console.log(JSON.stringify(err)); }) },
{ "applinks": { "apps": [], "details": [ { "appID": "appTeamID.bundleid", "paths": ["/uni-universallinks/*"] } ] } }
<view v-if="isSuccess===0" class="fh-wechat-login-main" > <slot> <view class="fh-wechat-login-button" @click="LoginForWechat"> <view class="fh-wechat-icon"></view> 使用微信账号登录 </view> </slot> </view> <view v-if="isSuccess===1"> <button class="fh-wechat-login-main" open-type="launchApp" :app-parameter="params" @error="openError" style="font-size: 14px;">授权成功,点击返回APP登录</button> </view>
uni.getUserProfile({ desc: '登录后可同步数据', success: async (obj) => { console.log('obj', obj); // 调用 action ,请求登录接口 // await this.login(obj); uni.login({ provider: 'weixin', success: (res) => { console.log('res-login', res); this.code = res.code; console.log('code', res.code); if (res.errMsg == 'login:ok') { this.params = 'code=' + this.code; console.log('res', res); this.isSuccess = 1; } }, }); }, fail: err => { console.log(err) uni.showToast({ title: '授权已取消', icon: 'error', mask: true, }); }, complete: () => { // 隐藏loading uni.hideLoading(); }, });
onShow() { // #ifdef APP-PLUS setTimeout(() => { console.log('wechat login call back') let code = plus.runtime.arguments if (code && code.toString().indexOf('code=')>=0) { plus.runtime.arguments = '' // ... console.log('code=', code); code = code.replace('code=', ''); if (code.length > 4) { //使用 code 进行登录操作 this.httpLoginwechatWithCode(code); } } }, 10) // #endif },
The post 曲线救国 — 微信个人开发者使用微信账号登录实现 first appeared on obaby@mars.