index.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/1732174025169.png',
  19. // 'https://oss.dayaedu.com/ktyq/1732501351014.png',
  20. // 'https://oss.dayaedu.com/ktyq/1732174043543.png'
  21. ],
  22. goodsImgList: [
  23. 'https://oss.dayaedu.com/ktyq/1732175006625.png',
  24. 'https://oss.dayaedu.com/ktyq/1732175021350.png',
  25. 'https://oss.dayaedu.com/ktyq/1732175031878.png',
  26. 'https://oss.dayaedu.com/ktyq/1732617269723.png',
  27. 'https://oss.dayaedu.com/ktyq/1732617388991.png'
  28. ],
  29. serviceShow: true,
  30. scrollTop: 0,
  31. current: 0,
  32. autoplay: false,
  33. interval: 5000,
  34. duration: 500,
  35. popupShow: false,
  36. list: [] as any,
  37. isOverSaled: false, // 是否所有商品都没有库存
  38. selected: {} as any,
  39. opacity: 0,
  40. showSelectedProduct: false, // 是否显示选中商品值
  41. scrolIntoViewStr: '',
  42. scrolIntoView: '',
  43. scrollDiscount: false, // 是否扣减启
  44. isScrollTT: false,
  45. scrollIntoViewType: false,
  46. headerHeight: 0, // 头部的高度
  47. initialScrollHeight: 0, // 滚动高度
  48. isFromPreviewImage: false,
  49. },
  50. /**
  51. * 生命周期函数--监听页面加载
  52. */
  53. onLoad() {
  54. // this.onInit()
  55. },
  56. onReady() {
  57. const that = this
  58. wx.createSelectorQuery().select('#scroll-header').boundingClientRect(function (rect) {
  59. that.setData({
  60. headerHeight: rect.height
  61. })
  62. }).exec();
  63. wx.createSelectorQuery().select('#scroll-view').boundingClientRect(function (rect) {
  64. // console.log(rect, 'rect')
  65. that.setData({
  66. initialScrollHeight: rect.height
  67. })
  68. }).exec();
  69. },
  70. /**
  71. * 获取基础信息
  72. */
  73. async onInit() {
  74. try {
  75. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  76. const list = data.data || []
  77. let selected: any = {}
  78. let isOverSaled = true // 是否销售完
  79. list.forEach((item: any) => {
  80. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  81. item.showSalePrice = this.formatPrice(item.salePrice, 'ALL');
  82. item.typeName = this.formatPeriod(item.num, item.period);
  83. item.discountPrice = this.formatPrice(item.originalPrice - item.salePrice, 'ALL')
  84. const prices: any = this.formatPrice(item.salePrice)
  85. item.integerPart = prices.integerPart
  86. item.decimalPart = prices.decimalPart
  87. if(item.stockNum > 0) {
  88. isOverSaled = false
  89. if( !selected.id) {
  90. selected = item
  91. }
  92. }
  93. });
  94. if(isOverSaled) {
  95. // 没有可购买商品则默认选中第一个商品
  96. selected = list[0]
  97. }
  98. this.setData({
  99. list,
  100. isOverSaled,
  101. selected
  102. })
  103. } catch(e) {
  104. console.log(e, 'e')
  105. }
  106. },
  107. // 格式化价格
  108. formatPrice(price: number, type?: string) {
  109. const amountStr = price.toFixed(2)
  110. const [integerPart, decimalPart] = amountStr.split('.');
  111. if(type === 'ALL') {
  112. return amountStr
  113. }
  114. return {
  115. integerPart,
  116. decimalPart
  117. }
  118. },
  119. // 格式化类型
  120. formatPeriod(num: number, type: string) {
  121. const template: any = {
  122. DAY: "天卡",
  123. MONTH: "月卡",
  124. YEAR: "年卡"
  125. }
  126. if(type === "YEAR" && num >= 99) {
  127. return '永久卡'
  128. }
  129. return num + template[type]
  130. },
  131. // 选择
  132. onSelectGoods(e: any) {
  133. const { dataset } = e.currentTarget
  134. const item = this.data.list.find((item: any) => item.id === dataset.id)
  135. // 判断是否有库存
  136. if(item.stockNum <= 0) {
  137. return
  138. }
  139. this.setData({
  140. selected: item || {}
  141. })
  142. },
  143. // 事件处理函数
  144. changeSwiper(e: any) {
  145. const detail = e.detail;
  146. if(detail.source === 'touch' || detail.source == 'autoplay') {
  147. this.setData({
  148. current: detail.current
  149. })
  150. }
  151. },
  152. isLogin() {
  153. // 判断是否登录
  154. if(!app.globalData.isLogin) {
  155. wx.navigateTo({
  156. url: '../login/login',
  157. })
  158. return false
  159. }
  160. return true
  161. },
  162. /** 我的订单 */
  163. onOrder() {
  164. // 判断是否登录
  165. if(!this.isLogin()) {
  166. return
  167. }
  168. wx.navigateTo({
  169. url: '../orders/orders',
  170. })
  171. },
  172. onBuyShop() {
  173. // 判断是否登录
  174. if(!this.isLogin()) {
  175. return
  176. }
  177. this.setData({
  178. popupShow: true,
  179. showSelectedProduct: true,
  180. })
  181. },
  182. onClose() {
  183. this.setData({
  184. popupShow: false
  185. })
  186. },
  187. onSubmit() {
  188. // 判断是否登录
  189. const that = this
  190. debounce(function () {
  191. if(!that.isLogin()) {
  192. return
  193. }
  194. let info = JSON.stringify({
  195. ...that.data.selected
  196. });
  197. // console.log(that.data.selected, "that.data.selected")
  198. info = encodeURIComponent(info);
  199. wx.navigateTo({
  200. url: `../orders/order-detail?orderInfo=${info}`,
  201. success: () => {
  202. that.setData({
  203. popupShow: false
  204. })
  205. }
  206. })
  207. }, 500)()
  208. },
  209. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  210. wx.previewImage({
  211. current: e.currentTarget.dataset.src,
  212. urls: this.data.imgList,
  213. success: () => {
  214. this.setData({
  215. isFromPreviewImage: true
  216. })
  217. }
  218. })
  219. },
  220. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  221. wx.previewImage({
  222. current: e.currentTarget.dataset.src,
  223. urls: this.data.goodsImgList,
  224. success: () => {
  225. this.setData({
  226. isFromPreviewImage: true
  227. })
  228. }
  229. })
  230. },
  231. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  232. wx.previewImage({
  233. current: e.currentTarget.dataset.src,
  234. urls: [e.currentTarget.dataset.src],
  235. success: () => {
  236. this.setData({
  237. isFromPreviewImage: true
  238. })
  239. }
  240. })
  241. },
  242. /**
  243. * 生命周期函数--监听页面显示
  244. */
  245. onShow() {
  246. if(!this.data.isFromPreviewImage) {
  247. this.onInit()
  248. } else {
  249. this.setData({
  250. isFromPreviewImage: false
  251. })
  252. }
  253. this.setData({
  254. serviceShow: true
  255. })
  256. },
  257. onHide() {
  258. this.setData({
  259. serviceShow: false
  260. })
  261. },
  262. // onReady() {
  263. // const scrollView = this.selectComponent('#scrollarea');
  264. // console.log(scrollView, 'scrollView')
  265. // },
  266. // 页面滚动时颜色变化
  267. onScrollView(e: { detail: any }) {
  268. const top = e.detail.scrollTop || 0
  269. const scrollHeight = e.detail.scrollHeight || 0
  270. // 从100开始显示
  271. // console.log(top, scrollHeight, this.data.initialScrollHeight, '121121221')
  272. this.setData({
  273. // opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : (top - 100) / 150
  274. opacity: top < 100 ? 0 : (top - 100) > 150 ? 1 : 1
  275. })
  276. if (top + this.data.initialScrollHeight >= scrollHeight - 80) {
  277. // console.log('已经滑动到底部了');
  278. // 相应业务逻辑处理
  279. this.setData({
  280. scrolIntoViewStr: 'type2'
  281. })
  282. } else {
  283. if(this.data.scrollIntoViewType) {
  284. this.setData({
  285. scrollTop: this.data.scrollDiscount ? top - this.data.headerHeight : top,
  286. scrollIntoViewType: false,
  287. scrollDiscount: false,
  288. isScrollTT: true,
  289. })
  290. } else {
  291. if(!this.data.isScrollTT) {
  292. this.onChangeScroll()
  293. } else {
  294. this.setData({
  295. isScrollTT: false,
  296. })
  297. }
  298. }
  299. }
  300. },
  301. onChangeScroll() {
  302. const that = this;
  303. debounce(function() {
  304. wx.createSelectorQuery().select('#type2').boundingClientRect(function (rect) {
  305. if(rect.top > 0 && rect.top <= that.data.headerHeight) {
  306. that.setData({
  307. scrolIntoViewStr: 'type2'
  308. })
  309. }
  310. }).exec();
  311. wx.createSelectorQuery().select('#type3').boundingClientRect(function (rect) {
  312. if(rect.top > 0 && rect.top <= that.data.headerHeight) {
  313. that.setData({
  314. scrolIntoViewStr: 'type3',
  315. })
  316. }
  317. if(rect.top > 0 && rect.top > that.data.headerHeight) {
  318. that.setData({
  319. scrolIntoViewStr: 'type1'
  320. })
  321. }
  322. }).exec();
  323. }, 100)()
  324. },
  325. onTapAnchor(e: { currentTarget: { dataset: any } }) {
  326. const type = e.currentTarget.dataset.type
  327. this.setData({
  328. scrolIntoView: type,
  329. scrolIntoViewStr: type,
  330. scrollDiscount: type !== 'type2' ? true : false,
  331. scrollIntoViewType: true,
  332. })
  333. },
  334. onShareAppMessage() {
  335. return {
  336. title: '音乐数字AI',
  337. path: '/pages/index/index',
  338. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  339. }
  340. },
  341. onShareTimeline() {
  342. return {
  343. title: '/音乐数字AI',
  344. path: '/pages/index/index',
  345. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  346. }
  347. }
  348. // onLookMore() {
  349. // this.setData({
  350. // popupShow: true
  351. // })
  352. // }
  353. })