order-detail.ts 27 KB

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