index.ts 13 KB

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