Commit 13e66719 authored by 贾慧斌's avatar 贾慧斌

feat: 重写部分底层工具类

parent 4aa0f635
<template>
<view class="home">这里是test 页面</view>
</template>
<script></script>
//定义一个名为Cookies的对象
const Cookies = {
//表示cookie数据在本地存储中的键名
cookieDataKey: "__cookies__",
//定义一个set方法,用于设置cookie值,参数包括name、value和expires等属性
set(name, value, { expires, domain, path, secure, httpOnly } = {}) {
//获取本地存储中的cookie数据
let cookies = this._getCookieData();
//初始化过期时间字符串
let expiresFormatted = "";
//如果expires是Date类型,则将其转换为UTC格式的字符串
if (expires instanceof Date) {
expiresFormatted = expires.toUTCString();
} else if (typeof expires === "number") {
//如果expires是number类型,则计算出过期时间并转换为UTC格式的字符串
let expiresTime = new Date();
expiresTime.setTime(expires * 1000);
expiresFormatted = expiresTime.toUTCString();
}
//将新的cookie值添加到cookie对象中
cookies[name] = {
value,
expires: expiresFormatted,
domain,
path,
secure,
httpOnly
};
//将更新后的cookie数据存储到本地存储中
this._setCookieData(cookies);
},
//定义一个get方法,用于获取指定cookie的值
get(name) {
//获取本地存储中的cookie数据
let cookies = this._getCookieData();
//如果该cookie存在,则获取其值
// eslint-disable-next-line no-prototype-builtins
if (cookies.hasOwnProperty(name)) {
let { value, expires } = cookies[name];
//如果该cookie已过期,则删除该cookie并返回undefined
if (expires !== "" && new Date() > new Date(expires)) {
delete cookies[name];
this._setCookieData(cookies);
return undefined;
}
//返回该cookie的值
return value;
}
//如果该cookie不存在,则返回undefined
return undefined;
},
//定义一个getAll方法,用于获取所有cookie值
getAll() {
return this._getCookieData();
},
//定义一个remove方法,用于删除指定cookie
remove(name) {
let cookies = this._getCookieData();
//如果该cookie存在,则删除该cookie
// eslint-disable-next-line no-prototype-builtins
if (cookies.hasOwnProperty(name)) {
delete cookies[name];
this._setCookieData(cookies);
}
},
//定义一个私有的_getCookieData方法,用于获取本地存储中的cookie数据
_getCookieData() {
let cookies = uni.getStorageSync(this.cookieDataKey);
return cookies ? JSON.parse(cookies) : {};
},
//定义一个私有的_setCookieData方法,用于存储更新后的cookie数据到本地存储中
_setCookieData(cookies) {
uni.setStorageSync(this.cookieDataKey, JSON.stringify(cookies));
}
};
//将Cookies对象作为默认导出的模块
export default Cookies;
......@@ -281,14 +281,22 @@ export function isChnAddress(str) {
}
return false;
}
const ua = uni.getSystemInfoSync().ua
const ua = uni.$u.gData.ua
// 判断微信环境
export const isWechat = ua.match(/MicroMessenger/i) == 'micromessenger';
// 是否信用钱包
export const isXYQB = ua.indexOf("xyqb") > -1;
// 判断微信小程序环境
export const isWxMp = uni.getSystemInfoSync().platform === 'mp-weixin'
// #ifdef MP-WEIXIN
export const isWxMp =true
// #endif
// #ifdef H5
export const isWxMp = ua.match(/miniProgram/i) == 'miniprogram' || window.__wxjs_environment === 'miniprogram';
// #endif
// 判断羊小咩(信用钱包)环境
export const isApp = ua.match(/xyqb/i) == 'xyqb';
......
......@@ -2,6 +2,8 @@ import Vue from "vue";
import store from "./store";
import App from "./App";
import { saInit } from "@/utils/sa";
import uView from "uview-ui";
const msg = (title, duration = 1500, mask = false, icon = "none") => {
//统一提示方便全局修改
if (Boolean(title) === false) {
......@@ -50,6 +52,13 @@ Vue.prototype.$api = { msg, json, prePage };
Vue.prototype.$config = key => {
return getApp().globalData[key];
};
Vue.use(uView);
// 挂载全局变量
uni.$u.gData = {
ua: uni.getSystemInfoSync().ua || "" // 全局ua
};
console.log("jhb ua", uni.$u.gData.ua);
saInit();
App.mpType = "app";
......
import { isDef } from "./utils.service";
import cookies from "js-cookie";
const option = {
domain: "",
expires: 1
};
const Cookies = {
get(key) {
let result = cookies.get(key);
result = isDef(result) ? result : "";
try {
return JSON.parse(result);
} catch (e) {
return result;
cookieDataKey: "__cookies__",
set(name, value, { expires, domain, path, secure, httpOnly } = {}) {
let cookies = this._getCookieData();
let expiresFormatted = "";
if (expires instanceof Date) {
expiresFormatted = expires.toUTCString();
} else if (typeof expires === "number") {
let expiresTime = new Date();
expiresTime.setTime(expires * 1000);
expiresFormatted = expiresTime.toUTCString();
}
cookies[name] = {
value,
expires: expiresFormatted,
domain,
path,
secure,
httpOnly
};
this._setCookieData(cookies);
},
set(key, value) {
return cookies.set(key, value, option);
get(name) {
let cookies = this._getCookieData();
// eslint-disable-next-line no-prototype-builtins
if (cookies.hasOwnProperty(name)) {
let { value, expires } = cookies[name];
if (expires !== "" && new Date() > new Date(expires)) {
delete cookies[name];
this._setCookieData(cookies);
return undefined;
}
return value;
}
return undefined;
},
clear() {
return Object.keys(cookies.get()).forEach(function(cookie) {
Cookies.remove(cookie, option);
});
getAll() {
return this._getCookieData();
},
remove(key) {
return cookies.remove(key, option);
remove(name) {
let cookies = this._getCookieData();
// eslint-disable-next-line no-prototype-builtins
if (cookies.hasOwnProperty(name)) {
delete cookies[name];
this._setCookieData(cookies);
}
},
_getCookieData() {
let cookies = uni.getStorageSync(this.cookieDataKey);
return cookies ? JSON.parse(cookies) : {};
},
_setCookieData(cookies) {
uni.setStorageSync(this.cookieDataKey, JSON.stringify(cookies));
}
};
......
class Store {
constructor(store) {
// 检测是否支持localstorage
if (!store) {
console.log("不支持localStorage");
return;
}
this._store = store;
}
/**
* @function 设置值
* @param {string} _k 必须参数,属性
* @param {any} _v 非必须参数,属性值
*/
setItem(_k, _v) {
if (!this._store) return;
const kType = this.getType(_k);
if (kType === "string") {
this._store.setItem(_k, this.filterValue(_v));
} else {
console.log("key只能为字符串!");
}
}
/**
* @function 获取值
* @param {string} _k 必须参数,属性
*/
getItem(_k) {
if (!this._store) return;
let res;
const kType = this.getType(_k);
if (kType === "string") {
res = this._store.getItem(_k);
} else {
console.log("key只能为字符串!");
}
return res;
}
/**
* @function 移除值
* @param {string} _k 必须参数,属性
*/
removeItem(_k) {
if (!this._store) return;
let res;
const kType = this.getType(_k);
if (kType === "string") {
res = this._store.removeItem(_k);
} else {
console.log("key只能为字符串!", res);
}
}
/**
* @function 移除所有
*/
export const sStorage = {
setItem(key, value) {
uni.setStorageSync(key, value);
},
getItem(key) {
return uni.getStorageSync(key);
},
removeItem(key) {
uni.removeStorageSync(key);
},
clear() {
if (!this._store) return;
this._store.clear();
}
uni.clearStorageSync();
},
/**
* @function 判断类型
* @param {any} para 必须参数,判断的值
......@@ -76,27 +25,4 @@ class Store {
.split(" ")[1]
.toLowerCase();
}
/**
* @function 过滤值
* @param {any} val 必须参数,过滤的值
*/
filterValue(val) {
const vType = this.getType(val);
const nullVal = ["null", "undefined", "NaN"];
const stringVal = ["boolean", "number", "string"];
if (nullVal.indexOf(vType) >= 0) return "";
if (stringVal.indexOf(vType) >= 0) return val;
return JSON.stringify(val);
}
}
class SessionStorage extends Store {
constructor(store) { // eslint-disable-line
super(store);
}
}
const sStorage = new SessionStorage({});
export { sStorage };
};
......@@ -281,17 +281,25 @@ export function isChnAddress(str) {
}
return false;
}
const ua = uni.getSystemInfoSync().ua||''
const ua = uni.$u.gData.ua
// 判断微信环境
export const isWechat = true
export const isWechat = ua.match(/MicroMessenger/i) == 'micromessenger';
// 是否信用钱包
export const isXYQB = ua.indexOf("xyqb") > -1;
// 判断微信小程序环境
export const isWxMp = uni.getSystemInfoSync().platform === 'mp-weixin'
// #ifdef MP-WEIXIN
export const isWxMp =true
// #endif
// #ifdef H5
export const isWxMp = ua.match(/miniProgram/i) == 'miniprogram' || window.__wxjs_environment === 'miniprogram';
// #endif
// 判断羊小咩(信用钱包)环境
export const isApp = false
export const isApp = ua.match(/xyqb/i) == 'xyqb';
// 判断真享生活
export const isVcc = ua.match(/VCC/i) == 'vcc';
......
const path = require("path");
const resolve = dir => path.join(__dirname, dir);
module.exports = {
transpileDependencies: ["uview-ui", "@qg/miniapp-qa-tool"],
css: {
loaderOptions: {
less: {
......
......@@ -16010,6 +16010,11 @@ uuid@3.4.0, uuid@^3.3.2, uuid@^3.4.0:
resolved "http://npmprivate.quantgroups.com/uuid/-/uuid-3.4.0.tgz"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
uview-ui@^2.0.28:
version "2.0.36"
resolved "http://npmprivate.quantgroups.com/uview-ui/-/uview-ui-2.0.36.tgz#c821e73aa79bf62b55bf40b769ac02a07a3a755e"
integrity sha512-ASSZT6M8w3GTO1eFPbsgEFV0U5UujK+8pTNr+MSUbRNcRMC1u63DDTLJVeArV91kWM0bfAexK3SK9pnTqF9TtA==
v8-compile-cache@^2.0.3:
version "2.2.0"
resolved "http://npmprivate.quantgroups.com/v8-compile-cache/-/v8-compile-cache-2.2.0.tgz"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment