- wx.login()
有时候点击登录 调用微信 wx.login接口 对应传值给后端,有时候后端会返回我登录失败 但是每次值都传过去了 很奇怪。有朋友遇到过吗?[图片] [图片]
2018-05-19 - 需要登录的页面加一个判断利用Promise
我这边给你写一个简单的案例吧。 文件一 app.js [代码]//app.js[代码][代码]App({[代码][代码] [代码][代码]onLaunch: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]},[代码][代码] [代码][代码]cache: {[代码][代码] [代码][代码]getuserid: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]return[代码] [代码]wx.getStorageSync([代码][代码]'loginuserid'[代码][代码]);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]},[代码][代码] [代码][代码]wx:{[代码][代码] [代码][代码]request_post: [代码][代码]function[代码] [代码](url, data) {[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]Promise([代码][代码]function[代码] [代码](resolve, reject) {[代码][代码] [代码][代码]wx.request({[代码][代码] [代码][代码]url: url,[代码][代码] [代码][代码]data: data,[代码][代码] [代码][代码]method: [代码][代码]'POST'[代码][代码],[代码][代码] [代码][代码]header: { [代码][代码]"Content-Type"[代码][代码]: [代码][代码]"application/x-www-form-urlencoded"[代码] [代码]},[代码][代码] [代码][代码]success: [代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]resolve(res)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail: [代码][代码]function[代码] [代码](err) {[代码][代码] [代码][代码]reject(err)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码]})[代码] 文件二 helper.js [代码]//utils/helper.js[代码][代码]var[代码] [代码]app = getApp();[代码][代码]function[代码] [代码]weixinauth() {[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]Promise([代码][代码]function[代码] [代码](resolve, reject) {[代码][代码] [代码][代码]if[代码] [代码](app.cache.getuserid() > 0) {[代码][代码] [代码][代码]resolve([代码][代码]"无需登录"[代码][代码])[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]wx.login({[代码][代码] [代码][代码]success: [代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]if[代码] [代码](!res.code)[代码][代码] [代码][代码]return[代码][代码];[代码][代码] [代码][代码]//##################################[代码][代码] [代码][代码]app.wx.request_post([代码][代码]'https://api.xxx.com/api/weixin/auth'[代码][代码], {[代码][代码] [代码][代码]code: res.code,[代码][代码] [代码][代码]}).then([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]wx.setStorageSync([代码][代码]"loginuserid"[代码][代码], res.data.userid);[代码][代码] [代码][代码]resolve(res.data)[代码][代码] [代码][代码]}).[代码][代码]catch[代码][代码]([代码][代码]function[代码] [代码](err) {[代码][代码] [代码][代码]reject(err.data)[代码][代码] [代码][代码]})[代码][代码] [代码][代码]//##################################[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]//##################################[代码][代码] [代码][代码]});[代码][代码]}[代码][代码]module.exports.weixinauth = weixinauth;[代码] 文件三 index.js-------就是需要登录页面的调用 [代码]// pages/index.js[代码][代码]var[代码] [代码]helper = require([代码][代码]"../utils/helper.js"[代码][代码])[代码][代码]var[代码] [代码]that;[代码][代码]var[代码] [代码]app = getApp();[代码][代码]Page({[代码][代码] [代码][代码]data: {[代码][代码] [代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onLoad: [代码][代码]function[代码] [代码](options) {[代码][代码] [代码][代码]that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]helper.weixinauth().then([代码][代码]function[代码] [代码](resdata) {[代码][代码] [代码][代码]console.log([代码][代码]"resdata"[代码][代码]);[代码][代码] [代码][代码]that.GoLoad();[代码][代码] [代码][代码]}).[代码][代码]catch[代码][代码]([代码][代码]function[代码] [代码](errdata) {[代码][代码] [代码][代码]console.log([代码][代码]"errdata"[代码][代码]);[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]GoLoad:[代码][代码]function[代码][代码](){[代码][代码] [代码][代码]//这里写加载页面的方法[代码][代码] [代码][代码]var[代码] [代码]userid = app.cache.getuserid();[代码][代码] [代码][代码]console.log([代码][代码]"GoLoad"[代码][代码]);[代码][代码] [代码][代码]}[代码][代码]})[代码]
2018-05-10