123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- import { api_shopProduct } from "../../api/login";
- import { debounce } from '../../utils/util'
- const app = getApp<IAppOption>()
- Page({
-
- data: {
- serviceShow: true,
- scrollTop: 0,
- current: 0,
- autoplay: false,
- interval: 5000,
- duration: 500,
- popupShow: false,
- list: [] as any,
- isOverSaled: false,
- selected: {} as any,
- opacity: 0,
- },
-
- onLoad() {
- this.onInit()
- },
-
- async onInit() {
- try {
- const { data } = await api_shopProduct({ appId: app.globalData.appId });
- const list = data.data || []
- let selected: any = {}
- let isOverSaled = true
- list.forEach((item: any) => {
- item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
- item.showSalePrice = this.formatPrice(item.salePrice, 'ALL');
- item.typeName = this.formatPeriod(item.num, item.period);
- item.discountPrice = this.formatPrice(item.originalPrice - item.salePrice, 'ALL')
- const prices: any = this.formatPrice(item.salePrice)
- item.integerPart = prices.integerPart
- item.decimalPart = prices.decimalPart
- if(item.stockNum > 0) {
- isOverSaled = false
- if( !selected.id) {
- selected = item
- }
- }
- });
- if(isOverSaled) {
-
- selected = list[0]
- }
- this.setData({
- list,
- isOverSaled,
- selected
- })
- } catch(e) {
- console.log(e, 'e')
- }
- },
-
- formatPrice(price: number, type?: string) {
- const amountStr = price.toFixed(2)
- const [integerPart, decimalPart] = amountStr.split('.');
- if(type === 'ALL') {
- return amountStr
- }
- return {
- integerPart,
- decimalPart
- }
- },
-
- formatPeriod(num: number, type: string) {
- const template: any = {
- DAY: "天卡",
- MONTH: "月卡",
- YEAR: "年卡"
- }
- if(type === "YEAR" && num >= 99) {
- return '永久卡'
- }
- return num + template[type]
- },
-
- onSelectGoods(e: any) {
- const { dataset } = e.currentTarget
- const item = this.data.list.find((item: any) => item.id === dataset.id)
-
- if(item.stockNum <= 0) {
- return
- }
- this.setData({
- selected: item || {}
- })
- },
-
- changeSwiper(e: any) {
- const detail = e.detail;
- if(detail.source === 'touch' || detail.source == 'autoplay') {
- this.setData({
- current: detail.current
- })
- }
- },
- isLogin() {
-
- if(!app.globalData.isLogin) {
- wx.navigateTo({
- url: '../login/login',
- })
- return false
- }
- return true
- },
-
- onOrder() {
-
- if(!this.isLogin()) {
- return
- }
- wx.navigateTo({
- url: '../orders/orders',
- })
- },
- onBuyShop() {
-
- if(!this.isLogin()) {
- return
- }
- this.setData({
- popupShow: true
- })
- },
- onClose() {
- this.setData({
- popupShow: false
- })
- },
- onSubmit() {
-
- const that = this
- debounce(function () {
- if(!that.isLogin()) {
- return
- }
- let info = JSON.stringify({
- ...that.data.selected
- });
- console.log(that.data.selected, "that.data.selected")
- info = encodeURIComponent(info);
- wx.navigateTo({
- url: `../orders/order-detail?orderInfo=${info}`,
- })
- that.setData({
- popupShow: false
- })
- }, 500)()
- },
-
- onShow() {
- this.onInit()
- this.setData({
- serviceShow: true
- })
- },
- onHide() {
- this.setData({
- serviceShow: false
- })
- },
-
-
-
-
-
- onScrollView(e: { detail: any }) {
- const top = e.detail.scrollTop || 0
- this.setData({
- opacity: top > 150 ? 1 : top / 150
- })
- },
- onTapAnchor(e: { currentTarget: { dataset: any } }) {
- console.log(e, 'e')
- const scrollView = this.selectComponent('.scrollarea');
- console.log(scrollView, 'scrollView')
- const that = this
- wx.createSelectorQuery().select('#' + e.currentTarget.dataset.type).boundingClientRect(function (rect) {
- console.log(rect)
- that.setData({
- scrollTop: rect.top
- });
- }).exec();
- }
- })
|