266 lines
6.6 KiB
JavaScript
266 lines
6.6 KiB
JavaScript
import Config from './config.js'
|
||
import Store from '@/store/index.js'
|
||
|
||
export default {
|
||
/**
|
||
* 页面跳转
|
||
* @param {string} to 跳转链接 /pages/idnex/index
|
||
* @param {Object} param 参数 {key : value, ...}
|
||
* @param {string} mode 模式
|
||
*/
|
||
redirectTo(to, param, mode) {
|
||
let tabbar = [
|
||
"/pages/billing/index", // 开单
|
||
"/pages/reserve/index", // 预约
|
||
"/pages/buycard/index", // 售卡
|
||
"/pages/recharge/index", // 充值
|
||
"/pages/verify/index" // 核销
|
||
];
|
||
if (tabbar.indexOf(to) != -1) mode = 'tabbar';
|
||
|
||
Store.commit('setCurrRoute', to);
|
||
let url = to;
|
||
if (param != undefined) {
|
||
Object.keys(param).forEach(function(key) {
|
||
if (url.indexOf('?') != -1) {
|
||
url += "&" + key + "=" + param[key];
|
||
} else {
|
||
url += "?" + key + "=" + param[key];
|
||
}
|
||
});
|
||
}
|
||
switch (mode) {
|
||
case 'tabbar':
|
||
// 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
|
||
uni.switchTab({
|
||
url
|
||
})
|
||
break;
|
||
case 'redirectTo':
|
||
// 关闭当前页面,跳转到应用内的某个页面。
|
||
uni.redirectTo({
|
||
url
|
||
});
|
||
break;
|
||
case 'reLaunch':
|
||
// 关闭所有页面,打开到应用内的某个页面。
|
||
uni.reLaunch({
|
||
url
|
||
});
|
||
break;
|
||
default:
|
||
// 保留当前页面,跳转到应用内的某个页面
|
||
uni.navigateTo({
|
||
url
|
||
});
|
||
}
|
||
},
|
||
/**
|
||
* 图片路径转换
|
||
* @param {String} img_path 图片地址
|
||
* @param {Object} params 参数,针对商品、相册里面的图片区分大中小,size: big、mid、small
|
||
*/
|
||
img(img_path, params) {
|
||
var path = "";
|
||
if (img_path != undefined && img_path != "") {
|
||
if (img_path.split(',').length > 1) {
|
||
img_path = img_path.split(',')[0];
|
||
}
|
||
if (params && img_path) {
|
||
// 过滤默认图
|
||
let arr = img_path.split(".");
|
||
let suffix = arr[arr.length - 1];
|
||
arr.pop();
|
||
arr[arr.length - 1] = arr[arr.length - 1] + "_" + params.size.toUpperCase();
|
||
arr.push(suffix);
|
||
img_path = arr.join(".");
|
||
}
|
||
if (img_path.indexOf("http://") == -1 && img_path.indexOf("https://") == -1) {
|
||
path = Config.imgDomain + "/" + img_path;
|
||
} else {
|
||
path = img_path;
|
||
}
|
||
// 处理商品助手的图片路径
|
||
path = path.replace("addons/NsGoodsAssist/", "").replace("shop/goods/", "");
|
||
}
|
||
// path += '?t=' + parseInt(new Date().getTime() / 1000);
|
||
return path;
|
||
},
|
||
/**
|
||
* 验证当前数组或对象是否为空
|
||
* @param param 校验对象
|
||
*/
|
||
checkIsNotNull(param) {
|
||
if (param) {
|
||
if (typeof(param) == 'object') {
|
||
if (Array.isArray(param)) {
|
||
if (param.length > 0) return true
|
||
} else {
|
||
if (JSON.stringify(param) != '{}') return true
|
||
}
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
},
|
||
/**
|
||
* 金额格式化
|
||
* @param {Object} money
|
||
*/
|
||
moneyFormat(money) {
|
||
if (isNaN(parseFloat(money))) return money;
|
||
return parseFloat(money).toFixed(2);
|
||
},
|
||
/**
|
||
* 时间格式化
|
||
* @param {Object} time 时间戳
|
||
* @param {Object} format 输出格式
|
||
*/
|
||
timeFormat(time, format = 'y-m-d h:i:s') {
|
||
var date = new Date();
|
||
date.setTime(time * 1000);
|
||
|
||
var y = date.getFullYear();
|
||
var m = date.getMonth() + 1;
|
||
var d = date.getDate();
|
||
var h = date.getHours();
|
||
var i = date.getMinutes();
|
||
var s = date.getSeconds();
|
||
|
||
format = format.replace('y', y);
|
||
format = format.replace('m', (m < 10 ? '0' + m : m));
|
||
format = format.replace('d', (d < 10 ? '0' + d : d));
|
||
format = format.replace('h', (h < 10 ? '0' + h : h));
|
||
format = format.replace('i', (i < 10 ? '0' + i : i));
|
||
format = format.replace('s', (s < 10 ? '0' + s : s));
|
||
|
||
return format;
|
||
},
|
||
/**
|
||
* 日期格式转时间戳
|
||
* @param {Object} timeStamp
|
||
*/
|
||
timeTurnTimeStamp(string) {
|
||
var f = string.split(' ', 2);
|
||
var d = (f[0] ? f[0] : '').split('-', 3);
|
||
var t = (f[1] ? f[1] : '').split(':', 3);
|
||
return (new Date(
|
||
parseInt(d[0], 10) || null,
|
||
(parseInt(d[1], 10) || 1) - 1,
|
||
parseInt(d[2], 10) || null,
|
||
parseInt(t[0], 10) || null,
|
||
parseInt(t[1], 10) || null,
|
||
parseInt(t[2], 10) || null
|
||
)).getTime() / 1000;
|
||
},
|
||
/**
|
||
* 获取当前页面路由
|
||
*/
|
||
getCurrRoute() {
|
||
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
|
||
return routes.length ? routes[routes.length - 1].route : '';
|
||
},
|
||
/**
|
||
* 显示消息提示框
|
||
* @param {Object} params 参数
|
||
*/
|
||
showToast(params = {}) {
|
||
params.title = params.title || "";
|
||
params.icon = params.icon || "none";
|
||
// params.position = params.position || 'bottom';
|
||
params.duration = params.duration || 1500;
|
||
uni.showToast(params);
|
||
if (params.success) params.success();
|
||
},
|
||
/*
|
||
* 深度拷贝对象
|
||
* @param {Object} obj
|
||
*/
|
||
deepClone(obj) {
|
||
const isObject = function(obj) {
|
||
return typeof obj == 'object';
|
||
}
|
||
|
||
if (!isObject(obj)) {
|
||
throw new Error('obj 不是一个对象!')
|
||
}
|
||
//判断传进来的是对象还是数组
|
||
let isArray = Array.isArray(obj)
|
||
let cloneObj = isArray ? [] : {}
|
||
//通过for...in来拷贝
|
||
for (let key in obj) {
|
||
cloneObj[key] = isObject(obj[key]) ? this.deepClone(obj[key]) : obj[key]
|
||
}
|
||
return cloneObj
|
||
},
|
||
/**
|
||
* 图片选择加上传
|
||
* @param number num
|
||
* @param {Object} params
|
||
* @param {Object} callback
|
||
* @param string url
|
||
* return array
|
||
*/
|
||
upload: function(num, params, callback, url) {
|
||
const app_type = 'pc';
|
||
const app_type_name = 'PC';
|
||
|
||
var data = {
|
||
token: uni.getStorageSync('cashier_token'),
|
||
app_type: app_type,
|
||
app_type_name: app_type_name
|
||
}
|
||
data = Object.assign(data, params);
|
||
|
||
var imgs_num = num;
|
||
var _self = this;
|
||
|
||
uni.chooseImage({
|
||
count: imgs_num,
|
||
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
|
||
sourceType: ['album', 'camera'], //从相册或者拍照
|
||
success: async function(res) {
|
||
const tempFilePaths = res.tempFilePaths;
|
||
var _data = data;
|
||
var imgs = [];
|
||
for (var i = 0; i < tempFilePaths.length; i++) {
|
||
var path = await _self.upload_file_server(tempFilePaths[i], _data, params.path,
|
||
url);
|
||
imgs.push(path);
|
||
}
|
||
|
||
typeof callback == 'function' && callback(imgs);
|
||
},
|
||
fail: err => {
|
||
console.log('图片上传错误', err)
|
||
}
|
||
});
|
||
},
|
||
//上传
|
||
upload_file_server(tempFilePath, data, path, url = "") {
|
||
if (url) {
|
||
var uploadUrl = Config.baseUrl + url
|
||
} else {
|
||
var uploadUrl = Config.baseUrl + '/cashier/storeapi/upload/' + path
|
||
}
|
||
data.site_id = Config.siteId
|
||
return new Promise((resolve, reject) => {
|
||
uni.uploadFile({
|
||
url: uploadUrl,
|
||
filePath: tempFilePath,
|
||
name: 'file',
|
||
formData: data,
|
||
success: function(res) {
|
||
var path_str = JSON.parse(res.data);
|
||
if (path_str.code >= 0) {
|
||
resolve(path_str.data.pic_path);
|
||
} else {
|
||
reject("error");
|
||
}
|
||
},
|
||
});
|
||
});
|
||
}
|
||
}
|