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