app.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { CHANGE_IS_IN_MINIPROGRAM, CHANGE_SYSTERM_INFO, CHANGE_SYSTERM_TERMINAL } from './type'
  2. import { isInWx } from '../../utils'
  3. import { getCustomerServiceAppletKfApi, getCustomerServiceH5KfApi, getCustomerServicePCKfApi, getAllCustomerServiceApi } from '../../api/anotherTFInterface'
  4. export default {
  5. namespaced: true,
  6. state() {
  7. return {
  8. isInMiniProgram: false, // 是否套壳的
  9. systermInfo: {},
  10. terminal: 0,
  11. platformOperationShopId: 186
  12. }
  13. },
  14. mutations: {
  15. [CHANGE_IS_IN_MINIPROGRAM](state, tag) {
  16. if (tag) {
  17. state.isInMiniProgram = tag
  18. }
  19. },
  20. [CHANGE_SYSTERM_INFO](state, system) {
  21. state.systermInfo = system
  22. console.log(system)
  23. },
  24. [CHANGE_SYSTERM_TERMINAL](state, terminal) {
  25. state.terminal = terminal
  26. }
  27. },
  28. actions: {
  29. getUserSystermInfo({ commit }) {
  30. return new Promise((resolve, reject) => {
  31. uni.getSystemInfo({
  32. success: (systermInfo) => {
  33. commit(CHANGE_SYSTERM_INFO, systermInfo)
  34. resolve()
  35. },
  36. fail: () => {
  37. commit(CHANGE_SYSTERM_INFO, {})
  38. resolve()
  39. }
  40. })
  41. })
  42. },
  43. getSystermTerminal({ commit }) {
  44. return new Promise((resolve, reject) => {
  45. if (isInWx()) {
  46. commit(CHANGE_SYSTERM_TERMINAL, 3)
  47. } else {
  48. // #ifdef H5
  49. commit(CHANGE_SYSTERM_TERMINAL, 5) // H5包含pc和移动端浏览器和微信浏览器的可能
  50. // #endif
  51. // #ifdef APP
  52. commit(CHANGE_SYSTERM_TERMINAL, 1)
  53. // #endif
  54. // #ifdef MP-WEIXIN
  55. commit(CHANGE_SYSTERM_TERMINAL, 2)
  56. // #endif
  57. // #ifdef MP-ALIPAY
  58. commit(CHANGE_SYSTERM_TERMINAL, 4)
  59. // #endif
  60. }
  61. })
  62. },
  63. getCustomerServiceAction({ state, dispatch, commit }, { isToService, shopId }) {
  64. return new Promise((resolve, reject) => {
  65. uni.showLoading()
  66. getAllCustomerServiceApi({ shopId })
  67. .then((res) => {
  68. uni.hideLoading()
  69. if (isToService) {
  70. if (res.data.length) {
  71. uni.showActionSheet({
  72. title: '* 请选择客服 *',
  73. itemList: res.data.map((item) => `${item.name}(${item.state ? '在线' : '已下线'})`),
  74. itemColor: '#2c3e50',
  75. success: (res) => {
  76. dispatch('flyToServiceAction', { shopId, customerId: res.data[res.tapIndex].kfId })
  77. }
  78. })
  79. } else {
  80. uni.showToast({
  81. icon: 'none',
  82. title: '暂无客服~'
  83. })
  84. }
  85. }
  86. resolve(res)
  87. })
  88. .catch((err) => {
  89. uni.hideLoading()
  90. reject(err)
  91. })
  92. })
  93. },
  94. flyToServiceAction({ state, dispatch, commit }, { shopId, customerId }) {
  95. let _url = null
  96. if ([1, 2, 4].includes(state.terminal)) {
  97. _url = getCustomerServiceAppletKfApi
  98. } else if ([ 3 ].includes(state.terminal)) {
  99. _url = getCustomerServiceH5KfApi
  100. } else if ([ 5 ].includes(state.terminal)) {
  101. _url = getCustomerServicePCKfApi
  102. }
  103. let corpId = null
  104. let serviceURL = null
  105. uni.showLoading({
  106. title: '加载中...'
  107. })
  108. _url({ id: shopId, customerId })
  109. .then((res) => {
  110. if (res.code === '' && res.data.corpId && res.data.url) {
  111. corpId = res.data.corpId
  112. serviceURL = res.data.url
  113. }
  114. uni.hideLoading()
  115. if (!serviceURL || !corpId) {
  116. return uni.showToast({
  117. icon: 'none',
  118. title: '暂无客服~'
  119. })
  120. }
  121. // #ifdef MP-WEIXIN
  122. wx.openCustomerServiceChat({
  123. extInfo: { url: serviceURL },
  124. corpId
  125. })
  126. // #endif
  127. // #ifdef APP
  128. try {
  129. let wechatServices = null
  130. plus.share.getServices((res) => {
  131. wechatServices = res.find((wechatItem) => wechatItem.id === 'weixin')
  132. if (wechatServices) {
  133. wechatServices.openCustomerServiceChat({
  134. corpid: corpId,
  135. url: serviceURL
  136. }, (success) => { }, (err) => { })
  137. } else {
  138. plus.nativeUI.alert('当前环境不支持微信操作!')
  139. }
  140. }, (err) => {
  141. uni.showToast({ title: '获取服务失败,不支持该操作。' + JSON.stringify(err), icon: 'none' })
  142. })
  143. } catch (err) {
  144. uni.showToast({ title: '调用失败,不支持该操作。' + JSON.stringify(err), icon: 'none' })
  145. }
  146. // #endif
  147. // #ifdef H5
  148. // window.open(serviceURL) safari浏览器不支持window.open
  149. window.location.href = serviceURL
  150. // #endif
  151. })
  152. .catch((e) => {
  153. uni.hideLoading()
  154. })
  155. }
  156. }
  157. }