welcomePackRegistration.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import {
  2. createGuestDec,
  3. fetchBrandAmbassadorInfo,
  4. fetchGuestDec,
  5. fetchGuestFullInfo,
  6. fetchGuestInspectNetwork
  7. } from '@/api/user'
  8. import tool from '@/utils/tool'
  9. import usersInfo from '@/utils/usersInfo'
  10. // 注册页的私有业务实现源自旧 Welcome Pack;保留接口字段与计价规则,避免引入旧页全局样式。
  11. export default {
  12. data() {
  13. return {
  14. loading: false,
  15. draft: {},
  16. // 公开注册页使用固定选项,不依赖后台国家、语言列表请求或缓存。
  17. countriesList: [
  18. {
  19. ID: '620699181539139625',
  20. NAME: 'Nigeria',
  21. DEFAULT_LANGUAGE_ID: '620713695651307520'
  22. }
  23. ],
  24. languageList: [
  25. { ID: '620713695718416384', NAME: '中文' },
  26. { ID: '620713695651307520', NAME: 'English' }
  27. ],
  28. allGoods: [],
  29. payList: [],
  30. multipleSelection: [],
  31. sellPriceSum: 0.0,
  32. pricePvSum: 0.0,
  33. taxSum: 0.0,
  34. storeNums: [],
  35. tableData: [],
  36. payAmount: 0.0,
  37. submitButtonStat: false,
  38. visible: false,
  39. paymentComplete: false,
  40. paymentOrder: {
  41. orderSn: '', amount: 0, currency: '', reference: '', accessCode: ''
  42. },
  43. userBalance: {
  44. cash: 0
  45. },
  46. visibleProduct: false,
  47. product: {
  48. GOODS_NAME: '',
  49. GOODS_NO: '',
  50. COVER: '',
  51. SELL_PRICE: 0,
  52. PRICE_PV: 0,
  53. TAX_RATE: 0,
  54. taxAmount: 0
  55. },
  56. decWay: '2',
  57. regionData: [],
  58. form: {
  59. countryId: '',
  60. languageId: '',
  61. decType: 'normal',
  62. realName: '',
  63. decLv: '',
  64. insertUserName: '',
  65. decUserName: '',
  66. recUserName: '',
  67. conUserName: '',
  68. insertUserIdCard: '',
  69. consignee: '',
  70. acceptMobile: '',
  71. areaSelected: [],
  72. address: '',
  73. openBank: '',
  74. bankAddress: '',
  75. bankNo: '',
  76. bankAreaSelected: [],
  77. password: '111111',
  78. payPassword: '111111',
  79. mobile: '',
  80. email: '',
  81. packageId: '',
  82. goodsId: [],
  83. goodsNum: [],
  84. cityName: '',
  85. lgaName: '',
  86. way: 'express',
  87. payType: '',
  88. autoPlace: ''
  89. },
  90. conRealName: '-',
  91. recRealName: '-',
  92. areaDisabled: false,
  93. addressDisabled: false,
  94. userReadOnly: true,
  95. allDecLevel: [],
  96. allOpenBank: [],
  97. isDec: null,
  98. currencyUnitCode: ''
  99. }
  100. },
  101. computed: {
  102. submitLabel() {
  103. if (this.paymentComplete) return 'Payment submitted'
  104. return this.paymentOrder.accessCode ? 'Continue Payment' : 'Save'
  105. }
  106. },
  107. created() {
  108. // 邀请链接只提供初始值,不覆盖用户后续编辑,也不触发推荐人变更时的清空逻辑。
  109. const ref = this.$route && this.$route.query.ref
  110. if (typeof ref === 'string') {
  111. this.form.recUserName = ref.trim()
  112. this.form.conUserName = ref.trim()
  113. }
  114. },
  115. watch: {
  116. isDec: {
  117. handler(newValue) {
  118. if (String(newValue) === '1') {
  119. this.form.decUserName = usersInfo.userName()
  120. }
  121. }
  122. }
  123. },
  124. methods: {
  125. countriesChange() {
  126. const country = this.countriesList.find(item => item.ID === this.form.countryId)
  127. if (country) this.form.languageId = country.DEFAULT_LANGUAGE_ID
  128. this.fetchWelcomePack()
  129. },
  130. showLoadError(error) {
  131. this.$message.error(String(error.message || error))
  132. },
  133. // 游客报单初始化;临时标识只保存在本页,不写入会员登录缓存。
  134. fetchWelcomePack() {
  135. this.loading = true
  136. this.draft = {}
  137. this.form.insertUserName = ''
  138. const countryId = this.form.countryId
  139. fetchGuestDec({ countryId })
  140. .then((response) => {
  141. if (countryId !== this.form.countryId) return
  142. this.draft = response.data.draft || {}
  143. this.form.insertUserName = this.draft.insertUserName
  144. this.allOpenBank = response.data.allOpenBank || []
  145. this.allGoods = response.data.allGoods
  146. this.userBalance = response.data.userBalance || { cash: 0 }
  147. this.regionData = response.data.region
  148. this.allDecLevel = response.data.decLevel
  149. this.currencyUnitCode = this.draft.currency || ''
  150. if (!this.currencyUnitCode && response.data.allGoods && response.data.allGoods.length > 0) {
  151. this.currencyUnitCode = response.data.allGoods[0].CODE
  152. }
  153. // 是否是报单中心
  154. this.isDec = response.data.isDec
  155. const settingObj = this.allGoods
  156. for (const i in this.allGoods) {
  157. this.storeNums[i] = 1
  158. settingObj[i].goodsNum = 0
  159. }
  160. this.tableData = Object.values(settingObj)
  161. // 切换国家时清空选择与汇总,保持旧表格替换数据时的行为。
  162. this.multipleSelection = []
  163. this.handleSureChange()
  164. // 支付方式
  165. this.payList = response.data.payList || response.data.sellType[0]['sell_type']
  166. // 延续默认选中第二项的规则;单项或空列表时回退,避免读取不存在的选项。
  167. this.form.payType =
  168. (Object.values(this.payList)[1] || Object.values(this.payList)[0] || {}).label || ''
  169. setTimeout(() => {
  170. this.loading = false
  171. }, 0.5 * 1000)
  172. })
  173. .catch((err) => {
  174. if (countryId !== this.form.countryId) return
  175. this.showLoadError(err)
  176. this.loading = false
  177. })
  178. },
  179. // 商品详情
  180. handleProduct(row) {
  181. this.product = row
  182. this.product.taxAmount = tool.calculateTax(row.MARKET_PRICE, row.TAX_RATE)
  183. this.visibleProduct = true
  184. },
  185. // 选择商品计数
  186. handleInputNumber(current, row) {
  187. const pageList = this.multipleSelection
  188. let selectStatus = false
  189. for (const i in pageList) {
  190. if (pageList[i].ID === row.ID) {
  191. pageList[i].goodsNum = current
  192. selectStatus = true
  193. break
  194. }
  195. }
  196. if (selectStatus) {
  197. this.multipleSelection = pageList
  198. this.handleSureChange()
  199. }
  200. },
  201. // 汇总继续按市场价计费;销售价仅用于展示,税额使用原含税算法。
  202. handleSureChange() {
  203. if (this.multipleSelection.length > 0) {
  204. let accumulatorSellPrice = 0
  205. let accumulatorPricePv = 0
  206. let accumulatorTax = 0
  207. this.multipleSelection.forEach((accumulator) => {
  208. accumulatorSellPrice += accumulator.MARKET_PRICE * accumulator.goodsNum
  209. accumulatorPricePv += Number(accumulator.PRICE_PV) * Number(accumulator.goodsNum)
  210. accumulatorTax += tool.calculateTax(
  211. Number(accumulator.MARKET_PRICE),
  212. Number(accumulator.TAX_RATE),
  213. Number(accumulator.goodsNum)
  214. )
  215. })
  216. this.sellPriceSum = tool.formatPrice(accumulatorSellPrice)
  217. this.pricePvSum = tool.formatPrice(accumulatorPricePv)
  218. this.taxSum = tool.formatPrice(accumulatorTax)
  219. } else {
  220. this.sellPriceSum = this.pricePvSum = this.taxSum = 0.0
  221. }
  222. },
  223. // 选择商品
  224. handleSelectionChange(val) {
  225. let idx = -1
  226. let num
  227. for (const i in this.tableData) {
  228. for (const v in val) {
  229. if (val[v].ID === this.tableData[i].ID) {
  230. idx = i
  231. num = this.storeNums[idx]
  232. val[v]['goodsNum'] = num
  233. break
  234. }
  235. }
  236. }
  237. this.multipleSelection = val
  238. // 计算统计
  239. this.handleSureChange()
  240. },
  241. // 会员查询
  242. handleChkRecUser() {
  243. this.form.conUserName = ''
  244. this.conRealName = '-'
  245. this.form.autoPlace = ''
  246. if (this.form.recUserName) {
  247. this.loading = true
  248. fetchGuestFullInfo({ userName: this.form.recUserName })
  249. .then((response) => {
  250. this.recRealName = response.data.REAL_NAME
  251. this.loading = false
  252. })
  253. .catch(() => {
  254. this.recRealName = '-'
  255. this.loading = false
  256. })
  257. }
  258. },
  259. // 自提仅禁用地址输入;是否提交配送方式仍遵循旧接口契约。
  260. wayChange() {
  261. if (this.form.way === 'pickup') {
  262. this.areaDisabled = true
  263. this.addressDisabled = true
  264. } else {
  265. this.areaDisabled = false
  266. this.addressDisabled = false
  267. }
  268. },
  269. // 游客报单
  270. onSubmit() {
  271. if (this.submitButtonStat || this.paymentComplete) return
  272. if (!this.form.decLv) {
  273. this.$message({
  274. message: this.$t('profile.selectLevel'),
  275. type: 'error'
  276. })
  277. return false
  278. }
  279. if (String(this.isDec) === '0' && !this.form.decUserName) {
  280. this.$message({
  281. message: this.$t('shop.declarationCenterNumberTips'),
  282. type: 'error'
  283. })
  284. return false
  285. }
  286. if (!this.form.recUserName.trim()) {
  287. this.$message.error('Please enter the referrer ID.')
  288. return false
  289. }
  290. if (!this.form.conUserName.trim()) {
  291. this.$message.error('Please enter the actual referrer ID.')
  292. return false
  293. }
  294. // 已选择商品
  295. this.selectProduct()
  296. if (this.multipleSelection.length <= 0) {
  297. this.$message.error(this.$t('shop.chooseTips'))
  298. return false
  299. }
  300. // 余额
  301. const amountBalance = this.userBalance[this.form.payType] || 0
  302. // 余额是否充足
  303. if (this.form.payType === 'cash' && amountBalance - this.payAmount < 0) {
  304. // 账户类型提示信息
  305. const payListArray = Array.from(this.form.payType)
  306. const account = payListArray.filter((item) => {
  307. return this.form.payType === item.label
  308. })
  309. const accountType = (account[0] && account[0].name) || ''
  310. this.$message({
  311. message: accountType + this.$t('shop.balanceNotAllow'),
  312. type: 'error'
  313. })
  314. return false
  315. }
  316. if (!this.draft.draftId || !this.draft.accessToken) {
  317. this.$message.error('Please select your country again to load the guest order details.')
  318. return false
  319. }
  320. this.submitButtonStat = true
  321. // 字段与默认值沿用旧页,不能因新 UI 没有对应控件就删减提交参数。
  322. const params = {
  323. draftId: this.draft.draftId,
  324. accessToken: this.draft.accessToken,
  325. consignee: this.form.consignee,
  326. acceptMobile: this.form.acceptMobile,
  327. province: this.form.areaSelected[0] ? this.form.areaSelected[0] : '',
  328. city: this.form.areaSelected[1] ? this.form.areaSelected[1] : '',
  329. county: this.form.areaSelected[2] ? this.form.areaSelected[2] : '',
  330. address: this.form.address,
  331. decType: this.form.decType,
  332. insertUserName: this.form.insertUserName,
  333. decLv: this.form.decLv,
  334. realName: this.form.realName,
  335. decUserName: this.form.decUserName,
  336. conUserName: this.form.conUserName,
  337. recUserName: this.form.recUserName,
  338. insertUserIdCard: this.form.insertUserIdCard,
  339. openBank: this.form.openBank,
  340. bankAddress: this.form.bankAddress,
  341. mobile: this.form.mobile,
  342. email: this.form.email,
  343. cityName: this.form.cityName,
  344. lgaName: this.form.lgaName,
  345. bankProvince: this.form.bankAreaSelected[0] ? this.form.bankAreaSelected[0] : '',
  346. bankCity: this.form.bankAreaSelected[1] ? this.form.bankAreaSelected[1] : '',
  347. bankCounty: this.form.bankAreaSelected[2] ? this.form.bankAreaSelected[2] : '',
  348. bankNo: this.form.bankNo,
  349. password: this.form.password,
  350. payPassword: this.form.payPassword,
  351. packageId: this.form.packageId,
  352. goodsId: this.form.goodsId,
  353. goodsNum: this.form.goodsNum,
  354. location: 1, // 自动安置
  355. decWay: this.decWay,
  356. payType: this.form.payType,
  357. countryId: this.form.countryId,
  358. languageId: this.form.languageId
  359. }
  360. createGuestDec(params)
  361. .then((response) => {
  362. const order = response.data || response
  363. if (!order.accessCode || !order.reference) {
  364. throw new Error('Payment details are missing. Please contact customer service.')
  365. }
  366. this.paymentOrder = order
  367. this.submitButtonStat = false
  368. this.visible = true
  369. })
  370. .catch((err) => {
  371. this.$message({
  372. message: err.message || err,
  373. type: 'error'
  374. })
  375. this.submitButtonStat = false
  376. })
  377. },
  378. handleClose() {
  379. // 关闭只隐藏弹窗,继续支付时重新向后端获取支付信息。
  380. this.visible = false
  381. },
  382. processPayment(response) {
  383. if (!response || response.reference !== this.paymentOrder.reference) {
  384. this.showLoadError(new Error('Payment reference does not match the current order.'))
  385. return
  386. }
  387. this.visible = false
  388. this.paymentComplete = true
  389. this.$message({
  390. message: 'Payment submitted. Please wait for order confirmation.',
  391. type: 'success'
  392. })
  393. },
  394. // 已选择商品
  395. selectProduct() {
  396. this.payAmount = 0
  397. this.form.goodsId = []
  398. this.form.goodsNum = []
  399. this.multipleSelection.map((item) => {
  400. this.form.goodsId.push(item.ID)
  401. this.form.goodsNum.push(item.goodsNum)
  402. this.payAmount += item.goodsNum * item.MARKET_PRICE
  403. })
  404. },
  405. // BA会员查询
  406. checkBaUser() {
  407. if (this.form.decType === 'ba' && this.form.insertUserName !== '') {
  408. this.loading = true
  409. fetchBrandAmbassadorInfo({ userName: this.form.insertUserName })
  410. .then((response) => {
  411. this.form.recUserName = response.data.REC_USER_NAME
  412. this.form.realName = response.data.REAL_NAME
  413. this.handleChkRecUser()
  414. this.loading = false
  415. })
  416. .catch(() => {
  417. this.loading = false
  418. })
  419. }
  420. },
  421. // 查询安置人
  422. handleChkRecommenderActualName() {
  423. if (this.form.conUserName) {
  424. this.loading = true
  425. fetchGuestInspectNetwork({
  426. recommenderName: this.form.recUserName,
  427. recommenderActualName: this.form.conUserName
  428. })
  429. .then((response) => {
  430. this.conRealName = response.data.REAL_NAME
  431. this.loading = false
  432. })
  433. .catch(() => {
  434. this.conRealName = '-'
  435. this.loading = false
  436. })
  437. }
  438. }
  439. }
  440. }