文件
mashangyoujiu/Public/js/common.js

226 行
7.2 KiB
JavaScript
可执行文件

//门店权限
var store_auth = localStorage.store_auth;
if (store_auth) {
try {
Vue.prototype.storeAuth = JSON.parse(store_auth);
} catch (e) {
Vue.prototype.storeAuth = {};
}
} else {
Vue.prototype.storeAuth = {};
}
function handleFormData(data, formData, key = '') {
for (let i in data) {
if (typeof data[i] == "object") {
formData = handleFormData(data[i], formData, key != '' ? (key + '[' + i + ']') : i);
} else {
formData[key != '' ? (key + '[' + i + ']') : i] = data[i];
}
}
return formData;
}
//公共请求
Vue.prototype.sendRequest = function (url, params) {
let arr = window.location.href.split('://');
let request_url = arr[0] + '://' + arr[1].split('/')[0] + (url.indexOf('/index.php/') == -1 ? '/index.php/' : '') + url;
let formData = new FormData();
let data = typeof params.data == "object" ? params.data : {};
let swapData = handleFormData(data, {});
for (let i in swapData) {
formData.append(i, swapData[i]);
}
fetch(request_url, {
method: params.type && params.type.toUpperCase() == 'GET' ? 'GET' : 'POST',
headers: {},
credentials: 'include',
body: typeof params.data == "object" && JSON.stringify(params.data) != '{}' ? formData : null
}).then((response) => response.json()).then((jsonData) => {
if (typeof jsonData != "object") {
jsonData = {
status: 0,
message: '请求发生错误,请稍后重试'
};
} else {
jsonData.message = jsonData.message ? jsonData.message : (jsonData.msg ? jsonData.msg : '');
}
typeof params.success == "function" && params.success(jsonData);
}).catch((error) => {
console.log(error);
typeof params.success == "function" && params.success({
status: 0,
message: '请求发生错误,请稍后重试'
});
});
};
//获取当月时间到当前的时间范围
Vue.prototype.getThisMonthTime = function () {
let date = new Date();
let start = `${date.getFullYear()}-${date.getMonth() + 1}-1 00:00:00`;
let end = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
return [start, end];
};
//加载更多
Vue.directive('load-more', {
bind(el, binding) {
// 获取element-ui定义好的scroll盒子
const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
SELECTWRAP_DOM.addEventListener('scroll', function () {
const CONDITION = this.scrollHeight - this.scrollTop <= this.clientHeight;
if (CONDITION) {
binding.value();
}
})
}
});
/**
* 时间范围选择器
*/
Vue.component('date-picker-range', {
template: `<el-date-picker v-model="time"
:size="size"
:style="pickerStyle"
:type="hms?'datetimerange':'daterange'"
:clearable="clearable" @change="changeDate"
:format="hms?'yyyy-MM-dd HH:mm:ss':'yyyy-MM-dd'"
:value-format="hms?'yyyy-MM-dd HH:mm:ss':'yyyy-MM-dd'"
:range-separator="rangeSeparator"
:start-placeholder="startPlaceholder"
:end-placeholder="endPlaceholder"
:picker-options="pickerOptions.shortcuts.length?pickerOptions.shortcuts:null">
</el-date-picker>`,
props: {
'dateTime': {
default: [],
},//1商户 2门店
'hms': {
type: Boolean,
default: true,
},//是否需要选择时分秒
'autoFillHms': {
type: Boolean,
default: true,
},//不选择时分秒的时候是否自动补充起始时间
'startPlaceholder': {
type: String,
default() {
return this.hms ? '开始时间' : '开始日期'
}
},
'endPlaceholder': {
type: String,
default() {
return this.hms ? '结束时间' : '结束日期'
}
},
'clearable':{
type: Boolean,
default: true,
},
'rangeSeparator': {
type: String,
default: '至'
},
'size': {
type: String,
default: 'small'
},
'pickerStyle': {
type: String,
default: ''
},
'pickerRange': {
type: Number,
default: 3,
},//显示快捷时间的范围 1周 2一个月 3三个月
},
data() {
return {
time: [],
pickerOptions: {
shortcuts: []
},
};
},
methods: {
changeDate() {
if (this.time == null) {
this.time = [];
}
this.$nextTick(() => {
let time = [...this.time];
!this.hms && this.autoFillHms && time.length && (time[0] += ' 00:00:00') && (time[1] += ' 23:59:59')
this.$emit('update:dateTime', time);
this.$emit('change', time);
delete time;
})
}
},
created() {
if (this.pickerRange > 0) {
this.pickerOptions.shortcuts.push({
text: '最近一周',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
picker.$emit('pick', [start, end]);
}
})
}
if (this.pickerRange > 1) {
this.pickerOptions.shortcuts.push({
text: '最近一个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
picker.$emit('pick', [start, end]);
}
})
}
if (this.pickerRange > 2) {
this.pickerOptions.shortcuts.push({
text: '最近三个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
picker.$emit('pick', [start, end]);
}
})
}
if (this.dateTime) {
this.time = this.dateTime;
}
},
watch: {
dateTime(n) {
if (n !== this.time) this.time = n;
},
}
});
//获取操作权限
Vue.prototype.checkOperateAuth = function (auth_list, callback) {
Vue.prototype.sendRequest('Retail/Component/checkOperateAuth', {
data: {
auth_list: JSON.stringify(auth_list),
},
success: (res) => {
if (res.status == 1) {
callback(res.data);
} else {
callback({});
}
}
})
};