index.ts 3.8 KB

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