index.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. imgList: [
  13. 'https://oss.dayaedu.com/ktyq/1732585725698.png',
  14. 'https://oss.dayaedu.com/ktyq/1732519468124.png',
  15. 'https://oss.dayaedu.com/ktyq/1732519479416.png',
  16. 'https://oss.dayaedu.com/ktyq/1732519490037.png',
  17. 'https://oss.dayaedu.com/ktyq/1732519500580.png'
  18. // 'https://oss.dayaedu.com/ktyq/1732174025169.png',
  19. // 'https://oss.dayaedu.com/ktyq/1732501351014.png',
  20. // 'https://oss.dayaedu.com/ktyq/1732174043543.png'
  21. ],
  22. goodsImgList: [
  23. 'https://oss.dayaedu.com/ktyq/1732175006625.png',
  24. 'https://oss.dayaedu.com/ktyq/1732175021350.png',
  25. 'https://oss.dayaedu.com/ktyq/1732175031878.png',
  26. 'https://oss.dayaedu.com/ktyq/1732175041724.png',
  27. 'https://oss.dayaedu.com/ktyq/1732507279822.png'
  28. ],
  29. serviceShow: true,
  30. scrollTop: 0,
  31. current: 0,
  32. autoplay: false,
  33. interval: 5000,
  34. duration: 500,
  35. popupShow: false,
  36. list: [] as any,
  37. isOverSaled: false, // 是否所有商品都没有库存
  38. selected: {} as any,
  39. opacity: 0,
  40. scrolIntoViewStr: '',
  41. scrolIntoView: '',
  42. scrollDiscount: false, // 是否扣减启
  43. isScrollTT: false,
  44. scrollIntoViewType: false,
  45. headerHeight: 0, // 头部的高度
  46. },
  47. /**
  48. * 生命周期函数--监听页面加载
  49. */
  50. onLoad() {
  51. // this.onInit()
  52. },
  53. onReady() {
  54. const that = this
  55. wx.createSelectorQuery().select('#scroll-header').boundingClientRect(function (rect) {
  56. that.setData({
  57. headerHeight: rect.height
  58. })
  59. }).exec();
  60. },
  61. /**
  62. * 获取基础信息
  63. */
  64. async onInit() {
  65. try {
  66. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  67. const list = data.data || []
  68. let selected: any = {}
  69. let isOverSaled = true // 是否销售完
  70. list.forEach((item: any) => {
  71. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  72. item.showSalePrice = this.formatPrice(item.salePrice, 'ALL');
  73. item.typeName = this.formatPeriod(item.num, item.period);
  74. item.discountPrice = this.formatPrice(item.originalPrice - item.salePrice, 'ALL')
  75. const prices: any = this.formatPrice(item.salePrice)
  76. item.integerPart = prices.integerPart
  77. item.decimalPart = prices.decimalPart
  78. if(item.stockNum > 0) {
  79. isOverSaled = false
  80. if( !selected.id) {
  81. selected = item
  82. }
  83. }
  84. });
  85. if(isOverSaled) {
  86. // 没有可购买商品则默认选中第一个商品
  87. selected = list[0]
  88. }
  89. this.setData({
  90. list,
  91. isOverSaled,
  92. selected
  93. })
  94. } catch(e) {
  95. console.log(e, 'e')
  96. }
  97. },
  98. // 格式化价格
  99. formatPrice(price: number, type?: string) {
  100. const amountStr = price.toFixed(2)
  101. const [integerPart, decimalPart] = amountStr.split('.');
  102. if(type === 'ALL') {
  103. return amountStr
  104. }
  105. return {
  106. integerPart,
  107. decimalPart
  108. }
  109. },
  110. // 格式化类型
  111. formatPeriod(num: number, type: string) {
  112. const template: any = {
  113. DAY: "天卡",
  114. MONTH: "月卡",
  115. YEAR: "年卡"
  116. }
  117. if(type === "YEAR" && num >= 99) {
  118. return '永久卡'
  119. }
  120. return num + template[type]
  121. },
  122. // 选择
  123. onSelectGoods(e: any) {
  124. const { dataset } = e.currentTarget
  125. const item = this.data.list.find((item: any) => item.id === dataset.id)
  126. // 判断是否有库存
  127. if(item.stockNum <= 0) {
  128. return
  129. }
  130. this.setData({
  131. selected: item || {}
  132. })
  133. },
  134. // 事件处理函数
  135. changeSwiper(e: any) {
  136. const detail = e.detail;
  137. if(detail.source === 'touch' || detail.source == 'autoplay') {
  138. this.setData({
  139. current: detail.current
  140. })
  141. }
  142. },
  143. isLogin() {
  144. // 判断是否登录
  145. if(!app.globalData.isLogin) {
  146. wx.navigateTo({
  147. url: '../login/login',
  148. })
  149. return false
  150. }
  151. return true
  152. },
  153. /** 我的订单 */
  154. onOrder() {
  155. // 判断是否登录
  156. if(!this.isLogin()) {
  157. return
  158. }
  159. wx.navigateTo({
  160. url: '../orders/orders',
  161. })
  162. },
  163. onBuyShop() {
  164. // 判断是否登录
  165. if(!this.isLogin()) {
  166. return
  167. }
  168. this.setData({
  169. popupShow: true
  170. })
  171. },
  172. onClose() {
  173. this.setData({
  174. popupShow: false
  175. })
  176. },
  177. onSubmit() {
  178. // 判断是否登录
  179. const that = this
  180. debounce(function () {
  181. if(!that.isLogin()) {
  182. return
  183. }
  184. let info = JSON.stringify({
  185. ...that.data.selected
  186. });
  187. // console.log(that.data.selected, "that.data.selected")
  188. info = encodeURIComponent(info);
  189. wx.navigateTo({
  190. url: `../orders/order-detail?orderInfo=${info}`,
  191. })
  192. that.setData({
  193. popupShow: false
  194. })
  195. }, 500)()
  196. },
  197. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  198. wx.previewImage({
  199. current: e.currentTarget.dataset.src,
  200. urls: this.data.imgList
  201. })
  202. },
  203. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  204. wx.previewImage({
  205. current: e.currentTarget.dataset.src,
  206. urls: this.data.goodsImgList
  207. })
  208. },
  209. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  210. wx.previewImage({
  211. current: e.currentTarget.dataset.src,
  212. urls: [e.currentTarget.dataset.src]
  213. })
  214. },
  215. /**
  216. * 生命周期函数--监听页面显示
  217. */
  218. onShow() {
  219. this.onInit()
  220. this.setData({
  221. serviceShow: true
  222. })
  223. },
  224. onHide() {
  225. this.setData({
  226. serviceShow: false
  227. })
  228. },
  229. // onReady() {
  230. // const scrollView = this.selectComponent('#scrollarea');
  231. // console.log(scrollView, 'scrollView')
  232. // },
  233. // 页面滚动时颜色变化
  234. onScrollView(e: { detail: any }) {
  235. const top = e.detail.scrollTop || 0
  236. // 从100开始显示
  237. this.setData({
  238. // opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : (top - 100) / 150
  239. opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : 1
  240. })
  241. if(this.data.scrollIntoViewType) {
  242. this.setData({
  243. scrollTop: this.data.scrollDiscount ? top - this.data.headerHeight : top,
  244. scrollIntoViewType: false,
  245. scrollDiscount: false,
  246. isScrollTT: true,
  247. })
  248. } else {
  249. if(!this.data.isScrollTT) {
  250. this.onChangeScroll()
  251. } else {
  252. this.setData({
  253. isScrollTT: false,
  254. })
  255. }
  256. }
  257. },
  258. onChangeScroll() {
  259. const that = this;
  260. debounce(function() {
  261. wx.createSelectorQuery().select('#type3').boundingClientRect(function (rect) {
  262. if(rect.top > 0 && rect.top <= that.data.headerHeight) {
  263. that.setData({
  264. scrolIntoViewStr: 'type3',
  265. })
  266. }
  267. }).exec();
  268. wx.createSelectorQuery().select('#type2').boundingClientRect(function (rect) {
  269. if(rect.top > 0 && rect.top <= that.data.headerHeight) {
  270. that.setData({
  271. scrolIntoViewStr: 'type2'
  272. })
  273. }
  274. if(rect.top > 0 && rect.top > that.data.headerHeight) {
  275. that.setData({
  276. scrolIntoViewStr: 'type1'
  277. })
  278. }
  279. }).exec();
  280. }, 100)()
  281. },
  282. onTapAnchor(e: { currentTarget: { dataset: any } }) {
  283. const type = e.currentTarget.dataset.type
  284. this.setData({
  285. scrolIntoView: type,
  286. scrolIntoViewStr: type,
  287. scrollDiscount: type !== 'type3' ? true : false,
  288. scrollIntoViewType: true,
  289. })
  290. },
  291. onLookMore() {
  292. this.setData({
  293. popupShow: true
  294. })
  295. }
  296. })