修改Net工具
This commit is contained in:
parent
f9b148ca8a
commit
0f18776157
13
app.js
13
app.js
@ -1,13 +1,10 @@
|
|||||||
// app.js
|
//app.js
|
||||||
const request = require('./utils/http.js');
|
|
||||||
|
|
||||||
App({
|
App({
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
// 展示本地存储能力
|
// 展示本地存储能力
|
||||||
const logs = wx.getStorageSync('logs') || []
|
// const logs = wx.getStorageSync('logs') || []
|
||||||
logs.unshift(Date.now())
|
// logs.unshift(Date.now())
|
||||||
wx.setStorageSync('logs', logs)
|
// wx.setStorageSync('logs', logs)
|
||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
wx.login({
|
wx.login({
|
||||||
success: res => {
|
success: res => {
|
||||||
@ -16,6 +13,6 @@ App({
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
globalData: {
|
globalData: {
|
||||||
userInfo: null
|
userInfo: null,
|
||||||
}
|
}
|
||||||
})
|
})
|
29
app.json
29
app.json
@ -1,14 +1,17 @@
|
|||||||
{
|
{
|
||||||
"pages": [
|
"pages": [
|
||||||
"pages/index/index"
|
"pages/index/index"
|
||||||
],
|
],
|
||||||
"window": {
|
"window": {
|
||||||
"navigationBarTextStyle": "black",
|
"navigationBarTextStyle": "black",
|
||||||
"navigationBarTitleText": "Weixin",
|
"navigationBarTitleText": "Weixin",
|
||||||
"navigationBarBackgroundColor": "#ffffff"
|
"navigationBarBackgroundColor": "#ffffff"
|
||||||
},
|
},
|
||||||
"style": "v2",
|
"style": "v2",
|
||||||
"componentFramework": "glass-easel",
|
"componentFramework": "glass-easel",
|
||||||
"sitemapLocation": "sitemap.json",
|
"sitemapLocation": "sitemap.json",
|
||||||
"lazyCodeLoading": "requiredComponents"
|
"lazyCodeLoading": "requiredComponents",
|
||||||
}
|
"useExtendedLib": {
|
||||||
|
"weui": true
|
||||||
|
}
|
||||||
|
}
|
5
app.wxss
5
app.wxss
@ -3,8 +3,7 @@
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: flex-start;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
padding: 200rpx 0;
|
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// index.js
|
|
||||||
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
|
|
||||||
var app = getApp();
|
var app = getApp();
|
||||||
|
var defaultAvatarUrl = '';
|
||||||
|
import AuthService from '../../utils/api/testapi';
|
||||||
|
|
||||||
Page({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
@ -13,10 +13,22 @@ Page({
|
|||||||
canIUseGetUserProfile: wx.canIUse('getUserProfile'),
|
canIUseGetUserProfile: wx.canIUse('getUserProfile'),
|
||||||
canIUseNicknameComp: wx.canIUse('input.type.nickname'),
|
canIUseNicknameComp: wx.canIUse('input.type.nickname'),
|
||||||
},
|
},
|
||||||
|
onLoad() {
|
||||||
|
console.log(wx.getUserProfile)
|
||||||
|
},
|
||||||
|
getUserPhoneNumber(e) {
|
||||||
|
console.log(e.detail)
|
||||||
|
},
|
||||||
bindViewTap() {
|
bindViewTap() {
|
||||||
wx.navigateTo({
|
AuthService.login({
|
||||||
url: '../logs/logs'
|
name: '223',
|
||||||
})
|
pwd: '1234'
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
console.log(res)
|
||||||
|
}, err => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
onChooseAvatar(e) {
|
onChooseAvatar(e) {
|
||||||
const {
|
const {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"usingComponents": {
|
"usingComponents": {
|
||||||
}
|
"mp-icon": "weui-miniprogram/icon/icon"
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,13 @@
|
|||||||
<!--index.wxml-->
|
<!--index.wxml-->
|
||||||
<scroll-view class="scrollarea" scroll-y type="list">
|
<scroll-view class="scrollarea" scroll-y type="list">
|
||||||
<view class="container">
|
<view class="container">
|
||||||
<image src="/imgs/ss.png" />
|
<image src="{{userInfo.avatarUrl}}" style="width: 100rpx;height: 100rpx;"></image>
|
||||||
|
<mp-icon type="outline" icon="shop" color="black" size="{{25}}">
|
||||||
|
|
||||||
|
|
||||||
|
</mp-icon>
|
||||||
|
<button type="default" bind:tap="getUserProfile">获取信息</button>
|
||||||
|
<button open-type="getPhoneNumber" bind:tap="getUserPhoneNumber">获取电话</button>
|
||||||
|
<text>sss</text>
|
||||||
</view>
|
</view>
|
||||||
</scroll-view>
|
</scroll-view>
|
@ -7,6 +7,9 @@ page {
|
|||||||
.scrollarea {
|
.scrollarea {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
overflow-y: hidden;
|
overflow-y: hidden;
|
||||||
|
height: 100vh;
|
||||||
|
width: 100vw;
|
||||||
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
.userinfo {
|
.userinfo {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
|
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
|
||||||
"projectname": "gov_propagandize",
|
"projectname": "gov_propagandize",
|
||||||
"setting": {
|
"setting": {
|
||||||
"compileHotReLoad": true
|
"compileHotReLoad": true,
|
||||||
|
"urlCheck": false
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,83 +1,44 @@
|
|||||||
// 引入 request 文件
|
import {
|
||||||
import request from '@/utils/request';
|
request
|
||||||
|
} from "../http";
|
||||||
|
|
||||||
// 以下 api 为示例,实际与项目相匹配
|
// 管理地址,个人习惯,喜欢的下载request参数中也行
|
||||||
|
const apiPath = {
|
||||||
|
login: "/api/v1/login"
|
||||||
|
}
|
||||||
|
class AuthService {
|
||||||
|
static login(data) {
|
||||||
|
// 按住ctrl点击wx.request进入ts声明看options的类型,method是均大写的,对应即可
|
||||||
|
return request(apiPath.login, "GET", data)
|
||||||
|
|
||||||
// 分页查询学习列表
|
// 由于request的参数顺序第四个才是查询参数,所以如果需要传递,第三个传输传空对象即可
|
||||||
export const pageStudyInfo = (params) => {
|
// return request(apiPath.login, "GET", {}, data)
|
||||||
return request({
|
}
|
||||||
url: '/study/studyInfo/page',
|
}
|
||||||
method: 'GET',
|
|
||||||
data: params
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 查询学习列表
|
export default AuthService;
|
||||||
export const listStudyInfo = (params) => {
|
|
||||||
return request({
|
|
||||||
url: '/study/studyInfo/list',
|
|
||||||
method: 'GET',
|
|
||||||
data: params
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 获取学习列表详细信息
|
|
||||||
export const studyInfoById = (id) => {
|
|
||||||
return request({
|
|
||||||
url: `/study/studyInfo/${id}`,
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 增加学习列表
|
|
||||||
export const saveStudyInfo = (params) => {
|
|
||||||
return request({
|
|
||||||
url: '/study/studyInfo',
|
|
||||||
method: 'POST',
|
|
||||||
data: params
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 修改学习列表
|
|
||||||
export const updateStudyInfo = (params) => {
|
|
||||||
return request({
|
|
||||||
url: '/study/studyInfo',
|
|
||||||
method: 'PUT',
|
|
||||||
data: params
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 删除学习列表
|
|
||||||
export const deleteStudyInfo = (id) => {
|
|
||||||
return request({
|
|
||||||
url: `/study/studyInfo/delete?id=${id}`,
|
|
||||||
method: 'DELETE',
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
// import AuthService from "../../service/AuthService";
|
||||||
|
// // 导入封装好的服务层
|
||||||
|
|
||||||
|
// // 其他代码省略
|
||||||
|
// page({
|
||||||
|
// // Promise语法
|
||||||
// Page({
|
// submitForm(){
|
||||||
// data: {
|
// AuthService.login({username:"a",password:"2"}).then(res=>{
|
||||||
// studyList: []
|
// // 干点别的
|
||||||
// },
|
// },err=>{
|
||||||
|
// // 出错了干点啥
|
||||||
// onLoad: function () {
|
// })
|
||||||
// this.getStudyList();
|
// },
|
||||||
// },
|
// // async-await语法糖
|
||||||
|
// async asycsubmitForm(){
|
||||||
// getStudyList: async function () {
|
// try{
|
||||||
// try {
|
// const res = await AuthService.login({username:"abc",password:"123"});
|
||||||
// const res = await pageStudyInfo(this.data.queryParams);
|
// // 干点别的
|
||||||
// console.log(res.data.records);
|
// }catch(err){
|
||||||
// this.setData({
|
// // 错了干点啥?
|
||||||
// studyList: res.data.records
|
|
||||||
// });
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error(error);
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
// });
|
// }
|
||||||
|
// })
|
53
utils/cache.js
Normal file
53
utils/cache.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// 缓存工具类
|
||||||
|
const CACHE_PREFIX = 'daqi_';
|
||||||
|
const DEFAULT_CACHE_TIME = 5 * 60 * 1000; // 默认缓存5分钟
|
||||||
|
|
||||||
|
class Cache {
|
||||||
|
/**
|
||||||
|
* 设置缓存
|
||||||
|
* @param {string} key 缓存键
|
||||||
|
* @param {any} value 缓存值
|
||||||
|
* @param {number} expire 过期时间(毫秒)
|
||||||
|
*/
|
||||||
|
static set(key, value, expire = DEFAULT_CACHE_TIME) {
|
||||||
|
const data = {
|
||||||
|
value,
|
||||||
|
expire: expire ? new Date().getTime() + expire : null
|
||||||
|
};
|
||||||
|
wx.setStorageSync(CACHE_PREFIX + key, JSON.stringify(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存
|
||||||
|
* @param {string} key 缓存键
|
||||||
|
* @returns {any} 缓存值
|
||||||
|
*/
|
||||||
|
static get(key) {
|
||||||
|
const data = wx.getStorageSync(CACHE_PREFIX + key);
|
||||||
|
if (!data) return null;
|
||||||
|
|
||||||
|
const cache = JSON.parse(data);
|
||||||
|
if (cache.expire && cache.expire < new Date().getTime()) {
|
||||||
|
wx.removeStorageSync(CACHE_PREFIX + key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return cache.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除缓存
|
||||||
|
* @param {string} key 缓存键
|
||||||
|
*/
|
||||||
|
static remove(key) {
|
||||||
|
wx.removeStorageSync(CACHE_PREFIX + key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空所有缓存
|
||||||
|
*/
|
||||||
|
static clear() {
|
||||||
|
wx.clearStorageSync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Cache;
|
59
utils/format.js
Normal file
59
utils/format.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
const { formatNumber } = require('./date');
|
||||||
|
|
||||||
|
// 格式化金额
|
||||||
|
const formatAmount = (amount, decimals = 2) => {
|
||||||
|
if (!amount && amount !== 0) return '--';
|
||||||
|
return Number(amount).toFixed(decimals).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||||
|
};
|
||||||
|
|
||||||
|
// 格式化数量(支持万、亿单位)
|
||||||
|
const formatQuantity = (num) => {
|
||||||
|
if (!num && num !== 0) return '--';
|
||||||
|
if (num >= 100000000) {
|
||||||
|
return (num / 100000000).toFixed(2) + '亿';
|
||||||
|
}
|
||||||
|
if (num >= 10000) {
|
||||||
|
return (num / 10000).toFixed(2) + '万';
|
||||||
|
}
|
||||||
|
return formatNumber(num);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 格式化文件大小
|
||||||
|
const formatFileSize = (bytes) => {
|
||||||
|
if (!bytes && bytes !== 0) return '--';
|
||||||
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
let num = bytes;
|
||||||
|
let index = 0;
|
||||||
|
while (num >= 1024 && index < units.length - 1) {
|
||||||
|
num /= 1024;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return num.toFixed(2) + units[index];
|
||||||
|
};
|
||||||
|
|
||||||
|
// 格式化手机号
|
||||||
|
const formatPhone = (phone) => {
|
||||||
|
if (!phone) return '';
|
||||||
|
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
|
||||||
|
};
|
||||||
|
|
||||||
|
// 格式化身份证号
|
||||||
|
const formatIdCard = (idCard) => {
|
||||||
|
if (!idCard) return '';
|
||||||
|
return idCard.replace(/(\d{4})\d{10}(\d{4})/, '$1**********$2');
|
||||||
|
};
|
||||||
|
|
||||||
|
// 格式化银行卡号
|
||||||
|
const formatBankCard = (cardNo) => {
|
||||||
|
if (!cardNo) return '';
|
||||||
|
return cardNo.replace(/(\d{4})\d+(\d{4})/, '$1 **** **** $2');
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
formatAmount,
|
||||||
|
formatQuantity,
|
||||||
|
formatFileSize,
|
||||||
|
formatPhone,
|
||||||
|
formatIdCard,
|
||||||
|
formatBankCard
|
||||||
|
};
|
149
utils/http.js
149
utils/http.js
@ -1,86 +1,67 @@
|
|||||||
// 全局请求封装
|
// 定义api服务地址
|
||||||
const base_url = 'http://localhost:996';
|
const baseUrl = 'http://dev.com:12345';
|
||||||
|
|
||||||
export default (params) => {
|
/**
|
||||||
let url = params.url;
|
* 传入请求参数,返回Promise支持链试调用
|
||||||
let method = params.method || "GET";
|
* @param url 请求地址
|
||||||
let data = params.data || {};
|
* @param method 请求方法类型,不传入默认是"GET"
|
||||||
let header = {};
|
* @param data 请求体数据
|
||||||
|
* @param params 请求参数
|
||||||
|
*/
|
||||||
|
function request(url, method = "GET", data = {}, params = {}) {
|
||||||
|
const header = {
|
||||||
|
"content-type": "application/json"
|
||||||
|
// 有其他content-type需求加点逻辑判断处理即可
|
||||||
|
}
|
||||||
|
//TODO 获取token,有就丢进请求头
|
||||||
|
// const tokenString = wx.getStorageSync("access_token");
|
||||||
|
// if (tokenString) {
|
||||||
|
// header.Authorization = `Bearer ${tokenString}`;
|
||||||
|
// }
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
// 判断是否传递了query参数,有则进行地址拼接
|
||||||
|
// if (params) {
|
||||||
|
// url += "?" + urlEncode(params)
|
||||||
|
// }
|
||||||
|
wx.showLoading({
|
||||||
|
title: '加载中...',
|
||||||
|
mask: true //显示蒙层
|
||||||
|
})
|
||||||
|
wx.request({
|
||||||
|
url: baseUrl + url,
|
||||||
|
timeout: 8000,
|
||||||
|
method,
|
||||||
|
data,
|
||||||
|
dataType: "json", // 微信官方文档中介绍会对数据进行一次JSON.parse
|
||||||
|
header,
|
||||||
|
success(res) {
|
||||||
|
wx.hideLoading();
|
||||||
|
console.log(res);
|
||||||
|
// HTTP状态码为200才视为成功
|
||||||
|
if (res.statusCode === 200) {
|
||||||
|
// 真正的数据响应体中还有一层success字段判断业务状态,按实际情况处理
|
||||||
|
if (res.data.success) {
|
||||||
|
resolve(res.data.result)
|
||||||
|
} else {
|
||||||
|
// 业务判断错误
|
||||||
|
reject(res)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// wx.request的特性,只要有响应就会走success回调,所以在这里判断状态,非200的均视为请求失败
|
||||||
|
reject(res)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail(err) {
|
||||||
|
wx.hideLoading();
|
||||||
|
// 断网、服务器挂了都会fail回调,直接reject即可
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (method == "POST") {
|
// 导出请求和服务地址
|
||||||
header = {
|
export {
|
||||||
'content-type': 'application/json'
|
request,
|
||||||
};
|
baseUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取本地token
|
|
||||||
const token = wx.getStorageSync("token");
|
|
||||||
if (token) {
|
|
||||||
header['Authorization'] = 'Bearer ' + token;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
wx.request({
|
|
||||||
url: base_url + url,
|
|
||||||
method: method,
|
|
||||||
header: header,
|
|
||||||
data: data,
|
|
||||||
success(response) {
|
|
||||||
const res = response.data;
|
|
||||||
if (res.statusCode == 200) {
|
|
||||||
resolve(res);
|
|
||||||
} else {
|
|
||||||
wx.clearStorageSync();
|
|
||||||
switch (res.statusCode) {
|
|
||||||
case 401:
|
|
||||||
wx.showModal({
|
|
||||||
title: "提示",
|
|
||||||
content: "请登录",
|
|
||||||
showCancel: false,
|
|
||||||
success(res) {
|
|
||||||
setTimeout(() => {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: "/pages/login/index",
|
|
||||||
});
|
|
||||||
}, 1000);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case 404:
|
|
||||||
wx.showToast({
|
|
||||||
title: '请求地址不存在...',
|
|
||||||
duration: 2000,
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
wx.showToast({
|
|
||||||
title: '请重试...',
|
|
||||||
duration: 2000,
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail(err) {
|
|
||||||
console.log(err);
|
|
||||||
if (err.errMsg.indexOf('request:fail') !== -1) {
|
|
||||||
wx.showToast({
|
|
||||||
title: '网络异常',
|
|
||||||
icon: "error",
|
|
||||||
duration: 2000
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
wx.showToast({
|
|
||||||
title: '未知异常',
|
|
||||||
duration: 2000
|
|
||||||
});
|
|
||||||
}
|
|
||||||
reject(err);
|
|
||||||
},
|
|
||||||
complete() {
|
|
||||||
wx.hideLoading();
|
|
||||||
wx.hideToast();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}).catch((e) => {});
|
|
||||||
};
|
|
75
utils/storage.js
Normal file
75
utils/storage.js
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* 存储管理工具
|
||||||
|
*/
|
||||||
|
|
||||||
|
const STORAGE_PREFIX = 'enterprise_';
|
||||||
|
|
||||||
|
// 获取完整的存储键名
|
||||||
|
const getFullKey = (key) => {
|
||||||
|
return `${STORAGE_PREFIX}${key}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置存储
|
||||||
|
const set = (key, data) => {
|
||||||
|
try {
|
||||||
|
const fullKey = getFullKey(key);
|
||||||
|
wx.setStorageSync(fullKey, data);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Storage set error:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取存储
|
||||||
|
const get = (key, defaultValue = null) => {
|
||||||
|
try {
|
||||||
|
const fullKey = getFullKey(key);
|
||||||
|
const value = wx.getStorageSync(fullKey);
|
||||||
|
return value || defaultValue;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Storage get error:', error);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 移除存储
|
||||||
|
const remove = (key) => {
|
||||||
|
try {
|
||||||
|
const fullKey = getFullKey(key);
|
||||||
|
wx.removeStorageSync(fullKey);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Storage remove error:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 清除所有存储
|
||||||
|
const clear = () => {
|
||||||
|
try {
|
||||||
|
wx.clearStorageSync();
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Storage clear error:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取存储信息
|
||||||
|
const info = () => {
|
||||||
|
try {
|
||||||
|
return wx.getStorageInfoSync();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Storage info error:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
set,
|
||||||
|
get,
|
||||||
|
remove,
|
||||||
|
clear,
|
||||||
|
info
|
||||||
|
};
|
43
utils/validator.js
Normal file
43
utils/validator.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// 手机号验证
|
||||||
|
const isValidPhone = (phone) => {
|
||||||
|
return /^1[3-9]\d{9}$/.test(phone);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 邮箱验证
|
||||||
|
const isValidEmail = (email) => {
|
||||||
|
return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email);
|
||||||
|
};
|
||||||
|
|
||||||
|
// URL验证
|
||||||
|
const isValidUrl = (url) => {
|
||||||
|
try {
|
||||||
|
new URL(url);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 身份证号验证
|
||||||
|
const isValidIdCard = (idCard) => {
|
||||||
|
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idCard);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 企业统一社会信用代码验证
|
||||||
|
const isValidCreditCode = (creditCode) => {
|
||||||
|
return /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/.test(creditCode);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 密码强度验证(至少包含数字和字母,长度8-20位)
|
||||||
|
const isValidPassword = (password) => {
|
||||||
|
return /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/.test(password);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isValidPhone,
|
||||||
|
isValidEmail,
|
||||||
|
isValidUrl,
|
||||||
|
isValidIdCard,
|
||||||
|
isValidCreditCode,
|
||||||
|
isValidPassword
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user