index.ts 13 KB

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