common.js
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import axios from 'axios'
export const Common = {
// 提示框
confirmDialog: (tipData) => {
tipData.that.$confirm(tipData.tipText, tipData.tipTitle, {
confirmButtonText: tipData.confirmButtonText || '确定',
cancelButtonText: tipData.cancelButtonText || '取消',
type: tipData.type,
showCancelButton: tipData.showCancelButton
}).then(() => {
tipData.callBack ? tipData.callBack() : ''
}).catch(() => {
tipData.errorCallBack ? tipData.errorCallBack() : ''
})
},
/**
* 导出excel
* */
exportExcel: function (options) {
if (!options) {
return false
}
console.log(options.data)
axios({
method: options.method || 'POST',
url: options.url,
params: options.data,
responseType: 'blob',
headers: {
Authorization: localStorage.getItem('token') || ''
}
}).then(response => {
if (!response) {
return
}
let url = window.URL.createObjectURL(new Blob([response.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', options.fileName || 'excel.xlsx')
document.body.appendChild(link)
link.click()
if (options.success) {
(options.success)()
}
}).catch((error) => {
if (options.errorFunc) {
(options.errorFunc)(error)
}
})
},
/**
* 正整数
* */
checkRegExpNumber(rule, value, callback) {
var rxp = /^\+?[1-9][0-9]*$/
if (!rule.required) {
if (!value) {
callback()
}else if (!rxp.test(value)) {
return callback(new Error('只支持正整数'))
}
}
if (!value) {
return callback(new Error('请输入正整数'))
}else {
callback()
}
},
/**
* 手机号
* */
checkRegExpPhoneTel(rule, value, callback) {
var rxpPhone = /^1[345789]\d{9}$/
var rxpTel = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
if (!rule.required) {
if (!value) {
callback()
}else if (!rxpPhone.test(value)) {
return callback(new Error('请输入正确的手机号码'))
}
}
if (!value) {
return callback(new Error('请输入手机号码或固话'))
}else if (!rxpPhone.test(value) && !rxpTel.test(value)) {
return callback(new Error('请输入正确的手机号码或固话'))
}else {
callback()
}
}
}