From b49123cf09c77425141c5fdc9297e04fbcfa832e Mon Sep 17 00:00:00 2001
From: chenhaozhe <gaozhechen26@gmail.com>
Date: 星期四, 31 七月 2025 21:56:00 +0800
Subject: [PATCH] 修复app多次退出登录/登录时,部分页面请求地址不更新的问题
---
utils/common.js | 440 +++++++++++++++++++++++++++---------------------------
1 files changed, 222 insertions(+), 218 deletions(-)
diff --git a/utils/common.js b/utils/common.js
index d3dd163..2ba726f 100644
--- a/utils/common.js
+++ b/utils/common.js
@@ -1,251 +1,255 @@
class commonUtils {
- serverUrl
+ serverUrl
- constructor() {
- this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API';
- }
+ constructor() {
+ this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API/';
+ }
- // 闃叉姈鍑芥暟
- debounce(func, delay, immediate = false) {
- let timer = null;
- return function() {
- const context = this;
- const args = arguments;
+ setServerUrl(url) {
+ this.serverUrl = url
+ }
- const callNow = immediate && !timer;
- // 濡傛灉瀹氭椂鍣ㄥ凡缁忓瓨鍦紝娓呴櫎瀹�
- if (timer) {
- clearTimeout(timer);
- }
+ // 闃叉姈鍑芥暟
+ debounce(func, delay, immediate = false) {
+ let timer = null;
+ return function() {
+ const context = this;
+ const args = arguments;
- // 璁剧疆涓�涓柊鐨勫畾鏃跺櫒
- timer = setTimeout(() => {
- if (!immediate) {
- func.apply(context, args);
- }
- timer = null;
- }, delay);
+ const callNow = immediate && !timer;
+ // 濡傛灉瀹氭椂鍣ㄥ凡缁忓瓨鍦紝娓呴櫎瀹�
+ if (timer) {
+ clearTimeout(timer);
+ }
- // 閰嶇疆绔嬪嵆鎵ц
- if (callNow == true) {
- func.apply(context, args)
- }
- };
- }
+ // 璁剧疆涓�涓柊鐨勫畾鏃跺櫒
+ timer = setTimeout(() => {
+ if (!immediate) {
+ func.apply(context, args);
+ }
+ timer = null;
+ }, delay);
- // 鍒ょ┖鍑芥暟
- isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false) {
- let val = value
- // 妫�鏌ユ槸鍚︿负null 鎴栬�� undefind
- if (val === null || val === undefined) {
- return true;
- }
- //濡傛灉瀛楃涓插叏閮ㄦ槸鐢辨暟瀛楁瀯鎴愮殑锛屽垯杞寲涓烘暟瀛楀瀷
- if (this.isAllDigits(val) === true) {
- val = Number(val)
- }
+ // 閰嶇疆绔嬪嵆鎵ц
+ if (callNow == true) {
+ func.apply(context, args)
+ }
+ };
+ }
- // 鏄惁鏄瓧绗︿覆绫诲瀷
- if (typeof val === 'string') {
- return val.trim().length === 0;
- }
+ // 鍒ょ┖鍑芥暟
+ isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false) {
+ let val = value
+ // 妫�鏌ユ槸鍚︿负null 鎴栬�� undefind
+ if (val === null || val === undefined) {
+ return true;
+ }
+ //濡傛灉瀛楃涓插叏閮ㄦ槸鐢辨暟瀛楁瀯鎴愮殑锛屽垯杞寲涓烘暟瀛楀瀷
+ if (this.isAllDigits(val) === true) {
+ val = Number(val)
+ }
- // 鏄惁鏄暟缁�
- if (Array.isArray(val)) {
- return val.length === 0;
- }
+ // 鏄惁鏄瓧绗︿覆绫诲瀷
+ if (typeof val === 'string') {
+ return val.trim().length === 0;
+ }
- //鏄惁鏄璞�
- if (typeof val === 'object') {
- return Object.keys(val).length === 0;
- }
+ // 鏄惁鏄暟缁�
+ if (Array.isArray(val)) {
+ return val.length === 0;
+ }
- // 鏁板瓧绫诲瀷榛樿涓嶄负绌�
- if (typeof val === 'number') {
- // 鏁板瓧涓�0瑙嗕负绌�
- if (zeroIsEmpty == true) {
- if (val === 0) {
- return true
- }
- }
- return false
- }
+ //鏄惁鏄璞�
+ if (typeof val === 'object') {
+ return Object.keys(val).length === 0;
+ }
- // 甯冨皵绫诲瀷榛樿涓嶄负绌�
- if (typeof val === 'boolean') {
- // false鍊艰涓虹┖
- if (falseIsEmpty == true) {
- if (val === 0) {
- return true
- }
- }
- return false;
- }
- }
+ // 鏁板瓧绫诲瀷榛樿涓嶄负绌�
+ if (typeof val === 'number') {
+ // 鏁板瓧涓�0瑙嗕负绌�
+ if (zeroIsEmpty == true) {
+ if (val === 0) {
+ return true
+ }
+ }
+ return false
+ }
- // 鍒ゆ柇鏄惁鍏ㄦ槸鏁板瓧
- isAllDigits(str) {
- return /^\d+$/.test(str);
- }
+ // 甯冨皵绫诲瀷榛樿涓嶄负绌�
+ if (typeof val === 'boolean') {
+ // false鍊艰涓虹┖
+ if (falseIsEmpty == true) {
+ if (val === 0) {
+ return true
+ }
+ }
+ return false;
+ }
+ }
- isJson(str) {
- try {
- JSON.parse(str)
- return true
- } catch {
- return false
- }
- }
+ // 鍒ゆ柇鏄惁鍏ㄦ槸鏁板瓧
+ isAllDigits(str) {
+ return /^\d+$/.test(str);
+ }
- timeClock(callback, delay) {
- let timeoutId;
- let isRunning = false;
+ isJson(str) {
+ try {
+ JSON.parse(str)
+ return true
+ } catch {
+ return false
+ }
+ }
- function interval() {
- timeoutId = setTimeout(() => {
- callback();
- clearTimeout(timeoutId); // 绔嬪嵆娓呴櫎褰撳墠瀹氭椂鍣↖D
- if (isRunning) {
- interval();
- }
- }, delay);
- }
+ timeClock(callback, delay) {
+ let timeoutId;
+ let isRunning = false;
- return {
- start() {
- if (!isRunning) {
- isRunning = true;
- interval();
- }
- },
- stop() {
- if (isRunning) {
- isRunning = false;
- clearTimeout(timeoutId);
- }
- }
- };
- }
+ function interval() {
+ timeoutId = setTimeout(() => {
+ callback();
+ clearTimeout(timeoutId); // 绔嬪嵆娓呴櫎褰撳墠瀹氭椂鍣↖D
+ if (isRunning) {
+ interval();
+ }
+ }, delay);
+ }
- deepClone(target, map = new WeakMap()) {
- // 澶勭悊鍘熷鍊煎拰鍑芥暟(typeof 鏁扮粍浼氳繑鍥瀘bject)
- if (typeof target !== 'object' || target === null) {
- return target;
- }
+ return {
+ start() {
+ if (!isRunning) {
+ isRunning = true;
+ interval();
+ }
+ },
+ stop() {
+ if (isRunning) {
+ isRunning = false;
+ clearTimeout(timeoutId);
+ }
+ }
+ };
+ }
- // 澶勭悊寰幆寮曠敤
- if (map.has(target)) {
- return map.get(target);
- }
+ deepClone(target, map = new WeakMap()) {
+ // 澶勭悊鍘熷鍊煎拰鍑芥暟(typeof 鏁扮粍浼氳繑鍥瀘bject)
+ if (typeof target !== 'object' || target === null) {
+ return target;
+ }
- let clone;
+ // 澶勭悊寰幆寮曠敤
+ if (map.has(target)) {
+ return map.get(target);
+ }
- // 澶勭悊鏁扮粍
- if (Array.isArray(target)) {
- clone = [];
- map.set(target, clone);
- target.forEach((item, index) => {
- clone[index] = deepClone(item, map);
- });
- return clone;
- }
+ let clone;
- // 澶勭悊鏃ユ湡瀵硅薄
- if (target instanceof Date) {
- clone = new Date(target.getTime());
- map.set(target, clone);
- return clone;
- }
+ // 澶勭悊鏁扮粍
+ if (Array.isArray(target)) {
+ clone = [];
+ map.set(target, clone);
+ target.forEach((item, index) => {
+ clone[index] = deepClone(item, map);
+ });
+ return clone;
+ }
- // 澶勭悊姝e垯琛ㄨ揪寮�
- if (target instanceof RegExp) {
- clone = new RegExp(target);
- map.set(target, clone);
- return clone;
- }
+ // 澶勭悊鏃ユ湡瀵硅薄
+ if (target instanceof Date) {
+ clone = new Date(target.getTime());
+ map.set(target, clone);
+ return clone;
+ }
- // 澶勭悊鍑芥暟 (鐩存帴浣跨敤鍑芥暟鐨勫紩鐢�)
- if (typeof target === 'function') {
- return target;
- }
+ // 澶勭悊姝e垯琛ㄨ揪寮�
+ if (target instanceof RegExp) {
+ clone = new RegExp(target);
+ map.set(target, clone);
+ return clone;
+ }
- // 澶勭悊鍏朵粬瀵硅薄锛堟櫘閫氬璞°�佺被瀹炰緥绛夛級
- clone = Object.create(Object.getPrototypeOf(target));
- map.set(target, clone);
+ // 澶勭悊鍑芥暟 (鐩存帴浣跨敤鍑芥暟鐨勫紩鐢�)
+ if (typeof target === 'function') {
+ return target;
+ }
- // 鑾峰彇鎵�鏈夊睘鎬э紙鍖呮嫭 Symbol 绫诲瀷锛�
- const allKeys = [...Object.getOwnPropertyNames(target), ...Object.getOwnPropertySymbols(target)];
+ // 澶勭悊鍏朵粬瀵硅薄锛堟櫘閫氬璞°�佺被瀹炰緥绛夛級
+ clone = Object.create(Object.getPrototypeOf(target));
+ map.set(target, clone);
- allKeys.forEach(key => {
- const descriptor = Object.getOwnPropertyDescriptor(target, key);
- if (descriptor && descriptor.enumerable) {
- // 閫掑綊澶嶅埗灞炴�у��
- clone[key] = deepClone(target[key], map);
- }
- });
+ // 鑾峰彇鎵�鏈夊睘鎬э紙鍖呮嫭 Symbol 绫诲瀷锛�
+ const allKeys = [...Object.getOwnPropertyNames(target), ...Object.getOwnPropertySymbols(target)];
- return clone;
- }
+ allKeys.forEach(key => {
+ const descriptor = Object.getOwnPropertyDescriptor(target, key);
+ if (descriptor && descriptor.enumerable) {
+ // 閫掑綊澶嶅埗灞炴�у��
+ clone[key] = deepClone(target[key], map);
+ }
+ });
- // uni-app 浣跨敤 灏佽璇锋眰鍑芥暟 浣跨敤浼犵粺鍑芥暟褰撲綔鍥炶皟闇�瑕佷紶that锛岀澶村嚱鏁颁笉闇�瑕�
- doRequest(url, data, resFunction, errFunction, method, that) {
- that = that || this;
- let errorTip = null;
- uni.showLoading({
- title: '鍔犺浇涓�...'
- })
- uni.request({
- method: method || "GET",
- url: this.serverUrl + url,
- data: data || "",
- success: (res) => {
- if (typeof resFunction === 'function') {
- resFunction.call(that, res)
- } else if (typeof errFunction === 'undefined' || errFunction === null) {
- return
- } else {
- throw new TypeError("璁块棶鎴愬姛鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
- }
- },
- fail: (err) => {
- console.error(err)
- errorTip = () => {
- uni.showToast({
- icon: "none",
- title: err.errMsg || err.data.message || "鎺ュ彛寮傚父!",
- duration: 2000
- })
- }
- if (typeof errFunction === 'function') {
- errFunction.call(that, err)
- } else if (typeof errFunction === 'undefined' || errFunction === null) {
- return
- } else {
- throw new TypeError("璁块棶澶辫触鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
- }
+ return clone;
+ }
- },
- complete() {
- setTimeout(() => {
- uni.hideLoading()
- if (errorTip != null) {
- errorTip()
- }
- }, 1000)
- }
- })
- }
+ // uni-app 浣跨敤 灏佽璇锋眰鍑芥暟 浣跨敤浼犵粺鍑芥暟褰撲綔鍥炶皟闇�瑕佷紶that锛岀澶村嚱鏁颁笉闇�瑕�
+ doRequest(url, data, resFunction, errFunction, method, that) {
+ that = that || this;
+ let errorTip = null;
+ uni.showLoading({
+ title: '鍔犺浇涓�...'
+ })
+ uni.request({
+ method: method || "GET",
+ url: this.serverUrl + url,
+ data: data || "",
+ success: (res) => {
+ if (typeof resFunction === 'function') {
+ resFunction.call(that, res)
+ } else if (typeof errFunction === 'undefined' || errFunction === null) {
+ return
+ } else {
+ throw new TypeError("璁块棶鎴愬姛鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
+ }
+ },
+ fail: (err) => {
+ console.error(err)
+ errorTip = () => {
+ uni.showToast({
+ icon: "none",
+ title: err.errMsg || err.data.message || "鎺ュ彛寮傚父!",
+ duration: 2000
+ })
+ }
+ if (typeof errFunction === 'function') {
+ errFunction.call(that, err)
+ } else if (typeof errFunction === 'undefined' || errFunction === null) {
+ return
+ } else {
+ throw new TypeError("璁块棶澶辫触鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
+ }
- // uni-app 鎾斁闊抽灏佽
- playSound(e) {
- const innerAudioContext = uni.createInnerAudioContext();
- if (e == 1) {
- innerAudioContext.src = '/static/success.wav';
- } else {
- innerAudioContext.src = '/static/jingbao.wav';
- }
- innerAudioContext.play(); // 鎾斁闊抽
- }
+ },
+ complete() {
+ setTimeout(() => {
+ uni.hideLoading()
+ if (errorTip != null) {
+ errorTip()
+ }
+ }, 1000)
+ }
+ })
+ }
+
+ // uni-app 鎾斁闊抽灏佽
+ playSound(e) {
+ const innerAudioContext = uni.createInnerAudioContext();
+ if (e == 1) {
+ innerAudioContext.src = '/static/success.wav';
+ } else {
+ innerAudioContext.src = '/static/jingbao.wav';
+ }
+ innerAudioContext.play(); // 鎾斁闊抽
+ }
}
-export const CommonUtils = Object.freeze(new commonUtils());
\ No newline at end of file
+export const CommonUtils = new commonUtils()
\ No newline at end of file
--
Gitblit v1.9.1