index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // index.ts
  2. import { api_shopProduct } from "../../api/login";
  3. import { debounce } from '../../utils/util'
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. // pages/orders/orders.ts
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. current: 0,
  13. autoplay: false,
  14. interval: 5000,
  15. duration: 500,
  16. popupShow: false,
  17. list: [] as any,
  18. isOverSaled: false, // 是否所有商品都没有库存
  19. selected: {} as any,
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad() {
  25. this.onInit()
  26. },
  27. /**
  28. * 获取基础信息
  29. */
  30. async onInit() {
  31. try {
  32. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  33. const list = data.data || []
  34. let selected: any = {}
  35. let isOverSaled = true // 是否销售完
  36. list.forEach((item: any) => {
  37. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  38. item.typeName = this.formatPeriod(item.num, item.period);
  39. const prices: any = this.formatPrice(item.salePrice)
  40. item.integerPart = prices.integerPart
  41. item.decimalPart = prices.decimalPart
  42. if(item.stockNum > 0) {
  43. isOverSaled = false
  44. if( !selected.id) {
  45. selected = item
  46. }
  47. }
  48. });
  49. if(isOverSaled) {
  50. // 没有可购买商品则默认选中第一个商品
  51. selected = list[0]
  52. }
  53. this.setData({
  54. list,
  55. isOverSaled,
  56. selected
  57. })
  58. } catch(e) {
  59. console.log(e, 'e')
  60. }
  61. },
  62. // 格式化价格
  63. formatPrice(price: number, type?: string) {
  64. const amountStr = price.toFixed(2)
  65. const [integerPart, decimalPart] = amountStr.split('.');
  66. if(type === 'ALL') {
  67. return amountStr
  68. }
  69. return {
  70. integerPart,
  71. decimalPart
  72. }
  73. },
  74. // 格式化类型
  75. formatPeriod(num: number, type: string) {
  76. const template: any = {
  77. DAY: "天卡",
  78. MONTH: "月卡",
  79. YEAR: "年卡"
  80. }
  81. if(type === "YEAR" && num >= 99) {
  82. return '终生卡'
  83. }
  84. return num + template[type]
  85. },
  86. // 选择
  87. onSelectGoods(e: any) {
  88. const { dataset } = e.currentTarget
  89. const item = this.data.list.find((item: any) => item.id === dataset.id)
  90. // 判断是否有库存
  91. if(item.stockNum <= 0) {
  92. return
  93. }
  94. this.setData({
  95. selected: item || {}
  96. })
  97. },
  98. // 事件处理函数
  99. changeSwiper(e: any) {
  100. const detail = e.detail;
  101. if(detail.source === 'touch' || detail.source == 'autoplay') {
  102. this.setData({
  103. current: detail.current
  104. })
  105. }
  106. },
  107. isLogin() {
  108. // 判断是否登录
  109. if(!app.globalData.isLogin) {
  110. wx.navigateTo({
  111. url: '../login/login',
  112. })
  113. return false
  114. }
  115. return true
  116. },
  117. /** 我的订单 */
  118. onOrder() {
  119. // 判断是否登录
  120. if(!this.isLogin()) {
  121. return
  122. }
  123. wx.navigateTo({
  124. url: '../orders/orders',
  125. })
  126. },
  127. onBuyShop() {
  128. // 判断是否登录
  129. if(!this.isLogin()) {
  130. return
  131. }
  132. this.setData({
  133. popupShow: true
  134. })
  135. },
  136. onClose() {
  137. this.setData({
  138. popupShow: false
  139. })
  140. },
  141. onSubmit() {
  142. // 判断是否登录
  143. const that = this
  144. debounce(function () {
  145. if(!that.isLogin()) {
  146. return
  147. }
  148. let info = JSON.stringify({
  149. ...that.data.selected
  150. });
  151. console.log(that.data.selected, "that.data.selected")
  152. info = encodeURIComponent(info);
  153. wx.navigateTo({
  154. url: `../orders/order-detail?orderInfo=${info}`,
  155. })
  156. that.setData({
  157. popupShow: false
  158. })
  159. }, 500)()
  160. },
  161. /**
  162. * 生命周期函数--监听页面显示
  163. */
  164. onShow() {
  165. this.onInit()
  166. },
  167. })