提交 6d65d27f 编写于 作者: D DCloud_LXH

feat: uni.share add openCustomerServiceChat

上级 83efd79c
......@@ -10,7 +10,12 @@ const SCENE: Parameters<API_TYPE_SHARE>[0]['scene'][] = [
export const SahreOptions: ApiOptions<API_TYPE_SHARE> = {
formatArgs: {
scene(value, params) {
if (params.provider === 'weixin' && (!value || !SCENE.includes(value))) {
const { provider, openCustomerServiceChat } = params
if (
provider === 'weixin' &&
!openCustomerServiceChat &&
(!value || !SCENE.includes(value))
) {
return `分享到微信时,scene必须为以下其中一个:${SCENE.join('')}`
}
},
......@@ -39,6 +44,16 @@ export const SahreOptions: ApiOptions<API_TYPE_SHARE> = {
return '分享小程序时,miniProgram必填'
}
},
corpid(value, params) {
if (params.openCustomerServiceChat && !value) {
return `使用打开客服功能时 corpid 必填`
}
},
customerUrl(value, params) {
if (params.openCustomerServiceChat && !value) {
return `使用打开客服功能时 customerUrl 必填`
}
},
},
}
export const ShareProtocols: ApiProtocol<API_TYPE_SHARE> = {
......
......@@ -40,7 +40,7 @@ const TYPES = {
},
}
const parseParams = <T extends Data>(args: UniApp.ShareOptions): T | string => {
const parseParams = (args: UniApp.ShareOptions) => {
args.type = args.type || 0
let {
......@@ -53,6 +53,9 @@ const parseParams = <T extends Data>(args: UniApp.ShareOptions): T | string => {
mediaUrl: media,
scene,
miniProgram,
openCustomerServiceChat,
corpid,
customerUrl: url,
} = args
if (typeof imageUrl === 'string' && imageUrl) {
......@@ -74,55 +77,66 @@ const parseParams = <T extends Data>(args: UniApp.ShareOptions): T | string => {
extra: {
scene,
},
openCustomerServiceChat,
corpid,
url,
}
if (provider === 'weixin' && (type === 1 || type === 2)) {
delete sendMsg.thumbs
}
return sendMsg as unknown as T
return sendMsg
}
return '分享参数 type 不正确'
}
type ParseParams = ReturnType<typeof parseParams>
const sendShareMsg = function (
service: PlusShareShareService,
params: Data,
params: Exclude<ParseParams, string>,
resolve: (args?: any) => void,
reject: (errMsg: string, errRes?: any) => void,
method = 'share'
) {
const errorCallback = warpPlusErrorCallback(reject)
service.send(
params,
() => {
resolve()
},
errorCallback
)
const serviceMethod = params.openCustomerServiceChat
? 'openCustomerServiceChat'
: 'send'
try {
// @ts-expect-error openCustomerServiceChat
service[serviceMethod](
params,
() => {
resolve()
},
errorCallback
)
} catch (error) {
errorCallback({
message: `${params.provider} ${serviceMethod} 方法调用失败`,
})
}
}
export const share = defineAsyncApi<API_TYPE_SHARE>(
API_SHREA,
(params, { resolve, reject }) => {
const res = parseParams<typeof params>(params)
const parsedParams = parseParams(params)
const errorCallback = warpPlusErrorCallback(reject)
if (typeof res === 'string') {
return reject(res)
} else {
params = res
if (typeof parsedParams === 'string') {
return reject(parsedParams)
}
plus.share.getServices((services) => {
const service = services.find(({ id }) => id === params.provider)
const service = services.find(({ id }) => id === parsedParams.provider)
if (!service) {
reject('service not found')
} else {
if (service.authenticated) {
sendShareMsg(service, params, resolve, reject)
sendShareMsg(service, parsedParams, resolve, reject)
} else {
service.authorize(
() => sendShareMsg(service, params, resolve, reject),
() => sendShareMsg(service, parsedParams, resolve, reject),
errorCallback
)
}
......
......@@ -4,7 +4,7 @@ importers:
.:
specifiers:
'@dcloudio/types': ^2.5.17
'@dcloudio/types': 2.5.18
'@dcloudio/uni-api': 3.0.0-alpha-3040120220308001
'@dcloudio/uni-app': 3.0.0-alpha-3040120220308001
'@jest/types': ^27.0.2
......@@ -51,7 +51,7 @@ importers:
vue-router: ^4.0.13
yorkie: ^2.0.0
devDependencies:
'@dcloudio/types': 2.5.17
'@dcloudio/types': 2.5.18
'@dcloudio/uni-api': link:packages/uni-api
'@dcloudio/uni-app': link:packages/uni-app
'@jest/types': 27.4.2
......@@ -2480,8 +2480,8 @@ packages:
lodash.once: 4.1.1
dev: true
/@dcloudio/types/2.5.17:
resolution: {integrity: sha512-PMxXWdFOAmDQUo7BNx19O4htfaztdOT7RuD5jTVm8/jFcUd/hkNJJXLcSLePqXZwyTjjirstLBhC3FU709Y+8Q==}
/@dcloudio/types/2.5.18:
resolution: {integrity: sha512-1EMwZsi5j9KHI5wdLwZJmUyo5b8HnG60CRqsqW5kcd3cgSrKtgL3EKCYUjmJw+3gd9G/4NeEkSx5pT5l+4rNXg==}
dev: true
/@dcloudio/uni-cli-i18n/2.0.0-alpha-33020211130001:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册