index.ts 8.5 KB

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