index.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/1738822393434.png",
  25. "https://oss.dayaedu.com/ktyq/1738822501555.png",
  26. "https://oss.dayaedu.com/ktyq/1738822528040.png",
  27. "https://oss.dayaedu.com/ktyq/1738822590749.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. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  186. const list = data.data || [];
  187. let selected: any = {};
  188. let isOverSaled = true; // 是否销售完
  189. list.forEach((item: any) => {
  190. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  191. item.showSalePrice = formatPrice(item.salePrice, "ALL");
  192. item.typeName = this.formatPeriod(item.num, item.period);
  193. item.discountPrice = formatPrice(
  194. item.originalPrice - item.salePrice,
  195. "ALL"
  196. );
  197. const prices: any = formatPrice(item.salePrice);
  198. item.integerPart = prices.integerPart;
  199. item.decimalPart = prices.decimalPart;
  200. // 格式化赠送内容
  201. item.giftLongTime = item.giftFlag
  202. ? this.formatGiftPeriod(item.giftVipDay, item.giftPeriod)
  203. : "";
  204. if (item.stockNum > 0) {
  205. isOverSaled = false;
  206. if (!selected.id) {
  207. selected = item;
  208. }
  209. }
  210. });
  211. if (isOverSaled) {
  212. // 没有可购买商品则默认选中第一个商品
  213. selected = list[0];
  214. }
  215. this.setData({
  216. list,
  217. instrumentList, // 乐器列表
  218. isOverSaled,
  219. selected,
  220. selectInstrumentId: '',
  221. selectedInstrument: {}
  222. }, () => {
  223. this.onFormatGoods()
  224. });
  225. } catch (e) {
  226. console.log(e, "e");
  227. }
  228. },
  229. // 格式化类型
  230. formatPeriod(num: number, type: string) {
  231. const template: any = {
  232. DAY: "天卡",
  233. MONTH: "月卡",
  234. YEAR: "年卡",
  235. };
  236. if (type === "YEAR" && num >= 99) {
  237. return "永久卡";
  238. }
  239. return num + template[type];
  240. },
  241. formatGiftPeriod(num: number, type: string) {
  242. if (!num || !type) {
  243. return "";
  244. }
  245. const template: any = {
  246. DAY: "天",
  247. MONTH: "个月",
  248. YEAR: "年",
  249. };
  250. return num + template[type];
  251. },
  252. // 选择
  253. onSelectGoods(e: any) {
  254. const { dataset } = e.currentTarget;
  255. const item = this.data.list.find((item: any) => item.id === dataset.id);
  256. // 判断是否有库存
  257. if (item.stockNum <= 0) {
  258. return;
  259. }
  260. this.setData({
  261. selected: item || {}
  262. }, () => {
  263. this.onFormatGoods()
  264. });
  265. },
  266. /** 选中乐器 */
  267. onSelectInstrument(e: any) {
  268. const { dataset } = e.currentTarget;
  269. if (dataset.id === this.data.selectInstrumentId) {
  270. this.setData({
  271. selectInstrumentId: '',
  272. selectedInstrument: {}
  273. }, () => {
  274. this.onFormatGoods()
  275. })
  276. } else {
  277. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  278. this.setData({
  279. selectInstrumentId: dataset.id,
  280. selectedInstrument: item || {}
  281. }, () => {
  282. this.onFormatGoods()
  283. })
  284. }
  285. },
  286. /** 格式化选中的商品 */
  287. onFormatGoods() {
  288. const selected = this.data.selected;
  289. const selectedInstrument = this.data.selectedInstrument
  290. const params = {
  291. typeName: '',
  292. showSalePrice: '' as any, // 显示的现价
  293. originalPrice: 0, // 原价
  294. salePrice: 0, // 现价
  295. discountPrice: '' as any, // 已省
  296. integerPart: '',
  297. decimalPart: '',
  298. }
  299. // 选中期限
  300. if (selected.id) {
  301. params.typeName = selected.typeName
  302. params.showSalePrice = selected.showSalePrice
  303. params.originalPrice = selected.originalPrice
  304. params.salePrice = selected.salePrice
  305. params.discountPrice = selected.discountPrice
  306. const prices: any = formatPrice(params.salePrice);
  307. params.integerPart = prices.integerPart
  308. params.decimalPart = prices.decimalPart
  309. }
  310. // 选中乐器
  311. if (selectedInstrument.id) {
  312. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  313. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  314. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  315. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  316. params.discountPrice = formatPrice(
  317. params.originalPrice - params.salePrice,
  318. "ALL"
  319. );
  320. const prices: any = formatPrice(params.salePrice);
  321. params.integerPart = prices.integerPart
  322. params.decimalPart = prices.decimalPart
  323. }
  324. this.setData({
  325. formatSelectGood: params
  326. })
  327. },
  328. // 事件处理函数
  329. changeSwiper(e: any) {
  330. const detail = e.detail;
  331. if (detail.source === "touch" || detail.source == "autoplay") {
  332. this.setData({
  333. current: detail.current,
  334. });
  335. this.onTitlePlay();
  336. }
  337. },
  338. changeSwiperDetail(e: any) {
  339. const detail = e.detail;
  340. if (detail.source === "touch" || detail.source == "autoplay") {
  341. this.setData({
  342. detailCurrent: detail.current,
  343. });
  344. }
  345. },
  346. isLogin() {
  347. // 判断是否登录
  348. if (!app.globalData.isLogin) {
  349. wx.navigateTo({
  350. url: "../login/login",
  351. });
  352. return false;
  353. }
  354. return true;
  355. },
  356. /** 我的订单 */
  357. onOrder() {
  358. // 判断是否登录
  359. if (!this.isLogin()) {
  360. return;
  361. }
  362. wx.navigateTo({
  363. url: "../orders/orders",
  364. });
  365. },
  366. onBuyShop() {
  367. // 判断是否登录
  368. if (!this.isLogin()) {
  369. return;
  370. }
  371. this.setData({
  372. popupShow: true,
  373. // showSelectedProduct: true,
  374. });
  375. },
  376. onClose() {
  377. this.setData({
  378. popupShow: false,
  379. });
  380. },
  381. onSubmit() {
  382. // 判断是否登录
  383. const that = this;
  384. debounce(function () {
  385. if (!that.isLogin()) {
  386. return;
  387. }
  388. const params = [] as any
  389. const selected = that.data.selected
  390. if (selected.id) {
  391. params.push({
  392. pic: selected.pic,
  393. name: selected.name,
  394. originalPrice: selected.originalPrice,
  395. salePrice: selected.salePrice,
  396. shopId: selected.shopId,
  397. id: selected.id,
  398. goodsType: 'ACTIVATION_CODE', // INSTRUMENTS
  399. })
  400. }
  401. const selectedInstrument = that.data.selectedInstrument
  402. if (selectedInstrument.id) {
  403. params.push({
  404. pic: selectedInstrument.pic,
  405. name: selectedInstrument.name,
  406. originalPrice: selectedInstrument.originalPrice,
  407. salePrice: selectedInstrument.salePrice,
  408. shopId: selectedInstrument.shopId,
  409. id: selectedInstrument.id,
  410. goodsType: 'INSTRUMENTS', // INSTRUMENTS
  411. })
  412. }
  413. let info = JSON.stringify({
  414. ...params
  415. });
  416. info = encodeURIComponent(info);
  417. wx.navigateTo({
  418. url: `../orders/order-detail?orderInfo=${info}`,
  419. success: () => {
  420. that.setData({
  421. popupShow: false,
  422. });
  423. },
  424. });
  425. }, 200)();
  426. },
  427. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  428. wx.previewImage({
  429. current: e.currentTarget.dataset.src,
  430. urls: this.data.imgList,
  431. success: () => {
  432. this.setData({
  433. isFromPreviewImage: true,
  434. });
  435. },
  436. });
  437. },
  438. onPreivewDetailImg(e: { currentTarget: { dataset: any } }) {
  439. wx.previewImage({
  440. current: e.currentTarget.dataset.src,
  441. urls: [
  442. "https://oss.dayaedu.com/ktyq/1733403675345.png",
  443. "https://oss.dayaedu.com/ktyq/1733403707851.png",
  444. "https://oss.dayaedu.com/ktyq/1733403725644.png",
  445. ],
  446. success: () => {
  447. this.setData({
  448. isFromPreviewImage: true,
  449. });
  450. },
  451. });
  452. },
  453. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  454. wx.previewImage({
  455. current: e.currentTarget.dataset.src,
  456. urls: this.data.goodsImgList,
  457. success: () => {
  458. this.setData({
  459. isFromPreviewImage: true,
  460. });
  461. },
  462. });
  463. },
  464. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  465. wx.previewImage({
  466. current: e.currentTarget.dataset.src,
  467. urls: [e.currentTarget.dataset.src],
  468. success: () => {
  469. this.setData({
  470. isFromPreviewImage: true,
  471. });
  472. },
  473. });
  474. },
  475. /**
  476. * 生命周期函数--监听页面显示
  477. */
  478. onShow() {
  479. if (!this.data.isFromPreviewImage) {
  480. this.onInit();
  481. } else {
  482. this.setData({
  483. isFromPreviewImage: false,
  484. });
  485. }
  486. this.setData({
  487. serviceShow: true,
  488. });
  489. },
  490. onHide() {
  491. this.setData({
  492. serviceShow: false,
  493. });
  494. },
  495. // 页面滚动时颜色变化
  496. onScrollView(e: { detail: any }) {
  497. const top = e.detail.scrollTop || 0;
  498. // const scrollHeight = e.detail.scrollHeight || 0;
  499. // 从100开始显示
  500. this.setData({
  501. // opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : (top - 100) / 150
  502. opacity: top < 100 ? 0 : top - 100 > 150 ? 1 : 1,
  503. });
  504. // if (top + this.data.initialScrollHeight >= scrollHeight - 80) {
  505. // // console.log('已经滑动到底部了');
  506. // // 相应业务逻辑处理
  507. // this.setData({
  508. // scrolIntoViewStr: "type2",
  509. // });
  510. // } else {
  511. // console.log(this.data.scrollDiscount, top, this.data.headerHeight, "top");
  512. if (this.data.scrollIntoViewType) {
  513. this.setData({
  514. scrollTop: this.data.scrollDiscount
  515. ? top - this.data.headerHeight
  516. : top,
  517. scrollIntoViewType: false,
  518. scrollDiscount: false,
  519. isScrollTT: true,
  520. });
  521. } else {
  522. if (!this.data.isScrollTT) {
  523. this.onChangeScroll();
  524. } else {
  525. this.setData({
  526. isScrollTT: false,
  527. });
  528. }
  529. }
  530. // }
  531. },
  532. onChangeScroll() {
  533. const that = this;
  534. debounce(function () {
  535. wx.createSelectorQuery()
  536. .select("#type3")
  537. .boundingClientRect(function (rect) {
  538. let check = false;
  539. if (rect.top > 0 && rect.top <= that.data.headerHeight) {
  540. that.setData({
  541. scrolIntoViewStr: "type3",
  542. });
  543. check = true;
  544. }
  545. if (rect.top > 0 && rect.top > that.data.headerHeight) {
  546. that.setData({
  547. scrolIntoViewStr: "type1",
  548. });
  549. check = true;
  550. }
  551. // console.log('checked', check)
  552. if (!check) {
  553. wx.createSelectorQuery()
  554. .select("#type2")
  555. .boundingClientRect(function (rect) {
  556. if (rect.top > 0 && rect.top <= that.data.headerHeight) {
  557. that.setData({
  558. scrolIntoViewStr: "type2",
  559. });
  560. }
  561. if (rect.top > 0 && rect.top > that.data.headerHeight) {
  562. that.setData({
  563. scrolIntoViewStr: "type3",
  564. });
  565. }
  566. })
  567. .exec();
  568. }
  569. })
  570. .exec();
  571. }, 100)();
  572. },
  573. onTapAnchor(e: { currentTarget: { dataset: any } }) {
  574. const type = e.currentTarget.dataset.type;
  575. this.setData({
  576. scrolIntoView: type,
  577. scrolIntoViewStr: type,
  578. scrollDiscount: true, // type !== 'type2' ? true : false,
  579. scrollIntoViewType: true,
  580. });
  581. },
  582. onShareAppMessage() {
  583. return {
  584. title: "音乐数字AI",
  585. path: "/pages/index/index",
  586. imageUrl: "https://oss.dayaedu.com/ktyq/1733309357691.png",
  587. };
  588. },
  589. onShareTimeline() {
  590. return {
  591. title: "音乐数字AI",
  592. path: "/pages/index/index",
  593. imageUrl: "https://oss.dayaedu.com/ktyq/1733309357691.png",
  594. };
  595. },
  596. });