582 lines
14 KiB
Vue
582 lines
14 KiB
Vue
<script>
|
|
/**
|
|
* tabBar页面路径列表 (用于链接跳转时判断)
|
|
* tabBarLinks为常量, 无需修改
|
|
*/
|
|
const tabBarLinks = ['index/index', 'shop/index', 'order/index', 'user/index'];
|
|
import config from '@/config.js';
|
|
export default {
|
|
globalData: {
|
|
api_root: '', //接口
|
|
applet_id: '', //小程序管理端编号
|
|
},
|
|
/**
|
|
* 生命周期函数--监听小程序初始化
|
|
*/
|
|
onShow(options) {
|
|
let _this = this;
|
|
uni.removeStorageSync('shop'); //每次打开小程序先清楚门店信息
|
|
// #ifdef H5
|
|
_this.globalData.applet_id = options.query.applet_id || config.applet_id;
|
|
_this.globalData.api_root = config.domain + config.api_url;
|
|
//_this.h5test();
|
|
// #endif
|
|
// #ifdef MP-WEIXIN || MP-ALIPAY
|
|
let extConfig = uni.getExtConfigSync ? uni.getExtConfigSync() : {};
|
|
_this.globalData.applet_id = extConfig.applet_id;
|
|
_this.globalData.api_root = extConfig.api_url + config.api_url;
|
|
// #endif
|
|
_this.getAppletBase(); //获取基本配置
|
|
// 记录系统信息
|
|
uni.setStorageSync('system', uni.getSystemInfoSync());
|
|
},
|
|
methods: {
|
|
h5test() {
|
|
this._get('user/h5', {}, result => {
|
|
uni.setStorageSync('user', result.data.detail);
|
|
});
|
|
},
|
|
/**
|
|
* 小程序处理普通二维码数据
|
|
* is_q 是否是普通二维码进入
|
|
*/
|
|
getQrcode: function(scene,is_q=true) {
|
|
let _this = this;
|
|
if(is_q){
|
|
let q = scene.split('/food/' + _this.globalData.applet_id + '/');
|
|
scene = q[1];
|
|
}
|
|
scene = scene.split("-");
|
|
// #ifdef MP-WEIXIN || MP-ALIPAY
|
|
//如果是线下发的优惠券码
|
|
if (scene[0] == 'coupon') {
|
|
uni.showModal({
|
|
title: "提示",
|
|
content: "是否要领取优惠券?",
|
|
success: function(o) {
|
|
if (o.confirm) {
|
|
_this._get('market.coupon/offline', {
|
|
id: scene[1]
|
|
}, function(result) {
|
|
_this.showSuccess(result.msg);
|
|
}, false, false);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
//如果是小程序码
|
|
if (scene[0] == 'applet') {}
|
|
//如果是自定义二维码码
|
|
if (scene[0] == 'sn') {
|
|
_this.scanQrcode(scene[1]);//识别预生成二维码
|
|
}
|
|
// #endif
|
|
if(scene[0] == 'shop'){
|
|
uni.setStorageSync('shop_id',scene[1]);
|
|
_this.goTo('shop/index');
|
|
}
|
|
if(scene[0] == 'table'){
|
|
uni.setStorageSync('shop_id',scene[1]);
|
|
uni.setStorageSync('table_id',scene[2]);
|
|
_this.goTo('shop/index');
|
|
}
|
|
if(scene[0] == 'paybill'){
|
|
_this.goTo('order/pay_bill?shop_id='+scene[1]);
|
|
}
|
|
if(scene[0] == 'wifi'){
|
|
_this.goTo('shop/wifi?shop_id='+scene[1]);
|
|
}
|
|
},
|
|
/**
|
|
* 识别预生成普通二维码
|
|
*/
|
|
scanQrcode:function(id){
|
|
let _this = this,
|
|
q = '';
|
|
_this._get('qrcode/detail', {
|
|
id: id
|
|
}, function(result) {
|
|
let detail = result.data.detail;
|
|
//小程序码
|
|
if (detail.type.value == 10) {
|
|
q = 'applet-' + detail.applet_id;
|
|
}
|
|
//门店码
|
|
if (detail.type.value == 20) {
|
|
q = 'shop-' + detail.shop_id;
|
|
}
|
|
//餐桌码
|
|
if (detail.type.value == 30) {
|
|
q = 'table-' + detail.shop_id + '-' + detail.table_id;
|
|
}
|
|
//WIFI链接码
|
|
if (detail.type.value == 40) {
|
|
q = 'wifi-' + detail.shop_id;
|
|
}
|
|
//买单码
|
|
if (detail.type.value == 40) {
|
|
q = 'paybill-' + detail.shop_id;
|
|
}
|
|
_this.getQrcode(q,false);
|
|
});
|
|
},
|
|
/**
|
|
* 当前用户位置
|
|
*/
|
|
getLocation() {
|
|
return uni.getStorageSync('location') || '';
|
|
},
|
|
|
|
/**
|
|
* 当前用户id
|
|
*/
|
|
getUserId() {
|
|
let _this = this,
|
|
user = _this.getUser();
|
|
return user.user_id || '';
|
|
},
|
|
|
|
/**
|
|
* 当前用户
|
|
*/
|
|
getUser() {
|
|
return uni.getStorageSync('user') || '';
|
|
},
|
|
|
|
/**
|
|
* 当前门店
|
|
*/
|
|
getShopId() {
|
|
let shop = uni.getStorageSync('shop') || '';
|
|
return shop.shop_id || '';
|
|
},
|
|
|
|
/**
|
|
* 当前门店
|
|
*/
|
|
getShop() {
|
|
return uni.getStorageSync('shop') || '';
|
|
},
|
|
/**
|
|
* 应用配置
|
|
*/
|
|
getApplet() {
|
|
let _this = this,
|
|
applet;
|
|
if (applet = uni.getStorageSync('applet')) {
|
|
return applet;
|
|
} else {
|
|
_this.getAppletBase(() => {
|
|
_this.getApplet();
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取小程序基础信息
|
|
*/
|
|
getAppletBase(callback) {
|
|
let _this = this;
|
|
_this._get('applet/base', {}, result => {
|
|
let applet = result.data.applet;
|
|
applet.version = config.version;
|
|
applet.domain = config.domain;
|
|
applet.style = 'grey'; //色彩风格
|
|
uni.setStorageSync('applet', applet);
|
|
for (let n = 0; n < applet.tabbar.list.length; n++) {
|
|
uni.setTabBarItem({
|
|
index: n,
|
|
text: applet.tabbar.list[n].text,
|
|
iconPath: applet.tabbar.list[n].inactiveIcon,
|
|
selectedIconPath: applet.tabbar.list[n].activeIcon
|
|
});
|
|
uni.setTabBarStyle({
|
|
color: applet.tabbar.style.inactiveColor,
|
|
selectedColor: applet.tabbar.style.activeColor,
|
|
backgroundColor: '#ffffff',
|
|
borderStyle: 'black'
|
|
});
|
|
}
|
|
// #ifdef MP-WEIXIN || MP-ALIPAY
|
|
//自动登录
|
|
_this.autoLogin((rs) => {
|
|
_this.$isResolve();
|
|
});
|
|
_this.updateApp();
|
|
// #endif
|
|
callback && callback(applet);
|
|
}, false, false);
|
|
},
|
|
|
|
/**
|
|
* 自动登录
|
|
*/
|
|
autoLogin: function(callback) {
|
|
let _this = this;
|
|
// 执行微信登录
|
|
uni.login({
|
|
success(res) {
|
|
// 发送用户信息
|
|
_this._post_form('user/autoLogin', {
|
|
code: res.code
|
|
}, function(result) {
|
|
uni.setStorageSync('user', result.data.detail);
|
|
callback && callback(result.data.detail);
|
|
}, false, false);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 设置当前页面标题
|
|
*/
|
|
setTitle(callback) {
|
|
let _this = this,
|
|
applet;
|
|
if (applet = uni.getStorageSync('applet')) {
|
|
uni.setNavigationBarTitle({
|
|
title: applet.navbar.params.title
|
|
});
|
|
// 设置navbar标题、颜色
|
|
uni.setNavigationBarColor({
|
|
frontColor: applet.navbar.style.titleTextColor == 'black' ? '#000000' : '#ffffff',
|
|
backgroundColor: applet.navbar.style.titleBackgroundColor
|
|
});
|
|
callback && callback(applet);
|
|
} else {
|
|
_this.getAppletBase(() => {
|
|
_this.setTitle();
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取手机号
|
|
*/
|
|
getPhoneNumber(e, callback) {
|
|
let _this = this,
|
|
iv = '',
|
|
encryptedData = '';
|
|
// #ifdef MP-WEIXIN
|
|
iv = e.detail.iv;
|
|
encryptedData = e.detail.encryptedData;
|
|
// #endif
|
|
// #ifdef MP-ALIPAY
|
|
iv = e.sign;
|
|
encryptedData = e.response;
|
|
// #endif
|
|
uni.login({
|
|
success(res) {
|
|
_this._post_form('user/getPhoneNumber', {
|
|
code: res.code,
|
|
iv: iv,
|
|
encryptedData: encryptedData
|
|
}, function(result) {
|
|
uni.setStorageSync('user', result.data.detail);
|
|
callback && callback(result.msg);
|
|
}, false, function() {});
|
|
}
|
|
|
|
});
|
|
},
|
|
/**
|
|
* 验证登录
|
|
*/
|
|
checkIsLogin() {
|
|
let _this = this,
|
|
user = _this.getUser();
|
|
return user.is_login || false;
|
|
},
|
|
/**
|
|
* 执行用户登录
|
|
*/
|
|
doLogin() {
|
|
let _this = this,
|
|
pages = getCurrentPages(),
|
|
page = pages[pages.length - 1],
|
|
currentPage = page.route;
|
|
"pages/user/index" != currentPage &&
|
|
uni.setStorageSync("currentPage", currentPage);
|
|
_this.goTo("user/index");
|
|
},
|
|
|
|
/**
|
|
* 获取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,
|
|
mp = 'weixin';
|
|
data = data || {};
|
|
// #ifdef H5
|
|
mp = 'h5';
|
|
// #endif
|
|
// #ifdef MP-ALIPAY
|
|
mp = 'alipay';
|
|
// #endif
|
|
data.mp = mp;
|
|
data.applet_id = _this.globalData.applet_id;
|
|
data.user_id = _this.getUserId();
|
|
if (!data.shop_id) {
|
|
data.shop_id = _this.getShopId();
|
|
}
|
|
if (!data.location) {
|
|
data.location = _this.getLocation();
|
|
}
|
|
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: _this.globalData.api_root + 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: _this.globalData.api_root + 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: 3000,
|
|
success() {
|
|
callback && setTimeout(() => {
|
|
callback();
|
|
}, 1500);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 显示失败提示框
|
|
*/
|
|
showError(msg, callback) {
|
|
uni.showModal({
|
|
title: '友情提示',
|
|
content: msg,
|
|
showCancel: false,
|
|
success(res) {
|
|
callback && callback();
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 显示登录提示框
|
|
*/
|
|
showLogin() {
|
|
let _this = this;
|
|
uni.showModal({
|
|
title: '友情提示',
|
|
content: '您还没有登录',
|
|
confirmText: '去登录',
|
|
confirmColor: '#19be6b',
|
|
success(res) {
|
|
if (res.confirm) {
|
|
_this.goTo('login/index');
|
|
}
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* 小程序新版本检测与升级
|
|
*/
|
|
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> |