- 删除 NutUI Address 相关导入、状态及地区数据转换逻辑 - 新增派生 regionValue 用于绑定微信原生 Picker 的 value - 将 handleRegionChange 调整为处理微信 Picker 的事件 - 将所在地选择 UI 修改为微信原生 Picker 包裹的视图结构 - 地图定位失败使用弹窗提示,引导用户手动选择或重试 - 移除手动打开地区选择器的函数及 NutUI 组件 - 保留 RegionData 用于文本智能识别解析 - 编译通过,提升在无地图或无权限环境下的地区选择稳定性
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { useRef } from 'react'
|
||
import Taro, { useShareAppMessage, useShareTimeline } from '@tarojs/taro'
|
||
import { useUser } from './useUser'
|
||
|
||
export interface UseShareOptions {
|
||
/** 分享标题 */
|
||
title: string
|
||
/** 分享给好友的完整 path(含或不含 query),如 /pages/shop/product-detail?id=123 */
|
||
path?: string
|
||
/** 朋友圈的 query(不含 page 与 ?),如 id=123,会被拼到当前页 path 之后 */
|
||
query?: string
|
||
/** 封面图(海报临时路径或网络图地址),不传则微信截取页面 */
|
||
imageUrl?: string
|
||
}
|
||
|
||
/** 当前是否处于朋友圈单页模式(scene=1154) */
|
||
export function isTimelineSinglePage(): boolean {
|
||
try {
|
||
const options = Taro.getEnterOptionsSync()
|
||
return options?.scene === 1154
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
|
||
function buildPath(path: string | undefined, inviterId?: number | string): string {
|
||
if (!path) return ''
|
||
if (!inviterId) return path
|
||
const sep = path.includes('?') ? '&' : '?'
|
||
return `${path}${sep}inviter=${inviterId}`
|
||
}
|
||
|
||
function buildQuery(query: string | undefined, inviterId?: number | string): string {
|
||
const parts: string[] = []
|
||
if (query) parts.push(query)
|
||
if (inviterId) parts.push(`inviter=${inviterId}`)
|
||
return parts.join('&')
|
||
}
|
||
|
||
/**
|
||
* 统一注册「分享给好友」与「分享到朋友圈」回调。
|
||
* 自动在 path/query 中追加 inviter(当前登录用户 id),用于分销裂变。
|
||
* 注意:朋友圈分享仅对非 tabBar 页生效,需在页面 config 中设置 enableShareTimeline: true。
|
||
*/
|
||
export function useShare(options: UseShareOptions) {
|
||
const { user } = useUser()
|
||
const inviterId = user?.id ?? user?.userId
|
||
// 用 ref 保证分享触发时读取到最新 options(含异步生成的海报图)
|
||
const optionsRef = useRef(options)
|
||
optionsRef.current = options
|
||
|
||
useShareAppMessage(() => {
|
||
const o = optionsRef.current
|
||
return {
|
||
title: o.title,
|
||
path: buildPath(o.path, inviterId),
|
||
imageUrl: o.imageUrl,
|
||
}
|
||
})
|
||
|
||
useShareTimeline(() => {
|
||
const o = optionsRef.current
|
||
return {
|
||
title: o.title,
|
||
query: buildQuery(o.query, inviterId),
|
||
imageUrl: o.imageUrl,
|
||
}
|
||
})
|
||
}
|