index.ts 3.7 KB

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