//app.js
|
const QQMapWX = require("./utils/qqmap-wx-jssdk");
|
// const apiHost = 'http://39.108.148.228:80/user';
|
// const apiHost = 'https://0ifzoxq2516g.guyubao.com/user'
|
// const apiHost = 'http://120.220.4.138:9591/user'
|
const apiHost = "https://xn95128.cn/user";
|
// const apiHost = "http://192.168.110.15:7777/user";
|
// const apiHost = 'http://221.182.45.100:7777/user'
|
|
// const apiHost = 'https://zhentonggongsi.com/user'
|
|
App({
|
// 这里这么写,是要在其他界面监听,而不是在app.js中监听,而且这个监听方法,需要一个回调方法。
|
watch(method) {
|
var obj = this.globalData;
|
Object.defineProperty(obj, "resData", {
|
configurable: true,
|
enumerable: true,
|
set: function (value) {
|
this._resData = value;
|
method(value);
|
},
|
get: function () {
|
// 可以在这里打印一些东西,然后在其他界面调用getApp().globalData.name的时候,这里就会执行。
|
console.log(this._resData);
|
return this._resData;
|
},
|
});
|
},
|
//初始化socket
|
initSocket() {
|
let that = this;
|
that.globalData.localSocket = wx.connectSocket({
|
// url: "ws://192.168.110.34:9978",
|
// url: "ws://221.182.45.100:9978",
|
// url: "ws://0ifzoxq2516g.guyubao.com:9978",
|
url: "wss://xn95128.cn:9978",
|
// url: "ws://192.168.110.111:9978",
|
});
|
console.log("shuju//////////////////", this.globalData.localSocket);
|
that.globalData.localSocket.onOpen(function (res) {
|
console.log("----//////-------------", that.globalData.localSocket);
|
console.log(
|
"WebSocket连接已打开!readyState=" +
|
that.globalData.localSocket.readyState
|
);
|
that.globalData.sockTaskStatus = true;
|
let msg = {
|
code: 200,
|
method: "PING",
|
msg: "SUCCESS",
|
data: {
|
type: 1, //1:乘客,2:司机
|
userId: wx.getStorageSync("userId"), //登录人员id
|
token: wx.getStorageSync("token") || "",
|
},
|
};
|
setInterval(() => {
|
that.sendSocketMessage(JSON.stringify(msg));
|
}, 5000);
|
});
|
that.globalData.localSocket.onMessage(function (res) {
|
that.globalData.callback(res);
|
that.globalData.resData = res.data; // JSON.parse(res.data);
|
});
|
that.globalData.localSocket.onError(function (res) {
|
console.log("readyState=" + that.globalData.localSocket.readyState, res);
|
});
|
that.globalData.localSocket.onClose(function (res) {
|
that.globalData.sockTaskStatus = false;
|
console.log(
|
"WebSocket连接已关闭!readyState=" +
|
that.globalData.localSocket.readyState,
|
res
|
);
|
if (that.globalData.sockTaskStatus == true) {
|
that.initSocket();
|
}
|
});
|
},
|
|
//统一发送消息
|
sendSocketMessage: function (msg) {
|
if (this.globalData.localSocket.readyState === 1) {
|
this.globalData.localSocket.send({
|
data: msg,
|
});
|
}
|
},
|
|
//关闭socket
|
closeSocket() {
|
this.globalData.localSocket.close();
|
},
|
getFloatStr: function (num) {
|
num += "";
|
num = num.replace(/[^0-9|\.]/g, ""); //清除字符串中的非数字非.字符
|
if (/^0+/)
|
//清除字符串开头的0
|
num = num.replace(/^0+/, "");
|
if (!/\./.test(num))
|
//为整数字符串在末尾添加.00
|
num += ".00";
|
if (/^\./.test(num))
|
//字符以.开头时,在开头添加0
|
num = "0" + num;
|
num += "00"; //在字符串末尾补零
|
num = num.match(/\d+\.\d{2}/)[0];
|
console.log(num);
|
},
|
onShow(options) {
|
//socket
|
if (!this.globalData.sockTaskStatus) {
|
this.initSocket();
|
}
|
var obj = wx.getEnterOptionsSync();
|
if (options.scene == 1069) {
|
return false;
|
}
|
var comparisonToken = setInterval(() => {
|
this.globalData.comparisonToken = comparisonToken;
|
if (wx.getStorageSync("token") !== "") {
|
this.getsetTime();
|
}
|
}, 5000);
|
},
|
|
//单点登录接口
|
getsetTime() {
|
let token = wx.getStorageSync("token");
|
let header = {
|
"Content-Type": "application/json;charset=UTF-8,",
|
Authorization: "Bearer" + " " + token,
|
};
|
wx.request({
|
url: this.globalData.Url + "/api/user/comparisonToken",
|
header,
|
method: "POST",
|
success: (res) => {
|
if (res.data.code == 600 && token !== "" && token !== "quit") {
|
wx.setStorageSync("token", "");
|
wx.showModal({
|
title: "提示",
|
content: "您在其他地方登录了,是否重新登录",
|
showCancel: false,
|
confirmText: "确定", //默认是“确定”
|
confirmColor: "#F07207", //确定文字的颜色
|
success: (res) => {
|
if (res.cancel) {
|
//点击取消,默认隐藏弹框
|
} else {
|
wx.redirectTo({
|
url: "/pages/home/home",
|
});
|
clearInterval(this.globalData.comparisonToken);
|
}
|
},
|
fail: function (res) {}, //接口调用失败的回调函数
|
complete: function (res) {}, //接口调用结束的回调函数(调用成功、失败都会执行)
|
});
|
}
|
},
|
});
|
},
|
onLaunch: function (options) {
|
wx.getSystemInfo({
|
success: (res) => {
|
console.log(res);
|
// console.log('手机信息res'+res.model)
|
let modelmes = res.model;
|
if (
|
modelmes.search("iPhone X") != -1 ||
|
modelmes.search("iPhone 11") != -1
|
) {
|
this.globalData.isIphoneX = true;
|
}
|
},
|
});
|
// // 展示本地存储能力
|
// var logs = wx.getStorageSync('logs') || []
|
// logs.unshift(Date.now())
|
// wx.setStorageSync('logs', logs)
|
wx.setStorageSync("height", wx.getSystemInfoSync().windowHeight || "");
|
// 登录
|
wx.login({
|
success: (res) => {
|
// 发送 res.code 到后台换取 openId, sessionKey, unionId
|
},
|
});
|
// 获取用户信息
|
wx.getSetting({
|
success: (res) => {
|
if (res.authSetting["scope.userInfo"]) {
|
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
|
wx.getUserInfo({
|
success: (res) => {
|
// 可以将 res 发送给后台解码出 unionId
|
this.globalData.userInfo = res.userInfo;
|
|
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
|
// 所以此处加入 callback 以防止这种情况
|
if (this.userInfoReadyCallback) {
|
this.userInfoReadyCallback(res);
|
}
|
},
|
});
|
}
|
},
|
});
|
},
|
|
getQQMap() {
|
return new QQMapWX({
|
key: "PCMBZ-KGC65-TNAIK-INZ3F-2UJKO-PCFXU",
|
});
|
},
|
//showToast--弹出框
|
showToastSuccess: function (value, images, status, time) {
|
try {
|
let image = images || "";
|
wx.showToast({
|
title: value,
|
image,
|
icon: status,
|
duration: time,
|
});
|
} catch (err) {
|
console.log(err);
|
}
|
},
|
|
//正则手机号验证
|
testPhone: function (phoneNum) {
|
var myreg = /^1[3456789]\d{9}$/;
|
if (phoneNum.length == 0) {
|
wx.showToast({
|
title: "输入手机号",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else if (phoneNum.length < 11 || phoneNum.length > 11) {
|
wx.showToast({
|
title: "手机号有误",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else if (!myreg.test(phoneNum)) {
|
wx.showToast({
|
title: "手机号错误1",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else {
|
return true;
|
}
|
},
|
//验证码手机号验证
|
testPhoneCode: function (phoneNum) {
|
var myreg = /^1[3456789]\d{9}$/;
|
if (phoneNum.length == 0) {
|
wx.showToast({
|
title: "输入手机号",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else if (phoneNum.length < 11 || phoneNum.length > 11) {
|
wx.showToast({
|
title: "手机号有误",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else if (!myreg.test(phoneNum)) {
|
wx.showToast({
|
title: "手机号错误",
|
icon: "none",
|
duration: 1500,
|
});
|
return false;
|
} else {
|
console.log("1");
|
return true;
|
}
|
},
|
//request请求
|
apiRequest(url, data, method, upload) {
|
let that = this;
|
let token = wx.getStorageSync("token");
|
let header = "";
|
let havePhone = wx.getStorageSync("havePhone");
|
if (!havePhone) {
|
wx.redirectTo({
|
url: "/pages/wxChatPhone/wxChatPhone",
|
});
|
return false;
|
}
|
if (token) {
|
header = {
|
"Content-Type": "application/json;charset=UTF-8,",
|
Authorization: "Bearer" + " " + token,
|
};
|
if (method == "PUT" || method == "POST") {
|
if (upload == 1) {
|
header = {
|
"content-type": "multipart/form-data",
|
Authorization: "Bearer" + " " + token,
|
};
|
} else {
|
header = {
|
"content-type": "application/x-www-form-urlencoded",
|
Authorization: "Bearer" + " " + token,
|
};
|
}
|
}
|
} else {
|
header = {
|
"Content-Type": "application/json;charset=UTF-8,",
|
Authorization: "",
|
};
|
if (method == "PUT" || method == "POST") {
|
header = {
|
"content-type": "application/x-www-form-urlencoded",
|
Authorization: "",
|
};
|
}
|
}
|
|
return new Promise((resolve, reject) => {
|
if (upload != 2) {
|
wx.showLoading({
|
mask: true,
|
});
|
}
|
|
// 上传图片文件
|
if (upload == 1) {
|
// console.log(data);
|
wx.uploadFile({
|
url: apiHost + url,
|
header: header,
|
filePath: data,
|
name: "file",
|
formData: {},
|
success: (res) => {
|
let data = JSON.parse(res.data);
|
//do something
|
resolve(data);
|
},
|
fail: (res) => {
|
reject(res);
|
},
|
});
|
} else {
|
wx.request({
|
url: apiHost + url,
|
data: data || {},
|
header: header,
|
method: method || "GET",
|
dataType: "json",
|
responseType: "text",
|
success: (res) => {
|
// console.log(res);
|
if (upload != 2) {
|
wx.hideLoading();
|
}
|
resolve(res);
|
if (res.data.code == 200) {
|
} else if (res.data.code == 500) {
|
if (res.data.msg == "Token失效") {
|
// 清空本地token
|
wx.setStorageSync("token", "");
|
wx.showModal({
|
title: "登录失效,是否登录",
|
content: "",
|
showCancel: true,
|
cancelText: "取消",
|
cancelColor: "#000000",
|
confirmText: "确定",
|
confirmColor: "#ff3a3a",
|
success: (result) => {
|
if (result.confirm) {
|
wx.navigateTo({
|
url: "/pages/index/index",
|
});
|
}
|
},
|
});
|
} else if (res.data.msg == "验证码已失效") {
|
wx.setStorageSync("token", "");
|
wx.showToast({
|
title: "验证码有误,请重新输入",
|
icon: "none",
|
duration: 1500,
|
mask: true,
|
});
|
}
|
} else if (res.data.code == 600 || res.data.code == 700) {
|
// 清空本地token
|
if (wx.getStorageSync("token") == "") {
|
wx.showModal({
|
title: "您还未登录,是否登录",
|
content: "",
|
showCancel: false,
|
cancelColor: "#000000",
|
confirmText: "确定",
|
confirmColor: "#F07207",
|
success: (result) => {
|
if (result.confirm) {
|
wx.navigateTo({
|
url: "/pages/index/index",
|
});
|
}
|
},
|
});
|
setTimeout(() => {
|
this.setData({
|
btnDisabled: false,
|
});
|
}, 2000);
|
return;
|
} else {
|
wx.showModal({
|
title: "登录失效,是否登录",
|
content: "",
|
showCancel: false,
|
cancelColor: "#000000",
|
confirmText: "确定",
|
confirmColor: "#F07207",
|
success: (result) => {
|
if (result.confirm) {
|
wx.navigateTo({
|
url: "/pages/index/index",
|
});
|
}
|
},
|
});
|
setTimeout(() => {
|
this.setData({
|
btnDisabled: false,
|
});
|
}, 2000);
|
return;
|
}
|
} else {
|
wx.showToast({
|
title: res.data.msg,
|
icon: "none",
|
duration: 1500,
|
});
|
}
|
},
|
fail: (res) => {
|
console.log(res);
|
if (upload != 2) {
|
wx.hideLoading();
|
}
|
wx.showToast({
|
title: "网络异常,请检查网络",
|
icon: "none",
|
duration: 1500,
|
});
|
reject(res);
|
},
|
complete: (res) => {},
|
});
|
}
|
});
|
},
|
|
globalData: {
|
userInfo: null,
|
cityName: wx.getStorageSync("cityName") || "",
|
userLatitude: wx.getStorageSync("latitude") || "",
|
userLongitude: wx.getStorageSync("longitude") || "",
|
// Url:'http://192.168.110.34:7777/user',
|
// Url:'http://221.182.45.100:7777/user',
|
// Url:'https://0ifzoxq2516g.guyubao.com/user',
|
// Url:'https://okyueche.com:443/user',
|
// Url:'http://120.220.4.138:9591/user',
|
Url: "https://xn95128.cn/user",
|
// Url: "http://192.168.110.34:7777/user",
|
// Url: "http://192.168.110.111:7777/user",
|
|
localSocket: {},
|
callback: function () {},
|
sockTaskStatus: false,
|
resData: {},
|
isIphoneX: false,
|
state: 0,
|
comparisonToken: "",
|
timer: require("./utils/wxTime"),
|
},
|
});
|