@ningboyz/wxmp 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # @ningboyz/wxmp
2
+
3
+ 微信小程序请求库。目录、模块类和初始化方式沿用 PC 端 `@ningboyz/apis` 的结构,底层通过 `wx.request` 发送请求。
4
+
5
+ ```text
6
+ src/
7
+ index.ts Request 总入口
8
+ packages/
9
+ index.ts 参数、类型和模块导出
10
+ types.ts 请求配置与拦截器类型
11
+ wx.ts 微信请求底层
12
+ core/ 登录、单位接口
13
+ noti/ 公告接口
14
+ wtui/ 启动配置、公钥接口
15
+ ```
16
+
17
+ ## 初始化
18
+
19
+ 在应用启动时统一配置,业务模块直接由 `Request.createRequest` 创建:
20
+
21
+ ```ts
22
+ import Request from "@ningboyz/wxmp";
23
+
24
+ Request.createRequest({
25
+ baseURL: "https://your-api.example.com",
26
+ timeout: 10000,
27
+ interceptorsRequest,
28
+ interceptorsResponse
29
+ });
30
+ ```
31
+
32
+ 拦截器由使用方提供。动态登录态应在请求拦截器中读取;启动配置修改服务地址后,再调用一次 `Request.createRequest` 更新客户端。
33
+
34
+ ## 业务调用
35
+
36
+ ```ts
37
+ import Request, {
38
+ TNotiMainGet4NotiQuerys,
39
+ TNotiMainDetaildbQuerys
40
+ } from "@ningboyz/wxmp";
41
+
42
+ const query = new TNotiMainGet4NotiQuerys();
43
+ query.syspartc = 3001;
44
+ const result = await Request.noti.main.get4noti(query);
45
+ if (!result.ONSTATUS) throw new Error(result.ERORMEMO);
46
+ const notices = result.LISTDATA;
47
+
48
+ const detailQuery = new TNotiMainDetaildbQuerys();
49
+ detailQuery.notimain = 123;
50
+ const detail = await Request.noti.main.detaildb(detailQuery, {});
51
+
52
+ await Request.core.user.login({ code, encryptedData, iv });
53
+ await Request.wtui.envr.startcfg();
54
+ await Request.wtui.envr.startkey();
55
+ ```
56
+
57
+ 接口返回类型由包内方法推导,使用方负责业务结果检查和页面展示。路径、参数类和网络传输逻辑统一由本包维护。
58
+
59
+ `post` 通过表单字段 `params` 提交 JSON 字符串;`postJson` 直接提交 JSON。国密加解密由使用方的拦截器处理。
60
+
61
+ 网络错误、HTTP 非 2xx 响应和拦截器异常会使请求 Promise 失败,调用方通过 `catch` 处理;后端业务响应原样返回,由调用方检查 `ONSTATUS`。网络和 HTTP 错误使用 `WxRequestError`,HTTP 错误还保留 `statusCode`、`data` 和请求 `config`。
62
+
63
+ 配置 `loseTokenState` 后,HTTP 401 或业务返回共享类型库中的“会话过期”错误码时会触发该回调;`sessionExpiredErrorKey` 可覆盖默认业务错误码。
64
+
65
+ 业务统一通过 `Request.core`、`Request.noti`、`Request.wtui` 调用,由 `Request.createRequest` 完成模块初始化。
66
+
67
+ ## 构建
68
+
69
+ ```bash
70
+ npm run build
71
+ ```
72
+
73
+ 构建产物和类型声明输出到 `es/`。小程序更新依赖后,在微信开发者工具执行“工具 → 构建 npm”。
package/es/index.d.ts CHANGED
@@ -1,5 +1,14 @@
1
- import { CoreClient, createCoreClient, IWxLoginParams, TWxLoginParams } from './client/core';
2
- import { NoticeClient, createNoticeClient, INotiMainGet4NotiQuerys, TNotiMainGet4NotiQuerys } from './client/noti';
3
- import { WxRequest, createWxRequest } from './request/wxRequest';
4
- import { IWxConfig, IWxInternalRequestConfig, IWxQueryParams, IWxRequestOptions, IWxResponse, TWxConfig, TWxHttpMethod, TWxInterceptorsRequest, TWxInterceptorsResponse } from './types';
5
- export { type CoreClient, createCoreClient, type NoticeClient, createNoticeClient, type INotiMainGet4NotiQuerys, TNotiMainGet4NotiQuerys, type WxRequest, createWxRequest, type IWxConfig, type IWxInternalRequestConfig, type IWxLoginParams, type IWxQueryParams, type IWxRequestOptions, type IWxResponse, type TWxHttpMethod, type TWxInterceptorsRequest, type TWxInterceptorsResponse, TWxConfig, TWxLoginParams };
1
+ import { default as CoreRequest } from './packages/core';
2
+ import { default as NotiRequest } from './packages/noti';
3
+ import { default as WtuiRequest } from './packages/wtui';
4
+ import { IWxConfig } from './packages/types';
5
+ /** 统一请求入口,初始化和业务模块结构与 @ningboyz/apis 一致。 */
6
+ declare class Request {
7
+ static core: CoreRequest;
8
+ static noti: NotiRequest;
9
+ static wtui: WtuiRequest;
10
+ static createRequest(config: IWxConfig): void;
11
+ }
12
+ export default Request;
13
+ export { Request };
14
+ export * from './packages';
package/es/index.js CHANGED
@@ -1,14 +1,29 @@
1
- import { createCoreClient as r } from "./client/core/index.js";
2
- import { createNoticeClient as x } from "./client/noti/index.js";
3
- import { createWxRequest as f } from "./request/wxRequest.js";
4
- import { TWxConfig as a } from "./types.js";
5
- import { TNotiMainGet4NotiQuerys as n } from "./client/noti/types.js";
6
- import { TWxLoginParams as C } from "./client/core/types.js";
1
+ import { CoreRequest as e } from "./packages/core/index.js";
2
+ import { NotiRequest as r } from "./packages/noti/index.js";
3
+ import { WtuiRequest as o } from "./packages/wtui/index.js";
4
+ import { REQUEST_TIMEOUT_MILLISECONDS as x, TWxConfig as T } from "./packages/types.js";
5
+ import { WxRequestError as f } from "./packages/wx.js";
6
+ import { TCoreUnitSelectdbQuerys as q, TWxLoginParams as w } from "./packages/core/types.js";
7
+ import { TNotiMainDetaildbQuerys as N, TNotiMainGet4NotiQuerys as l } from "./packages/noti/types.js";
8
+ class m {
9
+ static core;
10
+ static noti;
11
+ static wtui;
12
+ static createRequest(t) {
13
+ this.core = new e(t), this.noti = new r(t), this.wtui = new o(t);
14
+ }
15
+ }
7
16
  export {
8
- n as TNotiMainGet4NotiQuerys,
9
- a as TWxConfig,
10
- C as TWxLoginParams,
11
- r as createCoreClient,
12
- x as createNoticeClient,
13
- f as createWxRequest
17
+ e as CoreRequest,
18
+ r as NotiRequest,
19
+ x as REQUEST_TIMEOUT_MILLISECONDS,
20
+ m as Request,
21
+ q as TCoreUnitSelectdbQuerys,
22
+ N as TNotiMainDetaildbQuerys,
23
+ l as TNotiMainGet4NotiQuerys,
24
+ T as TWxConfig,
25
+ w as TWxLoginParams,
26
+ o as WtuiRequest,
27
+ f as WxRequestError,
28
+ m as default
14
29
  };
@@ -0,0 +1,11 @@
1
+ import { IWxConfig } from '../types';
2
+ import { default as UserRequest } from './user';
3
+ import { default as UnitRequest } from './unit';
4
+ declare class CoreRequest {
5
+ user: UserRequest;
6
+ unit: UnitRequest;
7
+ constructor(config: IWxConfig);
8
+ }
9
+ export default CoreRequest;
10
+ export { CoreRequest };
11
+ export * from './types';
@@ -0,0 +1,15 @@
1
+ import { createWxRequest as r } from "../wx.js";
2
+ import s from "./user.js";
3
+ import o from "./unit.js";
4
+ class c {
5
+ user;
6
+ unit;
7
+ constructor(t) {
8
+ const e = r(t);
9
+ this.user = new s(e), this.unit = new o(e);
10
+ }
11
+ }
12
+ export {
13
+ c as CoreRequest,
14
+ c as default
15
+ };
@@ -9,3 +9,12 @@ export declare class TWxLoginParams implements IWxLoginParams {
9
9
  encryptedData: string;
10
10
  iv: string;
11
11
  }
12
+ /** ========== unit ========== */
13
+ export interface ICoreUnitSelectdbQuerys {
14
+ unitmain: number;
15
+ queuesby: string;
16
+ }
17
+ export declare class TCoreUnitSelectdbQuerys implements ICoreUnitSelectdbQuerys {
18
+ unitmain: number;
19
+ queuesby: string;
20
+ }
@@ -0,0 +1,13 @@
1
+ class a {
2
+ code = "";
3
+ encryptedData = "";
4
+ iv = "";
5
+ }
6
+ class s {
7
+ unitmain = -1;
8
+ queuesby = "-1";
9
+ }
10
+ export {
11
+ s as TCoreUnitSelectdbQuerys,
12
+ a as TWxLoginParams
13
+ };
@@ -0,0 +1,15 @@
1
+ import { TCore } from '@ningboyz/types';
2
+ import { WxRequest } from '../wx';
3
+ import { ICoreUnitSelectdbQuerys } from './types';
4
+ declare class UnitRequest {
5
+ private wxRequest;
6
+ constructor(wxRequest: WxRequest);
7
+ /**
8
+ * 获取单位列表
9
+ * @param querys
10
+ * @param params
11
+ * @returns
12
+ */
13
+ selectdb(querys: ICoreUnitSelectdbQuerys, params: object): Promise<import('@ningboyz/types/src/response').IResponse<TCore.IUnitResponse[]>>;
14
+ }
15
+ export default UnitRequest;
@@ -0,0 +1,18 @@
1
+ class u {
2
+ wxRequest;
3
+ constructor(t) {
4
+ this.wxRequest = t;
5
+ }
6
+ /**
7
+ * 获取单位列表
8
+ * @param querys
9
+ * @param params
10
+ * @returns
11
+ */
12
+ selectdb(t, e) {
13
+ return this.wxRequest.post("/gapi/core/tunit/selectdb", t, e);
14
+ }
15
+ }
16
+ export {
17
+ u as default
18
+ };
@@ -1,5 +1,5 @@
1
1
  import { TCore } from '@ningboyz/types';
2
- import { WxRequest } from '../../request/wxRequest';
2
+ import { WxRequest } from '../wx';
3
3
  import { IWxLoginParams } from './types';
4
4
  declare class UserRequest {
5
5
  private wxRequest;
@@ -0,0 +1,6 @@
1
+ export * from './core';
2
+ export * from './noti';
3
+ export * from './wtui';
4
+ export * from './types';
5
+ export { WxRequestError } from './wx';
6
+ export type { WxRequest } from './wx';
@@ -0,0 +1,9 @@
1
+ import { IWxConfig } from '../types';
2
+ import { default as MainRequest } from './main';
3
+ declare class NotiRequest {
4
+ main: MainRequest;
5
+ constructor(config: IWxConfig);
6
+ }
7
+ export default NotiRequest;
8
+ export { NotiRequest };
9
+ export * from './types';
@@ -0,0 +1,13 @@
1
+ import { createWxRequest as s } from "../wx.js";
2
+ import o from "./main.js";
3
+ class m {
4
+ main;
5
+ constructor(t) {
6
+ const e = s(t);
7
+ this.main = new o(e);
8
+ }
9
+ }
10
+ export {
11
+ m as NotiRequest,
12
+ m as default
13
+ };
@@ -1,6 +1,6 @@
1
1
  import { TNoti } from '@ningboyz/types';
2
- import { WxRequest } from '../../request/wxRequest';
3
- import { INotiMainGet4NotiQuerys } from './types';
2
+ import { WxRequest } from '../wx';
3
+ import { INotiMainDetaildbQuerys, INotiMainGet4NotiQuerys } from './types';
4
4
  declare class MainRequest {
5
5
  private wxRequest;
6
6
  constructor(wxRequest: WxRequest);
@@ -10,5 +10,12 @@ declare class MainRequest {
10
10
  * @returns
11
11
  */
12
12
  get4noti(querys: INotiMainGet4NotiQuerys): Promise<import('@ningboyz/types/src/response').IResponse<TNoti.INotiMainResponse[]>>;
13
+ /**
14
+ * 获取公告详情
15
+ * @param querys
16
+ * @param params
17
+ * @returns
18
+ */
19
+ detaildb(querys: INotiMainDetaildbQuerys, params: object): Promise<import('@ningboyz/types/src/response').IResponse<TNoti.INotiMainResponse[]>>;
13
20
  }
14
21
  export default MainRequest;
@@ -11,6 +11,15 @@ class s {
11
11
  get4noti(t) {
12
12
  return this.wxRequest.post("/gapi/noti/tmain/get4noti", t);
13
13
  }
14
+ /**
15
+ * 获取公告详情
16
+ * @param querys
17
+ * @param params
18
+ * @returns
19
+ */
20
+ detaildb(t, e) {
21
+ return this.wxRequest.post("/gapi/noti/tmain/detaildb", t, e);
22
+ }
14
23
  }
15
24
  export {
16
25
  s as default
@@ -7,3 +7,9 @@ export declare class TNotiMainGet4NotiQuerys implements INotiMainGet4NotiQuerys
7
7
  syspartc: number;
8
8
  sourcend: number;
9
9
  }
10
+ export interface INotiMainDetaildbQuerys {
11
+ notimain: number;
12
+ }
13
+ export declare class TNotiMainDetaildbQuerys implements INotiMainDetaildbQuerys {
14
+ notimain: number;
15
+ }
@@ -0,0 +1,11 @@
1
+ class i {
2
+ syspartc = -1;
3
+ sourcend = -1;
4
+ }
5
+ class t {
6
+ notimain = -1;
7
+ }
8
+ export {
9
+ t as TNotiMainDetaildbQuerys,
10
+ i as TNotiMainGet4NotiQuerys
11
+ };
@@ -51,9 +51,9 @@ export interface IWxConfig {
51
51
  * 在 HTTP 成功拿到 body 之后、返回给业务之前调用。
52
52
  */
53
53
  interceptorsResponse?: TWxInterceptorsResponse;
54
- /** 会话过期时回调(对齐 Web loseTokenState,按 ERRORKEY 判断) */
54
+ /** HTTP 401 或业务 ERRORKEY 表示会话过期时触发。 */
55
55
  loseTokenState?: () => void;
56
- /** 与 loseTokenState 配合;不传则不根据 ERRORKEY 踢登录 */
56
+ /** 自定义会话过期错误码,默认使用共享类型库中的“会话过期”常量。 */
57
57
  sessionExpiredErrorKey?: string;
58
58
  }
59
59
  export declare class TWxConfig implements IWxConfig {
@@ -0,0 +1,11 @@
1
+ import { TEnc, TWtui } from '@ningboyz/types';
2
+ import { WxRequest } from '../wx';
3
+ declare class EnvrRequest {
4
+ private wxRequest;
5
+ constructor(wxRequest: WxRequest);
6
+ /** 获取启动配置,接口保持明文。 */
7
+ startcfg(): Promise<import('@ningboyz/types/src/response').IResponse<TWtui.IWtuiMainResponse[]>>;
8
+ /** 获取 SM2 公钥,接口保持明文。 */
9
+ startkey(): Promise<import('@ningboyz/types/src/response').IResponse<TEnc.IEncConfigResponse[]>>;
10
+ }
11
+ export default EnvrRequest;
@@ -0,0 +1,16 @@
1
+ class e {
2
+ constructor(t) {
3
+ this.wxRequest = t;
4
+ }
5
+ /** 获取启动配置,接口保持明文。 */
6
+ startcfg() {
7
+ return this.wxRequest.post("/gapi/wtui/tenvr/startcfg");
8
+ }
9
+ /** 获取 SM2 公钥,接口保持明文。 */
10
+ startkey() {
11
+ return this.wxRequest.post("/gapi/wtui/tenvr/startkey");
12
+ }
13
+ }
14
+ export {
15
+ e as default
16
+ };
@@ -0,0 +1,8 @@
1
+ import { IWxConfig } from '../types';
2
+ import { default as EnvrRequest } from './envr';
3
+ declare class WtuiRequest {
4
+ envr: EnvrRequest;
5
+ constructor(config: IWxConfig);
6
+ }
7
+ export default WtuiRequest;
8
+ export { WtuiRequest };
@@ -0,0 +1,13 @@
1
+ import { createWxRequest as r } from "../wx.js";
2
+ import s from "./envr.js";
3
+ class c {
4
+ envr;
5
+ constructor(e) {
6
+ const t = r(e);
7
+ this.envr = new s(t);
8
+ }
9
+ }
10
+ export {
11
+ c as WtuiRequest,
12
+ c as default
13
+ };
@@ -1,9 +1,15 @@
1
1
  import { TResp } from '@ningboyz/types';
2
- import { IWxConfig, IWxRequestOptions } from '../types';
2
+ import { IWxConfig, IWxInternalRequestConfig, IWxRequestOptions } from './types';
3
+ /** 网络和 HTTP 异常。HTTP 错误保留状态码、响应体及请求配置。 */
4
+ declare class WxRequestError extends Error {
5
+ readonly config: IWxInternalRequestConfig;
6
+ readonly statusCode?: number | undefined;
7
+ readonly data?: unknown | undefined;
8
+ constructor(message: string, config: IWxInternalRequestConfig, statusCode?: number | undefined, data?: unknown | undefined);
9
+ }
3
10
  declare class WxRequest {
4
- private config;
5
- private constructor();
6
- static create(config: IWxConfig): WxRequest;
11
+ private readonly config;
12
+ constructor(config: IWxConfig);
7
13
  /**
8
14
  * 表单提交。body 字段名 params,值是 JSON 字符串,对应 apis 里的 post。
9
15
  */
@@ -15,7 +21,7 @@ declare class WxRequest {
15
21
  get<T>(url: string, querys?: object, headers?: Record<string, string>): Promise<TResp.IResponse<T>>;
16
22
  request<T>(url: string, options: IWxRequestOptions): Promise<TResp.IResponse<T>>;
17
23
  private send;
18
- private handleSuccess;
24
+ private handleResponse;
19
25
  private tryHandleSessionExpired;
20
26
  private getMiniProgramWx;
21
27
  private createRequestUrl;
@@ -24,4 +30,4 @@ declare class WxRequest {
24
30
  private toLowerCase;
25
31
  }
26
32
  declare const createWxRequest: (config: IWxConfig) => WxRequest;
27
- export { WxRequest, createWxRequest };
33
+ export { WxRequest, WxRequestError, createWxRequest };
@@ -0,0 +1,103 @@
1
+ import { Const as T } from "@ningboyz/types";
2
+ import n from "lodash";
3
+ import { REQUEST_TIMEOUT_MILLISECONDS as R } from "./types.js";
4
+ const E = 200, m = 300, h = "application/json", y = "application/x-www-form-urlencoded";
5
+ class u extends Error {
6
+ constructor(e, s, t, i) {
7
+ super(e), this.config = s, this.statusCode = t, this.data = i, this.name = "WxRequestError";
8
+ }
9
+ }
10
+ class w {
11
+ constructor(e) {
12
+ this.config = e;
13
+ }
14
+ /**
15
+ * 表单提交。body 字段名 params,值是 JSON 字符串,对应 apis 里的 post。
16
+ */
17
+ async post(e, s, t, i) {
18
+ const r = n.isNil(t) ? void 0 : { params: JSON.stringify(t) }, o = this.createHeader(n.isNil(t) ? void 0 : y, i);
19
+ return this.send("POST", e, s, r, o);
20
+ }
21
+ /**
22
+ * JSON 提交,对应 apis 里的 postJson。国密加密接口请走此方法。
23
+ */
24
+ async postJson(e, s, t, i) {
25
+ const r = this.createHeader(h, i);
26
+ return this.send("POST", e, s, t, r);
27
+ }
28
+ async get(e, s, t) {
29
+ const i = this.createHeader(void 0, t);
30
+ return this.send("GET", e, s, void 0, i);
31
+ }
32
+ async request(e, s) {
33
+ const t = s.contentType ?? (s.method === "GET" ? void 0 : h), i = this.createHeader(t, s.headers);
34
+ return this.send(s.method, e, s.querys, s.data, i);
35
+ }
36
+ async send(e, s, t, i, r) {
37
+ let o = {
38
+ url: s,
39
+ method: e,
40
+ querys: t,
41
+ data: i,
42
+ header: r
43
+ };
44
+ this.config.interceptorsRequest && (o = await this.config.interceptorsRequest(o));
45
+ const a = this.createRequestUrl(o.url, o.querys), d = this.config.timeout ?? R, f = await new Promise((g, l) => {
46
+ this.getMiniProgramWx().request({
47
+ url: a,
48
+ method: o.method,
49
+ data: o.data,
50
+ header: o.header,
51
+ timeout: d,
52
+ success: g,
53
+ fail: (p) => l(new u(p.errMsg || "网络请求失败", o))
54
+ });
55
+ });
56
+ return this.handleResponse(f, o);
57
+ }
58
+ async handleResponse(e, s) {
59
+ if (e.statusCode < E || e.statusCode >= m)
60
+ throw this.tryHandleSessionExpired(e.data, e.statusCode), new u(`请求失败,状态码 ${e.statusCode}`, s, e.statusCode, e.data);
61
+ let t = {
62
+ data: e.data,
63
+ statusCode: e.statusCode,
64
+ config: s
65
+ };
66
+ return n.isNil(this.config.interceptorsResponse) || (t = await this.config.interceptorsResponse(t)), this.tryHandleSessionExpired(t.data, t.statusCode), t.data;
67
+ }
68
+ tryHandleSessionExpired(e, s) {
69
+ if (!this.config.loseTokenState) return;
70
+ const t = this.config.sessionExpiredErrorKey || T.ErrorKey.CONST_ERROR_KEY_TO_会话过期, i = e !== null && typeof e == "object" ? e.ERRORKEY : void 0;
71
+ (s === 401 || i === t) && this.config.loseTokenState();
72
+ }
73
+ getMiniProgramWx() {
74
+ const e = globalThis.wx;
75
+ if (n.isNil(e))
76
+ throw new Error("当前环境不存在微信小程序 wx");
77
+ return e;
78
+ }
79
+ createRequestUrl(e, s) {
80
+ const t = this.config.baseURL.endsWith("/") ? this.config.baseURL.slice(0, -1) : this.config.baseURL, i = e.startsWith("/") ? e : `/${e}`, r = {
81
+ ...this.createAuthQuerys(),
82
+ ...this.toLowerCase(s)
83
+ }, o = Object.entries(r).filter(([, a]) => !n.isNil(a)).map(([a, d]) => `${encodeURIComponent(a)}=${encodeURIComponent(String(d))}`).join("&");
84
+ return o.length === 0 ? `${t}${i}` : `${t}${i}?${o}`;
85
+ }
86
+ createAuthQuerys() {
87
+ const e = {};
88
+ return !n.isNil(this.config.whobuild) && this.config.whobuild !== 0 && (e.whobuild = this.config.whobuild), !n.isNil(this.config.userIndx) && this.config.userIndx !== 0 && (e.userindx = this.config.userIndx), e;
89
+ }
90
+ createHeader(e, s) {
91
+ const t = {};
92
+ return n.isNil(e) || (t["content-type"] = e), !n.isNil(this.config.usrtoken) && this.config.usrtoken !== "" && (t.usrtoken = this.config.usrtoken), !n.isNil(this.config.dbConfig) && this.config.dbConfig !== "" && (t.dbconfig = this.config.dbConfig), n.isNil(s) || Object.assign(t, s), t;
93
+ }
94
+ toLowerCase(e) {
95
+ return n.isNil(e) ? {} : n.mapKeys(e, (s, t) => t.toLowerCase());
96
+ }
97
+ }
98
+ const S = (c) => new w(c);
99
+ export {
100
+ w as WxRequest,
101
+ u as WxRequestError,
102
+ S as createWxRequest
103
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ningboyz/wxmp",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "private": false,
5
5
  "description": "宁波甬政微信小程序库",
6
6
  "author": "nbyt-syq",
@@ -1,11 +0,0 @@
1
- import { IWxConfig } from '../../types';
2
- import { default as UserRequest } from './user';
3
- declare class CoreClient {
4
- user: UserRequest;
5
- private constructor();
6
- static create: (config: IWxConfig) => CoreClient;
7
- }
8
- declare const createCoreClient: (config: IWxConfig) => CoreClient;
9
- export default CoreClient;
10
- export { CoreClient, createCoreClient };
11
- export * from './types';
@@ -1,15 +0,0 @@
1
- import { createWxRequest as c } from "../../request/wxRequest.js";
2
- import o from "./user.js";
3
- class r {
4
- user;
5
- constructor(e) {
6
- const s = c(e);
7
- this.user = new o(s);
8
- }
9
- static create = (e) => new r(e);
10
- }
11
- const a = (t) => r.create(t);
12
- export {
13
- r as CoreClient,
14
- a as createCoreClient
15
- };
@@ -1,8 +0,0 @@
1
- class e {
2
- code = "";
3
- encryptedData = "";
4
- iv = "";
5
- }
6
- export {
7
- e as TWxLoginParams
8
- };
@@ -1,11 +0,0 @@
1
- import { IWxConfig } from '../../types';
2
- import { default as MainRequest } from './main';
3
- declare class NoticeClient {
4
- main: MainRequest;
5
- private constructor();
6
- static create: (config: IWxConfig) => NoticeClient;
7
- }
8
- declare const createNoticeClient: (config: IWxConfig) => NoticeClient;
9
- export default NoticeClient;
10
- export { NoticeClient, createNoticeClient };
11
- export * from './types';
@@ -1,15 +0,0 @@
1
- import { createWxRequest as n } from "../../request/wxRequest.js";
2
- import o from "./main.js";
3
- class t {
4
- main;
5
- constructor(e) {
6
- const c = n(e);
7
- this.main = new o(c);
8
- }
9
- static create = (e) => new t(e);
10
- }
11
- const i = (r) => t.create(r);
12
- export {
13
- t as NoticeClient,
14
- i as createNoticeClient
15
- };
@@ -1,7 +0,0 @@
1
- class t {
2
- syspartc = -1;
3
- sourcend = -1;
4
- }
5
- export {
6
- t as TNotiMainGet4NotiQuerys
7
- };
@@ -1,112 +0,0 @@
1
- import { TResp as f } from "@ningboyz/types";
2
- import i from "lodash";
3
- import { REQUEST_TIMEOUT_MILLISECONDS as T } from "../types.js";
4
- const m = 200, N = 300, g = "application/json", E = "application/x-www-form-urlencoded";
5
- class u {
6
- config;
7
- constructor(e) {
8
- this.config = e;
9
- }
10
- static create(e) {
11
- return new u(e);
12
- }
13
- /**
14
- * 表单提交。body 字段名 params,值是 JSON 字符串,对应 apis 里的 post。
15
- */
16
- post(e, s, t, o) {
17
- const n = i.isNil(t) ? void 0 : { params: JSON.stringify(t) }, r = this.createHeader(i.isNil(t) ? void 0 : E, o);
18
- return this.send("POST", e, s, n, r);
19
- }
20
- /**
21
- * JSON 提交,对应 apis 里的 postJson。国密加密接口请走此方法。
22
- */
23
- postJson(e, s, t, o) {
24
- const n = this.createHeader(g, o);
25
- return this.send("POST", e, s, t, n);
26
- }
27
- get(e, s, t) {
28
- const o = this.createHeader(void 0, t);
29
- return this.send("GET", e, s, void 0, o);
30
- }
31
- request(e, s) {
32
- const t = s.contentType ?? (s.method === "GET" ? void 0 : g), o = this.createHeader(t, s.headers);
33
- return this.send(s.method, e, s.querys, s.data, o);
34
- }
35
- send(e, s, t, o, n) {
36
- return (async () => {
37
- let r = {
38
- url: s,
39
- method: e,
40
- querys: t,
41
- data: o,
42
- header: n
43
- };
44
- i.isNil(this.config.interceptorsRequest) || (r = await this.config.interceptorsRequest(r));
45
- const a = this.createRequestUrl(r.url, r.querys), h = this.config.timeout ?? T;
46
- return await new Promise((d) => {
47
- try {
48
- this.getMiniProgramWx().request({
49
- url: a,
50
- method: r.method,
51
- data: r.data,
52
- header: r.header,
53
- timeout: h,
54
- success: (c) => {
55
- this.handleSuccess(c, r).then(d);
56
- },
57
- fail: (c) => {
58
- d(f.TResponse.toError(c.errMsg));
59
- }
60
- });
61
- } catch (c) {
62
- const p = c instanceof Error ? c.message : "请求失败";
63
- d(f.TResponse.toError(p));
64
- }
65
- });
66
- })();
67
- }
68
- async handleSuccess(e, s) {
69
- if (e.statusCode < m || e.statusCode >= N)
70
- return f.TResponse.toError(`请求失败,状态码 ${e.statusCode}`);
71
- let t = {
72
- data: e.data,
73
- statusCode: e.statusCode,
74
- config: s
75
- };
76
- return i.isNil(this.config.interceptorsResponse) || (t = await this.config.interceptorsResponse(t)), this.tryHandleSessionExpired(t.data), t.data;
77
- }
78
- tryHandleSessionExpired(e) {
79
- if (i.isNil(this.config.loseTokenState) || i.isNil(this.config.sessionExpiredErrorKey) || i.isNil(e) || typeof e != "object")
80
- return;
81
- e.ERRORKEY === this.config.sessionExpiredErrorKey && this.config.loseTokenState();
82
- }
83
- getMiniProgramWx() {
84
- const e = globalThis.wx;
85
- if (i.isNil(e))
86
- throw new Error("当前环境不存在微信小程序 wx");
87
- return e;
88
- }
89
- createRequestUrl(e, s) {
90
- const t = this.config.baseURL.endsWith("/") ? this.config.baseURL.slice(0, -1) : this.config.baseURL, o = e.startsWith("/") ? e : `/${e}`, n = {
91
- ...this.createAuthQuerys(),
92
- ...this.toLowerCase(s)
93
- }, r = Object.entries(n).filter(([, a]) => !i.isNil(a)).map(([a, h]) => `${encodeURIComponent(a)}=${encodeURIComponent(String(h))}`).join("&");
94
- return r.length === 0 ? `${t}${o}` : `${t}${o}?${r}`;
95
- }
96
- createAuthQuerys() {
97
- const e = {};
98
- return !i.isNil(this.config.whobuild) && this.config.whobuild !== 0 && (e.whobuild = this.config.whobuild), !i.isNil(this.config.userIndx) && this.config.userIndx !== 0 && (e.userindx = this.config.userIndx), e;
99
- }
100
- createHeader(e, s) {
101
- const t = {};
102
- return i.isNil(e) || (t["content-type"] = e), !i.isNil(this.config.usrtoken) && this.config.usrtoken !== "" && (t.usrtoken = this.config.usrtoken), !i.isNil(this.config.dbConfig) && this.config.dbConfig !== "" && (t.dbconfig = this.config.dbConfig), i.isNil(s) || Object.assign(t, s), t;
103
- }
104
- toLowerCase(e) {
105
- return i.isNil(e) ? {} : i.mapKeys(e, (s, t) => t.toLowerCase());
106
- }
107
- }
108
- const y = (l) => u.create(l);
109
- export {
110
- u as WxRequest,
111
- y as createWxRequest
112
- };
File without changes
File without changes