Blame view

ceres-uniapp-master/utils/services.js 2.92 KB
3f535f30   杨鑫   '初始'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  const NET = require("./request");
  const API = require("../config/api");
  
  /**
   * @FileDescription:客服相关函数
   * @Author: kahu
   * @Date: 2022/11/4
   * @LastEditors: kahu
   * @LastEditTime: 2022/11/4
   */
  
  export function Services(shopId) {
      let corpId = null, serviceURL = null
  
      const getServiceUrl = async () => {
          uni.showLoading({
              title:'加载中...'
          })
          const shopIds = uni.getStorageSync('service_shopids') || []
          const corpIds = uni.getStorageSync('service_corpIds') || []
          const urls = uni.getStorageSync('service_urls') || []
          try {
              const res = await NET.request(API.CustomerService, {}, 'get')
              if (res.code === '' && res.data.corpId && res.data.url) {
                  shopIds.push(shopId)
                  corpIds.push(res.data.corpId)
                  urls.push(res.data.url)
                  uni.setStorageSync('service_shopids', shopIds);
                  uni.setStorageSync('service_corpIds', corpIds);
                  uni.setStorageSync('service_urls', urls);
                  corpId = res.data.corpId
                  serviceURL = res.data.url
              }
          } finally {
              uni.hideLoading()
          }
      }
  
      const flyToService = ()=>{
          if (!serviceURL || !corpId) {
              return uni.showToast({
                  icon:'none',
                  title:'暂无客服~'
              })
          }
          // #ifdef MP-WEIXIN
          wx.openCustomerServiceChat({
              extInfo: {
                  url: serviceURL
              },
              corpId: corpId
          })
          // #endif
          // #ifdef APP-PLUS
          try {
              let wechatServices = null
              plus.share.getServices(res => {
                  wechatServices = res.find(wechatItem => wechatItem.id === 'weixin')
                  if (wechatServices) {
                      wechatServices.openCustomerServiceChat({
                          corpid: corpId,
                          url: serviceURL,
                      }, success => {
                          console.log("success", JSON.stringify(success))
                      }, err => {
                          console.log("error", JSON.stringify(err))
                      })
                  } else {
                      plus.nativeUI.alert('当前环境不支持微信操作!')
                  }
              },  err=>{
                  console.log(err)
                  uni.showToast({title: "获取服务失败,不支持该操作。" + JSON.stringify(err), icon: 'none'})
              })
          } catch (err) {
              console.log(err)
              uni.showToast({title: "调用失败,不支持该操作。" + JSON.stringify(err), icon: 'none'})
          }
          // #endif
          // #ifdef H5
          // window.open(serviceURL) safari浏览器不支持window.open
          window.location.href = serviceURL
          // #endif
      }
  
  
      return  getServiceUrl().then(res=>{
          return {
              flyToService
          }
      })
  
  }