index.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. ],
  19. goodsImgList: [
  20. 'https://oss.dayaedu.com/ktyq/1732175006625.png',
  21. 'https://oss.dayaedu.com/ktyq/1732175021350.png',
  22. 'https://oss.dayaedu.com/ktyq/1732175031878.png',
  23. 'https://oss.dayaedu.com/ktyq/1733406956046abd99e75.png',
  24. 'https://oss.dayaedu.com/ktyq/173340695604938755d8b.png',
  25. 'https://oss.dayaedu.com/ktyq/17333975985307c7f2bec.png'
  26. ],
  27. detailImgList: [
  28. {
  29. url: 'https://oss.dayaedu.com/ktyq/1733403675345.png',
  30. text: '合作学校音乐数字课堂公开课'
  31. },
  32. {
  33. url: 'https://oss.dayaedu.com/ktyq/1733403707851.png',
  34. text: '音乐课课堂器乐教学'
  35. },
  36. {
  37. url: 'https://oss.dayaedu.com/ktyq/1733403725644.png',
  38. text: '学期末汇报演出'
  39. }
  40. ],
  41. serviceShow: true,
  42. scrollTop: 0,
  43. current: 0,
  44. detailCurrent: 0,
  45. autoplay: false,
  46. interval: 5000,
  47. duration: 500,
  48. popupShow: false,
  49. list: [] as any,
  50. isOverSaled: false, // 是否所有商品都没有库存
  51. selected: {} as any,
  52. opacity: 0,
  53. showSelectedProduct: false, // 是否显示选中商品值
  54. scrolIntoViewStr: '',
  55. scrolIntoView: '',
  56. scrollDiscount: false, // 是否扣减启
  57. isScrollTT: false,
  58. scrollIntoViewType: false,
  59. headerHeight: 0, // 头部的高度
  60. initialScrollHeight: 0, // 滚动高度
  61. isFromPreviewImage: false,
  62. bannerPlay: false, // 视频是否播放
  63. titleControls: false, // 详情是否显示控制条
  64. liuControls: false, // 详情是否显示控制条
  65. bannerImageloaded: false, // Banner图片是否加载完成
  66. },
  67. /**
  68. * 生命周期函数--监听页面加载
  69. */
  70. onLoad() {
  71. // this.onInit()
  72. },
  73. onReady() {
  74. const that = this
  75. wx.createSelectorQuery().select('#scroll-header').boundingClientRect(function (rect) {
  76. that.setData({
  77. headerHeight: rect.height
  78. })
  79. }).exec();
  80. wx.createSelectorQuery().select('#scroll-view').boundingClientRect(function (rect) {
  81. // console.log(rect, 'rect')
  82. that.setData({
  83. initialScrollHeight: rect.height
  84. })
  85. }).exec();
  86. },
  87. onBannerVideoLoad() {
  88. this.setData({
  89. bannerImageloaded: true
  90. })
  91. },
  92. onBannerPlay() {
  93. const bannerVideo = wx.createVideoContext('bannerVideo')
  94. const titleVideo = wx.createVideoContext('titleVideo')
  95. const liuVideo = wx.createVideoContext('liuVideo')
  96. this.setData({
  97. bannerPlay: true
  98. }, () => {
  99. titleVideo.stop()
  100. bannerVideo.play()
  101. liuVideo.stop()
  102. })
  103. },
  104. onBannerVideoPlay() {
  105. const titleVideo = wx.createVideoContext('titleVideo')
  106. titleVideo.stop()
  107. const liuVideo = wx.createVideoContext('liuVideo')
  108. liuVideo.stop()
  109. },
  110. onTitlePlay() {
  111. const bannerVideo = wx.createVideoContext('bannerVideo')
  112. bannerVideo.stop()
  113. const liuVideo = wx.createVideoContext('liuVideo')
  114. liuVideo.stop()
  115. },
  116. onTItleVideoPlay() {
  117. const bannerVideo = wx.createVideoContext('bannerVideo')
  118. const titleVideo = wx.createVideoContext('titleVideo')
  119. const liuVideo = wx.createVideoContext('liuVideo')
  120. this.setData({
  121. titleControls: true
  122. }, () => {
  123. titleVideo.play()
  124. bannerVideo.stop()
  125. liuVideo.stop()
  126. })
  127. },
  128. onLiuPlay() {
  129. const bannerVideo = wx.createVideoContext('bannerVideo')
  130. bannerVideo.stop()
  131. const titleVideo = wx.createVideoContext('titleVideo')
  132. titleVideo.stop()
  133. },
  134. onLiuVideoPlay() {
  135. const bannerVideo = wx.createVideoContext('bannerVideo')
  136. const titleVideo = wx.createVideoContext('titleVideo')
  137. const liuVideo = wx.createVideoContext('liuVideo')
  138. this.setData({
  139. liuControls: true
  140. }, () => {
  141. liuVideo.play()
  142. bannerVideo.stop()
  143. titleVideo.stop()
  144. })
  145. },
  146. /**
  147. * 获取基础信息
  148. */
  149. async onInit() {
  150. try {
  151. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  152. const list = data.data || []
  153. let selected: any = {}
  154. let isOverSaled = true // 是否销售完
  155. list.forEach((item: any) => {
  156. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  157. item.showSalePrice = this.formatPrice(item.salePrice, 'ALL');
  158. item.typeName = this.formatPeriod(item.num, item.period);
  159. item.discountPrice = this.formatPrice(item.originalPrice - item.salePrice, 'ALL')
  160. const prices: any = this.formatPrice(item.salePrice)
  161. item.integerPart = prices.integerPart
  162. item.decimalPart = prices.decimalPart
  163. if(item.stockNum > 0) {
  164. isOverSaled = false
  165. if( !selected.id) {
  166. selected = item
  167. }
  168. }
  169. });
  170. if(isOverSaled) {
  171. // 没有可购买商品则默认选中第一个商品
  172. selected = list[0]
  173. }
  174. this.setData({
  175. list,
  176. isOverSaled,
  177. selected
  178. })
  179. } catch(e) {
  180. console.log(e, 'e')
  181. }
  182. },
  183. // 格式化价格
  184. formatPrice(price: number, type?: string) {
  185. const amountStr = price.toFixed(2)
  186. const [integerPart, decimalPart] = amountStr.split('.');
  187. if(type === 'ALL') {
  188. return amountStr
  189. }
  190. return {
  191. integerPart,
  192. decimalPart
  193. }
  194. },
  195. // 格式化类型
  196. formatPeriod(num: number, type: string) {
  197. const template: any = {
  198. DAY: "天卡",
  199. MONTH: "月卡",
  200. YEAR: "年卡"
  201. }
  202. if(type === "YEAR" && num >= 99) {
  203. return '永久卡'
  204. }
  205. return num + template[type]
  206. },
  207. // 选择
  208. onSelectGoods(e: any) {
  209. const { dataset } = e.currentTarget
  210. const item = this.data.list.find((item: any) => item.id === dataset.id)
  211. // 判断是否有库存
  212. if(item.stockNum <= 0) {
  213. return
  214. }
  215. this.setData({
  216. selected: item || {}
  217. })
  218. },
  219. // 事件处理函数
  220. changeSwiper(e: any) {
  221. const detail = e.detail;
  222. if(detail.source === 'touch' || detail.source == 'autoplay') {
  223. this.setData({
  224. current: detail.current
  225. })
  226. this.onTitlePlay()
  227. }
  228. },
  229. changeSwiperDetail(e: any) {
  230. const detail = e.detail
  231. if(detail.source === 'touch' || detail.source == 'autoplay') {
  232. this.setData({
  233. detailCurrent: detail.current
  234. })
  235. }
  236. },
  237. isLogin() {
  238. // 判断是否登录
  239. if(!app.globalData.isLogin) {
  240. wx.navigateTo({
  241. url: '../login/login',
  242. })
  243. return false
  244. }
  245. return true
  246. },
  247. /** 我的订单 */
  248. onOrder() {
  249. // 判断是否登录
  250. if(!this.isLogin()) {
  251. return
  252. }
  253. wx.navigateTo({
  254. url: '../orders/orders',
  255. })
  256. },
  257. onBuyShop() {
  258. // 判断是否登录
  259. if(!this.isLogin()) {
  260. return
  261. }
  262. this.setData({
  263. popupShow: true,
  264. showSelectedProduct: true,
  265. })
  266. },
  267. onClose() {
  268. this.setData({
  269. popupShow: false
  270. })
  271. },
  272. onSubmit() {
  273. // 判断是否登录
  274. const that = this
  275. debounce(function () {
  276. if(!that.isLogin()) {
  277. return
  278. }
  279. let info = JSON.stringify({
  280. ...that.data.selected
  281. });
  282. // console.log(that.data.selected, "that.data.selected")
  283. info = encodeURIComponent(info);
  284. wx.navigateTo({
  285. url: `../orders/order-detail?orderInfo=${info}`,
  286. success: () => {
  287. that.setData({
  288. popupShow: false
  289. })
  290. }
  291. })
  292. }, 500)()
  293. },
  294. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  295. wx.previewImage({
  296. current: e.currentTarget.dataset.src,
  297. urls: this.data.imgList,
  298. success: () => {
  299. this.setData({
  300. isFromPreviewImage: true
  301. })
  302. }
  303. })
  304. },
  305. onPreivewDetailImg(e: { currentTarget: { dataset: any } }) {
  306. wx.previewImage({
  307. current: e.currentTarget.dataset.src,
  308. urls: ['https://oss.dayaedu.com/ktyq/1733403675345.png','https://oss.dayaedu.com/ktyq/1733403707851.png','https://oss.dayaedu.com/ktyq/1733403725644.png',
  309. ],
  310. success: () => {
  311. this.setData({
  312. isFromPreviewImage: true
  313. })
  314. }
  315. })
  316. },
  317. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  318. wx.previewImage({
  319. current: e.currentTarget.dataset.src,
  320. urls: this.data.goodsImgList,
  321. success: () => {
  322. this.setData({
  323. isFromPreviewImage: true
  324. })
  325. }
  326. })
  327. },
  328. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  329. wx.previewImage({
  330. current: e.currentTarget.dataset.src,
  331. urls: [e.currentTarget.dataset.src],
  332. success: () => {
  333. this.setData({
  334. isFromPreviewImage: true
  335. })
  336. }
  337. })
  338. },
  339. /**
  340. * 生命周期函数--监听页面显示
  341. */
  342. onShow() {
  343. if(!this.data.isFromPreviewImage) {
  344. this.onInit()
  345. } else {
  346. this.setData({
  347. isFromPreviewImage: false
  348. })
  349. }
  350. this.setData({
  351. serviceShow: true
  352. })
  353. },
  354. onHide() {
  355. this.setData({
  356. serviceShow: false
  357. })
  358. },
  359. // 页面滚动时颜色变化
  360. onScrollView(e: { detail: any }) {
  361. const top = e.detail.scrollTop || 0
  362. const scrollHeight = e.detail.scrollHeight || 0
  363. // 从100开始显示
  364. // console.log(top, scrollHeight, this.data.initialScrollHeight, '121121221')
  365. this.setData({
  366. // opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : (top - 100) / 150
  367. opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : 1
  368. })
  369. if (top + this.data.initialScrollHeight >= scrollHeight - 80) {
  370. // console.log('已经滑动到底部了');
  371. // 相应业务逻辑处理
  372. this.setData({
  373. scrolIntoViewStr: 'type2'
  374. })
  375. } else {
  376. if(this.data.scrollIntoViewType) {
  377. this.setData({
  378. scrollTop: this.data.scrollDiscount ? top - this.data.headerHeight : top,
  379. scrollIntoViewType: false,
  380. scrollDiscount: false,
  381. isScrollTT: true,
  382. })
  383. } else {
  384. if(!this.data.isScrollTT) {
  385. this.onChangeScroll()
  386. } else {
  387. this.setData({
  388. isScrollTT: false,
  389. })
  390. }
  391. }
  392. }
  393. },
  394. onChangeScroll() {
  395. const that = this;
  396. debounce(function() {
  397. wx.createSelectorQuery().select('#type3').boundingClientRect(function (rect) {
  398. let check = false
  399. if(rect.top > 0 && (rect.top <= that.data.headerHeight )) {
  400. that.setData({
  401. scrolIntoViewStr: 'type3',
  402. })
  403. check = true
  404. }
  405. if(rect.top > 0 && rect.top > that.data.headerHeight) {
  406. that.setData({
  407. scrolIntoViewStr: 'type1'
  408. })
  409. check = true
  410. }
  411. // console.log('checked', check)
  412. if(!check) {
  413. wx.createSelectorQuery().select('#type2').boundingClientRect(function (rect) {
  414. if(rect.top > 0 && rect.top <= that.data.headerHeight) {
  415. that.setData({
  416. scrolIntoViewStr: 'type2'
  417. })
  418. }
  419. if(rect.top > 0 && rect.top > that.data.headerHeight) {
  420. that.setData({
  421. scrolIntoViewStr: 'type3'
  422. })
  423. }
  424. }).exec();
  425. }
  426. }).exec();
  427. }, 100)()
  428. },
  429. onTapAnchor(e: { currentTarget: { dataset: any } }) {
  430. const type = e.currentTarget.dataset.type
  431. this.setData({
  432. scrolIntoView: type,
  433. scrolIntoViewStr: type,
  434. scrollDiscount: true, // type !== 'type2' ? true : false,
  435. scrollIntoViewType: true,
  436. })
  437. },
  438. onShareAppMessage() {
  439. return {
  440. title: '音乐数字AI',
  441. path: '/pages/index/index',
  442. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  443. }
  444. },
  445. onShareTimeline() {
  446. return {
  447. title: '音乐数字AI',
  448. path: '/pages/index/index',
  449. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  450. }
  451. }
  452. // onLookMore() {
  453. // this.setData({
  454. // popupShow: true
  455. // })
  456. // }
  457. })