UNPKG

ipink-util

Version:

util.js

222 lines (219 loc) 6.52 kB
import { hasKey } from './util.mjs'; class Config { /** * App name */ static appName = ""; /** * App id */ static appId = ""; /** * 语言 */ static language = ""; /** * Url Option */ static socket_url = ""; // socket url static origin_url = ""; // 跳转的URL前缀 static login_url = ""; // 登陆url /** * 组件内部调用内部的toast方法时, 是否用内部定义的html toast, 否则使用uni.showToast */ static use_inner_toast = true; /** * toast 持续时间 */ static toast_duration = 2400; /** * 非H5使用,打开一个webview页面显示指定的 h5 url 页面 * 页面需接收 【url】query * 在 jump 方法中非H5平台会使用到该属性 */ static webview_path = "/pages/webview/index"; /** * 设置配置信息 * 如果设置Http相关的配置, 使用HttpConfig.setConfig * * 如果主题相关使用 ipink-theme.ThemeConfig.setToken */ static setConfig(params) { if (params) { Object.keys(params).forEach((key) => { Config[key] = params[key]; if (hasKey(HttpConfig, key)) { HttpConfig[key] = Config[key]; } }); } } } class HttpConfig { /** * 请求的URL */ static base_url = ""; /** * 超时时间 */ static timeout = 6e4; static content_type = "application/json"; /** * 默认方法 */ static default_method = "POST"; /** * 获取token函数吗未设置则取 Storage.get(AxiosConfig.token_key) */ static getToken = null; static token_type = "Bearer"; /** * 获取Token的Key, 默认使用的是 import {Storage} from "ipink-util";Storage.get */ static token_key = "A__APP_TOKEN__Z"; /** * 外部传入的axios包, uniapp 环境下使用 request;js环境下使用 http, 使用request此项忽略 * * @example ` * import { Config, HttpConfig, createHttp } from "ipink-util" * * HttpConfig.setConfig({ * base_url: "http://www.baidu.com" * }) * * const http = createHttp(); * * http<string>({ * url: "/api/public/upload", * method: "POST", * data: {}, * headers: { * 'Content-Type': 'multipart/form-data' * } * }) * ` */ static axios = null; /** * 请求前缀 */ static api_prefix = ""; /** * 上传请求前缀 */ static upload_prefix = ""; /** * 文件上传成功后用来取值,仅限于 example * 单文件上传 取data, data[0];多文件直接返回data * 如果设置了 upload_receive_key , 则会判定data不存在,会在顶级结构取该属性, 取不到则会取 data[upload_receive_key] * @example ` * { * data: "https://.../demo.png", * // or * data: ["https://.../demo.png"] * } * ` */ static upload_receive_key = ""; /** * 文件上传后后段接收的 key */ static upload_key = "file"; /** * 错误弹窗体方法 */ static toast = null; /** * 报错时是否调用toast */ static show_toast = false; /** * 接口请求时是否开启loading, 仅适用与uniapp环境下 */ static show_loading = false; static loading = null; static closeLoading = null; /** * 判断StatusCode == 200的情况下,根据该值判断接口出餐是否逻辑正确, 否则赋值 [ok:false] */ static code_key = "code"; /** * 判断StatusCode == 200的情况下,根据该值判断接口出餐是否逻辑正确,依赖为该值 默认为 1 */ static code_value = 1; /** * 默认错误信息 */ static default_error_msg = "数据响应失败!"; /** * http 请求拦截器, 在这里可设置自定义操作 * @param type 拦截到的类型 * @param data 可修改的源数据, return data 即修改完成 * * @example ` * HttpConfig.interceptor = (type: InterceptorTypeEnum, data: any) => { * * switch (type) { * // 请求前,对配置和入参做一些处理 * case "BeforeRequest": * // 执行中, data为RequestTask, 可执行 RequestTask.abort() 终止请求 * case "ExecRequest": * typeof data = { abort: () => void } * break; * // 请求完成,对出参做一些处理, 例如: 整体出参揭秘等 * case "AfterRequest": * break; * // upload 方法,选择文件后对选择的文件做一些处理;虽然该函数提供 choosedCallback:() => boolean 入参函数 * case "AfterChooseFile": * break; * // 调用UploadFile前,对配置和入参做一些处理 * case "BeforeUpload": * break; * // 调用UploadFile时, 对请求进行管理 * case "ExecUpload": * typeof data = { * abort: () => void * onProgressUpdate: ({progress: number, totalBytesSent: number, totalBytesExpectedToSend: number}) => {} // 监听上传进度变化 * onHeadersReceived // 仅微信小程序平台支持, 见官方文档 * offProgressUpdate // 仅微信小程序平台支持, 见官方文档 * offHeadersReceived // 仅微信小程序平台支持, 见官方文档 * } * break; * // UploadFile | UploadMoreFile 方法,对上传完成后的数据进行一些处理 * case "AfterUpload": * break; * * * } * return data * } * ` */ static interceptor = null; /** * 设置默认配置 * @param params { IHttpConfig } */ static setConfig(params) { Object.keys(params || {}).forEach((key) => { HttpConfig[key] = params[key]; }); } } function interceptor(type, data) { if (typeof HttpConfig.interceptor === "function") { const result = HttpConfig.interceptor(type, data); return result || data; } return data; } const win = "undefined" != typeof window ? window : "undefined" != globalThis ? globalThis : "undefined" != self ? self : null; const getSdk = () => { return "undefined" != typeof uni && uni?.getStorageSync ? uni : "undefined" != typeof wx && wx?.getStorageSync ? wx : null; }; const ConfigBase = Config; export { Config, ConfigBase, HttpConfig, getSdk, interceptor, win };