order-detail.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderDetail, api_userPaymentOrderUnpaid } from "../../api/login";
  3. import { api_sysAreaQueryAllProvince, api_userReceiveAddressPage, api_userReceiveAddressRemove, api_userReceiveAddressSave, api_userReceiveAddressUpdate } from "../../api/new";
  4. import { formatPrice, GRADE_ENUM } from "../../utils/util";
  5. // 获取应用实例
  6. const app = getApp<IAppOption>()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. backParams: null,
  13. serviceShow: true,
  14. status: '',
  15. statusList: {
  16. ing: {
  17. logo: './images/ing.png',
  18. title: '确认订单',
  19. content: '请尽快完成支付,以便我们为您处理订单'
  20. },
  21. },
  22. goodsInfo: {} as any,
  23. hasInstrument: false, // 是否有乐器
  24. receiveAddress: '', // 选择的地址信息
  25. receiveAddressInfo: {
  26. addressDetail: '',
  27. name: '',
  28. phoneNumber: ''
  29. },
  30. userBeneficiaryId: '', // 添加购买人信息
  31. userBeneficiaryInfo: {
  32. name: '',
  33. phoneNumber: '',
  34. schoolInfo: ''
  35. },
  36. paymentType: null as any, // 支付类型
  37. paymentChannel: null as any,
  38. showService: false,
  39. areaList: [] as any,
  40. currentValues: [] as any,
  41. showDialog: false, // 删除弹窗
  42. selectAddressId: '', // 选中的编号
  43. showArea: false, // 地区
  44. addressShow: false, // 添加/修改收货地址
  45. addressListShow: false, // 收货地址列表
  46. addressList: [] as any, // 收货地址列表
  47. // 添加地址表单信息
  48. id: "",
  49. name: '',
  50. phoneNumber: '',
  51. detailAddress: '',
  52. cityCode: 0,
  53. cityName: "",
  54. provinceCode: 0,
  55. provinceName: "",
  56. regionCode: '',
  57. regionName: "",
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad(options: any) {
  63. this.queryPayType()
  64. this.getAddresss()
  65. this.getAreas()
  66. if (options.orderInfo) {
  67. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  68. // console.log(goods, 'goods')
  69. // console.log(options, 'options')
  70. const infos = {
  71. allSalePrice: 0,
  72. allOriginPrice: 0,
  73. allDiscountPrice: '',
  74. integerPart: '',
  75. decimalPart: '',
  76. name: '',
  77. shopId: '',
  78. orderNo: options.orderNo || '',
  79. goodsList: [] as any
  80. }
  81. // 是否有乐器
  82. let hasInstrument = false
  83. for (let i in goods) {
  84. const item = goods[i]
  85. if (item.goodsType === "INSTRUMENTS") {
  86. hasInstrument = true
  87. }
  88. infos.name = infos.name ? infos.name + '+' + item.name : item.name
  89. infos.shopId = item.shopId
  90. const afterPrice: any = formatPrice(item.salePrice)
  91. infos.goodsList.push({
  92. ...item,
  93. ...afterPrice
  94. })
  95. infos.allSalePrice += Number(item.salePrice)
  96. infos.allOriginPrice += Number(item.originalPrice)
  97. }
  98. const allAfterPrice: any = formatPrice(infos.allSalePrice)
  99. // console.log(infos.allOriginPrice, infos.allSalePrice)
  100. infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string
  101. infos.integerPart = allAfterPrice.integerPart
  102. infos.decimalPart = allAfterPrice.decimalPart
  103. // console.log(infos, 'infos')
  104. this.setData({
  105. goodsInfo: infos,
  106. status: options.status || '',
  107. hasInstrument
  108. });
  109. }
  110. this.getOrderDetail()
  111. },
  112. onShow() {
  113. if (this.data.backParams) {
  114. // console.log(this.data.backParams, 'backParams'); // { key: 'value' }
  115. const backParams: any = this.data.backParams || {};
  116. this.setData({
  117. userBeneficiaryId: backParams.userBeneficiaryId,
  118. userBeneficiaryInfo: {
  119. name: backParams.name,
  120. phoneNumber: backParams.phone,
  121. schoolInfo: backParams.schoolInfo
  122. },
  123. backParams: null // 清空参数
  124. })
  125. }
  126. this.setData({
  127. serviceShow: true
  128. })
  129. },
  130. onHide() {
  131. this.setData({
  132. serviceShow: false
  133. })
  134. },
  135. /** 获取订单详情 */
  136. async getOrderDetail() {
  137. try {
  138. if (!this.data.goodsInfo.orderNo) return
  139. const { data } = await api_userPaymentOrderDetail(this.data.goodsInfo.orderNo, {
  140. version: 'V2'
  141. });
  142. // console.log(data, 'data')
  143. const result = data.data || {}
  144. const addresses: any = result.addresses
  145. const beneficiary: any = result.beneficiary
  146. const tempSchoolAddress = [beneficiary?.provinceName, beneficiary?.cityName, beneficiary?.regionName, beneficiary?.schoolAreaName, GRADE_ENUM[beneficiary?.currentGradeNum], beneficiary?.currentClass + '班']
  147. this.setData({
  148. receiveAddress: addresses?.id,
  149. receiveAddressInfo: {
  150. addressDetail: addresses?.detailAddress,
  151. name: addresses?.name,
  152. phoneNumber: addresses?.phoneNumber
  153. },
  154. userBeneficiaryId: beneficiary.schoolAreaId, // 添加购买人信息
  155. userBeneficiaryInfo: {
  156. name: beneficiary.name,
  157. phoneNumber: beneficiary.phone,
  158. schoolInfo: tempSchoolAddress.join('')
  159. },
  160. })
  161. } catch {
  162. //
  163. }
  164. },
  165. /** 地址列表 */
  166. async getAddresss() {
  167. try {
  168. const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 })
  169. this.setData({
  170. addressList: data.data.rows || []
  171. })
  172. } catch {
  173. //
  174. }
  175. },
  176. /** 获取省市区 */
  177. async getAreas() {
  178. try {
  179. const { data } = await api_sysAreaQueryAllProvince({})
  180. const areaList: any = this.formateArea(data.data)
  181. const currentValues = []
  182. if (areaList?.province_list) {
  183. // 获取第一个键值对
  184. const firstKey = Object.keys(areaList?.province_list)[0];
  185. // 通过键获取值
  186. const firstValue = areaList?.province_list[firstKey];
  187. currentValues.push({
  188. code: firstKey,
  189. name: firstValue
  190. })
  191. }
  192. if (areaList?.city_list) {
  193. // 获取第一个键值对
  194. const firstKey = Object.keys(areaList?.city_list)[0];
  195. // 通过键获取值
  196. const firstValue = areaList?.city_list[firstKey];
  197. currentValues.push({
  198. code: firstKey,
  199. name: firstValue
  200. })
  201. }
  202. if (areaList?.county_list) {
  203. // 获取第一个键值对
  204. const firstKey = Object.keys(areaList?.county_list)[0];
  205. // 通过键获取值
  206. const firstValue = areaList?.county_list[firstKey];
  207. currentValues.push({
  208. code: firstKey,
  209. name: firstValue
  210. })
  211. }
  212. this.setData({
  213. areaList,
  214. currentValues
  215. })
  216. } catch {
  217. //
  218. }
  219. },
  220. formateArea(area: any[]) {
  221. const province_list: { [_: string]: string } = {};
  222. const city_list: { [_: string]: string } = {};
  223. const county_list: { [_: string]: string } = {};
  224. area.forEach((item: any) => {
  225. province_list[item.code] = item.name;
  226. });
  227. area.forEach((item: any) => {
  228. item.areas && item.areas.forEach((city: any) => {
  229. city_list[city.code] = city.name;
  230. });
  231. });
  232. area.forEach((item: any) => {
  233. item.areas && item.areas.forEach((city: any) => {
  234. city.areas && city.areas.forEach((county: any) => {
  235. county_list[county.code] = county.name;
  236. });
  237. });
  238. });
  239. return {
  240. province_list,
  241. city_list,
  242. county_list
  243. };
  244. },
  245. // 获取后台配置的支付方式
  246. async queryPayType() {
  247. try {
  248. // wxlite_payment_service_provider
  249. const { data } = await api_queryByParamName({
  250. paramName: app.globalData.appId
  251. });
  252. if (data.code == 200) {
  253. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  254. this.setData({
  255. paymentType: paramValue.vendor,
  256. paymentChannel: paramValue.channel
  257. });
  258. }
  259. } catch (error) {
  260. console.log(error, "error");
  261. }
  262. },
  263. onPayError(message?: string) {
  264. wx.hideLoading()
  265. wx.showToast({
  266. title: message || '支付取消',
  267. icon: 'none'
  268. })
  269. },
  270. // 购买
  271. async onSubmit() {
  272. if (!this.data.receiveAddress && this.data.hasInstrument) {
  273. wx.showToast({
  274. title: '请选择收货地址',
  275. icon: 'none'
  276. })
  277. return
  278. }
  279. if (!this.data.userBeneficiaryId) {
  280. wx.showToast({
  281. title: '请添加购买人',
  282. icon: 'none'
  283. })
  284. return
  285. }
  286. wx.showLoading({
  287. mask: true,
  288. title: "订单提交中...",
  289. });
  290. try {
  291. // const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  292. const { allSalePrice, shopId, name, orderNo, goodsList } = this.data.goodsInfo
  293. const goodsInfos: any = []
  294. goodsList.forEach((item: any) => {
  295. goodsInfos.push({
  296. "goodsId": item.id,
  297. "goodsNum": 1,
  298. "goodsType": item.goodsType,
  299. "paymentCashAmount": item.salePrice,
  300. "paymentCouponAmount": 0
  301. })
  302. })
  303. if (orderNo) {
  304. const { data } = await api_userPaymentOrderUnpaid({
  305. orderNo: orderNo,
  306. paymentType: 'WECHAT_MINI'
  307. })
  308. if (data.code === 200) {
  309. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  310. this.onExecutePay(paymentConfig, paymentType, orderNo)
  311. } else {
  312. this.onPayError()
  313. }
  314. } else {
  315. const { data } = await api_executeOrder({
  316. "orderType": "WECHAT_MINI",
  317. "paymentType": this.data.paymentType,
  318. "paymentCashAmount": allSalePrice,
  319. "paymentCouponAmount": 0,
  320. "shopId": shopId,
  321. "openId": app.globalData.userInfo?.liteOpenid,
  322. "goodsInfos": goodsInfos,
  323. receiveAddress: this.data.receiveAddress,
  324. userBeneficiaryId: this.data.userBeneficiaryId,
  325. "orderName": name,
  326. "orderDesc": name
  327. })
  328. if (data.code === 200) {
  329. const { paymentConfig, paymentType, orderNo } = data.data
  330. this.onExecutePay(paymentConfig, paymentType, orderNo)
  331. } else if (data.code === 5200) {
  332. wx.hideLoading()
  333. wx.showToast({
  334. title: data.message,
  335. icon: 'none'
  336. })
  337. } else if([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  338. wx.navigateBack()
  339. } else {
  340. this.onPayError()
  341. }
  342. }
  343. } catch {
  344. wx.hideLoading()
  345. }
  346. },
  347. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  348. wx.login({
  349. success: async (wxres: any) => {
  350. const res = await api_executePayment({
  351. merOrderNo: paymentConfig.merOrderNo,
  352. paymentChannel: this.data.paymentChannel || 'wx_lite',
  353. paymentType,
  354. userId: app.globalData.userInfo?.id,
  355. code: wxres.code,
  356. wxMiniAppId: app.globalData.appId
  357. })
  358. wx.hideLoading()
  359. if (res.data.code === 200) {
  360. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  361. } else {
  362. this.onPayError(res.data.message)
  363. }
  364. },
  365. fail: () => {
  366. this.onPayError()
  367. }
  368. })
  369. },
  370. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  371. const isYeePay = paymentType.indexOf('yeepay') !== -1
  372. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  373. : paymentConfig?.expend
  374. ? JSON.parse(paymentConfig?.expend?.pay_info)
  375. : paymentConfig
  376. const that = this
  377. wx.requestPayment({
  378. timeStamp: prePayInfo.timeStamp,
  379. nonceStr: prePayInfo.nonceStr,
  380. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  381. paySign: prePayInfo.paySign,
  382. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  383. success() {
  384. wx.showToast({ title: '支付成功', icon: 'success' });
  385. wx.redirectTo({
  386. url: '/pages/orders/order-result?orderNo=' + orderNo
  387. })
  388. },
  389. fail(ressonInfo) {
  390. console.log('支付失败', ressonInfo)
  391. that.onPayError()
  392. const goodsInfo = that.data.goodsInfo
  393. goodsInfo.orderNo = orderNo
  394. that.setData({
  395. goodsInfo
  396. })
  397. }
  398. })
  399. },
  400. /** 客服 */
  401. onService() {
  402. // console.log("showService")
  403. this.setData({
  404. showService: true
  405. })
  406. },
  407. changePop(event: { detail: any }) {
  408. this.setData({
  409. showService: event.detail
  410. })
  411. },
  412. /** 选择收货地址 */
  413. onTagAddress() {
  414. if(this.data.goodsInfo.orderNo) return
  415. if (this.data.addressList.length > 0) {
  416. this.onShowAddressList()
  417. } else {
  418. this.onShowAddress()
  419. }
  420. },
  421. /** 显示添加/修改地址 */
  422. onShowAddress() {
  423. this.setData({
  424. addressShow: true
  425. })
  426. },
  427. /** 关闭添加/修改地址 */
  428. onCloseAddress() {
  429. this.setData({
  430. addressShow: false,
  431. id: "",
  432. name: '',
  433. phoneNumber: '',
  434. detailAddress: '',
  435. cityCode: 0,
  436. cityName: "",
  437. provinceCode: 0,
  438. provinceName: "",
  439. regionCode: null,
  440. regionName: "",
  441. })
  442. },
  443. /** 显示收货地址 */
  444. onShowAddressList() {
  445. this.setData({
  446. addressListShow: true
  447. })
  448. },
  449. /** 关闭收货地址 */
  450. onCloseAddressList() {
  451. this.setData({
  452. addressListShow: false
  453. })
  454. },
  455. /** 添加购买人 */
  456. onAddBuyer() {
  457. wx.navigateTo({
  458. url: "../buyerInformation/index?userBeneficiaryId=" + this.data.userBeneficiaryId,
  459. });
  460. },
  461. /** 显示选择地区 */
  462. onShowAreaList() {
  463. this.setData({
  464. showArea: true
  465. })
  466. },
  467. /** 关闭选择地区 */
  468. onCloseAreaList() {
  469. this.setData({
  470. showArea: false
  471. })
  472. },
  473. /** 确定选择地区 */
  474. submitArea() {
  475. const selectedOptions = this.data.currentValues
  476. this.setData({
  477. provinceCode: selectedOptions[0].code,
  478. cityCode: selectedOptions[1].code,
  479. regionCode: selectedOptions[2].code,
  480. provinceName: selectedOptions[0].name,
  481. cityName: selectedOptions[1].name,
  482. regionName: selectedOptions[2].name,
  483. showArea: false
  484. })
  485. },
  486. // cancelArea() {
  487. // this.setData({ showArea: false })
  488. // },
  489. // confirmArea(event: any) {
  490. // console.log(event)
  491. // // const selectedOptions = event.detail.values
  492. // const selectedOptions = this.data.currentValues
  493. // this.setData({
  494. // provinceCode: selectedOptions[0].code,
  495. // cityCode: selectedOptions[1].code,
  496. // regionCode: selectedOptions[2].code,
  497. // provinceName: selectedOptions[0].name,
  498. // cityName: selectedOptions[1].name,
  499. // regionName: selectedOptions[2].name,
  500. // showArea: false,
  501. // areaComponent: null as any
  502. // })
  503. // // forms.provinceCode = selectedOptions[0].value;
  504. // // forms.cityCode = selectedOptions[1].value;
  505. // // forms.regionCode = selectedOptions[2].value;
  506. // // data.cityName = selectedOptions
  507. // // .map((item: any) => item.text)
  508. // // .join('-');
  509. // // data.showArea = false;
  510. // // }}
  511. // },
  512. changeArea(e: any) {
  513. console.log(e.detail.values, 'e.detail.values')
  514. this.setData({
  515. currentValues: e.detail.values
  516. })
  517. },
  518. /** 创建/修改收货地址 */
  519. async onOperationAddress() {
  520. const addressForm = this.data
  521. try {
  522. if (!addressForm.name) {
  523. wx.showToast({
  524. title: '请输入收货人姓名',
  525. icon: "none"
  526. })
  527. return
  528. }
  529. if (!addressForm.phoneNumber || !/^1[3456789]\d{9}$/.test(addressForm.phoneNumber)) {
  530. wx.showToast({
  531. title: '请输入正确的手机号',
  532. icon: "none"
  533. })
  534. return
  535. }
  536. if (!addressForm.provinceCode || !addressForm.cityCode || !addressForm.regionCode) {
  537. wx.showToast({
  538. title: '请选择地区',
  539. icon: "none"
  540. })
  541. return
  542. }
  543. if (!addressForm.detailAddress) {
  544. wx.showToast({
  545. title: '请输入详细地址',
  546. icon: "none"
  547. })
  548. return
  549. }
  550. if (addressForm.id) {
  551. await api_userReceiveAddressUpdate({
  552. id: addressForm.id,
  553. name: addressForm.name,
  554. phoneNumber: addressForm.phoneNumber,
  555. province: addressForm.provinceCode,
  556. city: addressForm.cityCode,
  557. region: addressForm.regionCode,
  558. detailAddress: addressForm.detailAddress
  559. })
  560. this.getAddresss()
  561. this.onCloseAddress()
  562. } else {
  563. const { data } = await api_userReceiveAddressSave({
  564. name: addressForm.name,
  565. phoneNumber: addressForm.phoneNumber,
  566. province: addressForm.provinceCode,
  567. city: addressForm.cityCode,
  568. region: addressForm.regionCode,
  569. detailAddress: addressForm.detailAddress
  570. })
  571. this.setData({
  572. receiveAddress: this.data.receiveAddress || data.data,
  573. 'receiveAddressInfo.addressDetail': addressForm.provinceName + addressForm.cityName + addressForm.regionName + addressForm.detailAddress,
  574. 'receiveAddressInfo.name': addressForm.name,
  575. 'receiveAddressInfo.phoneNumber': addressForm.phoneNumber
  576. })
  577. this.getAddresss()
  578. this.onCloseAddress()
  579. }
  580. } catch {
  581. //
  582. }
  583. },
  584. /** 选择地址 */
  585. onSelectAddress(e: any) {
  586. const id = e.currentTarget.dataset.id
  587. const addressInfo = this.data.addressList.find((item: any) => item.id === id)
  588. this.setData({
  589. receiveAddress: addressInfo.id,
  590. 'receiveAddressInfo.addressDetail': addressInfo.provinceName + addressInfo.cityName + addressInfo.regionName + addressInfo.detailAddress,
  591. 'receiveAddressInfo.name': addressInfo.name,
  592. 'receiveAddressInfo.phoneNumber': addressInfo.phoneNumber,
  593. addressListShow: false
  594. })
  595. },
  596. /** Dialog 确定 */
  597. async onDialogConfirm() {
  598. try {
  599. await api_userReceiveAddressRemove({
  600. id: this.data.selectAddressId
  601. })
  602. this.getAddresss()
  603. // 如果删除的是已经选中的地址,则需要重置数据
  604. if (this.data.selectAddressId === this.data.receiveAddress) {
  605. this.setData({
  606. selectAddressId: '',
  607. receiveAddress: '',
  608. 'receiveAddressInfo.name': '',
  609. 'receiveAddressInfo.phoneNumber': '',
  610. 'receiveAddressInfo.addressDetail': ''
  611. })
  612. }
  613. this.onDialogClose()
  614. } catch {
  615. }
  616. },
  617. /** Dialog 隐藏 */
  618. onDialogClose() {
  619. this.setData({
  620. showDialog: false
  621. })
  622. },
  623. /** 删除地址 */
  624. onRemoveAddress(e: any) {
  625. this.setData({
  626. showDialog: true,
  627. selectAddressId: e.target.dataset.id
  628. })
  629. },
  630. /** 修改地址 */
  631. onUpdateAddress(e: any) {
  632. const id = e.target.dataset.id
  633. const addressInfo = this.data.addressList.find((item: any) => item.id === id)
  634. this.setData({
  635. addressShow: true,
  636. id: addressInfo.id,
  637. name: addressInfo.name,
  638. phoneNumber: addressInfo.phoneNumber,
  639. detailAddress: addressInfo.detailAddress,
  640. cityCode: addressInfo.city,
  641. cityName: addressInfo.cityName,
  642. provinceCode: addressInfo.province,
  643. provinceName: addressInfo.provinceName,
  644. regionCode: addressInfo.region,
  645. regionName: addressInfo.regionName,
  646. })
  647. },
  648. /**
  649. * 生命周期函数--监听页面初次渲染完成
  650. */
  651. onReady() {
  652. },
  653. /**
  654. * 生命周期函数--监听页面卸载
  655. */
  656. onUnload() {
  657. },
  658. /**
  659. * 页面相关事件处理函数--监听用户下拉动作
  660. */
  661. onPullDownRefresh() {
  662. },
  663. /**
  664. * 页面上拉触底事件的处理函数
  665. */
  666. onReachBottom() {
  667. },
  668. /**
  669. * 用户点击右上角分享
  670. */
  671. onShareAppMessage() {
  672. return {
  673. title: '音乐数字AI',
  674. path: '/pages/index/index',
  675. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  676. }
  677. }
  678. })