import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
const app = getApp();
const {interfaceUrl} = app.globalData;
Page({
data: {
authBtnShow:false,
code:"",
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onShow: function (options) {
const that = this;
wx.login({
success(res){
const {code} = res;
that.setData({
code,
},()=>{
that.getAuthSetting();
})
}
})
},
getAuthSetting(){
const that = this;
wx.getSetting({
success (res){
if (res.authSetting['scope.userInfo']) {
that.setData({
authBtnShow:false,
})
wx.getUserInfo({
success: function(res) {
const {encryptedData,iv} = res;
that.loginHandle({encryptedData,iv});
}
})
}else{
that.setData({
authBtnShow:true,
})
}
}
})
},
loginHandle(authData){
wx.showLoading({
title: '校验信息...',
})
const that = this;
const {code} = this.data;
const ob = {
code,
...authData,
}
wx.request({
url: `${interfaceUrl}/api/common/miniLogin`,
method:"POST",
data:ob,
success(res){
wx.hideLoading()
const {data} = res;
if(data.code!=200){
wx.hideLoading()
Dialog.alert({
title: '提示',
showConfirmButton:false,
showCancelButton:false,
message: data.msg,
})
return false;
}
wx.setStorage({
data: data.token,
key: 'token',
success(res){
wx.hideLoading()
that.launchOptionLogin();
},
fail(err){
console.log(err);
}
})
},
fail(err){
wx.hideLoading()
Dialog.alert({
title: '错误',
message: err.response.data.msg,
})
}
})
},
launchOptionLogin(){
const that = this;
const dataInfo = wx.getLaunchOptionsSync();
console.log("场景值:",dataInfo);
const {scene,query} = dataInfo;
const {taskid} = query;
if((scene=="1043" || scene=="1014") && query && taskid){
wx.navigateTo({
url:`/pages/tranManage/tranDetail/index?taskid=${taskid}`
})
}else{
wx.switchTab({
url:"/pages/index/index"
})
}
},
bindGetUserInfo (e) {
const {encryptedData,iv} = e.detail;
if(encryptedData && iv){
this.loginHandle({encryptedData,iv});
}else{
Notify({ type: 'warning', message: '请先授权登录', duration: 1000, });
}
}
})