feat(h5-auth): remember SMS sessions and simplify media access
CI / verify (pull_request) Has been cancelled
CI / verify (pull_request) Has been cancelled
Hide WeChat login for shop and partner users, keep verified sessions for seven days, and allow JSSDK camera or album access without an OpenID binding. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,17 +1,9 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { uploadFileToOss, type OssMediaType } from '../lib/upload';
|
||||
import { enqueueUpload } from '../lib/upload-lock';
|
||||
import {
|
||||
authorizePartnerWechat,
|
||||
fetchClientConfig,
|
||||
fetchPartnerProfile,
|
||||
needsWechatAuth,
|
||||
type PartnerProfile,
|
||||
} from '../lib/wechat-auth';
|
||||
import { formatChooseImageFailMessage } from '@dukang/weixin-sdk';
|
||||
import { isWechatEnv, weixinSdk } from '../lib/weixin';
|
||||
import { toastError } from '../lib/toast';
|
||||
import type { ClientRuntimeConfig } from '@dukang/shared-types';
|
||||
|
||||
type OssUploadFieldProps = {
|
||||
value?: string;
|
||||
@@ -22,7 +14,7 @@ type OssUploadFieldProps = {
|
||||
wide?: boolean;
|
||||
compact?: boolean;
|
||||
label?: string;
|
||||
/** 父级已确认微信授权时可跳过检查 */
|
||||
/** 兼容现有调用:选图不再依赖 openId 绑定 */
|
||||
wechatReady?: boolean;
|
||||
onWechatReadyChange?: (ready: boolean) => void;
|
||||
};
|
||||
@@ -52,15 +44,11 @@ export default function OssUploadField({
|
||||
wide,
|
||||
compact,
|
||||
label,
|
||||
wechatReady,
|
||||
onWechatReadyChange,
|
||||
}: OssUploadFieldProps) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [authorizing, setAuthorizing] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [profile, setProfile] = useState<PartnerProfile | null>(null);
|
||||
const [clientConfig, setClientConfig] = useState<ClientRuntimeConfig | null>(null);
|
||||
const [showAlbumFallback, setShowAlbumFallback] = useState(false);
|
||||
|
||||
function showUploadError(text: string) {
|
||||
setError(text);
|
||||
@@ -72,32 +60,14 @@ export default function OssUploadField({
|
||||
const inWechat = isWechatEnv();
|
||||
const useWechatPicker =
|
||||
inWechat && (mediaType === 'IMAGE' || (mediaType === 'FILE' && acceptsImages(resolvedAccept)));
|
||||
const needsAuth = useWechatPicker && needsWechatAuth(profile, clientConfig) && wechatReady !== true;
|
||||
const onWechatReadyChangeRef = useRef(onWechatReadyChange);
|
||||
onWechatReadyChangeRef.current = onWechatReadyChange;
|
||||
|
||||
useEffect(() => {
|
||||
if (!useWechatPicker) return;
|
||||
void fetchClientConfig()
|
||||
.then(setClientConfig)
|
||||
.catch(() => {
|
||||
/* 未登录等场景由上传接口报错 */
|
||||
});
|
||||
if (wechatReady === true) return;
|
||||
void fetchPartnerProfile()
|
||||
.then(setProfile)
|
||||
.catch(() => {
|
||||
/* 未登录等场景由上传接口报错 */
|
||||
});
|
||||
}, [useWechatPicker, wechatReady]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!useWechatPicker || needsAuth) return;
|
||||
weixinSdk.reset();
|
||||
void weixinSdk.init().catch(() => {
|
||||
/* 点击上传时会再次初始化 */
|
||||
});
|
||||
}, [useWechatPicker, needsAuth]);
|
||||
}, [useWechatPicker]);
|
||||
|
||||
async function persistUpload(file: File) {
|
||||
if (!file.size) {
|
||||
@@ -125,24 +95,6 @@ export default function OssUploadField({
|
||||
}
|
||||
}
|
||||
|
||||
async function startWechatAuth() {
|
||||
setAuthorizing(true);
|
||||
setError('');
|
||||
try {
|
||||
const result = await authorizePartnerWechat();
|
||||
if (result) {
|
||||
const me = await fetchPartnerProfile();
|
||||
setProfile(me);
|
||||
if (me.hasWechat) onWechatReadyChangeRef.current?.(true);
|
||||
}
|
||||
} catch (e) {
|
||||
const text = e instanceof Error ? e.message : '微信授权失败';
|
||||
showUploadError(text);
|
||||
} finally {
|
||||
setAuthorizing(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function pickWechatImage() {
|
||||
setUploading(true);
|
||||
setError('');
|
||||
@@ -161,29 +113,26 @@ export default function OssUploadField({
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : '无法打开相册';
|
||||
if (/cancel/i.test(msg)) return;
|
||||
showUploadError(formatWechatUploadError(e));
|
||||
throw e;
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function pickFile() {
|
||||
if (uploading || authorizing) return;
|
||||
if (uploading) return;
|
||||
setError('');
|
||||
|
||||
if (useWechatPicker && needsAuth) {
|
||||
const text = '请先完成微信授权后再上传照片';
|
||||
showUploadError(text);
|
||||
return;
|
||||
}
|
||||
|
||||
if (useWechatPicker) {
|
||||
try {
|
||||
await pickWechatImage();
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : '无法打开相册';
|
||||
if (/cancel/i.test(msg)) return;
|
||||
showUploadError(formatWechatUploadError(e));
|
||||
const formatted = formatWechatUploadError(e);
|
||||
setError(`${formatted},可改从系统相册选择`);
|
||||
setShowAlbumFallback(true);
|
||||
inputRef.current?.click();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -193,44 +142,31 @@ export default function OssUploadField({
|
||||
|
||||
const isImage = mediaType === 'IMAGE' && value;
|
||||
const isFile = mediaType === 'FILE' && value;
|
||||
const busy = uploading || authorizing;
|
||||
const busy = uploading;
|
||||
const pickerLabel = label ?? (useWechatPicker ? '拍照 / 从相册选择' : '点击上传');
|
||||
|
||||
const triggerProps = {
|
||||
type: 'button' as const,
|
||||
disabled: busy || needsAuth,
|
||||
disabled: busy,
|
||||
onClick: () => void pickFile(),
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="partner-oss-upload">
|
||||
{needsAuth && (
|
||||
<div className="partner-wechat-auth-hint" role="status">
|
||||
<p className="body-md">上传照片需先完成微信授权绑定</p>
|
||||
<button
|
||||
type="button"
|
||||
className="partner-btn-outline"
|
||||
style={{ marginTop: 8, width: '100%' }}
|
||||
disabled={authorizing}
|
||||
onClick={() => void startWechatAuth()}
|
||||
>
|
||||
{authorizing ? '跳转授权中…' : '微信授权绑定'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{!useWechatPicker && (
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept={resolvedAccept}
|
||||
className="partner-oss-upload-input"
|
||||
disabled={busy}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) void uploadSelectedFile(file);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept={resolvedAccept}
|
||||
className="partner-oss-upload-input"
|
||||
disabled={busy}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
setShowAlbumFallback(false);
|
||||
void uploadSelectedFile(file);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{isImage ? (
|
||||
<button {...triggerProps} className={`partner-upload-preview${wide ? ' partner-upload-preview--wide' : ''}`}>
|
||||
<img src={value} alt={label ?? '已上传'} />
|
||||
@@ -258,10 +194,21 @@ export default function OssUploadField({
|
||||
{busy ? 'hourglass_top' : 'add_a_photo'}
|
||||
</span>
|
||||
<span className="text-primary" style={{ fontWeight: 500 }}>
|
||||
{uploading ? '上传中…' : needsAuth ? '请先微信授权' : pickerLabel}
|
||||
{uploading ? '上传中…' : pickerLabel}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
{showAlbumFallback && useWechatPicker && (
|
||||
<button
|
||||
type="button"
|
||||
className="partner-btn-outline"
|
||||
style={{ marginTop: 8, width: '100%' }}
|
||||
disabled={busy}
|
||||
onClick={() => inputRef.current?.click()}
|
||||
>
|
||||
从系统相册选择
|
||||
</button>
|
||||
)}
|
||||
{error && <p className="partner-form-error" role="alert">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user