order-detail.ts 24 KB

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