2024-01-03 14:19:05 +08:00

349 lines
7.7 KiB
Vue

<script>
/**
* tabBar页面路径列表 (用于链接跳转时判断)
* tabBarLinks为常量, 无需修改
*/
const tabBarLinks = ['index/index', 'order/index', 'table/order', 'call/index', 'user/index'];
import config from '@/config.js';
export default {
/**
* 全局变量
*/
globalData: {},
/**
* 生命周期函数--监听小程序初始化
*/
onLaunch() {
let _this = this;
_this.getAppletBase();
//#ifdef MP-WEIXIN
_this.updateApp(); //小程序新版本检测与升级
//#endif
},
methods: {
/**
* 当前用户token
*/
getToken() {
let _this = this,
user = _this.getUser();
return user.token || '';
},
/**
* 获取applet_id
*/
getAppletId() {
let _this = this,
user = _this.getUser();
return user.applet_id || '';
},
/**
* 当前用户
*/
getUser() {
return uni.getStorageSync('user') || {};
},
/**
* 设置当前页面标题
*/
setTitle() {
let _this = this,
applet = _this.getApplet();
uni.setNavigationBarTitle({
title: applet.app_name
});
},
/**
* 执行用户登录
*/
doLogin() {
let _this = this,
pages = getCurrentPages(),
page = pages[pages.length - 1],
currentPage = page.route;
"pages/login/login" != currentPage &&
uni.setStorageSync("currentPage", currentPage);
_this.goTo("login/login");
},
/**
* 当前小程序设置
*/
getApplet() {
let _this = this,
applet = uni.getStorageSync('applet');
if (applet) {
return applet;
} else {
_this.getAppletBase(() => {
_this.getApplet();
});
}
},
/**
* 获取小程序基础信息
*/
getAppletBase(callback) {
let _this = this;
_this._get('applet/base', {}, result => {
// 记录小程序基础信息
let applet = result.data.applet;
applet.domain = config.domain;
applet.version = config.version;
uni.setStorageSync('applet', applet);
_this.$isResolve();
callback && callback(applet);
}, false, false);
},
/**
* 获取tabBar页面路径列表
*/
getTabBarLinks() {
return tabBarLinks;
},
/**
* 跳转到指定页面
* 支持tabBar页面
*/
goTo(url) {
let _this = this;
if (!url || url.length == 0) {
return false;
}
let tabBarLinks = _this.getTabBarLinks(); // tabBar页面
if (tabBarLinks.indexOf(url) > -1) {
uni.switchTab({
url: '/pages/' + url
});
} else {
// 普通页面
uni.navigateTo({
url: '/pages/' + url
});
}
},
/**
* 构造请求参数
*/
getParamet(data) {
let _this = this;
data = data || {};
data.token = _this.getToken();
data.applet_id = _this.getAppletId();
return data;
},
/**
* get请求
*/
_get(url, data, success, fail, complete, check_login) {
let _this = this;
uni.showLoading({
title: '正在加载中',
mask: true
});
// 构造请求参数
data = _this.getParamet(data);
let request = () => {
uni.request({
url: config.domain + config.api_url + url,
header: {
'content-type': 'application/json'
},
data: data,
success(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
_this.showError('网络请求出错');
return false;
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
uni.hideLoading();
_this.doLogin();
} else if (res.data.code === 0) {
_this.showError(res.data.msg, () => {
fail && fail(res);
});
return false;
} else {
success && success(res.data);
}
},
fail(res) {
_this.showError(res.errMsg, () => {
fail && fail(res);
});
},
complete(res) {
uni.hideLoading();
complete && complete(res);
}
});
}; // 判断是否需要验证登录
check_login ? _this.doLogin(request) : request();
},
/**
* post提交
*/
_post_form(url, data, success, fail, complete) {
uni.showLoading({
title: '正在加载中',
mask: true
});
let _this = this; // 构造请求参数
data = _this.getParamet(data);
uni.request({
url: config.domain + config.api_url + url,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'POST',
data: data,
success(res) {
if (res.statusCode !== 200 || typeof res.data !== 'object') {
_this.showError('网络请求出错');
return false;
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
_this.doLogin(() => {
_this._post_form(url, data, success, fail);
});
return false;
} else if (res.data.code === 0) {
_this.showError(res.data.msg, () => {
fail && fail(res);
});
return false;
}
success && success(res.data);
},
fail(res) {
_this.showError(res.errMsg, () => {
fail && fail(res);
});
},
complete(res) {
uni.hideLoading();
complete && complete(res);
}
});
},
/**
* 上传图片
*/
_upload_file(path, data, success, fail, complete) {
let _this = this; // 构造请求参数
data = _this.getParamet(data);
uni.uploadFile({
url: config.domain + '/addons/upload/upload/image', // 接口地址
filePath: path,
name: 'iFile',
formData: data,
success(res) {
if (res.statusCode !== 200) {
_this.showError('网络请求出错');
return false;
}
if (res.data.code === -1) {
// 登录态失效, 重新登录
_this.doLogin(() => {
_this._upload_file(url, data, success, fail);
});
return false;
} else if (res.data.code === 0) {
_this.showError(res.data.msg, () => {
fail && fail(res);
});
return false;
}
success && success(JSON.parse(res.data));
},
fail(res) {
_this.showError(res.errMsg, () => {
fail && fail(res);
});
},
complete(res) {
complete && complete(res);
}
});
},
/**
* 显示成功提示框
*/
showSuccess(msg, callback) {
uni.showToast({
title: msg,
icon: 'success',
mask: true,
duration: 1500,
success() {
callback && setTimeout(() => {
callback();
}, 1500);
}
});
},
/**
* 显示失败提示框
*/
showError(msg, callback) {
uni.showModal({
title: '友情提示',
content: msg,
showCancel: false,
success(res) {
callback && callback();
}
});
},
/**
* 小程序新版本检测与升级
*/
updateApp() {
if (!uni.canIUse('getUpdateManager')) {
return false;
}
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(res => {
// 请求完新版本信息的回调
});
updateManager.onUpdateReady(() => {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,即将重启应用',
showCancel: false,
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
updateManager.onUpdateFailed(() => {
// 新的版本下载失败
uni.showModal({
title: '更新提示',
content: '新版本下载失败',
showCancel: false
});
});
}
}
};
</script>
<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
@import "@/common/hemaphp.css";
</style>