order-detail.ts 23 KB

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