index.ts 4.0 KB

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