hooks.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { computed, nextTick, reactive, ref, watch } from 'vue'
  2. import {
  3. requestBehaviorClues,
  4. requestGetStaticInfo,
  5. requestRetainedCapital,
  6. requestSubmitLeaveInfo
  7. } from '../api/api'
  8. import { getSourceInfo } from './utils'
  9. import { showToast } from './toast'
  10. // pc静态弹窗卡片信息获取
  11. export function useStaticCustomInfo() {
  12. const configInfo = reactive({
  13. name: '',
  14. remark: '',
  15. wxer: '',
  16. phone: '',
  17. })
  18. // 获取静态信息
  19. useStaticInfoRequest().then((r) => {
  20. if (r) {
  21. Object.assign(configInfo, r)
  22. }
  23. })
  24. return {
  25. configInfo
  26. }
  27. }
  28. // 线索弹窗卡片信息获取
  29. export function usePreLeaveInfo(options = {}) {
  30. const { props } = options
  31. const visible = ref(false)
  32. const configInfo = reactive({
  33. name: '',
  34. remark: '',
  35. wxer: '',
  36. phone: '', // 客服手机号
  37. userPhone: '', // 用户留资手机号
  38. isCurrentPhone: true,
  39. })
  40. const source = computed(() => props.source)
  41. const sourceDesc = computed(() => props.sourceDesc)
  42. const staticInfo = computed(() => props.staticInfo)
  43. function updateVisible(e) {
  44. visible.value = e
  45. if (props.onPopupVisibleChange && typeof props.onPopupVisibleChange === 'function') {
  46. props.onPopupVisibleChange(e)
  47. }
  48. if (e) {
  49. createClueWhenOpen()
  50. }
  51. }
  52. function close() {
  53. updateVisible(false)
  54. }
  55. watch(() => source.value, async (val) => {
  56. if (source) {
  57. const r = await useLeaveInfoRequest({ source: val })
  58. if (r.info) {
  59. configInfo.userPhone = r.info.phone
  60. configInfo.isCurrentPhone = r.info.isCurrentPhone
  61. }
  62. }
  63. })
  64. async function createClueWhenOpen() {
  65. if (staticInfo.value) {
  66. // 获取静态信息
  67. const r = await useStaticInfoRequest()
  68. if (r) {
  69. Object.assign(configInfo, r)
  70. }
  71. }
  72. else {
  73. // 获取动态分配信息
  74. const r = await useBehaviorCluesRequest(sourceDesc.value)
  75. if (r) {
  76. Object.assign(configInfo, r)
  77. }
  78. }
  79. }
  80. return {
  81. updateVisible,
  82. close,
  83. visible,
  84. configInfo
  85. }
  86. }
  87. // 与我联系-判断逻辑
  88. export function useContactMeLogic(options = {}) {
  89. const { configInfo, props } = options
  90. const userPhone = ref(configInfo?.userPhone || '')
  91. const source = computed(() => props.source)
  92. const isLoginPhone = computed(() => configInfo.isCurrentPhone || false)
  93. watch(() => configInfo?.userPhone, (val) => {
  94. userPhone.value = val
  95. })
  96. const changePhoneDialog = reactive({
  97. show: false,
  98. phoneNumber: '',
  99. })
  100. const successCheckDialog = reactive({
  101. show: false,
  102. title: '提交成功',
  103. confirmButtonText: '我知道了',
  104. cancelButtonText: '',
  105. contentText: '我们的专属客服会在24小时内尽快与您联系,请注意电话接听。'
  106. })
  107. const confirmContentText = computed(() => {
  108. return `专属客服将致电 <span class="highlight-text">${userPhone.value}</span>,<br />请核对联系电话是否准确`
  109. })
  110. const confirmPhoneDialog = reactive({
  111. show: false,
  112. title: '联系电话确认',
  113. confirmButtonText: '准确,我要提交',
  114. cancelButtonText: '有误,我要修改',
  115. contentText: confirmContentText,
  116. })
  117. function closeAllDialog() {
  118. changePhoneDialog.show = false
  119. confirmPhoneDialog.show = false
  120. successCheckDialog.show = false
  121. }
  122. function showSuccessDialog() {
  123. changePhoneDialog.show = false
  124. confirmPhoneDialog.show = false
  125. successCheckDialog.show = true
  126. }
  127. const dialogEvents = {
  128. confirmPhoneConfirm() {
  129. doConfirmLeave()
  130. },
  131. confirmPhoneCancel() {
  132. confirmPhoneDialog.show = false
  133. changePhoneDialog.show = true
  134. },
  135. successConfirm() {
  136. successCheckDialog.show = false
  137. },
  138. async successClose() {
  139. closeAllDialog()
  140. },
  141. phoneChangeConfirm() {
  142. doConfirmLeave()
  143. },
  144. phoneChangeCancel() {
  145. confirmPhoneDialog.show = true
  146. changePhoneDialog.show = false
  147. },
  148. }
  149. function checkPhonePass(phone) {
  150. const reg = /^1[3-9]\d{9}$/
  151. return reg.test(phone)
  152. }
  153. const actualPhone = computed(() => {
  154. if (changePhoneDialog.show) {
  155. return changePhoneDialog.phoneNumber
  156. }
  157. else {
  158. return userPhone.value
  159. }
  160. })
  161. async function doConfirmLeave() {
  162. const phonePass = checkPhonePass(actualPhone.value)
  163. if (!phonePass) {
  164. return showToast('联系电话格式不正确')
  165. }
  166. const payload = {
  167. source: source.value,
  168. phone: actualPhone.value,
  169. source_desc: '',
  170. }
  171. const info = getSourceInfo(payload.source)
  172. if (info.desc) {
  173. payload.source_desc = info.desc
  174. }
  175. try {
  176. const res = await requestSubmitLeaveInfo(payload)
  177. if (res && res.data) {
  178. showSuccessDialog()
  179. }
  180. }
  181. catch (error) {
  182. console.log(error)
  183. }
  184. }
  185. function contactMe() {
  186. if (isLoginPhone.value) {
  187. doConfirmLeave()
  188. }
  189. else {
  190. confirmPhoneDialog.show = true
  191. }
  192. }
  193. return {
  194. userPhone,
  195. changePhoneDialog,
  196. successCheckDialog,
  197. confirmPhoneDialog,
  198. closeAllDialog,
  199. showSuccessDialog,
  200. checkPhonePass,
  201. dialogEvents,
  202. contactMe
  203. }
  204. }
  205. export async function useBehaviorCluesRequest(value) {
  206. const res = await requestBehaviorClues({ source_desc: value })
  207. const { error_code: code, data } = res
  208. if (code === 0 && data) {
  209. return data
  210. }
  211. }
  212. export async function useLeaveInfoRequest(options = {}) {
  213. const { source } = options
  214. const res = await requestRetainedCapital({ source })
  215. const { error_code: code } = res
  216. if (code === 0) {
  217. return res
  218. }
  219. }
  220. export async function useStaticInfoRequest() {
  221. const res = await requestGetStaticInfo()
  222. const { error_code: code, data } = res
  223. if (code === 0 && data) {
  224. return data
  225. }
  226. }