初始化项目
This commit is contained in:
13
attractor-ui/utils/auth.js
Normal file
13
attractor-ui/utils/auth.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const TokenKey = 'App-Token'
|
||||
|
||||
export function getToken() {
|
||||
return uni.getStorageSync(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return uni.setStorageSync(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return uni.removeStorageSync(TokenKey)
|
||||
}
|
||||
78
attractor-ui/utils/common.js
Normal file
78
attractor-ui/utils/common.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 显示消息提示框
|
||||
* @param content 提示的标题
|
||||
*/
|
||||
export function toast(content) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: content
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示模态弹窗
|
||||
* @param content 提示的标题
|
||||
*/
|
||||
export function showConfirm(content) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: content,
|
||||
cancelText: '取消',
|
||||
confirmText: '确定',
|
||||
success: function(res) {
|
||||
resolve(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数处理
|
||||
* @param params 参数
|
||||
*/
|
||||
export function tansParams(params) {
|
||||
let result = ''
|
||||
for (const propName of Object.keys(params)) {
|
||||
const value = params[propName]
|
||||
var part = encodeURIComponent(propName) + "="
|
||||
if (value !== null && value !== "" && typeof (value) !== "undefined") {
|
||||
if (typeof value === 'object') {
|
||||
for (const key of Object.keys(value)) {
|
||||
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
|
||||
let params = propName + '[' + key + ']'
|
||||
var subPart = encodeURIComponent(params) + "="
|
||||
result += subPart + encodeURIComponent(value[key]) + "&"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result += part + encodeURIComponent(value) + "&"
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取设备类型(安卓/苹果)
|
||||
export function getDeviceType() {
|
||||
try {
|
||||
// 同步获取系统信息(uniapp核心API,支持多端)
|
||||
const systemInfo = uni.getSystemInfoSync();
|
||||
// platform字段返回值:android / ios / devtools(微信开发者工具) / windows / mac 等
|
||||
const { platform } = systemInfo;
|
||||
|
||||
// 精准判断设备类型
|
||||
if (platform === 'android') {
|
||||
return 'android'; // 安卓设备
|
||||
} else if (platform === 'ios') {
|
||||
return 'ios'; // 苹果设备
|
||||
} else {
|
||||
// 非安卓/苹果的情况(如小程序开发者工具、Windows、Mac、鸿蒙等)
|
||||
return 'unknown';
|
||||
}
|
||||
} catch (error) {
|
||||
// 捕获获取系统信息失败的异常(极低概率,如权限问题)
|
||||
console.error('获取设备类型失败:', error);
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
9
attractor-ui/utils/constant.js
Normal file
9
attractor-ui/utils/constant.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const constant = {
|
||||
avatar: 'user_avatar',
|
||||
id: 'user_id',
|
||||
name: 'user_name',
|
||||
roles: 'user_roles',
|
||||
permissions: 'user_permissions'
|
||||
}
|
||||
|
||||
export default constant
|
||||
6
attractor-ui/utils/errorCode.js
Normal file
6
attractor-ui/utils/errorCode.js
Normal file
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
'401': '认证失败,无法访问系统资源',
|
||||
'403': '当前操作没有权限',
|
||||
'404': '访问资源不存在',
|
||||
'default': '系统未知错误,请反馈给管理员'
|
||||
}
|
||||
51
attractor-ui/utils/permission.js
Normal file
51
attractor-ui/utils/permission.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import store from '@/store'
|
||||
|
||||
/**
|
||||
* 字符权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function checkPermi(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissions = store.getters && store.getters.permissions
|
||||
const permissionDatas = value
|
||||
const all_permission = "*:*:*"
|
||||
|
||||
const hasPermission = permissions.some(permission => {
|
||||
return all_permission === permission || permissionDatas.includes(permission)
|
||||
})
|
||||
|
||||
if (!hasPermission) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function checkRole(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const roles = store.getters && store.getters.roles
|
||||
const permissionRoles = value
|
||||
const super_admin = "admin"
|
||||
|
||||
const hasRole = roles.some(role => {
|
||||
return super_admin === role || permissionRoles.includes(role)
|
||||
})
|
||||
|
||||
if (!hasRole) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
console.error(`need roles! Like checkRole="['admin','editor']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
73
attractor-ui/utils/request.js
Normal file
73
attractor-ui/utils/request.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import store from '@/store'
|
||||
import config from '@/config'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
import { toast, showConfirm, tansParams } from '@/utils/common'
|
||||
|
||||
let timeout = 10000
|
||||
const baseUrl = config.baseUrl
|
||||
|
||||
const request = config => {
|
||||
// 是否需要设置 token
|
||||
const isToken = (config.headers || {}).isToken === false
|
||||
config.header = config.header || {}
|
||||
if (getToken() && !isToken) {
|
||||
config.header['Authorization'] = 'Bearer ' + getToken()
|
||||
}
|
||||
// get请求映射params参数
|
||||
if (config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params)
|
||||
url = url.slice(0, -1)
|
||||
config.url = url
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
method: config.method || 'get',
|
||||
timeout: config.timeout || timeout,
|
||||
url: config.baseUrl || baseUrl + config.url,
|
||||
data: config.data,
|
||||
header: config.header,
|
||||
dataType: 'json'
|
||||
}).then(response => {
|
||||
let [error, res] = response
|
||||
if (error) {
|
||||
toast('后端接口连接异常')
|
||||
reject('后端接口连接异常')
|
||||
return
|
||||
}
|
||||
const code = res.data.code || 200
|
||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||
if (code === 401) {
|
||||
showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
|
||||
if (res.confirm) {
|
||||
store.dispatch('LogOut').then(res => {
|
||||
uni.reLaunch({ url: '/pages/login' })
|
||||
})
|
||||
}
|
||||
})
|
||||
reject('无效的会话,或者会话已过期,请重新登录。')
|
||||
} else if (code === 500) {
|
||||
toast(msg)
|
||||
reject('500')
|
||||
} else if (code !== 200) {
|
||||
toast(msg)
|
||||
reject(code)
|
||||
}
|
||||
resolve(res.data)
|
||||
})
|
||||
.catch(error => {
|
||||
let { message } = error
|
||||
if (message === 'Network Error') {
|
||||
message = '后端接口连接异常'
|
||||
} else if (message.includes('timeout')) {
|
||||
message = '系统接口请求超时'
|
||||
} else if (message.includes('Request failed with status code')) {
|
||||
message = '系统接口' + message.slice(-3) + '异常'
|
||||
}
|
||||
toast(message)
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default request
|
||||
32
attractor-ui/utils/storage.js
Normal file
32
attractor-ui/utils/storage.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import constant from './constant'
|
||||
|
||||
// 存储变量名
|
||||
let storageKey = 'storage_data'
|
||||
|
||||
// 存储节点变量名
|
||||
let storageNodeKeys = [constant.avatar, constant.id, constant.name, constant.roles, constant.permissions]
|
||||
|
||||
const storage = {
|
||||
set: function(key, value) {
|
||||
if (storageNodeKeys.indexOf(key) != -1) {
|
||||
let tmp = uni.getStorageSync(storageKey)
|
||||
tmp = tmp ? tmp : {}
|
||||
tmp[key] = value
|
||||
uni.setStorageSync(storageKey, tmp)
|
||||
}
|
||||
},
|
||||
get: function(key) {
|
||||
let storageData = uni.getStorageSync(storageKey) || {}
|
||||
return storageData[key] || ""
|
||||
},
|
||||
remove: function(key) {
|
||||
let storageData = uni.getStorageSync(storageKey) || {}
|
||||
delete storageData[key]
|
||||
uni.setStorageSync(storageKey, storageData)
|
||||
},
|
||||
clean: function() {
|
||||
uni.removeStorageSync(storageKey)
|
||||
}
|
||||
}
|
||||
|
||||
export default storage
|
||||
331
attractor-ui/utils/upgrade.js
Normal file
331
attractor-ui/utils/upgrade.js
Normal file
@@ -0,0 +1,331 @@
|
||||
|
||||
// 更新配置
|
||||
const baseURL = 'http://49.232.154.205:10900/fadapp-update/attractor'
|
||||
|
||||
// 匹配第一个出现的数字版本号
|
||||
function extractVersionNum(str) {
|
||||
const match = str.match(/(\d+\.\d+(?:\.\d+)?)/);
|
||||
return match ? match[1] : '0.0.0';
|
||||
}
|
||||
|
||||
function compareVersion(v1, v2) {
|
||||
v1 = extractVersionNum(v1).split('.').map(Number);
|
||||
v2 = extractVersionNum(v2).split('.').map(Number);
|
||||
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
|
||||
const n1 = v1[i] || 0;
|
||||
const n2 = v2[i] || 0;
|
||||
if (n1 > n2) return 1;
|
||||
if (n1 < n2) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function clearIgnoredVersion() {
|
||||
uni.removeStorageSync('ignoredVersion');
|
||||
console.log('已清除忽略的版本设置');
|
||||
}
|
||||
|
||||
function getIgnoredVersion() {
|
||||
return uni.getStorageSync('ignoredVersion');
|
||||
}
|
||||
|
||||
function setIgnoredVersion(version) {
|
||||
console.log('已设置忽略版本:', version);
|
||||
}
|
||||
|
||||
function checkVersionCompatibility(wgtVersion, baseVersion) {
|
||||
console.log('检查版本兼容性:', { wgtVersion, baseVersion });
|
||||
const wgtMajor = extractVersionNum(wgtVersion).split('.')[0];
|
||||
const baseMajor = extractVersionNum(baseVersion).split('.')[0];
|
||||
if (wgtMajor !== baseMajor) {
|
||||
console.warn('主版本号不匹配:', { wgtMajor, baseMajor });
|
||||
return false;
|
||||
}
|
||||
if (compareVersion(wgtVersion, baseVersion) <= 0) {
|
||||
console.warn('wgt版本不高于基座版本');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function checkStorageSpace() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef APP-PLUS
|
||||
if (typeof plus !== 'undefined' && plus.io) {
|
||||
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
|
||||
fs.root.getDirectory('temp', { create: true }, (dir) => {
|
||||
resolve(true);
|
||||
}, (error) => {
|
||||
console.error('检查存储空间失败:', error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
resolve(true);
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
function checkUpdate(forceCheck = false) {
|
||||
// 1. 准备本地版本信息
|
||||
const localVersion = plus.runtime.version; // 基座版本
|
||||
const staticVersion = '0.1.0'; // 静态默认版本
|
||||
// const localWgtVersion = staticVersion;
|
||||
const localWgtVersion = uni.getStorageSync('wgtVersion') || staticVersion; // 本地wgt版本(从存储获取或用默认)
|
||||
const currentVersion = compareVersion(localWgtVersion, localVersion) > 0
|
||||
? localWgtVersion
|
||||
: localVersion; // 当前有效版本(取两者较高者)
|
||||
|
||||
// 2. 请求远程版本信息
|
||||
uni.request({
|
||||
url: `${baseURL}/version.json?t=${Date.now()}`, // 加时间戳防缓存
|
||||
success: handleRemoteVersionSuccess,
|
||||
fail: handleCheckFail
|
||||
});
|
||||
|
||||
|
||||
// 处理远程版本请求成功
|
||||
function handleRemoteVersionSuccess(res) {
|
||||
const remoteVersion = res.data.version;
|
||||
const wgtUrl = res.data.wgtUrl;
|
||||
|
||||
console.log(
|
||||
'本地基座版本:', localVersion,
|
||||
'本地wgt版本:', localWgtVersion,
|
||||
'当前对比版本:', currentVersion,
|
||||
'远程版本:', remoteVersion
|
||||
);
|
||||
|
||||
// 远程版本高于本地wgt版本时才需要处理更新
|
||||
if (compareVersion(remoteVersion, localWgtVersion) > 0) {
|
||||
handleNeedUpdate(remoteVersion, wgtUrl);
|
||||
} else {
|
||||
console.log('当前已是最新版本');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理需要更新的情况
|
||||
function handleNeedUpdate(remoteVersion, wgtUrl) {
|
||||
// 2.1 先检查版本兼容性
|
||||
if (!checkVersionCompatibility(remoteVersion, localWgtVersion)) {
|
||||
handleIncompatibleVersion(remoteVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2.2 检查用户是否忽略过该版本(非强制检查时)
|
||||
const ignoredVersion = uni.getStorageSync('ignoredVersion');
|
||||
if (!forceCheck && ignoredVersion === remoteVersion) {
|
||||
console.log('用户已选择忽略此版本:', remoteVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2.3 显示更新提示
|
||||
showUpdateModal(remoteVersion, wgtUrl);
|
||||
}
|
||||
|
||||
|
||||
// 处理版本不兼容的情况(引导下载完整安装包)
|
||||
function handleIncompatibleVersion(remoteVersion) {
|
||||
console.warn('版本不兼容,跳转到浏览器下载新版安装包');
|
||||
uni.showModal({
|
||||
title: '版本不兼容',
|
||||
content: `新版本 ${remoteVersion} 与当前基座版本 ${localVersion} 不兼容,请前往官网下载最新安装包。`,
|
||||
showCancel: true,
|
||||
confirmText: '去下载',
|
||||
cancelText: '取消',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
const downloadUrl = `${baseURL}/klp.apk`;
|
||||
// 区分环境打开下载链接
|
||||
plus.runtime.openURL(downloadUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 显示更新提示模态框
|
||||
function showUpdateModal(remoteVersion, wgtUrl) {
|
||||
uni.showModal({
|
||||
title: '发现新版本',
|
||||
content: `检测到新版本(${remoteVersion}),是否立即下载并更新?`,
|
||||
confirmText: '立即更新',
|
||||
cancelText: '暂不更新',
|
||||
showCancel: true,
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
// 用户确认更新:检查存储空间 -> 下载 -> 安装
|
||||
handleConfirmUpdate(wgtUrl, remoteVersion);
|
||||
} else {
|
||||
// 直接退出
|
||||
|
||||
// 用户取消更新:询问是否忽略该版本
|
||||
// handleCancelUpdate(remoteVersion);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理用户确认更新
|
||||
function handleConfirmUpdate(wgtUrl, remoteVersion) {
|
||||
checkStorageSpace()
|
||||
.then(() => {
|
||||
// 存储空间充足,开始下载
|
||||
uni.showLoading({ title: '正在下载更新包...' });
|
||||
console.log('开始下载wgt包:', wgtUrl);
|
||||
|
||||
uni.downloadFile({
|
||||
url: wgtUrl,
|
||||
success: (downloadResult) => handleDownloadSuccess(downloadResult, remoteVersion),
|
||||
fail: handleDownloadFail
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
// 存储空间不足
|
||||
console.error('存储空间检查失败:', error);
|
||||
uni.showModal({
|
||||
title: '存储空间不足',
|
||||
content: '设备存储空间不足,无法下载更新包',
|
||||
showCancel: false,
|
||||
confirmText: '确定'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理下载成功
|
||||
function handleDownloadSuccess(downloadResult, remoteVersion) {
|
||||
uni.hideLoading();
|
||||
console.log('下载结果:', downloadResult);
|
||||
|
||||
if (downloadResult.statusCode === 200) {
|
||||
// 下载成功,开始安装wgt包
|
||||
installWgtPackage(downloadResult.tempFilePath, remoteVersion);
|
||||
} else {
|
||||
// 下载状态码异常
|
||||
console.error('下载失败,状态码:', downloadResult.statusCode);
|
||||
uni.showModal({
|
||||
title: '下载失败',
|
||||
content: `服务器返回错误,状态码: ${downloadResult.statusCode}`,
|
||||
showCancel: false,
|
||||
confirmText: '确定'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 安装wgt包
|
||||
function installWgtPackage(filePath, remoteVersion) {
|
||||
console.log('开始安装wgt包:', filePath);
|
||||
|
||||
plus.io.resolveLocalFileSystemURL(filePath,
|
||||
// 文件存在,执行安装
|
||||
() => {
|
||||
plus.runtime.install(filePath, { force: true },
|
||||
// 安装成功
|
||||
() => {
|
||||
console.log('wgt包安装成功');
|
||||
uni.setStorageSync('wgtVersion', remoteVersion); // 更新本地wgt版本记录
|
||||
showRestartConfirm();
|
||||
},
|
||||
// 安装失败
|
||||
(e) => handleInstallFail(e)
|
||||
);
|
||||
},
|
||||
// 文件不存在/损坏
|
||||
(error) => {
|
||||
console.error('文件不存在:', error);
|
||||
uni.showModal({
|
||||
title: '安装失败',
|
||||
content: '下载的文件不存在或已损坏',
|
||||
showCancel: false,
|
||||
confirmText: '确定'
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 显示重启确认
|
||||
function showRestartConfirm() {
|
||||
uni.showModal({
|
||||
title: '更新完成',
|
||||
content: '应用需要重启才能生效,是否立即重启?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
plus.runtime.restart();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理安装失败
|
||||
function handleInstallFail(e) {
|
||||
console.error('wgt包安装失败:', e);
|
||||
let errorMsg = '安装失败';
|
||||
if (e?.message) errorMsg += ': ' + e.message;
|
||||
if (e?.code) errorMsg += ' (错误代码: ' + e.code + ')';
|
||||
|
||||
uni.showModal({
|
||||
title: '安装失败',
|
||||
content: `${errorMsg}\n\n可能的原因:\n1. 版本不兼容\n2. 文件损坏\n3. 权限不足\n4. 存储空间不足`,
|
||||
showCancel: false,
|
||||
confirmText: '确定'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理下载失败
|
||||
function handleDownloadFail(error) {
|
||||
uni.hideLoading();
|
||||
console.error('下载失败:', error);
|
||||
uni.showModal({
|
||||
title: '下载失败',
|
||||
content: '网络连接异常,请检查网络后重试',
|
||||
showCancel: false,
|
||||
confirmText: '确定'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理用户取消更新(询问是否忽略)
|
||||
function handleCancelUpdate(remoteVersion) {
|
||||
uni.showModal({
|
||||
title: '忽略更新',
|
||||
content: `是否忽略版本 ${remoteVersion}?忽略后下次启动时将不再提示此版本更新。`,
|
||||
confirmText: '忽略此版本',
|
||||
cancelText: '下次提醒',
|
||||
success: (ignoreRes) => {
|
||||
if (ignoreRes.confirm) {
|
||||
uni.setStorageSync('ignoredVersion', remoteVersion);
|
||||
uni.showToast({ title: '已忽略此版本更新' });
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 处理检查更新请求失败
|
||||
function handleCheckFail(error) {
|
||||
console.error('检查更新失败:', error);
|
||||
uni.showToast({ title: '网络异常,请稍后重试' });
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
checkUpdate,
|
||||
clearIgnoredVersion,
|
||||
getIgnoredVersion,
|
||||
setIgnoredVersion,
|
||||
checkVersionCompatibility,
|
||||
checkStorageSpace,
|
||||
compareVersion,
|
||||
extractVersionNum
|
||||
};
|
||||
70
attractor-ui/utils/upload.js
Normal file
70
attractor-ui/utils/upload.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import store from '@/store'
|
||||
import config from '@/config'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
import { toast, showConfirm, tansParams } from '@/utils/common'
|
||||
|
||||
let timeout = 10000
|
||||
const baseUrl = config.baseUrl
|
||||
|
||||
const upload = config => {
|
||||
// 是否需要设置 token
|
||||
const isToken = (config.headers || {}).isToken === false
|
||||
config.header = config.header || {}
|
||||
if (getToken() && !isToken) {
|
||||
config.header['Authorization'] = 'Bearer ' + getToken()
|
||||
}
|
||||
// get请求映射params参数
|
||||
if (config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params)
|
||||
url = url.slice(0, -1)
|
||||
config.url = url
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
timeout: config.timeout || timeout,
|
||||
url: baseUrl + config.url,
|
||||
filePath: config.filePath,
|
||||
name: config.name || 'file',
|
||||
header: config.header,
|
||||
formData: config.formData,
|
||||
success: (res) => {
|
||||
let result = JSON.parse(res.data)
|
||||
const code = result.code || 200
|
||||
const msg = errorCode[code] || result.msg || errorCode['default']
|
||||
if (code === 200) {
|
||||
resolve(result)
|
||||
} else if (code == 401) {
|
||||
showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
|
||||
if (res.confirm) {
|
||||
store.dispatch('LogOut').then(res => {
|
||||
uni.reLaunch({ url: '/pages/login/login' })
|
||||
})
|
||||
}
|
||||
})
|
||||
reject('无效的会话,或者会话已过期,请重新登录。')
|
||||
} else if (code === 500) {
|
||||
toast(msg)
|
||||
reject('500')
|
||||
} else if (code !== 200) {
|
||||
toast(msg)
|
||||
reject(code)
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
let { message } = error
|
||||
if (message == 'Network Error') {
|
||||
message = '后端接口连接异常'
|
||||
} else if (message.includes('timeout')) {
|
||||
message = '系统接口请求超时'
|
||||
} else if (message.includes('Request failed with status code')) {
|
||||
message = '系统接口' + message.substr(message.length - 3) + '异常'
|
||||
}
|
||||
toast(message)
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default upload
|
||||
114
attractor-ui/utils/validate.js
Normal file
114
attractor-ui/utils/validate.js
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* 路径匹配器
|
||||
* @param {string} pattern
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isPathMatch(pattern, path) {
|
||||
const regexPattern = pattern.replace(/\//g, '\\/').replace(/\*\*/g, '.*').replace(/\*/g, '[^\\/]*')
|
||||
const regex = new RegExp(`^${regexPattern}$`)
|
||||
return regex.test(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断value字符串是否为空
|
||||
* @param {string} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isEmpty(value) {
|
||||
if (value == null || value == "" || value == undefined || value == "undefined") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断url是否是http或https
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isHttp(url) {
|
||||
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断path是否为外链
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isExternal(path) {
|
||||
return /^(https?:|mailto:|tel:)/.test(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validUsername(str) {
|
||||
const valid_map = ['admin', 'editor']
|
||||
return valid_map.indexOf(str.trim()) >= 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validURL(url) {
|
||||
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
|
||||
return reg.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validLowerCase(str) {
|
||||
const reg = /^[a-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validUpperCase(str) {
|
||||
const reg = /^[A-Z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validAlphabets(str) {
|
||||
const reg = /^[A-Za-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} email
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validEmail(email) {
|
||||
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||
return reg.test(email)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isString(str) {
|
||||
return typeof str === 'string' || str instanceof String
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array} arg
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isArray(arg) {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]'
|
||||
}
|
||||
return Array.isArray(arg)
|
||||
}
|
||||
Reference in New Issue
Block a user