mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-06-12 13:19:31 +08:00
v1.0.0-beta 源码正式开源
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
export const Platform = {
|
||||
ios: 'ios',
|
||||
android: 'android'
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前环境是什么环境
|
||||
*/
|
||||
export const checkPlatform = (name) => {
|
||||
return uni.getSystemInfoSync().platform == name;
|
||||
}
|
||||
|
||||
// 默认的应用设置
|
||||
export const _DefaultAppSettings = {
|
||||
showStartPage: false, // 是否每次启动都显示启动页
|
||||
banner: {
|
||||
useDot: true,
|
||||
dotPosition: 'right'
|
||||
},
|
||||
// 布局配置
|
||||
layout: {
|
||||
// h_row_col1 = 一行一列
|
||||
// h_row_col2 = 一行两列
|
||||
home: 'h_row_col1',
|
||||
// lr_image_text=左图右文
|
||||
// lr_text_image=左文右图
|
||||
// tb_image_text=上图下文
|
||||
// tb_text_image=上文下图
|
||||
// only_text=仅文字
|
||||
cardType: 'lr_image_text',
|
||||
},
|
||||
// 广告配置(todo)
|
||||
ad: {
|
||||
timeout: 3, // 屏蔽广告时长,时间到后自动恢复展示(单位小时)
|
||||
disabled: false, // 是否屏蔽广告(看广告可以关闭应用内设置的广告)
|
||||
},
|
||||
gallery: {
|
||||
// 是否使用瀑布流
|
||||
useWaterfull: false
|
||||
},
|
||||
links: {
|
||||
// 是否使用简约模式
|
||||
useSimple: false,
|
||||
useGroup: false,
|
||||
},
|
||||
about: {
|
||||
showAdmin: false, // 显示后台登录入口
|
||||
showAllCount: true, // 默认显示所有的统计信息(关于页面)
|
||||
},
|
||||
|
||||
// 文章配置
|
||||
article: {
|
||||
|
||||
},
|
||||
// 联系博主页面
|
||||
contact: {
|
||||
// 链接是否使用复制的方式,否则直接在内部打开(小程序需要配置对应链接的业务域名)
|
||||
isLinkCopy: true,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用设置
|
||||
*/
|
||||
export const getAppSettings = () => {
|
||||
let _appSettings = uni.getStorageSync('APP_GLOBAL_SETTINGS')
|
||||
if (_appSettings) return JSON.parse(_appSettings)
|
||||
|
||||
uni.setStorageSync('APP_GLOBAL_SETTINGS', JSON.stringify(_DefaultAppSettings))
|
||||
return _appSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存应用设置
|
||||
*/
|
||||
export const setAppSettings = (appSettings) => {
|
||||
uni.setStorageSync('APP_GLOBAL_SETTINGS', JSON.stringify(appSettings))
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 功能:token操作
|
||||
* 作者:小莫唐尼
|
||||
* 邮箱:studio@925i.cn
|
||||
* 时间:2022年07月21日 18:00:05
|
||||
* 版本:v0.1.0
|
||||
* 修改记录:
|
||||
* 修改内容:
|
||||
* 修改人员:
|
||||
* 修改时间:
|
||||
*/
|
||||
|
||||
import {
|
||||
getCache
|
||||
} from "./storage";
|
||||
|
||||
const TokenAuthKey = "EvanIAlley_App_Token";
|
||||
|
||||
/**
|
||||
* 获取登录token
|
||||
*/
|
||||
export function getAdminAccessToken() {
|
||||
const tokens = getCache('APP_ADMIN_LOGIN_TOKEN');
|
||||
if (tokens) {
|
||||
return tokens.access_token;
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取刷新token
|
||||
*/
|
||||
export function getAdminRefreshToken() {
|
||||
const tokens = getCache('APP_ADMIN_LOGIN_TOKEN');
|
||||
if (tokens) {
|
||||
return tokens.refresh_token;
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信登录信息
|
||||
*/
|
||||
export function setWxLoginInfo(info) {
|
||||
uni.setStorageSync('APP_WX_LOGIN_INFO', info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信登录信息
|
||||
*/
|
||||
export function getWxLoginInfo() {
|
||||
let wxLoginInfo = uni.getStorageSync('APP_WX_LOGIN_INFO');
|
||||
if (wxLoginInfo) {
|
||||
return JSON.parse(wxLoginInfo)
|
||||
} else {
|
||||
return null;
|
||||
// return {
|
||||
// avatarUrl: '',
|
||||
// nickName: '',
|
||||
// email: '',
|
||||
// url: ''
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已经微信授权
|
||||
*/
|
||||
export function checkHasWxLogin() {
|
||||
return !!uni.getStorageSync('APP_WX_LOGIN_INFO')
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查超管是否已经登录
|
||||
*/
|
||||
export function checkHasAdminLogin() {
|
||||
return !!getCache('APP_ADMIN_LOGIN_TOKEN')
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 图片缓存
|
||||
*/
|
||||
|
||||
/*
|
||||
* @description 获取文件的缓存路径,如果文件未缓存,则直接返回网络路径,并下载缓存
|
||||
* @method getImageCache
|
||||
* @param {String} filePath 完整的图片下载路径,如果没有从缓存中获取到,则用这个路径去下载
|
||||
* @param {String} fileMd5 文件md5,必须唯一
|
||||
* @return {Object} promise对象
|
||||
*/
|
||||
const getImageCache = (filePath, fileMd5) => {
|
||||
// 图片缓存key值
|
||||
let storageKey = 'IMAGE_CACHE_INFO_' + fileMd5
|
||||
// 首先获取本地存储的数据,查询是否有对应文件路径,如果有缓存内容,直接返回
|
||||
const cacheFileInfo = uni.getStorageSync(storageKey)
|
||||
if (cacheFileInfo) {
|
||||
// console.log("已缓存为:" + cacheFileInfo)
|
||||
return cacheFileInfo
|
||||
} else {
|
||||
// console.log("未缓存,进行下载保存")
|
||||
// 如果没有,执行下载,并存储起来后
|
||||
uni.downloadFile({
|
||||
url: filePath,
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
// console.log('下载成功');
|
||||
// 再进行本地保存
|
||||
uni.saveFile({
|
||||
tempFilePath: res.tempFilePath,
|
||||
success: function(res2) {
|
||||
uni.setStorageSync(storageKey, res2.savedFilePath)
|
||||
return res2.savedFilePath
|
||||
},
|
||||
fail: function(res2) {
|
||||
return filePath
|
||||
}
|
||||
})
|
||||
} else {
|
||||
return filePath
|
||||
}
|
||||
},
|
||||
fail: (res) => {
|
||||
return filePath
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getImageCache
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* 功能:工具类
|
||||
* 作者:小莫唐尼
|
||||
* 邮箱:studio@925i.cn
|
||||
* 时间:2022年07月21日 17:30:14
|
||||
* 版本:v0.1.0
|
||||
* 修改记录:
|
||||
* 修改内容:
|
||||
* 修改人员:
|
||||
* 修改时间:
|
||||
*/
|
||||
|
||||
import HaloConfig from '@/config/halo.config.js';
|
||||
|
||||
const utils = {
|
||||
/**
|
||||
* 检查是否为http/https链接
|
||||
*/
|
||||
checkIsUrl: function(s) {
|
||||
var reg = /^(http(s)?:\/\/)/i;
|
||||
return reg.test(s)
|
||||
},
|
||||
|
||||
// 检查链接
|
||||
checkUrl: function(url) {
|
||||
if (!url) return '';
|
||||
if (!this.checkIsUrl(url)) return HaloConfig.apiUrl + url;
|
||||
return url
|
||||
},
|
||||
|
||||
// 检查封面图
|
||||
checkThumbnailUrl: function(thumbnail) {
|
||||
if (!thumbnail) return HaloConfig.defaultThumbnailUrl + `&r=${new Date().getTime()}`;
|
||||
if (!this.checkIsUrl(thumbnail)) return HaloConfig.apiUrl + thumbnail;
|
||||
return thumbnail
|
||||
},
|
||||
|
||||
// 检查图片
|
||||
checkImageUrl: function(image) {
|
||||
if (!image) return HaloConfig.defaultImageUrl + `&r=${new Date().getTime()}`;
|
||||
if (!this.checkIsUrl(image)) return HaloConfig.apiUrl + image;
|
||||
return image
|
||||
},
|
||||
|
||||
// 检查头像
|
||||
checkAvatarUrl: function(avatar, isAdmin = false) {
|
||||
if (isAdmin) return HaloConfig.author.avatar;
|
||||
if (!avatar) {
|
||||
return HaloConfig.defaultAvatarUrl + `&r=${new Date().getTime()}`;
|
||||
}
|
||||
if (!this.checkIsUrl(avatar)) return HaloConfig.apiUrl + avatar;
|
||||
return avatar
|
||||
},
|
||||
|
||||
// 检查文件类型
|
||||
fnCheckIsFileType(type, attachment) {
|
||||
if (!attachment) return false;
|
||||
if (!attachment.mediaType) return false;
|
||||
if (type == 'video') return attachment.mediaType.indexOf('video/') != -1;
|
||||
else if (type == 'image') return attachment.mediaType.indexOf('image/') != -1;
|
||||
else return false;
|
||||
},
|
||||
groupData: function(arr, fun) {
|
||||
const groups = {}
|
||||
arr.forEach((el) => {
|
||||
const group = fun(el)
|
||||
groups[group] = groups[group] || []
|
||||
groups[group].push(el)
|
||||
})
|
||||
return Object.keys(groups).map((group) => {
|
||||
// 更改data的数据结构 可以改变新数据的结构 data即为newData的数组中每个数据的结构
|
||||
let data = {
|
||||
groupName: group,
|
||||
data: groups[group]
|
||||
}
|
||||
return data
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 数据分组
|
||||
* @param {Object} oldData 数据源
|
||||
* @param {Object} prop 分组属性
|
||||
*/
|
||||
arrayGroupBy: function(oldData, prop) {
|
||||
const newData = this.groupData(oldData, (item) => {
|
||||
return item[prop]
|
||||
})
|
||||
return newData
|
||||
},
|
||||
/**
|
||||
* 深克隆
|
||||
* @param {Object} obj 数据源
|
||||
*/
|
||||
deepClone(obj) {
|
||||
var objClone = Array.isArray(obj) ? [] : {};
|
||||
if (obj && typeof obj === "object") {
|
||||
for (const key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
if (obj[key] && typeof obj[key] === "object") {
|
||||
objClone[key] = this.deepClone(obj[key]);
|
||||
} else {
|
||||
objClone[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return objClone;
|
||||
},
|
||||
/**
|
||||
* 复制内容到剪贴板
|
||||
* @param {Object} obj 数据源
|
||||
*/
|
||||
copyText(content, tips = '复制成功') {
|
||||
uni.setClipboardData({
|
||||
data: content,
|
||||
showToast: false,
|
||||
success: () => {
|
||||
if (tips) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: tips
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
previewImage(list) {
|
||||
list = list.map(item => {
|
||||
return this.checkUrl(item)
|
||||
})
|
||||
uni.previewImage({
|
||||
urls: list,
|
||||
loop: true
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export default utils;
|
||||
@@ -0,0 +1,26 @@
|
||||
export const CheckNetWork = () => {
|
||||
let _text = ''
|
||||
uni.getNetworkType({
|
||||
success: (res) => {
|
||||
switch (res) {
|
||||
case '2g':
|
||||
case '3g':
|
||||
_text = '当前网络较慢,请稍等...'
|
||||
break;
|
||||
case 'none':
|
||||
_text = '啊偶,您的网络似乎断开啦~'
|
||||
break;
|
||||
}
|
||||
if (_text) {
|
||||
uni.$eShowModal({
|
||||
title: '提示',
|
||||
content: _text,
|
||||
showCancel: false,
|
||||
cancelColor: '#999999',
|
||||
confirmText: '知道啦',
|
||||
confirmColor: '#03a9f4'
|
||||
}).then(res => {}).catch(err => {})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 功能:路由权限和拦截
|
||||
* 作者:小莫唐尼
|
||||
* 邮箱:studio@925i.cn
|
||||
* 时间:2022年07月21日 18:05:24
|
||||
* 版本:v0.1.0
|
||||
* 修改记录:
|
||||
* 修改内容:
|
||||
* 修改人员:
|
||||
* 修改时间:
|
||||
*/
|
||||
|
||||
/**
|
||||
* @description 自定义路由拦截
|
||||
*/
|
||||
|
||||
import {
|
||||
checkHasAdminLogin
|
||||
} from "./auth.js";
|
||||
// 白名单
|
||||
const whiteList = [
|
||||
"/", // 注意入口页必须直接写 '/'
|
||||
{
|
||||
pattern: /^\/pages\/list.*/
|
||||
}, // 支持正则表达式
|
||||
{
|
||||
pattern: /^\/pages\/guidePage.*/
|
||||
}, // 支持正则表达式
|
||||
{
|
||||
pattern: /^\/pages\/login\/*/
|
||||
},
|
||||
];
|
||||
|
||||
export default async function() {
|
||||
const list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
|
||||
// 用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
|
||||
list.forEach((item) => {
|
||||
uni.addInterceptor(item, {
|
||||
invoke(e) {
|
||||
// 获取要跳转的页面路径(url去掉"?"和"?"后的参数)
|
||||
const url = e.url.split("?")[0];
|
||||
// 判断当前窗口是白名单,如果是则不重定向路由
|
||||
let pass;
|
||||
if (whiteList) {
|
||||
pass = whiteList.some((item) => {
|
||||
if (typeof item === "object" && item.pattern) {
|
||||
return item.pattern.test(url);
|
||||
}
|
||||
return url === item;
|
||||
});
|
||||
}
|
||||
|
||||
// 不是白名单并且没有token
|
||||
if (!pass && !checkHasAdminLogin()) {
|
||||
uni.showModal({
|
||||
title: "未登录",
|
||||
content: "当前未登录,是否前往登录?",
|
||||
showCancel: true,
|
||||
success: ({
|
||||
confirm,
|
||||
cancel
|
||||
}) => {
|
||||
if (confirm) {
|
||||
uni.navigateTo({
|
||||
url: "/pages/login/login",
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return e;
|
||||
},
|
||||
fail(err) {
|
||||
// 失败回调拦截
|
||||
console.log(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 功能:随机数工具
|
||||
* 作者:小莫唐尼
|
||||
* 邮箱:studio@925i.cn
|
||||
* 时间:2022年07月19日 11:34:02
|
||||
* 版本:v0.1.0
|
||||
* 修改记录:
|
||||
* 修改内容:
|
||||
* 修改人员:
|
||||
* 修改时间:
|
||||
*/
|
||||
|
||||
/**
|
||||
* 指定范围生成随机数
|
||||
* @param {Number} m
|
||||
* @param {Number} n
|
||||
* @return {Number}
|
||||
*/
|
||||
export function GetRandomNumberByRange(m, n) {
|
||||
return Math.floor(Math.random() * (m - n) + n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机数组长度
|
||||
* @param {*} len 数组长度
|
||||
* @param {*} min 最小值
|
||||
* @param {*} max 最大值
|
||||
*/
|
||||
export function GetRandomNumberArray(len, min, max) {
|
||||
let arr = [];
|
||||
for (let index = 0; index < len; index++) {
|
||||
arr.push(GetRandomNumberByRange(min, max));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 设置缓存
|
||||
* @param {缓存key} key
|
||||
* @param {需要存储的缓存值} value
|
||||
* @param {过期时间,默认0表示永久有效} expire
|
||||
*/
|
||||
export const setCache = (key, value, expire = 0) => {
|
||||
let obj = {
|
||||
data: value, //存储的数据
|
||||
time: Date.now() / 1000, //记录存储的时间戳
|
||||
expire: expire //记录过期时间,单位秒
|
||||
}
|
||||
uni.setStorageSync(key, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param {缓存key} key
|
||||
*/
|
||||
export const getCache = (key) => {
|
||||
let val = uni.getStorageSync(key)
|
||||
if (!val) {
|
||||
return null
|
||||
}
|
||||
val = JSON.parse(val)
|
||||
if (val.expire && Date.now() / 1000 - val.time > val.expire) {
|
||||
uni.removeStorageSync(key)
|
||||
return null
|
||||
}
|
||||
return val.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param {缓存key} key
|
||||
*/
|
||||
export const delCache = (key) => {
|
||||
uni.removeStorageSync(key)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
let timer, flag;
|
||||
/**
|
||||
* 节流原理:在一定时间内,只能触发一次
|
||||
*
|
||||
* @param {Function} func 要执行的回调函数
|
||||
* @param {Number} wait 延时的时间
|
||||
* @param {Boolean} immediate 是否立即执行
|
||||
* @return null
|
||||
*/
|
||||
function throttle(func, wait = 500, immediate = true) {
|
||||
if (immediate) {
|
||||
if (!flag) {
|
||||
flag = true;
|
||||
// 如果是立即执行,则在wait毫秒内开始时执行
|
||||
typeof func === 'function' && func();
|
||||
timer = setTimeout(() => {
|
||||
flag = false;
|
||||
}, wait);
|
||||
}
|
||||
} else {
|
||||
if (!flag) {
|
||||
flag = true
|
||||
// 如果是非立即执行,则在wait毫秒内的结束处执行
|
||||
timer = setTimeout(() => {
|
||||
flag = false
|
||||
typeof func === 'function' && func();
|
||||
}, wait);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
export default throttle
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 检查微信小程序更新
|
||||
*/
|
||||
export const CheckWxUpdate = (useTip = false) => {
|
||||
if (uni.canIUse('getUpdateManager')) {
|
||||
const updateManager = wx.getUpdateManager();
|
||||
updateManager && updateManager.onCheckForUpdate((res) => {
|
||||
if (res.hasUpdate) {
|
||||
updateManager.onUpdateReady(() => {
|
||||
uni.showModal({
|
||||
title: '更新提示',
|
||||
content: '新版本已经准备就绪,是否需要重新启动应用?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
uni.clearStorageSync() // 更新完成后刷新storage的数据
|
||||
updateManager.applyUpdate()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
updateManager.onUpdateFailed(() => {
|
||||
uni.showModal({
|
||||
title: '已有新版本上线',
|
||||
content: '小程序自动更新失败,请删除该小程序后重新搜索打开哟~~~',
|
||||
showCancel: false
|
||||
})
|
||||
})
|
||||
} else {
|
||||
if (useTip) {
|
||||
//没有更新
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: '已经是最新版本!'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '当前微信版本过低,无法使用该功能,请更新到最新的微信后再重试。',
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user