const app = getApp()
const conf = require('../../lib/js/config.js');
const util = require('../../utils/util.js');
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
mechanism_id: ''
},
onLoad: function (options) {
if(wx.getStorageSync('openId') != ''){
if (app.globalData.userInfo) {
wx.switchTab({
url: '../index/index',
})
console.log(app.globalData.userInfo,1111111)
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse){
console.log(this.data.canIUse, 22222222)
app.userInfoReadyCallback = res => {
wx.switchTab({
url: '../index/index',
})
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
console.log(3333333)
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
}
if(options.id){
this.setData({
mechanism_id: options.id
})
}
},
getUserInfo: function(e) {
app.globalData.userInfo = e.detail.userInfo;
var that = this;
wx.login({
success:resp=>{
const code = resp.code;
console.log(code);
console.log(1111111111111111111111);
util.request(
conf.itUrl.getOpenid,
{
code: code,
mechanism_id: that.data.mechanism_id,
},
'post',
function (res) {
if(res.code == 0){
console.log(res.msg)
wx.showToast({
icon: "error",
title: res.msg,
duration: 1000,
})
}else{
console.log(2222222222222222222222)
let openId = res.data;
wx.setStorageSync('openId', res.data);
util.request(
conf.itUrl.saveUserInfo,
{
nickname: e.detail.userInfo.nickName,
avatarurl: e.detail.userInfo.avatarUrl,
gender: e.detail.userInfo.gender,
city: e.detail.userInfo.city,
province: e.detail.userInfo.province,
country: e.detail.userInfo.country,
openid: openId,
},
'post',
function(result){
if(result.code == 0){
that.setData({
openid: result.data,
hasUserInfo: true,
userInfo: e.detail.userInfo
})
wx.navigateBack({
delta: 0,
})
}
},
function(res){}
)
}
},
function (res) {}
)
}
})
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
你有没有配置服务器的请求合法域名呢?
https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html