代码拉取完成,页面将自动刷新
//app.js
let https = require('utils/https.js')
function Login(code, userInfo) {
let that = this
let nickName = userInfo.nickName;
let avatarUrl = userInfo.avatarUrl;
let gender = userInfo.gender;
let language = userInfo.language;
let url = `https://api.stiles.cc/wechat/onLogin?code=${code}&nickName=${nickName}&avatarUrl=${avatarUrl}&gender=${gender}&language=${language}`;
https.get(url, function (res) {
let token = res.token;
wx.setStorageSync('token', token);
},function(){
//wx.setStorageSync('token', "defalutToken")
// 在这里你要考虑到用户登录失败的情况
},false)
}
App({
globalData: {
domain: "https://api.stiles.cc",
detailUrl:"https://api.stiles.cc/novels/get",
novelsUrl: "https://api.stiles.cc/novels",
recommendUrl: "https://api.stiles.cc/novels/recommend",
chaptersUrl: "https://api.stiles.cc/novels/getChapters",
chaptersContentUrl: "https://api.stiles.cc/novels/getChaptersContent",
categoryUrl: "https://api.stiles.cc/novels/getCategory",
searchUrl: "https://api.stiles.cc/novels/search",
feedbackUrl: "https://api.stiles.cc/feedback/send",
novelSerialNumbersUrl: "https://api.stiles.cc/novels/getNovelSerialNumbers",
searchKeywords: "https://api.stiles.cc/novels/getSearchKeywords",
token:null,
userInfo: null
},
onLaunch: function () {
// wx.clearStorage();
this.getUserInfo();
this.removeCache();
},
removeCache:function(){
let value = wx.getStorageSync('cateListTime')
let nowTime = new Date().getTime();
if(value){
if (value < nowTime) {
wx.removeStorage("cateList");
}
}
value = wx.getStorageSync('searchKeysTime')
if (value) {
if (value < nowTime) {
wx.removeStorage("searchKeysTime");
}
}
},
WechatLogin:function(){
let that = this
wx.login({
success: function (res) {
//登录成功
if (res.code) {
// 这里是用户的授权信息每次都不一样
let code = res.code;
wx.getUserInfo({
success: function (info) {
wx.setStorageSync('userInfo', info.userInfo);
that.globalData.userInfo = info.userInfo;
typeof cb == "function" && cb(that.globalData.userInfo)
// 请求自己的服务器
Login(code, info.userInfo);
},fail:function(e){
console.log(e);
}
})
}
}, fail: function (e) {
console.log(e);
}
})
},
getUserInfo: function (cb) {
let that = this
if (this.globalData.userInfo) {
console.log(cb);
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
wx.checkSession({
success: function () {
let userInfo=wx.getStorageSync('userInfo');
if(userInfo){
that.globalData.userInfo = userInfo;
}else{
that.WechatLogin();
}
console.log("success login");
},fail(){
that.WechatLogin();
}});
}
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。