588 lines
19 KiB
JavaScript
588 lines
19 KiB
JavaScript
var initial_url = 'https://offer.gter.net/miniprogram/index/init';
|
|
var app = null;
|
|
import util from './util'
|
|
|
|
function initial(self) {
|
|
app = self;
|
|
|
|
if (app.globalData.options.scene == 1154) {
|
|
app.globalData.code = 200
|
|
// app.globalData.code1 = res.code
|
|
app.globalData.isgetinit = true
|
|
sendData(200)
|
|
} else {
|
|
login().then(res => {
|
|
app.globalData.code1 = res.code
|
|
app.globalData.isgetinit = true
|
|
sendData(res.code)
|
|
});
|
|
}
|
|
|
|
return true;
|
|
}
|
|
//获取登录信息
|
|
let isgetUserInfo = false;
|
|
|
|
function getUserInfo(e) {
|
|
return new Promise((resolve, reject) => {
|
|
if (isgetUserInfo) {
|
|
return false
|
|
}
|
|
isgetUserInfo = true;
|
|
wx.showLoading({
|
|
title: '正在授权',
|
|
})
|
|
|
|
login().then(res => {
|
|
|
|
let info = Object.assign({}, res, e)
|
|
// request(app.globalData.config.user.synchronize, info).then(data => {
|
|
// request(app.globalData.baseURL + "/miniprogram/register/sendsms", info).then(data => {
|
|
request(app.globalData.baseURL + "/miniprogram/synchronize", info).then(data => {
|
|
wx.hideLoading()
|
|
if (data.code == 200) {
|
|
//授权成功
|
|
wx.showToast({
|
|
title: data.message,
|
|
})
|
|
|
|
// wx.setStorageSync('Authorization', data.data.session)
|
|
app.globalData.user = data.data;
|
|
app.globalData.isUserAuthorization = 2;
|
|
app.globalData.isgetinit = true;
|
|
app.globalData.code = data.code;
|
|
isgetUserInfo = false;
|
|
app.computeNavigateBarHeight()
|
|
resolve(data)
|
|
} else {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: data.message,
|
|
})
|
|
isgetUserInfo = false;
|
|
reject(data)
|
|
}
|
|
})
|
|
}).catch(res => {
|
|
wx.hideLoading()
|
|
console.log(res)
|
|
})
|
|
})
|
|
|
|
}
|
|
//用户登录
|
|
function login() {
|
|
return new Promise((resolve, reject) => wx.login({
|
|
success: resolve,
|
|
fail: reject
|
|
}))
|
|
}
|
|
|
|
function sendData(code) {
|
|
haveAuthorization()
|
|
let Authorization = wx.getStorageSync('Authorization') || ''
|
|
let session = wx.getStorageSync('session') || ''
|
|
let miucms_session = wx.getStorageSync('_miucms_session') || ''
|
|
|
|
// 如果要测试进入不登录就运行下面代码
|
|
// Authorization = ''
|
|
// session = ''
|
|
// miucms_session = ''
|
|
// code = ''
|
|
|
|
wx.request({
|
|
url: initial_url,
|
|
data: {
|
|
session,
|
|
code: code,
|
|
options: app.globalData.options || '',
|
|
appid: "wxd7ebf76a0a87057d",
|
|
},
|
|
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
|
|
header: {
|
|
'content-type': 'application/json',
|
|
'Accept': 'application/json, text/plain',
|
|
'Cookie': '_miucms_session_gter=' + miucms_session,
|
|
'Authorization': Authorization, //
|
|
envVersion: getApp().globalData['envVersion'],
|
|
},
|
|
success: function (res) {
|
|
var data = typeof res.data == 'string' ? JSON.parse(res.data) : res.data;
|
|
|
|
// util.bindingUser(data.user)
|
|
wx.xstat && wx.xstat.setUserid(data.user.uid); // 登录后手动设置用户id , 如果有
|
|
|
|
// 将配置与用户session存于本地
|
|
// if (!data.session) {
|
|
// // 这里报错.用户授权不成功
|
|
// return false;
|
|
// }
|
|
|
|
app.globalData.agreeacceptterms = data.user.agreeacceptterms
|
|
// 判断是否要 跳去确认页面
|
|
if (data.user.agreeacceptterms == 0 && app.globalData.scene != 1047) {
|
|
let pages = getCurrentPages(); //页面对象
|
|
let prevpage = pages[pages.length - 1]; //上一个页面对象
|
|
let path = prevpage.route;
|
|
app.globalData.initPath = path
|
|
wx.redirectTo({
|
|
url: '/pages/accredit/accredit',
|
|
})
|
|
}
|
|
|
|
// wx.setStorageSync('session', data.user.session == null ? '' : data.user.session)
|
|
app.globalData.session = data.user.session;
|
|
// wx.setStorageSync('Authorization', data.user.session == null ? '' : data.user.session)
|
|
app.globalData.Authorization = data.user.session;
|
|
app.globalData.isUserAuthorization = data.user.session && data.user.session.length > 0 ? 2 : 1;
|
|
// app.globalData.expiration_day = data.expiration_day;
|
|
app.globalData.config = data.config;
|
|
// shareConfig 分享功能的 title 数据
|
|
// app.globalData.shareConfig = data.shareConfig;
|
|
app.globalData.shareConfig['mj'] = data.shareConfig['interviewExperience'];
|
|
// app.globalData.notice = data.notice;
|
|
// app.globalData.title = data.title;
|
|
app.globalData.user = data.user;
|
|
// app.globalData.status = data.status;
|
|
app.globalData.StudentapartmentNew = data.StudentapartmentNew;
|
|
app.globalData.code = data.code;
|
|
app.globalData.offerSummarypost = data.config.offerSummarypost;
|
|
app.globalData.offerpost = data.config.offerpost;
|
|
app.globalData.postList = data.postList;
|
|
app.globalData.navigation = data.navigation;
|
|
app.globalData.gtergreenonionqrcode = data.gtergreenonionqrcode;
|
|
app.globalData.popup = data.popup;
|
|
|
|
app.globalData.header = {
|
|
'content-type': 'application/json',
|
|
'Accept': 'application/json, text/plain',
|
|
'Cookie': '_miucms_session_gter=' + data._miucms_session,
|
|
'Authorization': wx.getStorageSync('Authorization') || ''
|
|
};
|
|
|
|
let iswechatmessagepush = data.user['iswechatmessagepush']
|
|
// iswechatmessagepush = 0
|
|
// data.user['ishongkonganswer'] = 1
|
|
if (iswechatmessagepush >= 0) app.globalData.iswechatmessagepush = iswechatmessagepush
|
|
app.globalData['ishongkonganswer'] = data.user['ishongkonganswer']
|
|
|
|
app.globalData.initializeLoginState = data.user.uid > 0 ? true : false;
|
|
|
|
// 开屏广告
|
|
// if (app.globalData['envVersion'] != "develop") {
|
|
// app.globalData.offerkaipingadvertisement = data.offerkaipingadvertisement || {}
|
|
// const openAdTimer = wx.getStorageSync('openAdTimer');
|
|
// if (openAdTimer && isToday(openAdTimer)) app.globalData.offerkaipingadvertisementState = true
|
|
// }
|
|
|
|
app.globalData.offerkaipingadvertisement = data.offerkaipingadvertisement || {}
|
|
const openAdTimer = wx.getStorageSync("openAdTimer")
|
|
if (openAdTimer && isToday(openAdTimer)) app.globalData.offerkaipingadvertisementState = true
|
|
|
|
},
|
|
fail: function () {
|
|
// fail
|
|
},
|
|
complete: function () {
|
|
useSocket()
|
|
// complete
|
|
}
|
|
})
|
|
}
|
|
|
|
function isToday(timestamp) {
|
|
const date = new Date(timestamp);
|
|
const today = new Date();
|
|
return date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth() && date.getDate() === today.getDate();
|
|
}
|
|
|
|
// 当前页开始
|
|
var timer = null;
|
|
|
|
function pageStart(app) {
|
|
return new Promise((resolve, reject) => {
|
|
if (app.globalData.options.scene == 1154) {
|
|
resolve()
|
|
}
|
|
if (app.globalData.code == 200) {
|
|
resolve()
|
|
} else {
|
|
const timer = setInterval(function () {
|
|
if (app.globalData.code == 200) {
|
|
app.globalData.canGetData = true;
|
|
resolve()
|
|
clearInterval(timer)
|
|
}
|
|
if (app.globalData.code > 1 && app.globalData.code != 200) {
|
|
if (app.globalData.route && app.globalData.route.statindex) {
|
|
// 可以拿数据
|
|
clearInterval(timer)
|
|
app.globalData.canGetData = true;
|
|
resolve()
|
|
} else {
|
|
wx.showToast({
|
|
title: '' + app.globalData.code,
|
|
icon: 'none'
|
|
})
|
|
clearInterval(timer)
|
|
reject()
|
|
}
|
|
|
|
}
|
|
}, 200)
|
|
}
|
|
})
|
|
}
|
|
|
|
function closePage() {
|
|
clearInterval(timer)
|
|
}
|
|
|
|
//封装Request请求方法
|
|
function request(url, data = {}, needGet = false) {
|
|
haveAuthorization()
|
|
return new Promise((resolve, reject) => {
|
|
let pages = getCurrentPages();
|
|
|
|
let path = '';
|
|
if (pages.length > 0) {
|
|
path = pages[pages.length - 1].route;
|
|
}
|
|
|
|
let send_data = Object.assign({}, app.globalData.options, {
|
|
session: wx.getStorageSync('session'),
|
|
path,
|
|
}, data)
|
|
// try {
|
|
wx.request({
|
|
url: url,
|
|
data: send_data,
|
|
header: {
|
|
'content-type': 'application/json',
|
|
'Accept': 'application/json, text/plain',
|
|
'Cookie': '_miucms_session_gter=' + wx.getStorageSync('_miucms_session'),
|
|
'Authorization': wx.getStorageSync('Authorization') || '',
|
|
envVersion: getApp().globalData['envVersion'],
|
|
},
|
|
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
|
|
success: function (res) {
|
|
// wx.hideNavigationBarLoading()
|
|
var json = res.data;
|
|
if (typeof json != 'object') {
|
|
if (json != null) {
|
|
json = json.replace("\ufeff", "");
|
|
var jj = JSON.parse(json);
|
|
res.data = jj;
|
|
}
|
|
}
|
|
var data = res.data;
|
|
resolve(data)
|
|
|
|
},
|
|
fail: function (msg) {
|
|
wx.hideLoading();
|
|
requestFail(msg)
|
|
|
|
// 下面的代码 showToast 感觉出不来 因为 title 是 String
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: 'none',
|
|
duration: 2000,
|
|
mask: true
|
|
})
|
|
reject('fail')
|
|
}
|
|
})
|
|
// }catch(e){
|
|
// console.log(e)
|
|
// }
|
|
})
|
|
}
|
|
// 验证验证码并注册
|
|
function register(o) {
|
|
haveAuthorization()
|
|
var config = wx.getStorageSync('config')
|
|
var self = this;
|
|
wx.request({
|
|
url: config.user.register,
|
|
data: {
|
|
session: wx.getStorageSync('session'),
|
|
data: wx.getStorageSync('rawData'),
|
|
uniqid: null, //上一步获取的ID
|
|
code: null, //接收到的验证码
|
|
},
|
|
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
|
|
header: {
|
|
'content-type': 'application/json',
|
|
'Accept': 'application/json, text/plain'
|
|
},
|
|
success: function (res) {
|
|
var data = typeof res.data == 'string' ? JSON.parse(res.data) : res.data;
|
|
|
|
initial(initial_url, app)
|
|
},
|
|
fail: function () {
|
|
// fail
|
|
},
|
|
complete: function () {
|
|
// complete
|
|
}
|
|
})
|
|
}
|
|
|
|
function sendShare(e) {
|
|
let froms = e['from'];
|
|
if (froms == "button") {
|
|
froms = e.target.dataset.btn
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (!app.globalData.canGetData) {
|
|
reject()
|
|
return false
|
|
}
|
|
request(app.globalData.config.user.share, {
|
|
'from': froms
|
|
}).then(data => {
|
|
if (data.code == 200) {
|
|
//授权成功
|
|
|
|
resolve(data.data)
|
|
} else {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: data.message,
|
|
})
|
|
reject(data)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
// 计算元素宽高
|
|
function caculateQueryHeight(className) {
|
|
return new Promise((resolve, reject) => {
|
|
const query = wx.createSelectorQuery();
|
|
query.select('.' + className).boundingClientRect(function (rect) {
|
|
resolve({
|
|
width: rect.width,
|
|
height: rect.height,
|
|
left: rect.left,
|
|
right: rect.right,
|
|
top: rect.top,
|
|
bottom: rect.bottom
|
|
})
|
|
}).exec();
|
|
})
|
|
}
|
|
|
|
// 处理请求网络错误 不用导出 miucms 文件
|
|
const requestFail = (res) => {
|
|
if (res.errMsg.indexOf('time out') > -1 || res.errMsg.indexOf('timeout') > -1) {
|
|
wx.showToast({
|
|
title: '请求超时,请检查您的网络',
|
|
icon: 'none'
|
|
})
|
|
} else if (res.errMsg.indexOf('connect error') > -1) {
|
|
wx.showToast({
|
|
title: '当前网络不佳,请稍后重试',
|
|
icon: 'none'
|
|
})
|
|
} else {
|
|
wx.showToast({
|
|
title: '加载数据失败,请稍后尝试',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
};
|
|
|
|
|
|
// 验证 Authorization 存不存再 ,如不存在创建一个
|
|
const haveAuthorization = () => {
|
|
let Authorization = wx.getStorageSync('Authorization')
|
|
if (!Authorization) {
|
|
Authorization = app.randomString(32)
|
|
wx.setStorageSync('Authorization', Authorization)
|
|
wx.setStorageSync('session', Authorization)
|
|
}
|
|
}
|
|
|
|
let restartCount = 0 // 重启次数 超过5次不在重启
|
|
|
|
// 开启socket
|
|
const useSocket = () => {
|
|
if (app.globalData.isConnected) {
|
|
console.log("已经连接,不再重复连接")
|
|
return // 如果已经连接,则不再创建新的 socketTask
|
|
}
|
|
app.globalData.socketTask = wx.connectSocket({
|
|
url: `wss://socket.gter.net/?token=${wx.getStorageSync('Authorization') || ''}`,
|
|
success: function (res) {
|
|
restartCount = 0
|
|
app.globalData.isConnected = true // 连接成功,设置为 true
|
|
},
|
|
fail: function (err) {
|
|
app.globalData.isConnected = false // 连接成功,设置为 true
|
|
}
|
|
})
|
|
app.globalData.socketTask.onOpen(function () {
|
|
// 初始化发消息
|
|
if (wx.getStorageSync('Authorization')) {
|
|
let dataToSend = {
|
|
type: "bind",
|
|
data: {
|
|
token: wx.getStorageSync('Authorization') || '',
|
|
app: "wxd7ebf76a0a87057d",
|
|
// uid: user.uid || 0
|
|
}
|
|
}
|
|
app.globalData.socketTask.send({
|
|
data: JSON.stringify(dataToSend),
|
|
})
|
|
}
|
|
// 开始定时发
|
|
setTimeout(() => timedTransmission(), 50000)
|
|
|
|
// // 主动发一个 获取 未读消息
|
|
// let dataToSend = {
|
|
// type: 'messageCount'
|
|
// }
|
|
// app.globalData.socketTask.send({
|
|
// data: JSON.stringify(dataToSend)
|
|
// })
|
|
})
|
|
|
|
app.globalData.socketTask.onMessage(res => {
|
|
let data = res.data
|
|
data = JSON.parse(data)
|
|
// console.log("全局收到新消息", data);
|
|
|
|
// 接收到 签到信息
|
|
if (data.type == 'todaysigned') {
|
|
app.globalData.signInState = data.data
|
|
app.globalData.getSignInState = true
|
|
}
|
|
|
|
// 接受到 新消息
|
|
if (data.type == 'message') {
|
|
// console.log("全局收到新消息", data);
|
|
let targetData = JSON.parse(JSON.stringify(app.globalData.messageData))
|
|
targetData.push(data.data)
|
|
// app.globalData.messageData.push(data.data)
|
|
app.globalData.messageData = targetData
|
|
|
|
// console.log("messageData", app.globalData.messageData);
|
|
}
|
|
|
|
// 接收到 发送新消息的 返回
|
|
if (data.type == 'messageSend') {
|
|
const id = data.data.id || 0
|
|
let targetData = JSON.parse(JSON.stringify(app.globalData.messageSendData))
|
|
targetData.push({
|
|
code: data.code,
|
|
id,
|
|
message: data.message,
|
|
})
|
|
app.globalData.messageSendData = targetData
|
|
|
|
// wx.showToast({
|
|
// icon: 'none',
|
|
// title: data.message,
|
|
// })
|
|
}
|
|
|
|
// 获取未读消息的
|
|
if (data.type == 'messageCount') {
|
|
const count = data.data.count
|
|
app.globalData.unreadMessageCount = count
|
|
// console.log("data.data.popup", data.data.popup);
|
|
app.globalData.popup = data.data.popup || null
|
|
}
|
|
|
|
|
|
|
|
if (app.globalData.onMessage) app.globalData.onMessage(data)
|
|
|
|
})
|
|
|
|
// 自动发送
|
|
let ii = 0
|
|
setInterval(() => {
|
|
return
|
|
ii++
|
|
let data = {
|
|
event: "text",
|
|
message: ii + '',
|
|
messageid: "360",
|
|
talkdataid: 51,
|
|
talkid: 37,
|
|
uid: 148564,
|
|
uin: 4148762,
|
|
}
|
|
app.globalData.messageData.push(data)
|
|
console.log("全局接收到新消息:", app.globalData.messageData);
|
|
|
|
}, 400);
|
|
|
|
app.globalData.socketTask.onClose(function (res) {
|
|
console.log('socket关闭了', new Date(), res)
|
|
restartCount += 1
|
|
// 断连后 10秒重连接
|
|
if (restartCount <= 5) setTimeout(() => useSocket(), 10000);
|
|
// setTimeout(() => useSocket(), 10000);
|
|
app.globalData.isConnected = false // 连接关闭,重置为 false
|
|
|
|
})
|
|
}
|
|
|
|
// 定时发送
|
|
const timedTransmission = () => {
|
|
if (app.globalData.socketTask.readyState != 1) return
|
|
var dataToSend = {
|
|
type: 'ping'
|
|
}
|
|
app.globalData.socketTask.send({
|
|
data: JSON.stringify(dataToSend),
|
|
complete: () => setTimeout(() => timedTransmission(), 50000)
|
|
})
|
|
}
|
|
|
|
// 将一个JavaScript对象转换为路由参数的形式将键值对转换为key=value的形式
|
|
function objectToQueryString(obj) {
|
|
return '?' + Object.keys(obj).map(key => key + '=' + obj[key]).join('&');
|
|
}
|
|
|
|
module.exports = {
|
|
initial: initial,
|
|
sendData,
|
|
request,
|
|
pageStart,
|
|
closePage,
|
|
caculateQueryHeight,
|
|
getUserInfo,
|
|
sendShare,
|
|
haveAuthorization,
|
|
useSocket,
|
|
https: function (url, data, success, fail) {
|
|
wx.request({
|
|
url: url,
|
|
data: data,
|
|
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
|
|
header: {
|
|
'content-type': 'application/json',
|
|
'Accept': 'application/json, text/plain',
|
|
'Cookie': '_miucms_session_gter=' + wx.getStorageSync('_miucms_session'),
|
|
envVersion: getApp().globalData['envVersion'],
|
|
},
|
|
success: function (res) {
|
|
typeof success == 'function' && success(res);
|
|
},
|
|
fail: function () {
|
|
typeof fail == 'function' && fail(res);
|
|
},
|
|
complete: function () {
|
|
typeof fail == 'function' && fail(res);
|
|
}
|
|
})
|
|
},
|
|
objectToQueryString,
|
|
} |