index.ts 8.7 KB

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