index.ts 17 KB

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