import request from '@/utils/request' const v = '1.0' /** * 微信授权相关接口(module wechat) * * wechat.getAuthUrl —— 获取微信授权链接:前端拿到链接后在**新小窗口**中打开, * 用户完成授权后由后端接收微信回调并落库,前端再调用业务侧查询接口刷新。 */ export type WeChatAuthUrlPayload = { /** 授权链接(后端返回的字段名可能是 url / auth_url / authorize_url 等,取值时做兼容) */ url?: string auth_url?: string authorize_url?: string /** 授权状态校验串 */ state?: string [key: string]: unknown } /** wechat.getAuthUrl 入参:warehouse_id 可选,缺省取当前登录用户所属仓库 */ export type WeChatAuthUrlParams = { /** 仓库 ID(可选;缺省取当前登录用户所属仓库) */ warehouse_id?: number } /** * 获取微信授权链接(由用户点击「去授权」时调用,避免页面加载即跳转/弹窗被拦截)。 * 返回值中链接字段名做多形态兼容,取值见页面 pickUrl()。 */ export function wechatGetAuthUrl( data: WeChatAuthUrlParams = {} ): Promise<{ code?: number; message?: string; data?: WeChatAuthUrlPayload } & Record> { return request({ url: '/system', method: 'post', params: { method: 'wechat.getAuthUrl', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 查询当前仓库小程序的微信认证状态(wechat.queryauth)—— 入参可选 `taskid`,仓库由登录态上下文(ctx.user.store_id)决定。 * * 三种返回形态(均为 code 200 / success:true,需看 `is_verified` 与 `taskid`): * 1) 已认证:`{is_verified:true, appid, verify_type_id, verify_type_text, principal_name, signature, * service_type_id, account_status}`,message「小程序已认证」; * 2) 未认证但有认证任务:微信 `wxa/sec/queryauth` 结果 + `{appid, is_verified, task_status_text, apply_status_text}`; * 3) 未认证且无认证任务:`{is_verified:false, appid, taskid:null, task_status:null, apply_status:null}`, * message「尚未认证,且无认证任务」。 */ export type WeChatQueryAuthData = { /** 小程序 AppID */ appid?: string /** 是否已认证(verify_type_id >= 0 或 apply_status=4) */ is_verified?: boolean /** 认证类型 ID:0 微信认证、1 新浪微博认证、3 资质通过未名称认证、4 资质通过+微博认证(-1 未认证) */ verify_type_id?: number | null /** 认证类型中文文案 */ verify_type_text?: string /** 认证主体名称 */ principal_name?: string /** 帐号简介 */ signature?: string /** 主体类型 ID(微信原值) */ service_type_id?: number | null /** 帐号状态(微信原值) */ account_status?: number | null /** 认证任务 ID(为空=后端没有认证任务) */ taskid?: string | null /** 认证任务状态(微信 task_status,0/1/2/3…15) */ task_status?: number | null /** 认证任务状态中文文案 */ task_status_text?: string /** 认证审核单状态:0 审核单不存在、1 待支付、2 审核中、3 打回重填、4 认证通过、5 认证最终失败 */ apply_status?: number | null /** 审核单状态中文文案 */ apply_status_text?: string [key: string]: unknown } export type WeChatQueryAuthParams = { /** 认证任务 ID(可选;缺省时用库中 auth_task_id;小程序已认证时无需传入) */ taskid?: string } export function wechatQueryAuth( data: WeChatQueryAuthParams = {} ): Promise< { code?: number; message?: string; data?: WeChatQueryAuthData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.queryauth', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 发起小程序认证(wechat.wxaauth)—— 后端分步: * 图片字段传 COS 文件 URL(http/https)→ 后端下载并调微信 `wxa/sec/uploadauthmaterial` 换 mediaid * → 替换请求体 → 以 `{ auth_data }` 包裹调微信 `wxa/sec/wxaauth`(权限集 18)一步提交。 * 成功后后端回写 `wms_wechat_app.auth_task_id`/`auth_apply_status`,返回 `taskid` 与 `auth_url`; * 小程序已认证(verify_type_id >= 0)时会拒绝重复发起。 */ export type WeChatWxaauthContactInfo = { /** 联系人姓名(必填) */ name: string /** 联系人邮箱(必填) */ email: string /** 联系人手机(选填) */ mobile?: string } /** wechat.wxaauth 入参(新版 auth_data:6 个必填 + 下列可选字段) */ export type WeChatWxaauthParams = { /** 账号主体类型:1 企业、12 个体工商户、15 个人 */ customer_type: number /** 小程序账号名称 */ account_name: string /** 命名类型:1 基于自选词汇、2 基于商标 */ account_name_type: number /** 支付方式:1 消耗服务商预购包、2 小程序开发者自行支付 */ pay_type: number /** 第三方联系电话 */ third_party_phone: string /** 联系人信息(name/email 必填,mobile 选填) */ contact_info: WeChatWxaauthContactInfo /** 发票信息(服务商代缴可不传) */ invoice_info?: Record /** 主体资质材料(mediaid 或 COS URL;**非个人主体必填**,单张) */ qualification?: string /** 资质其他材料(mediaid / COS URL 数组,≤10 张) */ qualification_other?: string[] /** 名称关键词补充材料(mediaid / COS URL 数组) */ account_supplemental?: string[] /** 个人认证身份信息(选填) */ auth_identification?: Record /** 个人认证身份材料(mediaid / COS URL 数组) */ auth_ident_material?: string[] /** 服务商代缴时必填:服务商 AppID */ service_appid?: string /** 打回重填(reauth)时必填:原认证任务 ID */ taskid?: string /** * 其余白名单字段(后端透传 auth_data 原样给微信)。 * 图片字段(qualification / qualification_other[] / account_supplemental[] / auth_ident_material[]) * 支持传 COS 文件 URL;传非 URL 时视为已传 mediaid 原样透传。 */ [key: string]: unknown } export type WeChatWxaauthData = { /** 认证任务 ID(已回写 auth_task_id,供 wechat.queryauth 查进度) */ taskid?: string /** 小程序管理员认证链接(可选,打开后继续完成认证) */ auth_url?: string /** 审核单状态:0 不存在、1 待支付、2 审核中 */ apply_status?: number [key: string]: unknown } export function wechatWxaauth(data: WeChatWxaauthParams): Promise< { code?: number; message?: string; data?: WeChatWxaauthData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.wxaauth', v, timestamp: new Date().getTime() }, data, isSystemApi: true, // 材料图片需后端下载 + 上传微信换 media_id,耗时较长 timeout: 180000 } as any) } /** * 获取并同步当前仓库小程序基础信息(wechat.getAuthorizerInfo)—— * 后端调微信 `api_get_authorizer_info` 拉取授权账号详情,将昵称/头像/原始ID/主体名称/签名/ * 认证类型/账号状态/权限集等落库 `wms_wechat_app` 后返回完整数据;认证进度请用 wechat.queryauth。 */ export type WeChatAuthorizerInfoData = { /** 当前仓库小程序 AppID */ appid?: string /** 授权账号基本信息(nick_name/head_img/user_name/principal_name/signature/verify_type_info 等) */ authorizer_info?: Record /** 授权信息(authorizer_appid/func_info/authorizer_refresh_token) */ authorization_info?: Record [key: string]: unknown } export function wechatGetAuthorizerInfo(): Promise< { code?: number; message?: string; data?: WeChatAuthorizerInfoData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getAuthorizerInfo', v, timestamp: new Date().getTime() }, data: {}, isSystemApi: true } as any) } /** * 快速注册企业小程序(wechat.registerMiniprogram)—— * 调微信 fastregisterweapp?action=create(component_access_token)提交企业信息创建注册任务, * 法人微信收到模板消息做人脸核身;审核结果由 notify_third_fasteregister 事件推送到后端授权事件接收 URL。 * * 注意:本接口返回 errcode/errmsg,**不返回可打开的链接**, * 前端不应再按「取链接开小窗」的方式处理。 */ export type WeChatRegisterMiniprogramParams = { /** 企业名(与工商登记一致;无主体名称个体工商户填「个体户+法人姓名」) */ name: string /** 企业代码 */ code: string /** 企业代码类型:1 统一社会信用代码(18位)、2 组织机构代码(9位)、3 营业执照注册号(15位) */ code_type: 1 | 2 | 3 /** 法人微信号(不支持手机号/微信名) */ legal_persona_wechat: string /** 法人姓名(绑定银行卡) */ legal_persona_name: string /** 第三方联系电话 */ component_phone?: string } export function wechatRegisterMiniprogram( data: WeChatRegisterMiniprogramParams ): Promise< { code?: number; message?: string; errcode?: number; errmsg?: string } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.registerMiniprogram', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 获取小程序模板库列表(wechat.getTemplateList)—— 只拉取普通模板(template_type=0), * 后端 Redis 缓存 24h(命中缓存直接返回,from_cache=true),用于「上传代码生成体验版」时选模板。 */ export type WeChatTemplateItem = { /** 代码模板 ID(= wechat.commit 的 template_id 入参) */ template_id?: number /** 模板版本号 */ user_version?: string /** 模板描述 */ user_desc?: string /** 模板创建时间(Unix 秒) */ create_time?: number /** 草稿 ID */ draft_id?: number /** 模板类型:0 普通模板 */ template_type?: number /** 源小程序 AppID */ source_miniprogram_appid?: string /** 源小程序账号 */ source_miniprogram?: string [key: string]: unknown } export type WeChatTemplateListData = { list: WeChatTemplateItem[] /** 是否来自后端缓存(Redis 24h) */ from_cache: boolean } export function wechatGetTemplateList(): Promise< { code?: number; message?: string; data?: WeChatTemplateListData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getTemplateList', v, timestamp: new Date().getTime() }, data: {}, isSystemApi: true } as any) } /** * 上传代码并生成体验版(wechat.commit)—— * 入参**只有 template_id**(正整数):后端取当前仓库启用小程序 → 校验授权权限集 18(代码管理) * → 从模板库取该模板、直接用它自己的 `user_version`/`user_desc` 作为版本号与代码描述 * (模板缺版本号/描述会直接失败,前端不再传) * → `ext_json` 由后端固定生成 `{extEnable:true, extAppid:当前小程序appid}` * → 调微信 wxa/commit → 成功后回写 `wms_wechat_app.template_id` 与 `user_version`。 */ export type WeChatCommitParams = { /** 代码库中的代码模板 ID(由 wechat.getTemplateList 获取,必须为正整数) */ template_id: number } export type WeChatCommitData = { /** 小程序 AppID */ appid: string /** 已上传的模板 ID(同时回写 wms_wechat_app.template_id) */ template_id: number /** 版本号(取自模板,由后端回传,非前端传入) */ user_version?: string /** 代码描述(取自模板) */ user_desc?: string /** 后端生成的 ext_json 字符串 */ ext_json?: string /** 微信返回的 errcode(0=成功) */ errcode?: number /** 微信返回的 errmsg */ errmsg?: string [key: string]: unknown } export function wechatCommit( data: WeChatCommitParams ): Promise<{ code?: number; message?: string; data?: WeChatCommitData } & Record> { return request({ url: '/system', method: 'post', params: { method: 'wechat.commit', v, timestamp: new Date().getTime() }, data, isSystemApi: true, // 上传代码到微信耗时较长,超时放宽到 3 分钟 timeout: 180000 } as any) } /** * 获取体验版二维码(wechat.getTrialQRCode)—— * 后端取当前仓库启用小程序,校验权限集 18 / 86 后调微信 wxa/get_qrcode 取二维码(jpeg), * 转存 COS 后返回可访问的图片 URL。 */ export type WeChatTrialQRCodeParams = { /** 扫码后进入的页面路径(可选,如 page/index?action=1,后端自动 urlencode) */ path?: string } export type WeChatTrialQRCodeData = { /** 小程序 AppID */ appid: string /** 二维码图片 URL(COS) */ url: string } export function wechatGetTrialQRCode( data: WeChatTrialQRCodeParams = {} ): Promise< { code?: number; message?: string; data?: WeChatTrialQRCodeData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getTrialQRCode', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** 审核项(item_list 元素):微信审核类目信息,后端原样透传 */ export type WeChatAuditItem = { /** 类目路径(如 教育/在线教育) */ address?: string /** 类目标签 */ tag?: string /** 一级类目 */ first_class?: string [key: string]: unknown } /** * 提交代码审核(wechat.submitAudit)—— * 后端取当前仓库启用小程序,校验权限集 18;**仅 audit_status != 1(无审核中的版本)时允许提交**, * 成功后记录 audit_status=1 与 audit_id(回写 wms_wechat_app.audit_id)。 * * 审核结果由微信事件(weapp_audit_success / weapp_audit_fail)回写后端:2 通过、3 驳回(含原因); * ⚠️ 目前没有查询审核状态的接口,前端只能得到「已提交」与 auditid。 */ export type WeChatSubmitAuditParams = { /** 审核项列表(可选,含 address/tag/first_class 等类目信息) */ item_list?: WeChatAuditItem[] /** 版本描述(可选) */ version_desc?: string /** 不使用地理位置接口声明(可选) */ privacy_api_not_use?: boolean } export type WeChatSubmitAuditData = { /** 小程序 AppID */ appid: string /** 微信审核单 ID(同时回写 wms_wechat_app.audit_id) */ auditid: number } export function wechatSubmitAudit( data: WeChatSubmitAuditParams = {} ): Promise< { code?: number; message?: string; data?: WeChatSubmitAuditData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.submitAudit', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** 用户列表 where 条件值:operator 结构(operator 支持 eq/ne/gt/lt/ge/le/in/notin/between/like/contains/notlike/startswith/endswith/isnull/isnotnull) */ export type WeChatUserWhereValue = { operator?: string value?: unknown } /** userList 的 where 字段白名单(后端) */ export type WeChatUserWhereField = | 'id' | 'user_id' | 'app_id' | 'openid' | 'unionid' | 'nickname' | 'sex' | 'login_time' | 'login_count' | 'created_at' | 'updated_at' /** * userList 的 sort 字段白名单(后端)= where 白名单 + `agreement_version_id` * (协议版本记录 id,小程序登录时自动回写) */ export type WeChatUserSortField = WeChatUserWhereField | 'agreement_version_id' export type WeChatUserListParams = { /** 指定 wms_wechat_app.id(可选,须属当前仓库且启用;缺省取当前仓库全部启用小程序) */ app_id?: number /** 查询条件:字段级 `{ operator, value }`(字段白名单见 WeChatUserWhereField) */ where?: Partial> /** 分页:默认 page=1、pageSize=20 */ pagination?: { page?: number; pageSize?: number } /** 排序:sortField 白名单见 WeChatUserSortField,默认 updated_at DESC */ sort?: { sortField?: WeChatUserSortField; sortOrder?: 'asc' | 'desc' | 'ASC' | 'DESC' } } /** 用户列表项(后端已排除 session_key / access_token / refresh_token 等敏感字段) */ export type WeChatUserItem = { id?: number /** 绑定的真实账号 ID(0=未绑定) */ user_id?: number app_id?: number openid?: string unionid?: string nickname?: string /** 微信性别:0 未知、1 男、2 女 */ sex?: number headimgurl?: string login_time?: number | string login_count?: number /** 是否已绑定真实账号(user_id > 0) */ is_bound?: boolean /** * 用户最近一次登录时同意的**协议版本记录 id**(wms_wechat_agreement_version.id,由小程序 loginByCode 自动回写); * 0 / 空表示未记录同意版本;可传给 `wechat.getAgreementVersion` 查看该版本协议原文。 */ agreement_version_id?: number created_at?: string updated_at?: string [key: string]: unknown } export type WeChatUserListData = { list: WeChatUserItem[] pagination: { page?: number; pageSize?: number; total?: number } /** 本次查询命中的小程序列表(id/appid/nickname) */ apps?: { id?: number; appid?: string; nickname?: string }[] [key: string]: unknown } /** * 拉取当前仓库小程序的用户列表(wechat.userList)—— * 按 ctx.user.store_id 取仓库启用小程序(可用 app_id 指定单个,须属当前仓库), * 支持 where(operator 结构)/ pagination / sort;响应已排除 session_key / access_token / refresh_token 等敏感字段, * 并附 `is_bound`(user_id > 0 已绑定真实账号)与 `agreement_version_id`(协议版本记录 id, * 由小程序 loginByCode 自动回写,可传给 wechat.getAgreementVersion 拉取该版本协议内容)。 */ export function wechatUserList( data: WeChatUserListParams = {} ): Promise<{ code?: number; message?: string; data?: WeChatUserListData } & Record> { return request({ url: '/system', method: 'post', params: { method: 'wechat.userList', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 获取指定协议版本内容(wechat.getAgreementVersion)—— **需登录、免接口权限**。 * 传版本记录 id(`wms_wechat_agreement_version.id`,可由 `wechat.userList` 的 `agreement_version_id` 获取), * 后端校验该版本对应 appid 属于**当前仓库的启用小程序**后返回协议内容(含 appid / nickname / text)。 */ export type WeChatAgreementVersionParams = { /** 协议版本记录 id(wms_wechat_agreement_version.id) */ id: number } export type WeChatAgreementVersionData = { /** 协议版本记录 id */ id: number /** 小程序 AppID */ appid: string /** 小程序名称 */ nickname?: string /** 协议富文本内容 */ text: string } export function wechatGetAgreementVersion( data: WeChatAgreementVersionParams ): Promise< { code?: number; message?: string; data?: WeChatAgreementVersionData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getAgreementVersion', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 按工号绑定微信(wechat.bindByEmployee)—— PC 管理员操作,需登录 + 仓库上下文 + `wechat.bindByEmployee` 权限。 * 传工号 id(wms_account_store_employee.id)与微信绑定记录 id(wms_user_oauth.id,来自 wechat.userList 的 id), * 后端查工号关联的账号 id 后写入 `wms_user_oauth.user_id` 完成绑定。 * * - 已绑定**同一**账号时幂等返回(`already_bound=true`); * - 以下情况会被拒绝:该微信已绑定其他账号、工号已解除或不属于当前仓库、 * 微信记录不属于当前仓库的小程序、账号被禁用。 */ export type WeChatBindByEmployeeParams = { /** 工号 id(wms_account_store_employee.id) */ id: number /** 微信绑定记录 id(wms_user_oauth.id,可在 wechat.userList 结果中获取) */ oauth_id: number } export type WeChatBindByEmployeeData = { /** 微信绑定记录 id */ oauth_id: number /** 是否此前已绑定同一账号(幂等调用) */ already_bound: boolean /** 工号信息 */ employee: { id?: number; account_id?: number; store_id?: number; real_name?: string } /** 绑定的账号信息 */ account: { id?: number; username?: string; real_name?: string } } export function wechatBindByEmployee( data: WeChatBindByEmployeeParams ): Promise< { code?: number; message?: string; data?: WeChatBindByEmployeeData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.bindByEmployee', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 获取当前仓库小程序的「用户须知」(wechat.getUserAgreement)—— 需登录 + 仓库上下文。 * 按当前登录仓库取启用小程序(wms_wechat_app),从副表拉取**最新版本**(id 自增即最新); * 仓库未绑定小程序时提示「无小程序,需要先绑定」,无版本时提示「该小程序暂无用户须知」。 */ export type WeChatUserAgreementData = { /** 当前仓库小程序 AppID */ appid: string /** 小程序名称 */ nickname?: string /** 最新协议版本记录 id(wms_wechat_agreement_version.id,id 自增即最新) */ version_id: number /** 用户须知富文本内容 */ text: string } export function wechatGetUserAgreement(): Promise< { code?: number; message?: string; data?: WeChatUserAgreementData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getUserAgreement', v, timestamp: new Date().getTime() }, data: {}, isSystemApi: true } as any) } /** * 获取小程序用户须知富文本(wechat.getUserAgreementText)—— **免登录**,入参 `appId`。 * 按 appid 查 `wms_wechat_app.user_agreement`,Redis 缓存 24 小时(`from_cache` 标识是否命中)。 * * ⚠️ 这是给**小程序端**(无登录态)读取用户须知用的;网页端有登录态,回显请用 wechat.getUserAgreement。 */ export type WeChatUserAgreementTextParams = { /** 小程序 AppID(wx...) */ appId: string } export type WeChatUserAgreementTextData = { /** 小程序 AppID */ appid: string /** 最新协议版本记录 id(wms_wechat_agreement_version.id,id 自增即最新) */ version_id: number /** 用户须知富文本内容 */ text: string /** 是否命中 24h 缓存 */ from_cache: boolean } export function wechatGetUserAgreementText( data: WeChatUserAgreementTextParams ): Promise< { code?: number; message?: string; data?: WeChatUserAgreementTextData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.getUserAgreementText', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } /** * 设置当前仓库小程序的「用户须知」(wechat.setUserAgreement)—— 需登录 + 仓库上下文。 * **每次提交自动新增一个版本**(直接插入副表 `wms_wechat_agreement_version`,id 自增即最新), * 并清除 getUserAgreementText 的 24h 缓存;`text` 可传空字符串。 */ export type WeChatSetUserAgreementParams = { /** 用户须知富文本内容(可传空字符串清空) */ text: string } export function wechatSetUserAgreement( data: WeChatSetUserAgreementParams ): Promise< { code?: number; message?: string; data?: WeChatUserAgreementData } & Record > { return request({ url: '/system', method: 'post', params: { method: 'wechat.setUserAgreement', v, timestamp: new Date().getTime() }, data, isSystemApi: true } as any) } export const wechat = { getAuthUrl: wechatGetAuthUrl, queryAuth: wechatQueryAuth, wxaauth: wechatWxaauth, getAuthorizerInfo: wechatGetAuthorizerInfo, registerMiniprogram: wechatRegisterMiniprogram, getTemplateList: wechatGetTemplateList, commit: wechatCommit, getTrialQRCode: wechatGetTrialQRCode, submitAudit: wechatSubmitAudit, userList: wechatUserList, bindByEmployee: wechatBindByEmployee, getUserAgreement: wechatGetUserAgreement, getAgreementVersion: wechatGetAgreementVersion, getUserAgreementText: wechatGetUserAgreementText, setUserAgreement: wechatSetUserAgreement } export default wechat