Commit d48b10fd by lijiabin

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

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