You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

479 lines
14 KiB
JavaScript

2 years ago
App({
2 years ago
globalData: {
userInfo: null,
2 years ago
ip: 'https://test.renminshitang.com.cn/daotian/api_dev',
// ip: "https://daotian.matripe.com.cn",
2 years ago
// ip: 'http://localhost:8001',
sessionId: "",
header: {
2 years ago
"content-type": "application/x-www-form-urlencoded",
appId:"wxb0c590fd696b79be",
"g-open-env":'MP_MINI',
2 years ago
Authorization: "",
Cookie: "",
},
headers2: {
2 years ago
"content-type": "multipart/form-data",
appId:"wxb0c590fd696b79be",
"g-open-env":'MP_MINI',
2 years ago
Authorization: "",
Cookie: "",
},
headers: {
2 years ago
"content-type": "application/x-www-form-urlencoded",
appId:"wxb0c590fd696b79be",
"g-open-env":'MP_MINI',
2 years ago
Authorization: "",
Cookie: "",
},
userLoginTokenInfo: {
tel: "", //用户电话
token: "", //用户token
},
isLogin: false, //是否登录
loginUserInfo: {
id: "",
userName: "",
tel: "",
imgSrc: "",
},
lng: "",
lat: "",
autoLoginTag: 0, //是否是自动登录0否、1是
isScanQRCodes: 0, //是否是扫码进来的0否、1是
hasUserInfo: 0, //是否有用户授权信息
agencyStatus: 0, //是否是代理
openId: "",
unionid: "",
isCommission: 0, //是否计算佣金0否、1是
user: {}, //用户信息
serviceInfo: {}, // 客服电话
version: null,
loginKey:'',
2 years ago
appId:'wxb0c590fd696b79be'
2 years ago
},
// "permission": {
// "scope.userLocation": {
// "desc": "你的位置信息将用于小程序位置接口的效果展示"
// }
// },
onLaunch() {
let that = this;
wx.hideShareMenu();
Array.prototype.logResult = function (data) {
console.log("Array");
return data;
};
const updateManager = wx.getUpdateManager();
2 years ago
updateManager.onCheckForUpdate(function (res) {
console.log("onCheckForUpdate====", res);
// 请求完新版本信息的回调
if (res.hasUpdate) {
console.log("res.hasUpdate====");
updateManager.onUpdateReady(function () {
wx.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
2 years ago
confirmColor: "#ff4400",
2 years ago
success: function (res) {
console.log("success====", res);
// res: {errMsg: "showModal: ok", cancel: false, confirm: true}
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: "已经有新版本了哟~",
content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~",
2 years ago
confirmColor: "#ff4400",
2 years ago
});
});
}
});
2 years ago
// 获取小程序版本
const miniProgram = wx.getAccountInfoSync();
console.log(miniProgram);
this.globalData.version = miniProgram.miniProgram.version || "1.1.1";
that.getServiceInfo().then(() => {
console.log("getServiceInfo");
});
2 years ago
2 years ago
// 展示本地存储能力
const { statusBarHeight, platform } = wx.getSystemInfoSync();
2 years ago
2 years ago
const { top, height } = wx.getMenuButtonBoundingClientRect();
console.log(statusBarHeight);
console.log(wx.getMenuButtonBoundingClientRect());
console.log(top, height);
// 状态栏高度
wx.setStorageSync("statusBarHeight", statusBarHeight);
// 胶囊按钮高度 一般是32 如果获取不到就使用32
wx.setStorageSync("menuButtonHeight", height ? height : 32);
// 胶囊按钮信息
wx.setStorageSync("menuButtonInfo", wx.getMenuButtonBoundingClientRect());
// 判断胶囊按钮信息是否成功获取
if (top && top !== 0 && height && height !== 0) {
const navigationBarHeight = (top - statusBarHeight) * 2 + height;
// 导航栏高度
wx.setStorageSync("navigationBarHeight", navigationBarHeight);
} else {
wx.setStorageSync("navigationBarHeight", platform === "android" ? 48 : 40);
}
const logs = wx.getStorageSync("logs") || [];
logs.unshift(Date.now());
wx.setStorageSync("logs", logs);
this.getLoginUserTokenInfo();
// 登录
wx.login({
success: (res) => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
// this.appLoginByTel()
},
});
// 引入云开发
// wx.cloud.init({
// env: "cloud1-2gnvowo5e7ab00ec",
// });
},
isEmptyCheck: function (str) {
if (str == null || str == "" || str == "null" || str == "undefined" || typeof str == "undefined") {
return true;
}
return false;
},
isNotEmptyCheck: function (str) {
return !this.isEmptyCheck(str);
},
logout: function () {
var that = this;
return new Promise(function (reslove, reject) {
try {
wx.removeStorageSync("loginUserTokenInfo");
that.globalData.autoLoginTag = 0;
that.globalData.sessionId = "";
that.globalData.loginUserInfo = {};
that.globalData.userLoginTokenInfo = {};
that.globalData.headers.Cookie = "";
that.globalData.headers2.Cookie = "";
that.globalData.header.Authorization = "";
that.globalData.headers.Authorization = "";
that.globalData.headers2.Authorization = "";
that.globalData.isLogin = false;
that.globalData.hasUserInfo = 0;
reslove();
} catch (e) {
console.log("退出登录失败");
console.log(e);
reject();
}
});
},
load: function (text) {
wx.showLoading({
title: text,
mask: true,
});
},
hideLoad: function () {
wx.hideLoading();
},
showTips: function (_that, msg) {
/*_that.setData({
popErrorMsg: msg,
pop: 1,
});
setTimeout(() => {
_that.setData({
popErrorMsg: '',
pop: 0,
});
return;
}, 2000)*/
wx.showToast({
icon: "none",
title: msg,
duration: 3000,
});
},
setLoginUserTokenInfo: function (tel, token) {
var that = this;
return new Promise(function (resolve, reject) {
try {
that.globalData.userLoginTokenInfo.tel = tel;
that.globalData.userLoginTokenInfo.token = token;
2 years ago
// debugger
2 years ago
wx.setStorageSync("loginUserTokenInfo", that.globalData.userLoginTokenInfo);
that.appLoginByTel(resolve, reject);
} catch (e) {
console.log("设置登录信息错误");
console.log(e);
reject();
}
});
},
appLoginByTel: function (resolve, reject) {
var that = this;
2 years ago
2 years ago
var channelContactId = "";
try {
var value = wx.getStorageSync("fromQrCodeChannelContactId");
if (value) {
console.log("fromQrCodeChannelContactId======", value);
channelContactId = value;
}
} catch (e) {
console.log("获取缓存设置的参数错误:", e);
}
var agencyUserId = "";
try {
var value = wx.getStorageSync("storageSyncAgencyUserId");
if (value) {
console.log("storageSyncAgencyUserId======", value);
agencyUserId = value;
}
} catch (e) {
console.log("获取缓存设置的参数错误:", e);
}
wx.request({
url: that.globalData.ip + "/appLoginByTelYiShouDan",
data: {
tel: that.globalData.userLoginTokenInfo.tel,
autoLoginTag: that.globalData.autoLoginTag,
isScanQRCodes: that.globalData.isScanQRCodes,
channelContactId: channelContactId,
agencyUserId: agencyUserId,
openId: that.globalData.openId,
unionid: that.globalData.unionid,
loginKey: that.globalData.loginKey,
appId:that.globalData.appId,
2 years ago
2 years ago
},
header: {
"content-type": "application/json",
},
success: function (res) {
console.log(res);
2 years ago
2 years ago
if (res.data.status == 200) {
that.globalData.user = res.data.data.user;
// that.globalData.user.idauth = 0;
that.globalData.sessionId = res.data.data.sessionId;
that.globalData.headers.Cookie = "JSESSIONID=" + res.data.data.sessionId;
2 years ago
that.globalData.headers2.Cookie = "JSESSIONID=" + res.data.data.sessionId;
that.globalData.headers.Authorization = "Bearer " + res.data.data.token;
that.globalData.headers2.Authorization = "Bearer " + res.data.data.token;
2 years ago
that.globalData.isLogin = true;
that.globalData.read = res.data.data.read;
that.globalData.userLoginTokenInfo.tel = res.data.data.tel;
that.globalData.userLoginTokenInfo.token = res.data.data.token;
(that.globalData.hasUserInfo = res.data.data.imgSrc != "" && res.data.data.imgSrc != null && res.data.data.imgSrc != undefined && res.data.data.nickName != "" && res.data.data.nickName != null && res.data.data.nickName != undefined ? 1 : 0), (that.globalData.loginUserInfo = res.data.data);
wx.setStorageSync("loginUserTokenInfo", that.globalData.userLoginTokenInfo);
console.log("readySet");
wx.showToast({
title: "登录成功",
icon: "success",
duration: 2000,
});
2 years ago
2 years ago
that.getServiceInfo().then(() => {
console.log("getServiceInfo");
});
console.log("appLoginByTel" + "+++++++++++++++++++++++++++++++++++++++++++");
resolve();
} else {
wx.showToast({
icon: "none",
title: res.data.msg,
duration: 2000,
});
reject();
}
if (that.userLoginCallback) {
that.userLoginCallback(res);
}
console.log("login====result=======1");
console.log(that.globalData);
console.log("login====result=======2");
},
});
},
/**
* 获取客户经理信息
*
*
*/
getServiceInfo() {
let that = this;
console.log("fromLogin");
return new Promise((resolve, reject) => {
wx.request({
url: that.globalData.ip + "/costumer/manager",
header: that.globalData.headers,
success(res) {
console.log(res);
if (res.data.status == 200) {
that.globalData.serviceInfo = res.data.data;
console.log(that.globalData.serviceInfo);
if (that.globalData.serviceInfo.workPhone) {
let tel = that.globalData.serviceInfo.workPhone;
let num1 = tel.slice(0, 3);
let num2 = tel.slice(3, 7);
let num3 = tel.slice(7, 11);
that.globalData.serviceInfo.tel = num1 + "-" + num2 + "-" + num3;
console.log(that.globalData.serviceInfo.tel);
}
resolve(true);
}
},
});
});
},
getLoginUserTokenInfo: function () {
var that = this;
// debugger;
return new Promise((resloveOutter, rejectOutter) => {
2 years ago
try {
var value = wx.getStorageSync("loginUserTokenInfo");
console.log(value);
if (value) {
that.globalData.userLoginTokenInfo = value;
// console.log(that.isNotEmptyCheck(that.globalData.userLoginTokenInfo.token));
if (that.isNotEmptyCheck(that.globalData.userLoginTokenInfo.token)) {
let token = that.globalData.userLoginTokenInfo.token;
that.globalData.header.Authorization = "Bearer " + token;
that.globalData.headers2.Authorization = "Bearer " + token;
that.globalData.headers.Authorization = "Bearer " + token;
console.log(that.globalData.headers);
wx.request({
url: that.globalData.ip + "/checkToken",
method: "GET",
header: that.globalData.headers,
2 years ago
data:{loginKey: that.globalData.loginKey},
2 years ago
success: function (res) {
console.log(res);
if (res.data.status == 200) {
if (wx.getStorageSync("messageClick" + res.data.data.id)) {
console.log("messageClick" + res.data.data.id);
} else {
wx.setStorageSync("messageClick" + res.data.data.id, false);
}
that.globalData.isLogin = true;
that.globalData.test = "b";
that.globalData.user = res.data.data.user;
// that.globalData.user.idauth = 0;
that.globalData.loginUserInfo = res.data.data;
console.log(that.globalData.loginUserInfo);
that.getServiceInfo().then(() => {
console.log("getServiceInfo");
});
if (that.userLoginCallback) {
that.userLoginCallback(res);
}
2 years ago
let loginKeyTmp = that.globalData.loginKey;
resloveOutter(loginKeyTmp);
2 years ago
} else {
//已过期
that.globalData.autoLoginTag = 1;
// that.appLoginByTel();
// that.globalData.isLogin = false;
return new Promise(function (resolve, reject) {
try {
2 years ago
that.appLoginByTel(resolve, reject);
2 years ago
} catch (e) {
console.log("设置登录信息错误");
2 years ago
console.log(e);
reject();
2 years ago
}
});
2 years ago
rejectOutter();
2 years ago
}
},
});
// console.log(that.globalData.isLogin);
} else if (that.isNotEmptyCheck(that.globalData.userLoginTokenInfo.tel)) {
that.globalData.autoLoginTag = 1;
2 years ago
// that.appLoginByTel();
2 years ago
return new Promise(function (resolve, reject) {
try {
that.appLoginByTel(resolve, reject);
} catch (e) {
console.log("设置登录信息错误");
console.log(e);
reject();
}
});
2 years ago
rejectOutter();
2 years ago
}
} else {
2 years ago
console.log("nologinInfo");
2 years ago
if (that.userLoginCallback) {
that.userLoginCallback();
2 years ago
}
2 years ago
rejectOutter();
2 years ago
}
2 years ago
} catch (e) {
2 years ago
}
})
2 years ago
},
onTabItemTap(e) {
console.log(123);
console.log(e);
let that = this;
if (!that.globalData.isLogin) {
// wx.showModal({
// title: '提示',
// content: '账号尚未登录,请先登录账号',
// success: res => {
// if (res.confirm) {
wx.navigateTo({
url: "/pages/login/login",
});
// } else if (res.cancel) {
// wx.reLaunch({
// url: '/pages/main/homePage'
// })
// }
// }
// })
}
},
dialog: function (title, content, btxt) {
wx.showModal({
title: title,
content: content,
showCancel: false,
confirmColor: "#0dcc91",
confirmText: btxt,
success: function (res) {
if (res.confirm) {
console.log("用户点击确定");
} else if (res.cancel) {
console.log("用户点击取消");
}
},
});
},
});