19 lines
783 B
TypeScript
19 lines
783 B
TypeScript
import { postRequest, type ApiResponse } from "./customFetch";
|
||
import { getWecomSignatureUrl } from "./fetchUrl";
|
||
import type { SignatureData } from "@wecom/jssdk";
|
||
|
||
/**
|
||
* 向后端换取企业微信 JS-SDK 签名。
|
||
* - type=config:企业级(wx.config);
|
||
* - type=agent:应用级(agentConfig)。
|
||
* 后端用 jsapi_ticket 对传入的 url 计算 { timestamp, nonceStr, signature }。
|
||
*/
|
||
export const getWecomSignature = async (url: string, type: "config" | "agent"): Promise<SignatureData> => {
|
||
const res: ApiResponse = await postRequest(getWecomSignatureUrl(), { url, type });
|
||
const result = (res.code === 200 ? res.result : null) as SignatureData | null;
|
||
if (!result) {
|
||
throw new Error("获取企业微信 JS-SDK 签名失败");
|
||
}
|
||
return result;
|
||
};
|