index.ts 8.7 KB

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