You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
5.6 KiB
230 lines
5.6 KiB
//app.js |
|
var Api = require('./utils/common.js'); |
|
var code; |
|
App({ |
|
onLaunch: function () { |
|
this.globalData = {}; |
|
}, |
|
onShow: function () { |
|
this.getPower(); |
|
// wx.getUserProfile({ |
|
// success: function(res) { |
|
// console.log(res.userInfo) |
|
// } |
|
// }) |
|
}, |
|
getPower: function (res) { |
|
var that = this; |
|
try { |
|
const value = wx.getStorageSync('openid') |
|
if (!value || value == null) { |
|
wx.redirectTo({ |
|
url: "/pages/login" |
|
}) |
|
} else { |
|
// that.getUser(); |
|
} |
|
} catch (e) {} |
|
}, |
|
getUser: function () { |
|
var that = this; |
|
wx.getUserProfile({ |
|
desc:'获取用户信息', |
|
success: res => { |
|
console.log('xinxi',res) |
|
// 保存用户信息到服务端 |
|
var pid = wx.getStorageSync('user_pid') |
|
if (!pid || pid == null) { |
|
pid = 0; |
|
} |
|
var iv = res.iv; |
|
var encryptedData = res.encryptedData; |
|
that.goIndex(code, iv, encryptedData, pid) |
|
} |
|
}) |
|
// wx.getSetting({ |
|
// success: setting => { |
|
wx.login({ |
|
success: function (res) { |
|
if (res.code) { |
|
code = res.code; |
|
|
|
|
|
// wx.getUserProfile({ |
|
// desc:'获取用户信息', |
|
// success: res => { |
|
// console.log('xinxi',res) |
|
// // 保存用户信息到服务端 |
|
// var pid = wx.getStorageSync('user_pid') |
|
// if (!pid || pid == null) { |
|
// pid = 0; |
|
// } |
|
// var iv = res.iv; |
|
// var encryptedData = res.encryptedData; |
|
// that.goIndex(code, iv, encryptedData, pid) |
|
// } |
|
// }) |
|
|
|
|
|
|
|
} else { |
|
console.log("error code " + res.errMsg); |
|
} |
|
} |
|
}) |
|
// } |
|
// }) |
|
}, |
|
goIndex: function (code, iv, encryptedData, pid) { |
|
var that = this; |
|
var phone; |
|
wx.showLoading({ |
|
title: '加载中', |
|
}) |
|
wx.request({ |
|
url: Api.getAPILoader(), |
|
data: { |
|
jsCode: code, |
|
}, |
|
method: "get", |
|
header: { |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
success: function (res) { |
|
var res = res.data; |
|
if (res.code == 0) { |
|
var datas = res.data; |
|
wx.request({ |
|
url: Api.getphone(), |
|
data: { |
|
encryptedData: encryptedData, |
|
iv: iv, |
|
sessionKey: datas.sessionKey, |
|
}, |
|
method: "get", |
|
header: { |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
success: function (res) { |
|
var res = res.data; |
|
if (res.code == 0) { |
|
phone = res.data.phoneNumber |
|
} |
|
}, |
|
fail: function (error) { |
|
console.log(error); |
|
} |
|
}) |
|
wx.request({ |
|
url: Api.getUserInfo(), |
|
data: { |
|
encryptedData: encryptedData, |
|
iv: iv, |
|
sessionKey: datas.sessionKey, |
|
}, |
|
method: "get", |
|
header: { |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
success: function (res) { |
|
var res = res.data; |
|
if (res.code == 0) { |
|
that.sign(datas.openid, datas.unionid, res.data, phone); |
|
} |
|
}, |
|
fail: function (error) { |
|
console.log(error); |
|
} |
|
}) |
|
} else { |
|
wx.hideLoading(); |
|
wx.showToast({ |
|
title: '登录失败', |
|
icon: 'none' |
|
}) |
|
} |
|
}, |
|
fail: function (error) { |
|
console.log(error); |
|
} |
|
}) |
|
|
|
}, |
|
sign: function (openid, unionid, info, phone) { |
|
var that = this; |
|
wx.request({ |
|
url: Api.getAPISign(), |
|
data: { |
|
openId: openid, |
|
unionId: unionid, |
|
gender: info.gender, |
|
nickname: info.nickName, |
|
userPhoto: info.avatarUrl, |
|
phone: phone |
|
}, |
|
method: "post", |
|
header: { |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
success: function (res) { |
|
var res = res.data; |
|
if (res.code == 0) { |
|
var datas = res.data; |
|
wx.setStorage({ |
|
key: 'user', |
|
data: datas, |
|
}) |
|
that.getImgUrl(); |
|
//判断用户是否初次登录 |
|
if (!wx.getStorageSync('openid')) { |
|
wx.setStorage({ |
|
key: 'openid', |
|
data: datas.openId, |
|
}); |
|
wx.setStorage({ |
|
key: 'token', |
|
data: datas.token, |
|
}); |
|
wx.switchTab({ |
|
url: "/pages/index/index" |
|
}) |
|
} else { |
|
wx.navigateBack({ |
|
delta: 1 |
|
}) |
|
} |
|
wx.hideLoading(); |
|
}else { |
|
wx.hideLoading(); |
|
wx.showToast({ |
|
title: '登录失败', |
|
icon: 'none' |
|
}) |
|
} |
|
}, |
|
fail: function (error) { |
|
console.log(error); |
|
} |
|
}) |
|
}, |
|
getImgUrl:function(){ |
|
wx.request({ |
|
url: Api.getImgUrl(), |
|
data: {}, |
|
method: "post", |
|
header: { |
|
"Content-Type": "application/x-www-form-urlencoded" |
|
}, |
|
success: function (res) { |
|
wx.setStorage({ |
|
key: 'imgUrl', |
|
data: res.data.msg, |
|
}); |
|
}, |
|
fail: function (error) { |
|
console.log(error); |
|
} |
|
}) |
|
|
|
}, |
|
}) |