From abd2e1dd7dfdf1ad50cce74c4e72edfd94fecf85 Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期一, 15 十二月 2025 12:56:48 +0800
Subject: [PATCH] 1
---
utils/common.js | 266 +++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 207 insertions(+), 59 deletions(-)
diff --git a/utils/common.js b/utils/common.js
index 87f3b4e..5898bb7 100644
--- a/utils/common.js
+++ b/utils/common.js
@@ -1,13 +1,22 @@
class commonUtils {
serverUrl
-
+ audioContext // 鍏ㄥ眬闊抽瀹炰緥
+ requestLock // 璇锋眰鎺у埗閿�(鍚屾)
constructor() {
this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API/';
+ this.audioContext = null;
+ this.requestLock = false
}
setServerUrl(url) {
this.serverUrl = url
}
+
+ getServerUrl() {
+ return this.serverUrl
+ }
+
+
// 闃叉姈鍑芥暟
debounce(func, delay, immediate = false) {
@@ -239,59 +248,131 @@
}
})
}
-
- doRequest2({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("璁块棶澶辫触鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
- }
-
- },
- complete: () => {
- setTimeout(() => {
- uni.hideLoading()
- if (errorTip != null) {
- errorTip()
- }
- }, 1000)
- }
- })
- }
-
- stringToBoolean(str) {
- // 蹇界暐澶у皬鍐欑殑杞崲
- return str?.toLowerCase() === "true";
- }
+
+ doRequest2({
+ 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("璁块棶澶辫触鍥炶皟鍑芥暟绫诲瀷涓嶄负鍑芥暟鎴栬�呯┖!")
+ }
+
+ },
+ complete: () => {
+
+ setTimeout(() => {
+ uni.hideLoading()
+ if (errorTip != null) {
+ errorTip()
+ }
+ }, 1000)
+ }
+ })
+ }
+
+ // 鍚屾鎵ц璇锋眰 (閰嶅悎await鎴栬�卼hen)
+ async doRequest2Sync({
+ url,
+ data,
+ method,
+ }) {
+ if (this.requestLock) {
+ console.log("璇ヨ姹傝閿佸畾,涓嶈兘閲嶅璇锋眰!!!")
+ return
+ }
+ this.requestLock = true
+ return new Promise((resolve, reject) => {
+ // that = that || this;
+ let errorTip = null;
+ uni.showLoading({
+ title: '鍔犺浇涓�...'
+ })
+ uni.request({
+ method: method || "GET",
+ url: this.serverUrl + url,
+ data: data || "",
+ success: (res) => {
+ resolve(res)
+ },
+ fail: (err) => {
+ reject(err)
+ },
+ complete: () => {
+ // 閲婃斁璇锋眰閿�
+ this.requestLock = false
+ uni.hideLoading()
+ }
+ })
+ })
+ }
+
+ async doRequest2Async({
+ url,
+ data,
+ method,
+ }) {
+ return new Promise((resolve, reject) => {
+ // that = that || this;
+ let errorTip = null;
+ uni.showLoading({
+ title: '鍔犺浇涓�...'
+ })
+ uni.request({
+ method: method || "GET",
+ url: this.serverUrl + url,
+ data: data || "",
+ success: (res) => {
+ resolve(res)
+ },
+ fail: (err) => {
+ reject(err)
+ },
+ complete: () => {
+ uni.hideLoading()
+ }
+ })
+ })
+ }
+
+ stringToBoolean(str) {
+ // 蹇界暐澶у皬鍐欑殑杞崲
+ return str?.toLowerCase() === "true";
+ }
// uni-app 鎾斁闊抽灏佽
playSound(e) {
@@ -302,13 +383,80 @@
innerAudioContext.src = '/static/jingbao.wav';
}
innerAudioContext.play(); // 鎾斁闊抽
+
+ innerAudioContext.onPlay(() => {
+ //console.log('寮�濮嬫挱鏀�');
+ });
+ innerAudioContext.onError((res) => {
+ console.log(res.errMsg);
+ console.log(res.errCode);
+ });
+ innerAudioContext.onPause(function() {
+ //console.log('鎾斁鏆傚仠锛岄攢姣�');
+ innerAudioContext.destroy();
+ })
}
-
+ // playSound(e) {
+ // // 鍏ㄥ眬缁存姢涓�涓煶棰戝疄渚嬶紝闃叉缂撳瓨婧㈠嚭
+ // if (this.audioContext) {
+ // this.audioContext.destroy();
+ // }
+ // this.audioContext = uni.createInnerAudioContext();
+ // if (e == 1) {
+ // this.audioContext.src = '/static/success.wav';
+ // } else {
+ // this.audioContext.src = '/static/jingbao.wav';
+ // }
+ // this.audioContext.play(); // 鎾斁闊抽
+ //
+ // // 鎾斁缁撴潫鍚庨攢姣佸疄渚�
+ // this.audioContext.onEnded(() => {
+ // this.audioContext.destroy();
+ // this.audioContext = null;
+ // });
+ //
+ // // 閿欒澶勭悊
+ // this.audioContext.onError((err) => {
+ // uni.showToast({
+ // icon: 'none',
+ // title: `闊抽鎾斁閿欒: ${err}`
+ // })
+ //
+ // this.audioContext.destroy();
+ // this.audioContext = null;
+ // });
+ // }
+
+
+ showTips({
+ type,
+ message,
+ title,
+ duration
+ }) {
+ if (!message) {
+ return
+ }
+
+ if (message.length < 20) {
+ return uni.showToast({
+ icon: type || 'none',
+ title: message
+ })
+ }
+
+ return uni.showModal({
+ title: title,
+ content: message,
+ showCancel: false
+ })
+ }
+
replaceWithFunction(str, handler) {
- return str.replace(/\{(.+?)\}/g, (match, key) => {
- // 璋冪敤澶勭悊鍑芥暟锛屼紶鍏ュ尮閰嶅埌鐨勯敭
- return handler(key, match);
- });
+ return str.replace(/\{(.+?)\}/g, (match, key) => {
+ // 璋冪敤澶勭悊鍑芥暟锛屼紶鍏ュ尮閰嶅埌鐨勯敭
+ return handler(key, match);
+ });
}
}
--
Gitblit v1.9.1