// js文件
<script>
//接口请求配置
// 获取用户信息
// export const getUserInfo = () => {
// return request({
// url: `${globalConfig.nginxUrl}/gateway/gp-auth-center/rest/v1/user/userInfo`,
// method: 'get'
// })
// };
// // 获取udesk相关客户信息
// export function getUDeskByInfo(data) {
// return request({
// url: `${globalConfig.nginxUrl}/gateway/gp-auth-center/udesk/getCustomerInformation`,
// method: 'get'
// })
// }
// // 获取udesk配置
// export function getUdeskConfig(data) {
// return request({
// url: `${globalConfig.nginxUrl}/gateway/gp-auth-center/udesk/getUdeskConfig`,
// method: 'get',
// params: data
// })
// }
//vue文件的js部分
import { getUserInfo,getUDeskByInfo,getUdeskConfig } from '@/api/user';
export default {
data() {
},
created() {
},
mounted() {
this.OpenOnlineCustomerService()
},
methods: {
// 在线客服
OpenOnlineCustomerService() {
console.log("在线客服")
getUserInfo().then((res) => {
var data = {
userType: res.data.data.userTypeNow // userTypeNow为登录用户的类型
}
getUdeskConfig(data).then(res => {
// 开关开启时启用
const udeskConfig = res.data.data[Object.keys(res.data.data)[0]]
if(udeskConfig.switch && udeskConfig.plugInCode) {
var udeskCode = udeskConfig.plugInCode.replace(/[\r\n]/g,"") // 去除换行
// var reg = RegExp(/});<\/script>/);
if (udeskCode.indexOf("});<\/script>") != -1 ) {
var udeskScript = udeskCode.slice(8,-12)
getUDeskByInfo().then((res) => {
var customer = JSON.stringify(res.data.data)
if(document.getElementById("onlineCustomerService")){
var element = document.getElementById("onlineCustomerService")
element.parentNode.removeChild(element);
}
//赋值到页面
var onlineCustomerService = document.createElement('script')
onlineCustomerService.id = 'onlineCustomerService'
onlineCustomerService.text = `${udeskScript},"customer":${customer}});`
console.log(`${udeskScript},"customer":${customer}`)
window.document.body.appendChild(onlineCustomerService)
})
} else {
this.$message({ type: 'error', showClose: true, message: '智能客服配置错误!' })
}
} else {
console.log("禁用或无配置")
}
})
})
},
}
}
</script>