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

308 lines
6.5 KiB
Vue

<script>
import {
mapGetters,
mapActions,
mapMutations
} from 'vuex';
import config from '@/config.js';
export default {
onLaunch: function() {
let _this = this;
_this.init();
_this.getAppletBase();
// 登录成功回调
/*uni.$on('uni-id-pages-login-success', () => {
// this.setToken()
this.init()
})*/
},
onPageNotFound(msg) {
uni.redirectTo({
url: config.error.url
})
},
methods: {
...mapActions({
init: 'app/init'
}),
/**
* 当前用户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') || {};
},
/**
* 当前小程序设置
*/
getApplet() {
let _this = this,
applet = uni.getStorageSync('applet');
if (applet) {
return applet;
} else {
_this.getAppletBase(() => {
_this.getApplet();
});
}
},
/**
* 设置当前页面标题
*/
setTitle() {
let _this = this,
applet = _this.getApplet();
uni.setNavigationBarTitle({
title: applet.web_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(config.login.url);
},
/**
* 获取小程序基础信息
*/
getAppletBase(callback) {
let _this = this;
_this._get('applet/base', {}, result => {
// 记录小程序基础信息
let applet = result.data;
applet.domain = config.domain;
applet.version = config.version;
uni.setStorageSync('applet', applet);
callback && callback(applet);
}, false, false);
},
/**
* 跳转到指定页面
* 支持tabBar页面
*/
goTo(url) {
if (!url || url.length == 0) {
return false;
}
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 + '/api/foodcash.' + 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 + '/api/foodcash.' + 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();
}
});
}
}
}
</script>
<style lang="scss">
@import '@/common/uni.css';
@import '@/common/uni-icons.css';
@import '@/common/admin-icons.css';
@import '@/common/theme.scss';
@import '@/common/hemaphp.css';
</style>