hooks.js 5.5 KB

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