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

208 lines
8.7 KiB

3 years ago
//app.js
3 years ago
function splitQueryParams(params, key) {
let strParams = '';
if (Object.keys(params).length <= 0) return strParams;
for (let i in params) {
3 years ago
if (params[i] instanceof Object) {
3 years ago
let k = key ? `${key}.${i}` : i;
strParams += splitQueryParams(params[i], k);
3 years ago
} else if (params[i] instanceof Array) {
for (let j = 0; j < params[i].length; j++) {
3 years ago
let k = key ? `${key}[${j}].${i}` : i;
strParams += splitQueryParams(params[i][j], k);
}
} else {
let k = key ? `${key}.${i}` : i;
strParams += `&${k}=${params[i]}`
}
3 years ago
}
3 years ago
return strParams;
3 years ago
}
3 years ago
3 years ago
! function () {
//获取页面配置并进行页面分享配置
var PageTmp = Page
Page = function (pageConfig) {
3 years ago
//1. 获取当前页面路由
let routerUrl = ""
wx.onAppRoute(function (res) {
//app.js中需要在隐式路由中才能用getCurrentPages获取到页面路由
let pages = getCurrentPages(),
view = pages[pages.length - 1];
routerUrl = view.route
})
//2. 全局开启分享配置
pageConfig = Object.assign({
onShareAppMessage: function () {
//根据不同路由设置不同分享内容(微信小程序分享自带参数,如非特例,不需配置分享路径)
let shareInfo = {}
let noGlobalSharePages = ["index/index"]
//全局分享配置,如部分页面需要页面默认分享或自定义分享可以单独判断处理
if (!routerUrl.includes(noGlobalSharePages)) {
shareInfo = {
title: "硕为思汽车智能洞察",
// imageUrl: wx.getStorageSync("shareUrl")
}
}
return shareInfo
3 years ago
}
3 years ago
}, pageConfig);
// 配置页面模板
PageTmp(pageConfig);
3 years ago
}
}();
3 years ago
App({
3 years ago
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
const systemInfo = wx.getSystemInfoSync(); //获取系统信息
const menuInfo = wx.getMenuButtonBoundingClientRect(); // 获取胶囊按钮的信息
this.globalData.menuHeight = menuInfo.height; // 获取胶囊按钮的高
this.globalData.statusBarHeight = systemInfo.statusBarHeight; // 获取状态栏的高
this.globalData.menuRight = menuInfo.right; // 获取胶囊按钮的距离屏幕最右边的距离(此处用于设置导航栏左侧距离屏幕的距离)
this.globalData.navBarHeight = (menuInfo.top - systemInfo.statusBarHeight) * 2 + menuInfo.height; // 计算出导航栏的高度
},
globalData: {
imageUrl: "https://cdn.sws010.com/wxapp/images",
navBarHeight: 0, // 导航栏高度
menuHeight: 0, //胶囊按钮 高度
statusBarHeight: 0, //状态栏高度
menuRight: 0, //胶囊按钮 距离屏幕右边的距离
request: function (prarms, callback) {
3 years ago
let obj = {
3 years ago
version: "1.0.8",
3 years ago
...prarms
}
3 years ago
return new Promise((resolve, reject) => {
wx.request({
3 years ago
url: 'https://cloud.sws010.com/api/v7.ashx',
3 years ago
header: {
'content-type': 'application/x-www-form-urlencoded' //修改此处即可
3 years ago
},
3 years ago
data: splitQueryParams(obj),
method: 'post',
3 years ago
success: function (res) {
//接口实际返回的内容在res.data中
if (res.data.Code == 1) {
if (typeof callback === 'function') {
callback(res.data);
}
resolve(res.data.Data);
3 years ago
} else if (res.data.Code == -99) {
wx.showModal({
title: '提示',
content: "账号已过期,退出账号并清除缓存数据",
success: function (res) {
if (res.confirm) {
wx.clearStorage()
}
}
})
3 years ago
} else {
reject(res.data)
}
},
fail: function (err) {
reject(err);
}
})
})
3 years ago
},
3 years ago
requestImg: function (data) {
let obj = {
3 years ago
version: '1.0.8',
3 years ago
data
};
3 years ago
return new Promise((resolve, reject) => {
wx.request({
url: 'https://saas.kaidalai.cn/word_cloud/wordCloud/getWordCloud',
data: obj,
method: 'post',
success: function (res) {
resolve(res)
},
fail: function (err) {
reject(err);
}
})
})
3 years ago
}
3 years ago
},
3 years ago
// 版本更新
3 years ago
autoUpdate: function () {
var self = this // 获取小程序更新机制兼容
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好调用applyUpdate应用新版本并重启
updateManager.applyUpdate();
} else if (res.cancel) {
wx.showModal({
title: '温馨提示~',
content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false, //隐藏取消按钮
confirmText: "确定更新", //只保留确定更新按钮
success: function (res) {
if (res.confirm) {
//下载新版本,并重新应用
self.downLoadAndUpdate(updateManager)
}
}
})
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
})
})
3 years ago
}
})
3 years ago
} else {
// 此时微信版本太低(一般而言版本都是支持的)
3 years ago
wx.showModal({
3 years ago
title: '溫馨提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
3 years ago
})
}
3 years ago
},
/**
* 下载小程序新版本并重启应用
*/
downLoadAndUpdate: function (updateManager) {
var self = this
wx.showLoading();
//静默下载更新小程序新版本
updateManager.onUpdateReady(function () {
wx.hideLoading()
//新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
})
})
},
onShow() {
this.autoUpdate()
3 years ago
}
3 years ago
3 years ago
})