// 导入请求 import { request } from '../request.uts' // 基础 URL 配置 // #ifdef APP-ANDROID const BASE_URL = 'https://dev.xiaodingyun.cn' // #endif // #ifdef APP-IOS const BASE_URL = 'https://dev.xiaodingyun.cn' // #endif // #ifdef H5 const BASE_URL = '' // #endif // #ifdef MP-WEIXIN const BASE_URL = 'https://dev.xiaodingyun.cn' // #endif // #ifdef MP-HARMONY const BASE_URL = 'https://dev.xiaodingyun.cn' // #endif export function getAllData(params : any) : Promise { return request({ url: `${BASE_URL}/api/coach/v3/orders/statistics`, method: "GET", params: params }) } export function editCoachWorkState(data : any) : Promise { return request({ url: `${BASE_URL}/api/coach/v3/account/work-status`, method: "POST", data: data }) } export function editWorkTimeSetting(data : any) : Promise { return request({ url: `${BASE_URL}/api/coach/v3/account/schedule`, method: "POST", data: data }) } export function toggleProject(data : any) : Promise { return request({ url: `${BASE_URL}/api/coach/v3/projects/open`, method: "POST", data: data }) } import { makePhoneCall } from "./location"; /** * 拨打电话 * @param {string} phoneNumber - 电话号码 */ export const navigateToMobile = (phoneNumber : String) => { makePhoneCall(phoneNumber ?? '4000678313'); }; /** * @description 防抖函数 * @param {Function} fn - 需要防抖的函数 * @param {number} delay - 延迟时间,默认1000ms * @returns {Function} - 防抖后的函数 */ export const debounce = (fn : Function, delay : number = 2000) : Function => { let timer : number | null = null return function (...args : any[]) { if (timer) clearTimeout(timer) timer = setTimeout(() => { fn.apply(this, args) }, delay) } } /** * @description 节流函数 * @param {Function} fn - 需要节流的函数 * @param {number} delay - 延迟时间,默认1000ms * @returns {Function} - 节流后的函数 */ export const throttle = (fn : Function, delay : number = 300) : Function => { let timer : number | null = null let start : number = Date.now() return function (...args : any[]) { const current : number = Date.now() const remaining : number = delay - (current - start) clearTimeout(timer!) if (remaining <= 0) { fn.apply(this, args) start = Date.now() } else { timer = setTimeout(() => { fn.apply(this, args) start = Date.now() }, remaining) } } } // 缓存相关工具函数 export const cacheTools = { // 获取缓存 get(key : string) : any | null { // 显式声明参数类型和返回类型 try { const data = uni.getStorageSync(key) as UTSJSONObject | null // 类型断言 if (data == null) return null // 检查缓存是否过期(5分钟) const timestamp = data['timestamp'] as number // 通过索引访问并断言类型 if (Date.now() - timestamp > 5 * 60 * 1000) { uni.removeStorageSync(key) return null } return data } catch (e) { return null } }, // 设置缓存 set(key : string, value : any) : void { // 显式声明参数类型 try { uni.setStorageSync(key, { data: value, timestamp: Date.now() }) } catch (e) { console.error('缓存设置失败', e) } } } // 网络状态检测 export const checkNetwork = () => { return new Promise((resolve) => { uni.getNetworkType({ success: (res) => { resolve(res.networkType !== "none"); }, fail: () => { resolve(false); }, }); }); }; /** * 格式化日期 * @param {Date} date - 要格式化的日期对象 * @param {string} fmt - 格式化模板,如 'YYYY-MM-DD HH:mm:ss' * @returns {string} 格式化后的日期字符串 */ export const formatDate = (date : any, fmt : string) : string => { if (!date) return ""; if (typeof date === "string") { date = new Date(date.replace(/-/g, "/")); } if (typeof date === "number") { date = new Date(date); } const o = { "M+": date.getMonth() + 1, "D+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), S: date.getMilliseconds(), }; if (/(Y+)/.test(fmt)) { fmt = fmt.replace( RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length) ); } for (let k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace( RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) ); } } return fmt; }; //最新上传图片2025-02-17 export async function newUploadImage( bucket : string = "photo-images", module : string = "real-photo" ) : Promise { return new Promise((resolve, reject) => { uni.chooseImage({ count: 1, success: (res) => { const tempFilePaths = res.tempFilePaths uni.showLoading({ title: "上传中...", mask: true, }) uni.uploadFile({ url: "https://dev.xiaodingyun.cn/api/upload", filePath: tempFilePaths[0], name: "file", formData: { bucket: bucket, module: module, }, success: (uploadFileRes) => { let data = JSON.parse(uploadFileRes.data) as UTSJSONObject if (data['code'] as number == 200) { resolve({ code: 200, data: data['data'], message: "上传成功" }) } else { reject({ code: 202, data: {}, message: "上传失败" }) } }, fail: (err) => { reject({ code: 202, data: {}, message: "上传失败" }) }, complete: () => { uni.hideLoading() }, }) }, fail: (err) => { reject({ code: 202, data: {}, message: "上传失败" }) }, }) }) } //汇付天下 export const huifuUpload = async () => { //获取token const token = await uni.getStorageSync("token"); return new Promise((resolve, reject) => { uni.chooseImage({ count: 1, fileType: "image", success: (res) => { const tempFilePaths = res.tempFilePaths; uni.showLoading({ title: "上传中...", mask: true, }); uni.uploadFile({ url: "https://dev.xiaodingyun.cn/api/coach/v3/wallet/BsUpload", filePath: tempFilePaths[0], name: "file", header: { Authorization: "Bearer " + token, }, success: (uploadFileRes) => { let data = JSON.parse(uploadFileRes.data); if (data.code == 200) { resolve({ code: 200, data: data.data, message: "上传成功" }); } else { reject({ code: 202, data: {}, message: "上传失败" }); } }, fail: (err) => { reject({ code: 202, data: {}, message: "上传失败" }); }, complete: () => { uni.hideLoading(); }, }); }, fail: (err) => { reject({ code: 202, data: {}, message: "上传失败" }); }, }); }); };