安卓手机扫码进入小程序,有时后会获取不到参数
发布于 6 年前 作者 mingfan 2812 次浏览 来自 问答

安卓手机扫二维码进入小程序后,获取得参数不正确,第一次扫码进入小程序正常获取到参数,第二次扫码进入小程序,获取得是上次扫码获得的参数

//app.js

var util = require(’./utils/util.js’);

App({

onLaunch: function (options) {

},

onShow: function (options) {

console.log(‘app’,options);//此处获取的参数第一次正常,第二次获取的是上次获取的参数

var self = this;

var sess;

if (options.query.session) {

sess = options.query.session;

// 打印sess

wx.showToast({

title: ‘scan:’ + sess,

});

wx.setStorageSync(‘sess’, sess);

console.log(‘扫码sess’, sess);

self.getSessionCallback();

} else if (options.query.q) {

var str = decodeURIComponent(options.query.q);

sess = str.split(’=’)[1];

// 打印sess

wx.showToast({

title: ‘scan:’ + sess,

});

wx.setStorageSync(‘sess’, sess);

console.log(‘sess=’ + sess);

self.getSessionCallback();

}

this.init();

// debugger

//1、调用微信登录接口,获取code

wx.login({

success: function (r) {

console.log(‘111’, r)

var code = r.code;//登录凭证

wx.setStorage({

key: ‘code’,

data: code,

})

if (code) {

//2、调用获取用户信息接口

wx.getUserInfo({

success: function (res) {

// console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })

//3.请求自己的服务器,解密用户信息 获取unionId等加密信息

wx.request({

url: self.globalData.server + ‘/jscode2session’,//自己的服务接口地址

method: ‘post’,

header: {

‘content-type’: ‘application/x-www-form-urlencoded’

},

data: { encryptedData: res.encryptedData, iv: res.iv, code: code },

success: function (data) {

console.log(‘成功了啊!’);

console.log(data);

//4.解密成功后 获取自己服务器返回的结果

if (data.data) {

console.log(111, data);

wx.setStorageSync(‘userInfoOfWechat’, data.data);

var userInfo = data.data;

if (sess) {

console.log(‘二维码有sess’);

userInfo.session = sess;

self.thirdPartyLogin(userInfo);

} else {

console.log(‘二维码没有sess或者没扫’);

self.getSession(data.data.openId, data.data.unionId);

}

} else {

console.log(‘解密失败’)

}

self.getSessionCallback();

},

fail: function (err) {

console.log(110, err)

}

})

},

fail: function (err) {

console.log(‘err’)

}

})

} else {

console.log(‘获取用户登录态失败!’ + r.errMsg)

}

},

fail: function () {

console.log(‘登陆失败’)

}

})

},

getState: function () {

var self = this

var data = util.getParameter(‘shareingPiano.getState’, {});

wx.request({

url: self.globalData.apiUrl,

method: ‘post’,

data: data,

success: function (res) {

console.log(res);

// debugger

if (res.statusCode !== 200 || res.data.header.code) {

wx.showToast({

title: res.data.header.desc,

})

} else {

let data = res.data.body

let currentTime = data.currentTime - 0

let startTime = data.startTime - 0

let paidTime = data.paidTime - 0

// debugger

if (currentTime && startTime && paidTime) {

console.log(‘嘿嘿嘿’);

wx.navigateTo({

url: “…/endPractice/endPractice”,

});

} else {

console.log(‘哈哈哈’);            

}

}

}

})

},

init: function () {

wx.removeStorageSync(‘qrcode’)

},

thirdPartyLogin: function (userInfo, cb) {

var self = this;

var data = util.getParameter(‘shareingPiano.thirdPartyLogin’, {

openId: userInfo.openId,

nickName: userInfo.nickName,

type: 0,

sex: userInfo.gender - 0,

city: userInfo.city,

province: userInfo.province,

country: userInfo.country,

uId: userInfo.unionId,

headImgUrl: userInfo.avatarUrl,

clientType: 0,

session: userInfo.session

});

wx.request({

url: self.globalData.apiUrl,

method: ‘post’,

data: data,

success: function (ee) {

console.log(ee);

if (ee.data.header.code) {

} else {

console.log(1211, ee);

wx.setStorageSync(‘sess’, ee.data.body.sess);

}

if (cb) {

cb(ee);

}

self.getState();

}

})

},

getSession: function (openid, unionid) {

var self = this;

var data = util.getParameter(‘shareingPiano.getSession’, {

openId: openid,

uId: unionid

});

wx.request({

url: self.globalData.apiUrl,

method: ‘post’,

data: data,

success: function (res) {

console.log(‘getsession’, res)

// debugger

if (res.statusCode !== 200 || res.data.header.code || !res.data.body.sess) {

console.info(‘获取session失败’)

self.createSession()

} else {

wx.setStorageSync(‘sess’, res.data.body.sess);

// 成功回调

self.getSessionCallback();

wx.showToast({

title: ‘getS:’ + res.data.body.sess,

})

}

}, fail: function (err) {

console.log(1211, err);

}

})

},

createSession: function () {

var self = this

var data = util.getParameter(‘shareingPiano.createSession’, {})

wx.request({

url: self.globalData.apiUrl,

method: ‘post’,

data: data,

success: function (res) {

debugger

if (res.statusCode !== 200 || res.data.header.code || !res.data.body.sessionId) {

console.info(‘获取session失败’)

} else {

wx.setStorageSync(‘sess’, res.data.body.sessionId)

let userInfo = wx.getStorageSync(‘userInfoOfWechat’)

userInfo.session = res.data.body.sessionId;

self.getSessionCallback();

}

}

})

},

getUserInfo: function (cb) {

var that = this

if (this.globalData.userInfo) {

typeof cb == “function” && cb(this.globalData.userInfo)

} else {

//调用登录接口

wx.getUserInfo({

withCredentials: true,

success: function (res) {

console.log(res)

that.globalData.userInfo = res.userInfo

typeof cb == “function” && cb(that.globalData.userInfo)

}

})

}

},

getSessionCallback: function () {

if (getCurrentPages().length != 0) {

setTimeout(function () {

getCurrentPages()[getCurrentPages().length - 1].onLoad();

}, 500)

}

},

globalData: {

userInfo: null,

apiUrl: https://spapi.ktunes.cn/reqForward,

// apiUrl: ‘http://13.13.13.10:3001/’,

server: https://spapi.ktunes.cn,

code: ‘’

}

})

回到顶部