1 Star 0 Fork 2

lightenvoy / remove-water-mark-mp

forked from Future. / remove-water-mark-mp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 3.85 KB
一键复制 编辑 原始数据 按行查看 历史
Future. 提交于 2021-04-14 18:19 . Update app.js
App({
onLaunch: function () {
this.getUserInfo();
},
/**
* 登陆并获取用户信息、token
* @param {*} callback
*/
getUserInfo: function(callback = null) {
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
var code = res.code;
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
console.log(1)
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
console.log(2)
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
this.globalData.hasUserInfo = true
if (!this.checkIsLogin()) {
console.log(3)
this.getToken(code, res.encryptedData, res.iv);
}
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
callback && callback(res);
}
})
}
}
})
}
});
},
globalData: {
userInfo: null,
hasUserInfo: false,
apiDomain: 'http://127.0.0.1:8000/api', //生产
downloadPrefix: 'http://127.0.0.1:8000/download?url=', // 通过代理服务器中转(微信限制资源域名,不同平台cdn域名千变万化)
defaultDailyFreeParseNum: 10,
},
//全局统一调用接口的方法
apiRequest: function (options) {
wx.request({
url: this.globalData.apiDomain + options.url,
method: options.method ? options.method : 'GET',
header: {
'Authorization': 'Bearer ' + wx.getStorageSync('token'),
'Accept': 'application/json',
},
dataType: 'json',
data: options.data,
success: res => {
switch (res.statusCode) {
case 200:
options.success(res);
break;
case 401:
this.toLogin();
break;
case 422:
break;
case 404:
wx.showToast({
title: '请求地址不存在',
icon: 'none'
})
break;
default:
wx.showToast({
title: '出错了~请稍后再试',
icon: 'none'
})
}
},
fail: res => {
if (options.fail) {
options.fail(res);
}
},
complete: res => {
if (options.complete) {
options.complete(res);
}
}
});
},
/**
* 验证登录
*/
checkIsLogin(autoLogin = false) {
if (wx.getStorageSync('token') != '') {
return true;
}
if (autoLogin) {
this.toLogin();
} else {
return false;
}
},
/**
* 跳转登陆页
*/
toLogin() {
this.globalData.hasUserInfo = false;
this.globalData.userInfo = null;
wx.removeStorageSync('token');
wx.showToast({
title: '请先登陆!',
icon: 'none',
success: res => {
wx.switchTab({
url: '/pages/mine/mine'
})
}
})
},
/**
* 获取token
*/
getToken(code, encryptedData, iv, callback = null) {
//调后端接口获取token
this.apiRequest({
url: '/auth/login',
method: 'POST',
data: {
'code': code,
'data': encryptedData,
'iv': iv
},
success: res => {
wx.setStorageSync('token', res.data.token);
callback && callback();
}
});
},
});
微信
1
https://gitee.com/robyte/remove-water-mark-mp.git
git@gitee.com:robyte/remove-water-mark-mp.git
robyte
remove-water-mark-mp
remove-water-mark-mp
master

搜索帮助