320 lines
11 KiB
JavaScript
320 lines
11 KiB
JavaScript
//app.js
|
||
var miucms = require('./utils/miucms.js');
|
||
// var script = require('./utils/script');
|
||
// console.log(script());
|
||
var initial_url = 'https://api.gter.net/xiaoapp_offer.init.json';
|
||
var __ = require('./utils/miucms.js');
|
||
import util from './utils/util'
|
||
|
||
// 保存原始的Page构造器
|
||
const originPage = Page;
|
||
|
||
// 重写Page构造器
|
||
Page = function (pageConfig) {
|
||
// 添加全局的onShow逻辑
|
||
const originOnLoad = pageConfig.onLoad;
|
||
pageConfig.onLoad = function () {
|
||
util.statistics({})
|
||
if (originOnLoad) originOnLoad.apply(this, arguments);
|
||
};
|
||
|
||
const originOnShare = pageConfig.onShareAppMessage;
|
||
if (originOnShare) {
|
||
pageConfig.onShareAppMessage = function (e) {
|
||
util.statistics({
|
||
name: "share"
|
||
})
|
||
return originOnShare.apply(this, [e]);
|
||
};
|
||
}
|
||
|
||
// 调用原始的Page函数
|
||
return originPage(pageConfig);
|
||
};
|
||
|
||
App({
|
||
randomString(n) {
|
||
let str = 'abcdefghijklmnopqrstuvwxyz9876543210';
|
||
let tmp = '',
|
||
i = 0,
|
||
l = str.length;
|
||
for (i = 0; i < n; i++) {
|
||
tmp += str.charAt(Math.floor(Math.random() * l));
|
||
}
|
||
return tmp;
|
||
},
|
||
|
||
onLaunch: function (options) {
|
||
util.statistics({
|
||
data: {
|
||
scene: options.scene
|
||
},
|
||
path: options.path,
|
||
query: options.query,
|
||
name: "onLaunch"
|
||
})
|
||
|
||
const accountInfo = wx.getAccountInfoSync();
|
||
const envVersion = accountInfo.miniProgram.envVersion;
|
||
this.globalData['envVersion'] = envVersion
|
||
this.globalData.options = options;
|
||
if (wx.getUserProfile) {
|
||
this.globalData.canIUseGetUserProfile = true;
|
||
}
|
||
|
||
var Authorization = wx.getStorageSync('Authorization');
|
||
|
||
if (!Authorization) {
|
||
Authorization = this.randomString(32);
|
||
wx.setStorageSync('Authorization', Authorization)
|
||
wx.setStorageSync('session', Authorization)
|
||
}
|
||
|
||
try {
|
||
var systemInfo = wx.getSystemInfoSync();
|
||
this.computeNavigateBarHeight(systemInfo);
|
||
} catch (e) {
|
||
// 获取系统信息失败
|
||
this.globalData.screen_data = {
|
||
windowHeight: 812,
|
||
totalTopHeight: 68,
|
||
statusBarHeight: 20,
|
||
titleBarHeight: 48,
|
||
title: '',
|
||
index: false
|
||
}
|
||
}
|
||
__.initial(this)
|
||
},
|
||
|
||
computeNavigateBarHeight: function () {
|
||
|
||
var systemInfo = wx.getSystemInfoSync();
|
||
if (wx.getSystemInfoSync) {
|
||
var data = wx.getMenuButtonBoundingClientRect()
|
||
let totalTopHeight = data.bottom;
|
||
let windowHeight = systemInfo.windowHeight,
|
||
statusBarHeight = data.top,
|
||
windowWidth = systemInfo.windowWidth
|
||
let platform = systemInfo.platform
|
||
this.globalData.platform = platform
|
||
if (platform == 'windows' || platform == 'mac') this.globalData.isPC = true
|
||
|
||
this.globalData.screen_data = {
|
||
windowHeight: windowHeight,
|
||
windowWidth,
|
||
totalTopHeight: totalTopHeight + 6,
|
||
statusBarHeight: statusBarHeight,
|
||
titleBarHeight: data.height,
|
||
titleWidth: data.widthinitial,
|
||
bottomLift: systemInfo.screenHeight - systemInfo.safeArea.bottom,
|
||
isIos: systemInfo.platform === 'ios' ? true : false,
|
||
}
|
||
} else {
|
||
// 兼容低版本
|
||
this.globalData.screen_data = {
|
||
windowHeight: systemInfo.windowHeight,
|
||
totalTopHeight: systemInfo.statusBarHeight + 48,
|
||
statusBarHeight: systemInfo.statusBarHeight,
|
||
titleBarHeight: 48,
|
||
title: '',
|
||
index: false
|
||
}
|
||
}
|
||
|
||
},
|
||
|
||
onShow: function (options) {
|
||
// 检查小程序更新问题
|
||
/* 版本自动更新代码开始 */
|
||
const updateManager = wx.getUpdateManager()
|
||
updateManager.onCheckForUpdate(function (res) {
|
||
// console.log("新版本信息的回调", res.hasUpdate) // 请求完新版本信息的回调 true说明有更新
|
||
})
|
||
updateManager.onUpdateReady(function () {
|
||
wx.showModal({
|
||
title: '更新检测', // 此处可自定义提示标题
|
||
content: '检测到新版本,是否重启小程序?', // 此处可自定义提示消息内容
|
||
success: function (res) {
|
||
if (res.confirm) {
|
||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||
updateManager.applyUpdate()
|
||
}
|
||
}
|
||
})
|
||
})
|
||
updateManager.onUpdateFailed(function () {
|
||
// 新的版本下载失败
|
||
wx.showModal({
|
||
title: '更新提示',
|
||
content: '新版本下载失败',
|
||
showCancel: false
|
||
})
|
||
})
|
||
/* 版本自动更新代码结束 */
|
||
|
||
this.globalData.scene = options.scene
|
||
|
||
this.globalData.options = options;
|
||
__.request('https://offer.gter.net/miniprogram/share/overall').then(res => {
|
||
if (res.data) {
|
||
this.globalData.shareConfig = res.data;
|
||
}
|
||
}).catch(res => {})
|
||
|
||
// 判断socket从后台打开 并且已经断开链接了,需要重启
|
||
// if (this.globalData.isHideState && this.globalData.socketTask && this.globalData.socketTask['readyState'] != 1) {
|
||
// miucms.useSocket()
|
||
// }
|
||
miucms.pageStart(this).then(() => {
|
||
miucms.useSocket()
|
||
})
|
||
|
||
this.globalData.isHideState = false
|
||
},
|
||
onHide: function () {
|
||
this.globalData.isHideState = true
|
||
},
|
||
onError: function () {
|
||
// 这里回调错误信息
|
||
// console.log('onError')
|
||
},
|
||
// get: function () {
|
||
// return this.globalData.config;
|
||
// },
|
||
return_data: function (res) {
|
||
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;
|
||
return data;
|
||
},
|
||
// 判断微信提醒弹窗请求
|
||
judgeWechatAlertsPop() {
|
||
return new Promise((resolve, reject) => {
|
||
util.wxpost("/miniprogram/user/getPopup").then(res => {
|
||
// res.data.iswechatmessagepush = 0
|
||
let iswechatmessagepush = res.data.iswechatmessagepush
|
||
this.globalData.iswechatmessagepush = iswechatmessagepush
|
||
resolve(res.data)
|
||
}).finally(() => {
|
||
this.globalData.iswechatmessagepushState = false
|
||
})
|
||
})
|
||
},
|
||
|
||
watch: function (method) {
|
||
var obj = this.globalData;
|
||
let setSate = obj['setSate']
|
||
Object.defineProperty(this.globalData, "setSate", { //这里的 globalData对应 上面 globalData
|
||
configurable: true,
|
||
enumerable: true,
|
||
set: function (value) { //动态赋值,传递对象,为 globalData 中对应变量赋值
|
||
setSate = value
|
||
method(value);
|
||
},
|
||
get: function () { //获取全局变量值,直接返回全部
|
||
return setSate;
|
||
}
|
||
})
|
||
},
|
||
|
||
globalData: {
|
||
setSate: false,
|
||
baseURL: "https://offer.gter.net",
|
||
token: null,
|
||
title: null,
|
||
session: null,
|
||
code: null,
|
||
user: {},
|
||
userInfo: {},
|
||
options: {},
|
||
config: {
|
||
follow: {
|
||
init: "https://offer.gter.net/miniprogram/follow/init",
|
||
lists: "https://offer.gter.net/miniprogram/follow/lists",
|
||
professionSearch: "https://offer.gter.net/miniprogram/follow/professionSearch",
|
||
schoolSearch: "https://offer.gter.net/miniprogram/follow/schoolSearch",
|
||
},
|
||
interviewExperience: {
|
||
details: "https://offer.gter.net/miniprogram/interviewExperience/details",
|
||
lists: "https://offer.gter.net/miniprogram/interviewExperience/lists"
|
||
},
|
||
offer: {
|
||
details: "https://offer.gter.net/miniprogram/offer/details",
|
||
lists: "https://offer.gter.net/miniprogram/offer/lists"
|
||
},
|
||
thread: {
|
||
lists: "https://offer.gter.net/miniprogram/thread/lists",
|
||
reply: "https://offer.gter.net/miniprogram/thread/reply"
|
||
},
|
||
vote: {
|
||
commentList: "https://offer.gter.net/miniprogram/vote/commentList",
|
||
commentPost: "https://offer.gter.net/miniprogram/vote/commentPost",
|
||
create: "https://offer.gter.net/miniprogram/vote/create",
|
||
delete: "https://offer.gter.net/miniprogram/vote/delete",
|
||
details: "https://offer.gter.net/miniprogram/vote/details",
|
||
lists: "https://offer.gter.net/miniprogram/vote/lists",
|
||
operation: "https://offer.gter.net/miniprogram/vote/operation",
|
||
unvote: "https://offer.gter.net/miniprogram/vote/unvote"
|
||
}
|
||
},
|
||
source: '',
|
||
header: {
|
||
'content-type': 'application/json',
|
||
'Accept': 'application/json, text/plain',
|
||
'Cookie': '_miucms_session_gter=' + wx.getStorageSync('_miucms_session')
|
||
},
|
||
new_reply: false,
|
||
// 寄托租房独有字段
|
||
choose: {},
|
||
kw: '',
|
||
status: 0,
|
||
screen_data: {},
|
||
canGetData: false, //是否可以开始获取数据
|
||
StudentapartmentNew: null,
|
||
isUserAuthorization: 0, //用户点击授权:0:未知,1:未授权,2:已授权
|
||
follow_data: {},
|
||
ischangeFollow: false, //是否有更新关注列表
|
||
shareConfig: {},
|
||
canIUseGetUserProfile: false,
|
||
|
||
// 优惠券的隐藏与否
|
||
iscoupon: false,
|
||
isgetinit: false,
|
||
mysquareAsk: {},
|
||
favUpdateList: [],
|
||
isPC: false,
|
||
platform: {},
|
||
initializeLoginState: false, // 初始化登录状态
|
||
iswechatmessagepush: null,
|
||
iswechatmessagepushState: -1,
|
||
|
||
socketTask: null, // 全局的 socket 值
|
||
isHideState: true, // 判断是否 隐藏了 在 onshow 为 true 是代表小程序后台打开
|
||
|
||
messageData: [], // 全局的私信信息
|
||
messageSendData: [], // 全局的 发送新消息的返回
|
||
unreadMessageCount: 0, // 未读消息数量
|
||
onMessage: res => {
|
||
// console.log("res拿到的信息", res);
|
||
},
|
||
|
||
postList: [], // 一级页面的 发布 列表
|
||
navigation: [], // 一级页面的 导航栏 列表
|
||
|
||
voteAmend: {}, // 新投票列表的详情和列表交互数据
|
||
|
||
signInState: 0, // 签到状态
|
||
getSignInState: false, // 获取完签到状态
|
||
popup: undefined, // 全局弹窗数据 undefined 是还没获取到 null 是没有弹窗 {} 是数据
|
||
offerkaipingadvertisement: {}, // 开屏广告 数据
|
||
offerkaipingadvertisementState: false, // 开屏广告 状态
|
||
isConnected: false,
|
||
}
|
||
}) |