门店开通流程
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { getRuntimePlatform } from './env';
|
||||
import { ensureJssdkReady } from './jssdk';
|
||||
import type { WeixinSdkConfig } from './types';
|
||||
|
||||
export type ChooseWechatImageOptions = {
|
||||
count?: number;
|
||||
sourceType?: Array<'album' | 'camera'>;
|
||||
};
|
||||
|
||||
function base64ToFile(base64: string, fileName: string): File {
|
||||
const normalized = base64.startsWith('data:')
|
||||
? base64
|
||||
: `data:image/jpeg;base64,${base64.replace(/\s/g, '')}`;
|
||||
const [header, body] = normalized.split(',');
|
||||
const mime = header.match(/:(.*?);/)?.[1] ?? 'image/jpeg';
|
||||
const binary = atob(body);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
||||
return new File([bytes], fileName, { type: mime });
|
||||
}
|
||||
|
||||
function localIdToFile(localId: string): Promise<File> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!window.wx?.getLocalImgData) {
|
||||
reject(new Error('微信 getLocalImgData 不可用'));
|
||||
return;
|
||||
}
|
||||
window.wx.getLocalImgData({
|
||||
localId,
|
||||
success: (res) => {
|
||||
try {
|
||||
resolve(base64ToFile(res.localData, `wx-${Date.now()}.jpg`));
|
||||
} catch (e) {
|
||||
reject(e instanceof Error ? e : new Error('图片解析失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => reject(new Error(err.errMsg || '读取图片失败')),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** 微信内选图(相册/拍照),返回 File 列表;非微信环境返回 null */
|
||||
export async function chooseWechatImages(
|
||||
config: WeixinSdkConfig,
|
||||
options: ChooseWechatImageOptions = {},
|
||||
): Promise<File[] | null> {
|
||||
const platform = getRuntimePlatform();
|
||||
if (platform === 'browser') return null;
|
||||
|
||||
const count = options.count ?? 1;
|
||||
const sourceType = options.sourceType ?? ['album', 'camera'];
|
||||
|
||||
if (platform === 'wechat-h5') {
|
||||
await ensureJssdkReady({
|
||||
apiBase: config.apiBase ?? '/api/v1',
|
||||
clientApp: config.clientApp,
|
||||
getAccessToken: config.getAccessToken,
|
||||
});
|
||||
if (!window.wx?.chooseImage) return null;
|
||||
|
||||
const localIds = await new Promise<string[]>((resolve, reject) => {
|
||||
window.wx!.chooseImage!({
|
||||
count,
|
||||
sizeType: ['compressed'],
|
||||
sourceType,
|
||||
success: (res) => resolve(res.localIds ?? []),
|
||||
fail: (err) => reject(new Error(err.errMsg || '无法打开相册')),
|
||||
});
|
||||
});
|
||||
|
||||
const files: File[] = [];
|
||||
for (const localId of localIds) {
|
||||
files.push(await localIdToFile(localId));
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function canUseWechatChooseImage(): boolean {
|
||||
return getRuntimePlatform() !== 'browser';
|
||||
}
|
||||
@@ -7,6 +7,8 @@ export {
|
||||
} from './location';
|
||||
export { scanQrCode } from './scan';
|
||||
export { invokeWechatPay } from './pay';
|
||||
export { chooseWechatImages, canUseWechatChooseImage } from './chooseImage';
|
||||
export type { ChooseWechatImageOptions } from './chooseImage';
|
||||
export {
|
||||
getWechatOAuthUrl,
|
||||
startWechatOAuthLogin,
|
||||
@@ -25,6 +27,7 @@ import { initWechatJssdk } from './jssdk';
|
||||
import { getWechatLocation } from './location';
|
||||
import { scanQrCode } from './scan';
|
||||
import { invokeWechatPay } from './pay';
|
||||
import { chooseWechatImages } from './chooseImage';
|
||||
import {
|
||||
wechatLogin,
|
||||
handleWechatOAuthCallback,
|
||||
@@ -43,6 +46,8 @@ export function createWeixinSdk(config: WeixinSdkConfig) {
|
||||
getPhoneNumber: () => getWechatPhoneNumber(config),
|
||||
getLocation: () => getWechatLocation(config),
|
||||
scanQrCode: () => scanQrCode(config),
|
||||
chooseImages: (options?: Parameters<typeof chooseWechatImages>[1]) =>
|
||||
chooseWechatImages(config, options),
|
||||
pay: (prepay: Parameters<typeof invokeWechatPay>[0]) =>
|
||||
invokeWechatPay(prepay, { apiBase: config.apiBase, clientApp: config.clientApp }),
|
||||
};
|
||||
|
||||
@@ -34,6 +34,18 @@ export type WxApi = {
|
||||
fail?: (res: { errMsg: string }) => void;
|
||||
cancel?: () => void;
|
||||
}) => void;
|
||||
chooseImage: (options: {
|
||||
count?: number;
|
||||
sizeType?: Array<'original' | 'compressed'>;
|
||||
sourceType?: Array<'album' | 'camera'>;
|
||||
success?: (res: { localIds: string[] }) => void;
|
||||
fail?: (res: { errMsg: string }) => void;
|
||||
}) => void;
|
||||
getLocalImgData: (options: {
|
||||
localId: string;
|
||||
success?: (res: { localData: string }) => void;
|
||||
fail?: (res: { errMsg: string }) => void;
|
||||
}) => void;
|
||||
};
|
||||
|
||||
export type MiniProgramWx = {
|
||||
@@ -80,6 +92,8 @@ export const DEFAULT_JS_API_LIST = [
|
||||
'getLocation',
|
||||
'scanQRCode',
|
||||
'chooseWXPay',
|
||||
'chooseImage',
|
||||
'getLocalImgData',
|
||||
'updateAppMessageShareData',
|
||||
'updateTimelineShareData',
|
||||
] as const;
|
||||
|
||||
Reference in New Issue
Block a user