多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

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