order-detail.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_userReceiveAddressSave } 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. status: 'WAIT_PAY',
  13. statusList: {
  14. WAIT_PAY: {
  15. logo: './images/ing.png',
  16. title: '等待付款',
  17. content: '请尽快完成支付,以便我们为您处理订单'
  18. },
  19. },
  20. backParams: null,
  21. addressList: [] as any,
  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. showArea: false,
  40. areaList: [] as any,
  41. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad(options: any) {
  46. this.queryPayType()
  47. this.getAddress()
  48. if (options.orderInfo) {
  49. // console.log('goods', options)
  50. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  51. // console.log(goods, 'goods', options)
  52. const infos = {
  53. allSalePrice: 0,
  54. allOriginPrice: 0,
  55. allDiscountPrice: '',
  56. discountIntegerPart: '',
  57. discountDecimalPart: '',
  58. integerPart: '',
  59. decimalPart: '',
  60. name: '',
  61. shopId: '',
  62. orderNo: options.orderNo || '',
  63. goodsList: [] as any,
  64. createTime: '', // 订单时间
  65. }
  66. // 是否有乐器
  67. let hasInstrument = false
  68. for (let i in goods) {
  69. const item = goods[i]
  70. if (item.goodsType === "INSTRUMENTS") {
  71. hasInstrument = true
  72. }
  73. infos.name = infos.name ? infos.name + '+' + item.name : item.name
  74. infos.shopId = item.shopId
  75. const afterPrice: any = formatPrice(item.salePrice)
  76. // console.log(infos.goodsList, 'infos.goodsList')
  77. infos.goodsList.push({
  78. ...item,
  79. typePeriod: this.formatPeriod(item.num, item.period),
  80. ...afterPrice
  81. })
  82. infos.allSalePrice += Number(item.salePrice)
  83. infos.allOriginPrice += Number(item.originalPrice)
  84. }
  85. const allAfterPrice: any = formatPrice(infos.allSalePrice)
  86. infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string
  87. const allDiscount: any = formatPrice(infos.allOriginPrice - infos.allSalePrice)
  88. infos.integerPart = allAfterPrice.integerPart
  89. infos.decimalPart = allAfterPrice.decimalPart
  90. infos.discountIntegerPart = allDiscount.integerPart
  91. infos.discountDecimalPart = allDiscount.decimalPart
  92. this.setData({
  93. goodsInfo: infos,
  94. userBeneficiaryId: options.userBeneficiaryId,
  95. status: options.status || '',
  96. hasInstrument
  97. }, () => {
  98. this.getOrderDetail()
  99. });
  100. }
  101. },
  102. /** 获取订单详情 */
  103. async getOrderDetail() {
  104. try {
  105. if (!this.data.goodsInfo.orderNo) return
  106. const { data } = await api_userPaymentOrderDetail(this.data.goodsInfo.orderNo, {
  107. version: 'V2'
  108. });
  109. const result = data.data || {}
  110. const addresses: any = result.addresses
  111. const tempSchoolAddress = [result.beneficiary?.provinceName || '', result.beneficiary?.cityName || '', result.beneficiary?.regionName || '', result.beneficiary?.schoolAreaName, GRADE_ENUM[result.beneficiary?.currentGradeNum], result.beneficiary?.currentClass + '班']
  112. const beneficiary = {
  113. name: result.beneficiary?.name,
  114. phoneNumber: result.beneficiary?.phone,
  115. schoolInfo: tempSchoolAddress.join('')
  116. }
  117. this.setData({
  118. receiveAddress: addresses?.id,
  119. receiveAddressInfo: {
  120. addressDetail: addresses?.detailAddress,
  121. name: addresses?.name,
  122. phoneNumber: addresses?.phoneNumber
  123. },
  124. userBeneficiaryId: result.beneficiary.schoolAreaId, // 添加购买人信息
  125. userBeneficiaryInfo: beneficiary,
  126. status: result.wechatStatus,
  127. 'goodsInfo.createTime': result.createTime
  128. }, () => {
  129. console.log(this.data)
  130. })
  131. } catch {
  132. //
  133. }
  134. },
  135. // 格式化类型
  136. formatPeriod(num: number, type: string) {
  137. if (!type) return ''
  138. const template: any = {
  139. DAY: "天卡",
  140. MONTH: "月卡",
  141. YEAR: "年卡"
  142. }
  143. if (type === "YEAR" && num >= 99) {
  144. return '永久卡'
  145. }
  146. return num + (template[type] || '')
  147. },
  148. // 获取后台配置的支付方式
  149. async queryPayType() {
  150. try {
  151. // wxlite_payment_service_provider
  152. const { data } = await api_queryByParamName({
  153. paramName: app.globalData.appId
  154. });
  155. if (data.code == 200) {
  156. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  157. this.setData({
  158. paymentType: paramValue.vendor,
  159. paymentChannel: paramValue.channel
  160. });
  161. }
  162. } catch (error) {
  163. console.log(error, "error");
  164. }
  165. },
  166. /** 添加收货地址 */
  167. onSelectAddress() {
  168. if (this.data.goodsInfo?.orderNo) {
  169. return
  170. }
  171. console.log(this.data.addressList.length > 0, this.data.receiveAddress)
  172. if (this.data.addressList.length > 0 || this.data.receiveAddress) {
  173. wx.navigateTo({
  174. url: `../address/index?receiveAddress=${this.data.receiveAddress}`,
  175. })
  176. } else {
  177. wx.navigateTo({
  178. url: `../address/address-detail`,
  179. })
  180. }
  181. },
  182. onPayError(message?: string) {
  183. wx.hideLoading()
  184. wx.showToast({
  185. title: message || '支付取消',
  186. icon: 'none'
  187. })
  188. },
  189. // 购买
  190. async onSubmit() {
  191. if (!this.data.receiveAddress && this.data.hasInstrument) {
  192. wx.showToast({
  193. title: '请选择收货地址',
  194. icon: 'none'
  195. })
  196. return
  197. }
  198. wx.showLoading({
  199. mask: true,
  200. title: "订单提交中...",
  201. });
  202. try {
  203. const { allSalePrice, shopId, name, orderNo, goodsList } = this.data.goodsInfo
  204. const goodsInfos: any = []
  205. goodsList.forEach((item: any) => {
  206. goodsInfos.push({
  207. "goodsId": item.id,
  208. "goodsNum": 1,
  209. "goodsType": item.goodsType,
  210. "paymentCashAmount": item.salePrice,
  211. "paymentCouponAmount": 0
  212. })
  213. })
  214. if (orderNo) {
  215. const { data } = await api_userPaymentOrderUnpaid({
  216. orderNo: orderNo,
  217. paymentType: 'WECHAT_MINI'
  218. })
  219. if (data.code === 200) {
  220. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  221. this.onExecutePay(paymentConfig, paymentType, orderNo)
  222. } else {
  223. this.onPayError()
  224. }
  225. } else {
  226. const { data } = await api_executeOrder({
  227. orderType: "WECHAT_MINI",
  228. paymentType: this.data.paymentType,
  229. paymentCashAmount: allSalePrice,
  230. paymentCouponAmount: 0,
  231. shopId: shopId,
  232. openId: app.globalData.userInfo?.liteOpenid,
  233. goodsInfos: goodsInfos,
  234. receiveAddress: this.data.receiveAddress,
  235. userBeneficiaryId: this.data.userBeneficiaryId,
  236. orderName: name,
  237. orderDesc: name
  238. })
  239. if (data.code === 200) {
  240. const { paymentConfig, paymentType, orderNo } = data.data
  241. this.onExecutePay(paymentConfig, paymentType, orderNo)
  242. } else if (data.code === 5200) {
  243. wx.hideLoading()
  244. wx.showToast({
  245. title: data.message,
  246. icon: 'none'
  247. })
  248. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  249. wx.hideLoading()
  250. wx.showToast({
  251. title: data.message,
  252. icon: 'none'
  253. })
  254. setTimeout(() => {
  255. wx.navigateBack()
  256. }, 1000);
  257. } else {
  258. this.onPayError()
  259. }
  260. }
  261. } catch {
  262. wx.hideLoading()
  263. }
  264. },
  265. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  266. wx.login({
  267. success: async (wxres: any) => {
  268. const res = await api_executePayment({
  269. merOrderNo: paymentConfig.merOrderNo,
  270. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  271. paymentType,
  272. userId: app.globalData.userInfo?.id,
  273. code: wxres.code,
  274. wxMiniAppId: app.globalData.appId
  275. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  276. // wxPubAppId: 'wxbde13f59d40cb4f2'
  277. })
  278. wx.hideLoading()
  279. if (res.data.code === 200) {
  280. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  281. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  282. wx.hideLoading()
  283. wx.showToast({
  284. title: res.data.message,
  285. icon: 'none'
  286. })
  287. setTimeout(() => {
  288. wx.navigateBack()
  289. }, 1000);
  290. } else {
  291. this.onPayError(res.data.message)
  292. }
  293. },
  294. fail: () => {
  295. this.onPayError()
  296. }
  297. })
  298. },
  299. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  300. const isYeePay = paymentType.indexOf('yeepay') !== -1
  301. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  302. : paymentConfig?.expend
  303. ? JSON.parse(paymentConfig?.expend?.pay_info)
  304. : paymentConfig
  305. const that = this
  306. wx.requestPayment({
  307. timeStamp: prePayInfo.timeStamp,
  308. nonceStr: prePayInfo.nonceStr,
  309. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  310. paySign: prePayInfo.paySign,
  311. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  312. success() {
  313. wx.showToast({ title: '支付成功', icon: 'success' });
  314. wx.redirectTo({
  315. url: '/pages/orders/order-result?orderNo=' + orderNo
  316. })
  317. },
  318. fail(ressonInfo) {
  319. console.log('支付失败', ressonInfo)
  320. that.onPayError()
  321. that.setData({
  322. 'goodsInfo.orderNo': orderNo
  323. }, () => {
  324. that.getOrderDetail()
  325. })
  326. }
  327. })
  328. },
  329. /** 客服 */
  330. onService() {
  331. // console.log("showService")
  332. this.setData({
  333. showService: true
  334. })
  335. },
  336. changePop(event: { detail: any }) {
  337. this.setData({
  338. showService: event.detail
  339. })
  340. },
  341. /**
  342. * 生命周期函数--监听页面显示
  343. */
  344. onShow() {
  345. if (this.data.backParams) {
  346. console.log(this.data.backParams, 'backParams'); // { key: 'value' }
  347. const backParams: any = this.data.backParams || {};
  348. this.setData({
  349. receiveAddress: backParams.receiveAddress,
  350. receiveAddressInfo: backParams.receiveAddressInfo,
  351. backParams: null // 清空参数
  352. })
  353. }
  354. this.getAddress()
  355. },
  356. /** 地址列表 */
  357. async getAddress() {
  358. try {
  359. const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 })
  360. this.setData({
  361. addressList: data.data.rows || []
  362. })
  363. } catch {
  364. //
  365. }
  366. },
  367. onCopy(e: { currentTarget: any }) {
  368. wx.setClipboardData({
  369. data: e.currentTarget.dataset.orderno,
  370. success: () => {
  371. wx.showToast({ title: '复制成功', icon: 'none' })
  372. },
  373. fail: () => {
  374. wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' })
  375. }
  376. })
  377. },
  378. /**
  379. * 用户点击右上角分享
  380. */
  381. onShareAppMessage() {
  382. return {
  383. title: '翼时代器乐数字Ai',
  384. path: '/pages/index/index',
  385. imageUrl: 'https://oss.dayaedu.com/ktyq/1739865626350.png'
  386. }
  387. }
  388. })