order-detail.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. }
  178. },
  179. onPayError(message?: string) {
  180. wx.hideLoading()
  181. wx.showToast({
  182. title: message || '支付取消',
  183. icon: 'none'
  184. })
  185. },
  186. // 购买
  187. async onSubmit() {
  188. if (!this.data.receiveAddress && this.data.hasInstrument) {
  189. wx.showToast({
  190. title: '请选择收货地址',
  191. icon: 'none'
  192. })
  193. return
  194. }
  195. wx.showLoading({
  196. mask: true,
  197. title: "订单提交中...",
  198. });
  199. try {
  200. const { allSalePrice, shopId, name, orderNo, goodsList } = this.data.goodsInfo
  201. const goodsInfos: any = []
  202. goodsList.forEach((item: any) => {
  203. goodsInfos.push({
  204. "goodsId": item.id,
  205. "goodsNum": 1,
  206. "goodsType": item.goodsType,
  207. "paymentCashAmount": item.salePrice,
  208. "paymentCouponAmount": 0
  209. })
  210. })
  211. if (orderNo) {
  212. const { data } = await api_userPaymentOrderUnpaid({
  213. orderNo: orderNo,
  214. paymentType: 'WECHAT_MINI'
  215. })
  216. if (data.code === 200) {
  217. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  218. this.onExecutePay(paymentConfig, paymentType, orderNo)
  219. } else {
  220. this.onPayError()
  221. }
  222. } else {
  223. const { data } = await api_executeOrder({
  224. orderType: "WECHAT_MINI",
  225. paymentType: this.data.paymentType,
  226. paymentCashAmount: allSalePrice,
  227. paymentCouponAmount: 0,
  228. shopId: shopId,
  229. openId: app.globalData.userInfo?.liteOpenid,
  230. goodsInfos: goodsInfos,
  231. receiveAddress: this.data.receiveAddress,
  232. userBeneficiaryId: this.data.userBeneficiaryId,
  233. orderName: name,
  234. orderDesc: name
  235. })
  236. if (data.code === 200) {
  237. const { paymentConfig, paymentType, orderNo } = data.data
  238. this.onExecutePay(paymentConfig, paymentType, orderNo)
  239. } else if (data.code === 5200) {
  240. wx.hideLoading()
  241. wx.showToast({
  242. title: data.message,
  243. icon: 'none'
  244. })
  245. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  246. wx.hideLoading()
  247. wx.showToast({
  248. title: data.message,
  249. icon: 'none'
  250. })
  251. setTimeout(() => {
  252. wx.navigateBack()
  253. }, 1000);
  254. } else {
  255. this.onPayError()
  256. }
  257. }
  258. } catch {
  259. wx.hideLoading()
  260. }
  261. },
  262. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  263. wx.login({
  264. success: async (wxres: any) => {
  265. const res = await api_executePayment({
  266. merOrderNo: paymentConfig.merOrderNo,
  267. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  268. paymentType,
  269. userId: app.globalData.userInfo?.id,
  270. code: wxres.code,
  271. wxMiniAppId: app.globalData.appId
  272. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  273. // wxPubAppId: 'wxbde13f59d40cb4f2'
  274. })
  275. wx.hideLoading()
  276. if (res.data.code === 200) {
  277. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  278. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  279. wx.hideLoading()
  280. wx.showToast({
  281. title: res.data.message,
  282. icon: 'none'
  283. })
  284. setTimeout(() => {
  285. wx.navigateBack()
  286. }, 1000);
  287. } else {
  288. this.onPayError(res.data.message)
  289. }
  290. },
  291. fail: () => {
  292. this.onPayError()
  293. }
  294. })
  295. },
  296. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  297. const isYeePay = paymentType.indexOf('yeepay') !== -1
  298. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  299. : paymentConfig?.expend
  300. ? JSON.parse(paymentConfig?.expend?.pay_info)
  301. : paymentConfig
  302. const that = this
  303. wx.requestPayment({
  304. timeStamp: prePayInfo.timeStamp,
  305. nonceStr: prePayInfo.nonceStr,
  306. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  307. paySign: prePayInfo.paySign,
  308. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  309. success() {
  310. wx.showToast({ title: '支付成功', icon: 'success' });
  311. wx.redirectTo({
  312. url: '/pages/orders/order-result?orderNo=' + orderNo
  313. })
  314. },
  315. fail(ressonInfo) {
  316. console.log('支付失败', ressonInfo)
  317. that.onPayError()
  318. that.setData({
  319. 'goodsInfo.orderNo': orderNo
  320. }, () => {
  321. that.getOrderDetail()
  322. })
  323. }
  324. })
  325. },
  326. /** 客服 */
  327. onService() {
  328. console.log("showService")
  329. this.setData({
  330. showService: true
  331. })
  332. },
  333. changePop(event: { detail: any }) {
  334. this.setData({
  335. showService: event.detail
  336. })
  337. },
  338. /**
  339. * 生命周期函数--监听页面显示
  340. */
  341. onShow() {
  342. if (this.data.backParams) {
  343. // console.log(this.data.backParams, 'backParams'); // { key: 'value' }
  344. const backParams: any = this.data.backParams || {};
  345. this.setData({
  346. receiveAddress: backParams.receiveAddress,
  347. receiveAddressInfo: backParams.receiveAddressInfo,
  348. backParams: null // 清空参数
  349. })
  350. }
  351. this.getAddress()
  352. },
  353. /** 地址列表 */
  354. async getAddress() {
  355. try {
  356. const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 })
  357. this.setData({
  358. addressList: data.data.rows || []
  359. })
  360. } catch {
  361. //
  362. }
  363. },
  364. onCopy(e: { currentTarget: any }) {
  365. wx.setClipboardData({
  366. data: e.currentTarget.dataset.orderno,
  367. success: () => {
  368. wx.showToast({ title: '复制成功', icon: 'none' })
  369. },
  370. fail: () => {
  371. wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' })
  372. }
  373. })
  374. },
  375. /**
  376. * 用户点击右上角分享
  377. */
  378. onShareAppMessage() {
  379. return {
  380. title: '器乐数字AI工具',
  381. path: '/pages/index/index',
  382. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  383. }
  384. }
  385. })