Commit d48b10fd by lijiabin

【需求 22261】 refactor: 重构关于初始化SDK相关代码

parent 0810b91c
...@@ -22,9 +22,7 @@ const locale = ref(zhCn) ...@@ -22,9 +22,7 @@ const locale = ref(zhCn)
onMounted(() => { onMounted(() => {
// console.table(__CORE_LIB_VERSION__) // console.table(__CORE_LIB_VERSION__)
if (import.meta.env.MODE === 'production') { if (import.meta.env.MODE === 'production') {
setTimeout(() => { initWxConfig()
initWxConfig()
}, 3000)
} }
}) })
</script> </script>
export * from './wxLogin' export * from './wxLogin'
export * from './initWXConfig' export * from './initWXConfig'
import * as ww from '@wecom/jssdk' import * as ww from '@wecom/jssdk'
import { initWxConfig } from './initWXConfig'
const DEFAULT_SHARE_TITLE = '企业文化' const DEFAULT_SHARE_TITLE = '企业文化'
const DEFAULT_SHARE_DESC = '点击查看详情' const DEFAULT_SHARE_DESC = '点击查看详情'
...@@ -14,15 +15,41 @@ interface IShareWxOption { ...@@ -14,15 +15,41 @@ interface IShareWxOption {
imgUrl?: string imgUrl?: string
} }
export async function wxShare(option: IShareWxOption) { interface IShareWxResult {
await ww.shareAppMessage({ err_msg?: string
title: option.title || DEFAULT_SHARE_TITLE, errMsg?: string
desc: option.desc || DEFAULT_SHARE_DESC, }
link: option.link || location.href,
imgUrl: option.imgUrl || DEFAULT_SHARE_IMAGE, export async function wxShare(option: IShareWxOption): Promise<IShareWxResult> {
await initWxConfig()
return new Promise((resolve, reject) => {
ww.invoke(
'shareAppMessage',
{
title: option.title || DEFAULT_SHARE_TITLE,
desc: option.desc || DEFAULT_SHARE_DESC,
link: option.link || location.href,
imgUrl: option.imgUrl || DEFAULT_SHARE_IMAGE,
},
function (res: IShareWxResult) {
const errMsg = res.err_msg || res.errMsg
if (errMsg === 'shareAppMessage:ok' || errMsg === 'openExistedChatWithMsg:ok') {
resolve(res)
} else {
reject(res)
}
},
)
}) })
} }
export function isWxShareCancel(error: unknown) {
const err = error as { errMsg?: string; err_msg?: string; message?: string }
return [err.errMsg, err.err_msg, err.message].some((msg) =>
msg?.includes('shareAppMessage:cancel'),
)
}
interface ISelectDepOrUser { interface ISelectDepOrUser {
err_msg: string err_msg: string
result: IResult result: IResult
...@@ -44,7 +71,8 @@ export interface ISelectDept { ...@@ -44,7 +71,8 @@ export interface ISelectDept {
name: string name: string
} }
export function selectDepOrUser(wxOption = {}): Promise<IResult> { export async function selectDepOrUser(wxOption = {}): Promise<IResult> {
await initWxConfig()
const defaultOption = { const defaultOption = {
fromDepartmentId: -1, fromDepartmentId: -1,
mode: 'single', mode: 'single',
......
/* 企业微信js配置 */
//注意:如果要在页面调用企业微信内部方法,请现在下面的jsApiList数组中添加方法
import { getWxSignature } from '@/api' import { getWxSignature } from '@/api'
import * as ww from '@wecom/jssdk' import * as ww from '@wecom/jssdk'
// export async function initWxConfig() { let wxConfigPromise: Promise<void> | null = null
// const url = location.href.split('#')[0]
// const response = await getWxSignature(url) export function initWxConfig() {
// const timestamp = response.data.timestamp //时间戳 if (wxConfigPromise) return wxConfigPromise
// const nonceStr = response.data.nonceStr //随机字符串
// const signature = response.data.signature //签名 wxConfigPromise = new Promise((resolve, reject) => {
// const appId = response.data.appId //企业id ;(async () => {
// wx.config({ try {
// beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题 const url = location.href.split('#')[0]
// debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 const { data } = await getWxSignature(url!)
// appId: appId, // 必填,企业微信的corpID let resolved = false
// timestamp: timestamp, // 必填,生成签名的时间戳 const timer = window.setTimeout(() => {
// nonceStr: nonceStr, // 必填,生成签名的随机串 if (!resolved) {
// signature: signature, // 必填,签名,见附录1 resolved = true
// jsApiList: [ console.warn('企业微信 SDK 初始化未返回成功回调,继续执行后续操作')
// // 所有要调用的 API 都要加到这个列表中 resolve()
// 'shareAppMessage', }
// 'selectEnterpriseContact', }, 3000)
// ],
// // success: function (result) { const handleFail = (error: unknown) => {
// // // 回调 window.clearTimeout(timer)
// // } wxConfigPromise = null
// }) reject(error)
// } }
export async function initWxConfig() {
const url = location.href.split('#')[0] const handleConfigSuccess = () => {
const response = await getWxSignature(url!) if (!resolved) {
const timestamp = response.data.timestamp //时间戳 resolved = true
const nonceStr = response.data.nonceStr //随机字符串 window.clearTimeout(timer)
const signature = response.data.signature //签名 resolve()
// const appId = response.data.appId //企业id }
const corpId = response.data.corpid //企业id }
const agentId = response.data.agentid //应用id
ww.register({ const signature = {
corpId, // 必填,当前用户企业所属企业ID timestamp: data.timestamp,
agentId, // 必填,当前应用的AgentID nonceStr: data.nonceStr,
jsApiList: ['shareAppMessage', 'selectEnterpriseContact'], // 必填,需要使用的JSAPI列表 signature: data.signature,
getConfigSignature: function () { }
return {
timestamp: timestamp, ww.register({
nonceStr: nonceStr, corpId: data.corpid,
signature: signature, agentId: data.agentid,
} jsApiList: ['shareAppMessage', 'selectEnterpriseContact'],
}, // 必填,根据url生成企业签名的回调函数 getConfigSignature() {
getAgentConfigSignature: function () { return signature
return { },
timestamp: timestamp, getAgentConfigSignature() {
nonceStr: nonceStr, return signature
signature: signature, },
onConfigSuccess: handleConfigSuccess,
onAgentConfigSuccess: handleConfigSuccess,
onConfigFail: handleFail,
onAgentConfigFail(error) {
console.warn(error)
},
})
} catch (error) {
console.error(error)
wxConfigPromise = null
reject(error)
} }
}, // 必填,根据url生成应用签名的回调函数 })()
}) })
return wxConfigPromise
} }
...@@ -36,7 +36,7 @@ import type { Component } from 'vue' ...@@ -36,7 +36,7 @@ import type { Component } from 'vue'
import { useScrollTop } from '@/hooks' import { useScrollTop } from '@/hooks'
import { addOrCanceArticlelCollect, addOrCanceArticlelLike } from '@/api' import { addOrCanceArticlelCollect, addOrCanceArticlelLike } from '@/api'
import { push } from 'notivue' import { push } from 'notivue'
import { wxShare } from '@/utils/wxUtil' import { isWxShareCancel, wxShare } from '@/utils/wxUtil'
const modelValue = defineModel<ArticleItemDto>('modelValue', { required: true }) const modelValue = defineModel<ArticleItemDto>('modelValue', { required: true })
...@@ -118,8 +118,13 @@ const stats = computed(() => { ...@@ -118,8 +118,13 @@ const stats = computed(() => {
}) })
push.success('分享成功') push.success('分享成功')
} catch (error) { } catch (error) {
if (isWxShareCancel(error)) {
console.warn(error)
push.warning('取消分享')
return
}
console.error(error) console.error(error)
push.error('分享失败') push.error('分享失败,请刷新页面重新分享')
} }
}, },
}, },
......
...@@ -470,7 +470,7 @@ import { storeToRefs } from 'pinia' ...@@ -470,7 +470,7 @@ import { storeToRefs } from 'pinia'
import { useNavigation, useScrollTop } from '@/hooks' import { useNavigation, useScrollTop } from '@/hooks'
import { useMessageBox } from '@/hooks' import { useMessageBox } from '@/hooks'
import { parseEmoji } from '@/utils/emoji' import { parseEmoji } from '@/utils/emoji'
import { wxShare } from '@/utils/wxUtil' import { isWxShareCancel, wxShare } from '@/utils/wxUtil'
import { ArticleTypeEnum, BooleanFlag } from '@/constants' import { ArticleTypeEnum, BooleanFlag } from '@/constants'
import { push } from 'notivue' import { push } from 'notivue'
import { CommentSortTypeEnum } from '@/constants' import { CommentSortTypeEnum } from '@/constants'
...@@ -587,6 +587,11 @@ const handleShareQuestion = async () => { ...@@ -587,6 +587,11 @@ const handleShareQuestion = async () => {
}) })
push.success('分享成功') push.success('分享成功')
} catch (error) { } catch (error) {
if (isWxShareCancel(error)) {
console.warn(error)
push.warning('取消分享')
return
}
console.error(error) console.error(error)
push.error('分享失败') push.error('分享失败')
} }
......
...@@ -356,7 +356,7 @@ import ActionMore from '@/components/common/ActionMore/index.vue' ...@@ -356,7 +356,7 @@ import ActionMore from '@/components/common/ActionMore/index.vue'
import SendMessageDialog from '@/components/common/SendMessageDialog/index.vue' import SendMessageDialog from '@/components/common/SendMessageDialog/index.vue'
import BackButton from '@/components/common/BackButton/index.vue' import BackButton from '@/components/common/BackButton/index.vue'
import { useNavigation } from '@/hooks' import { useNavigation } from '@/hooks'
import { wxShare } from '@/utils/wxUtil' import { isWxShareCancel, wxShare } from '@/utils/wxUtil'
import { import {
ArticleTypeEnum, ArticleTypeEnum,
BooleanFlag, BooleanFlag,
...@@ -564,6 +564,11 @@ const handleShareVideo = async () => { ...@@ -564,6 +564,11 @@ const handleShareVideo = async () => {
}) })
push.success('分享成功') push.success('分享成功')
} catch (error) { } catch (error) {
if (isWxShareCancel(error)) {
console.warn(error)
push.warning('取消分享')
return
}
console.error(error) console.error(error)
push.error('分享失败') push.error('分享失败')
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment