ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

原生小程序实现登录授权与封装统一调用接口

2020-10-14 18:31:47  阅读:392  来源: 互联网

标签:原生 function getUserInfo 封装 程序实现 res login data wx


一。登录思路

先通过 wx.login 返回 res.code 到后台接口换取 openId, sessionKey, unionId。然后通过 wx.getUserInfo 获取用户信息

如果要获取用户敏感信息则要 wx.getUserInfo 返回的数据传到后台进行解析(我这边是用大佬封装好的api进行解析 )。

 

二。代码

1。小程序封装统一请求接口

function Request(url, param, method, isJson) {   const resUrl = wx.getStorageSync('url') + url;   return new Promise((resolve, reject) => {     wx.request({       url: resUrl,       data: param,       header: {         'content-type': isJson ? 'application/json' : 'application/x-www-form-urlencoded'       },       method: method,       dataType: 'json',       responseType: 'text',       success: function (res) {         // console.log("返回结果------")         // console.log(res)         resolve(res.data)       },       fail: function (err) {         // console.log("请求失败:" + err.errMsg)         wx.showToast({           title: '请求失败',           icon: 'none',           duration: 2000,           mask: true         })       }     })   }).then((resData) => {     return resData;   }) } module.exports = {   Request: Request }   2。创建一个api包专门区别放调用后台接口,我这个是api包里的user.js const requests = require("../utils/request.js") module.exports = {   // 登录   wxLogin: (data) => {     return requests.Request("/wx/login/wx-login.json", { jsCode: data }, 'post', true);   },
  //获取用户信息   getUserInfo: (data) => {     return requests.Request("/wx/login/get-user-info.json", data, 'post', true);   },
  //获取用户手机号   getUserPhone: (data) => {     return requests.Request("/wx/login/get-user-phone.json", data, 'post', true);   } }   3.修改下app.js const userRequest = require("/api/user.js") App({   onLaunch: function () {     // 展示本地存储能力     var logs = wx.getStorageSync('logs') || []     logs.unshift(Date.now())     wx.setStorageSync('logs', logs)
    wx.setStorageSync('url', "http://localhost:8087");
    this.getUserInfo();   },   onShow() { },   getUserInfo() {     let that = this;     // 登录     wx.login({       success: res => {         // 发送 res.code 到后台换取 openId, sessionKey, unionId         userRequest.wxLogin(res.code).then((res) => {           if (res.code === "SUCCESS") {             wx.setStorageSync('sessionKey', res.data.sessionKey);             that.globalData.userInfo = res.data.wxUser;             // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回             // 所以此处加入 callback 以防止这种情况             if (that.userInfoReadyCallback) {               that.userInfoReadyCallback(res.data.wxUser)             }           } else {             wx.showToast({               title: '登录失败',               icon: 'none',               duration: 2000,               mask: true             })           }         });       }     })   },   globalData: {     userInfo: null   } })   4.别的页面调用 onLoad: function () {     let that = this;     wx.getSetting({       success: function (res) {         if (res.authSetting['scope.userInfo']) {           //用户授权了           that.setData({             isShowAuth: false           })           that.initData();         } else {           //用户还没授权,弹出授权窗           that.setData({             isShowAuth: true           })         }       }     })   }   5.

标签:原生,function,getUserInfo,封装,程序实现,res,login,data,wx
来源: https://www.cnblogs.com/GGDong/p/13816505.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有