index.ts 4.7 KB

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