初始化
This commit is contained in:
13
utils/auth.js
Normal file
13
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)
|
||||
}
|
||||
54
utils/common.js
Normal file
54
utils/common.js
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* 显示消息提示框
|
||||
* @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
|
||||
}
|
||||
9
utils/constant.js
Normal file
9
utils/constant.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const constant = {
|
||||
avatar: 'vuex_avatar',
|
||||
name: 'vuex_name',
|
||||
roles: 'vuex_roles',
|
||||
permissions: 'vuex_permissions',
|
||||
nickName: 'vuex_nickName'
|
||||
}
|
||||
|
||||
export default constant
|
||||
6
utils/errorCode.js
Normal file
6
utils/errorCode.js
Normal file
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
'401': '认证失败,无法访问系统资源',
|
||||
'403': '当前操作没有权限',
|
||||
'404': '访问资源不存在',
|
||||
'default': '系统未知错误,请反馈给管理员'
|
||||
}
|
||||
24
utils/formatTime.js
Normal file
24
utils/formatTime.js
Normal file
@@ -0,0 +1,24 @@
|
||||
function formatTime(dateTimeStr) {
|
||||
// 将字符串解析为Date对象
|
||||
const date = new Date(dateTimeStr);
|
||||
|
||||
// 验证日期是否有效
|
||||
if (isNaN(date.getTime())) {
|
||||
throw new Error('Invalid date string');
|
||||
}
|
||||
|
||||
// 获取各个时间部分
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
let hours = date.getHours();
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
|
||||
// 将24小时制转换为12小时制,并处理午夜情况
|
||||
hours = hours === 0 ? 12 : hours % 12;
|
||||
|
||||
// 拼接字符串
|
||||
return `${year}年${month}月${day}日 ${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
export default formatTime; // 直接导出函数,而不是一个包含函数的对象
|
||||
51
utils/permission.js
Normal file
51
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
|
||||
}
|
||||
}
|
||||
2607
utils/reqrcode.js
Normal file
2607
utils/reqrcode.js
Normal file
File diff suppressed because it is too large
Load Diff
83
utils/request.js
Normal file
83
utils/request.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import store from '@/store'
|
||||
import appConfig from '@/config'
|
||||
import {
|
||||
getToken
|
||||
} from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
import {
|
||||
toast,
|
||||
showConfirm,
|
||||
tansParams
|
||||
} from '@/utils/common'
|
||||
|
||||
let timeout = 10000
|
||||
const baseUrl = 'http://172.20.10.2:8081'
|
||||
|
||||
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: appConfig.baseUrl + config.url || 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.substr(message.length - 3) + '异常'
|
||||
}
|
||||
toast(message)
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default request
|
||||
7
utils/separateImgs.js
Normal file
7
utils/separateImgs.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import config from '@/config'
|
||||
const baseUrl = config.baseUrl
|
||||
|
||||
export separateImgs (imgUrl){
|
||||
let url = imgUrl.replace(baseUrl, "");
|
||||
return url
|
||||
}
|
||||
32
utils/storage.js
Normal file
32
utils/storage.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import constant from './constant'
|
||||
|
||||
// 存储变量名
|
||||
let storageKey = 'storage_data'
|
||||
|
||||
// 存储节点变量名
|
||||
let storageNodeKeys = [constant.avatar, constant.name, constant.roles, constant.permissions,constant.nickName]
|
||||
|
||||
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
|
||||
70
utils/upload.js
Normal file
70
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.data==undefined?config.filePath:config.data,
|
||||
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
|
||||
97
utils/watermark.js
Normal file
97
utils/watermark.js
Normal file
@@ -0,0 +1,97 @@
|
||||
// 新添加的水印功能----知无涯
|
||||
/**
|
||||
* 为图片添加水印
|
||||
* @param {File} file - 原始图片文件
|
||||
* @param {string} text - 水印文字
|
||||
* @param {object} options - 水印配置选项
|
||||
* @returns {Promise<File>} - 返回带水印的新图片文件
|
||||
*/
|
||||
export const addWatermarkToImage = (file, text, options = {}) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!file || !(file instanceof Blob)) {
|
||||
reject(new Error('参数必须是Blob或File对象'));
|
||||
return;
|
||||
}
|
||||
if (!text) {
|
||||
reject(new Error('水印文字不能为空'));
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
// 绘制原始图片
|
||||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
// 设置水印样式 - 增强可见性
|
||||
// ctx.font = options.font || 'bold 20px Arial'; // 加大字号并加粗
|
||||
// ctx.fillStyle = options.color || 'rgba(0, 0, 0, 0.7)'; // 改为黑色,提高不透明度
|
||||
// ctx.textAlign = 'center';
|
||||
// ctx.textBaseline = 'middle';
|
||||
// 计算字体大小
|
||||
const fontSize = Math.min(canvas.width, canvas.height) / 20; // 根据图片大小动态调整字体大小
|
||||
ctx.font = `bold ${fontSize}px Arial`; // 加大字号并加粗
|
||||
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)'; // 改为黑色,提高不透明度
|
||||
ctx.textAlign = 'right';
|
||||
ctx.textBaseline = 'bottom';
|
||||
|
||||
// 添加文字阴影增强可读性
|
||||
ctx.shadowColor = 'rgba(255, 255, 255, 0.8)';
|
||||
ctx.shadowBlur = 5;
|
||||
ctx.shadowOffsetX = 1;
|
||||
ctx.shadowOffsetY = 1;
|
||||
|
||||
// 添加水印文字
|
||||
//ctx.fillText(text, canvas.width / 2, canvas.height / 2);
|
||||
|
||||
// 拆分文本为多行
|
||||
const lines = text.split('\n');
|
||||
//const lineHeight = fontSize * 1.5; // 每行的高度,可以根据字体大小调整
|
||||
//const startY = (canvas.height / 2) - ((lines.length - 1) * lineHeight / 2);
|
||||
const lineHeight = fontSize * 1.5; // 每行的高度,可以根据字体大小调整
|
||||
const startX = canvas.width - 10; // 右下角的x坐标,留10像素的边距
|
||||
let startY = canvas.height - 10; // 右下角的y坐标,留10像素的边距
|
||||
|
||||
// 添加水印文字
|
||||
// lines.forEach((line, index) => {
|
||||
// ctx.fillText(line, canvas.width / 2, startY + index * lineHeight);
|
||||
// });
|
||||
lines.forEach((line) => {
|
||||
const textWidth = ctx.measureText(line).width;
|
||||
ctx.fillText(line, startX, startY);
|
||||
startY -= lineHeight; // 每行向上移动一行的高度
|
||||
});
|
||||
|
||||
// 清除阴影设置
|
||||
ctx.shadowColor = 'transparent';
|
||||
|
||||
// 转换为Blob
|
||||
canvas.toBlob((blob) => {
|
||||
if (!blob) {
|
||||
reject(new Error('图片转换失败'));
|
||||
return;
|
||||
}
|
||||
const newFile = new File([blob], file.name || 'watermarked_image', {
|
||||
type: file.type || 'image/jpeg',
|
||||
lastModified: Date.now()
|
||||
});
|
||||
resolve(newFile);
|
||||
}, file.type || 'image/jpeg');
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
img.onerror = () => reject(new Error('图片加载失败'));
|
||||
img.src = e.target.result;
|
||||
};
|
||||
reader.onerror = () => reject(new Error('文件读取失败'));
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user