tool.uts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // 导入请求
  2. import { request } from '../request.uts'
  3. // 基础 URL 配置
  4. // #ifdef APP-ANDROID
  5. const BASE_URL = 'https://dev.xiaodingyun.cn'
  6. // #endif
  7. // #ifdef APP-IOS
  8. const BASE_URL = 'https://dev.xiaodingyun.cn'
  9. // #endif
  10. // #ifdef H5
  11. const BASE_URL = ''
  12. // #endif
  13. // #ifdef MP-WEIXIN
  14. const BASE_URL = 'https://dev.xiaodingyun.cn'
  15. // #endif
  16. // #ifdef MP-HARMONY
  17. const BASE_URL = 'https://dev.xiaodingyun.cn'
  18. // #endif
  19. export function getAllData(params : any) : Promise<any> {
  20. return request({
  21. url: `${BASE_URL}/api/coach/v3/orders/statistics`,
  22. method: "GET",
  23. params: params
  24. })
  25. }
  26. export function editCoachWorkState(data : any) : Promise<any> {
  27. return request({
  28. url: `${BASE_URL}/api/coach/v3/account/work-status`,
  29. method: "POST",
  30. data: data
  31. })
  32. }
  33. export function editWorkTimeSetting(data : any) : Promise<any> {
  34. return request({
  35. url: `${BASE_URL}/api/coach/v3/account/schedule`,
  36. method: "POST",
  37. data: data
  38. })
  39. }
  40. export function toggleProject(data : any) : Promise<any> {
  41. return request({
  42. url: `${BASE_URL}/api/coach/v3/projects/open`,
  43. method: "POST",
  44. data: data
  45. })
  46. }
  47. import { makePhoneCall } from "./location";
  48. /**
  49. * 拨打电话
  50. * @param {string} phoneNumber - 电话号码
  51. */
  52. export const navigateToMobile = (phoneNumber : String) => {
  53. makePhoneCall(phoneNumber ?? '4000678313');
  54. };
  55. /**
  56. * @description 防抖函数
  57. * @param {Function} fn - 需要防抖的函数
  58. * @param {number} delay - 延迟时间,默认1000ms
  59. * @returns {Function} - 防抖后的函数
  60. */
  61. export const debounce = (fn : Function, delay : number = 2000) : Function => {
  62. let timer : number | null = null
  63. return function (...args : any[]) {
  64. if (timer) clearTimeout(timer)
  65. timer = setTimeout(() => {
  66. fn.apply(this, args)
  67. }, delay)
  68. }
  69. }
  70. /**
  71. * @description 节流函数
  72. * @param {Function} fn - 需要节流的函数
  73. * @param {number} delay - 延迟时间,默认1000ms
  74. * @returns {Function} - 节流后的函数
  75. */
  76. export const throttle = (fn : Function, delay : number = 300) : Function => {
  77. let timer : number | null = null
  78. let start : number = Date.now()
  79. return function (...args : any[]) {
  80. const current : number = Date.now()
  81. const remaining : number = delay - (current - start)
  82. clearTimeout(timer!)
  83. if (remaining <= 0) {
  84. fn.apply(this, args)
  85. start = Date.now()
  86. } else {
  87. timer = setTimeout(() => {
  88. fn.apply(this, args)
  89. start = Date.now()
  90. }, remaining)
  91. }
  92. }
  93. }
  94. // 缓存相关工具函数
  95. export const cacheTools = {
  96. // 获取缓存
  97. get(key : string) : any | null { // 显式声明参数类型和返回类型
  98. try {
  99. const data = uni.getStorageSync(key) as UTSJSONObject | null // 类型断言
  100. if (data == null) return null
  101. // 检查缓存是否过期(5分钟)
  102. const timestamp = data['timestamp'] as number // 通过索引访问并断言类型
  103. if (Date.now() - timestamp > 5 * 60 * 1000) {
  104. uni.removeStorageSync(key)
  105. return null
  106. }
  107. return data
  108. } catch (e) {
  109. return null
  110. }
  111. },
  112. // 设置缓存
  113. set(key : string, value : any) : void { // 显式声明参数类型
  114. try {
  115. uni.setStorageSync(key, {
  116. data: value,
  117. timestamp: Date.now()
  118. })
  119. } catch (e) {
  120. console.error('缓存设置失败', e)
  121. }
  122. }
  123. }
  124. // 网络状态检测
  125. export const checkNetwork = () => {
  126. return new Promise((resolve) => {
  127. uni.getNetworkType({
  128. success: (res) => {
  129. resolve(res.networkType !== "none");
  130. },
  131. fail: () => {
  132. resolve(false);
  133. },
  134. });
  135. });
  136. };
  137. /**
  138. * 格式化日期
  139. * @param {Date} date - 要格式化的日期对象
  140. * @param {string} fmt - 格式化模板,如 'YYYY-MM-DD HH:mm:ss'
  141. * @returns {string} 格式化后的日期字符串
  142. */
  143. export const formatDate = (date : any, fmt : string) : string => {
  144. if (!date) return "";
  145. if (typeof date === "string") {
  146. date = new Date(date.replace(/-/g, "/"));
  147. }
  148. if (typeof date === "number") {
  149. date = new Date(date);
  150. }
  151. const o = {
  152. "M+": date.getMonth() + 1,
  153. "D+": date.getDate(),
  154. "H+": date.getHours(),
  155. "m+": date.getMinutes(),
  156. "s+": date.getSeconds(),
  157. "q+": Math.floor((date.getMonth() + 3) / 3),
  158. S: date.getMilliseconds(),
  159. };
  160. if (/(Y+)/.test(fmt)) {
  161. fmt = fmt.replace(
  162. RegExp.$1,
  163. (date.getFullYear() + "").substr(4 - RegExp.$1.length)
  164. );
  165. }
  166. for (let k in o) {
  167. if (new RegExp("(" + k + ")").test(fmt)) {
  168. fmt = fmt.replace(
  169. RegExp.$1,
  170. RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  171. );
  172. }
  173. }
  174. return fmt;
  175. };
  176. //最新上传图片2025-02-17
  177. export async function newUploadImage(
  178. bucket : string = "photo-images",
  179. module : string = "real-photo"
  180. ) : Promise<any> {
  181. return new Promise((resolve, reject) => {
  182. uni.chooseImage({
  183. count: 1,
  184. success: (res) => {
  185. const tempFilePaths = res.tempFilePaths
  186. uni.showLoading({
  187. title: "上传中...",
  188. mask: true,
  189. })
  190. uni.uploadFile({
  191. url: "https://dev.xiaodingyun.cn/api/upload",
  192. filePath: tempFilePaths[0],
  193. name: "file",
  194. formData: {
  195. bucket: bucket,
  196. module: module,
  197. },
  198. success: (uploadFileRes) => {
  199. let data = JSON.parse(uploadFileRes.data) as UTSJSONObject
  200. if (data['code'] as number == 200) {
  201. resolve({ code: 200, data: data['data'], message: "上传成功" })
  202. } else {
  203. reject({ code: 202, data: {}, message: "上传失败" })
  204. }
  205. },
  206. fail: (err) => {
  207. reject({ code: 202, data: {}, message: "上传失败" })
  208. },
  209. complete: () => {
  210. uni.hideLoading()
  211. },
  212. })
  213. },
  214. fail: (err) => {
  215. reject({ code: 202, data: {}, message: "上传失败" })
  216. },
  217. })
  218. })
  219. }
  220. //汇付天下
  221. export const huifuUpload = async () => {
  222. //获取token
  223. const token = await uni.getStorageSync("token");
  224. return new Promise((resolve, reject) => {
  225. uni.chooseImage({
  226. count: 1,
  227. fileType: "image",
  228. success: (res) => {
  229. const tempFilePaths = res.tempFilePaths;
  230. uni.showLoading({
  231. title: "上传中...",
  232. mask: true,
  233. });
  234. uni.uploadFile({
  235. url: "https://dev.xiaodingyun.cn/api/coach/v3/wallet/BsUpload",
  236. filePath: tempFilePaths[0],
  237. name: "file",
  238. header: {
  239. Authorization: "Bearer " + token,
  240. },
  241. success: (uploadFileRes) => {
  242. let data = JSON.parse(uploadFileRes.data);
  243. if (data.code == 200) {
  244. resolve({ code: 200, data: data.data, message: "上传成功" });
  245. } else {
  246. reject({ code: 202, data: {}, message: "上传失败" });
  247. }
  248. },
  249. fail: (err) => {
  250. reject({ code: 202, data: {}, message: "上传失败" });
  251. },
  252. complete: () => {
  253. uni.hideLoading();
  254. },
  255. });
  256. },
  257. fail: (err) => {
  258. reject({ code: 202, data: {}, message: "上传失败" });
  259. },
  260. });
  261. });
  262. };