|
@@ -1,7 +1,7 @@
|
|
|
// index.ts
|
|
|
|
|
|
import { api_shopProduct } from "../../api/login";
|
|
|
-import { debounce } from '../../utils/util'
|
|
|
+import { debounce, formatPrice } from '../../utils/util'
|
|
|
|
|
|
// 获取应用实例
|
|
|
const app = getApp<IAppOption>()
|
|
@@ -34,6 +34,13 @@ Page({
|
|
|
'https://oss.dayaedu.com/ktyq/1739182557976.png',
|
|
|
'https://oss.dayaedu.com/ktyq/1739182545221.png'
|
|
|
],
|
|
|
+ formatSelectGood: {
|
|
|
+ typeName: '',
|
|
|
+ showSalePrice: '', // 显示的现价
|
|
|
+ originalPrice: 0, // 原价
|
|
|
+ salePrice: 0, // 现价
|
|
|
+ discountPrice: '' // 已省
|
|
|
+ } as any, // 格式化所有选中的数据
|
|
|
current: 0,
|
|
|
autoplay: false,
|
|
|
interval: 5000,
|
|
@@ -62,19 +69,19 @@ Page({
|
|
|
let selected: any = {}
|
|
|
let isOverSaled = true // 是否销售完
|
|
|
list.forEach((item: any) => {
|
|
|
- item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
|
|
|
+ item.originalPrice = formatPrice(item.originalPrice, 'ALL');
|
|
|
item.typeName = this.formatPeriod(item.num, item.period);
|
|
|
- const prices: any = this.formatPrice(item.salePrice)
|
|
|
+ const prices: any = formatPrice(item.salePrice)
|
|
|
item.integerPart = prices.integerPart
|
|
|
item.decimalPart = prices.decimalPart
|
|
|
- if(item.stockNum > 0) {
|
|
|
+ if (item.stockNum > 0) {
|
|
|
isOverSaled = false
|
|
|
- if( !selected.id) {
|
|
|
+ if (!selected.id) {
|
|
|
selected = item
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- if(isOverSaled) {
|
|
|
+ if (isOverSaled) {
|
|
|
// 没有可购买商品则默认选中第一个商品
|
|
|
selected = list[0]
|
|
|
}
|
|
@@ -84,22 +91,10 @@ Page({
|
|
|
isOverSaled,
|
|
|
selected
|
|
|
})
|
|
|
- } catch(e) {
|
|
|
+ } 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 = {
|
|
@@ -107,17 +102,17 @@ Page({
|
|
|
MONTH: "月卡",
|
|
|
YEAR: "年卡"
|
|
|
}
|
|
|
- if(type === "YEAR" && num >= 99) {
|
|
|
+ if (type === "YEAR" && num >= 99) {
|
|
|
return '永久卡'
|
|
|
}
|
|
|
- return num + template[type]
|
|
|
+ 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) {
|
|
|
+ if (item.stockNum <= 0) {
|
|
|
return
|
|
|
}
|
|
|
this.setData({
|
|
@@ -126,7 +121,7 @@ Page({
|
|
|
},
|
|
|
onFirstChange(e: any) {
|
|
|
const detail = e.detail;
|
|
|
- if(detail.source === 'touch' || detail.source == 'autoplay') {
|
|
|
+ if (detail.source === 'touch' || detail.source == 'autoplay') {
|
|
|
this.setData({
|
|
|
firstCurrent: detail.current
|
|
|
})
|
|
@@ -135,15 +130,61 @@ Page({
|
|
|
// 事件处理函数
|
|
|
changeSwiper(e: any) {
|
|
|
const detail = e.detail;
|
|
|
- if(detail.source === 'touch' || detail.source == 'autoplay') {
|
|
|
+ if (detail.source === 'touch' || detail.source == 'autoplay') {
|
|
|
this.setData({
|
|
|
current: detail.current
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
+ /** 格式化选中的商品 */
|
|
|
+ onFormatGoods() {
|
|
|
+ const selected = this.data.selected;
|
|
|
+ const selectedInstrument = this.data.selectedInstrument
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ typeName: '',
|
|
|
+ showSalePrice: '' as any, // 显示的现价
|
|
|
+ originalPrice: 0, // 原价
|
|
|
+ salePrice: 0, // 现价
|
|
|
+ discountPrice: '' as any, // 已省
|
|
|
+ integerPart: '',
|
|
|
+ decimalPart: '',
|
|
|
+ }
|
|
|
+ // 选中期限
|
|
|
+ if (selected.id) {
|
|
|
+ params.typeName = selected.typeName
|
|
|
+ params.showSalePrice = selected.showSalePrice
|
|
|
+ params.originalPrice = selected.originalPrice
|
|
|
+ params.salePrice = selected.salePrice
|
|
|
+ params.discountPrice = selected.discountPrice
|
|
|
+
|
|
|
+ const prices: any = formatPrice(params.salePrice);
|
|
|
+ params.integerPart = prices.integerPart
|
|
|
+ params.decimalPart = prices.decimalPart
|
|
|
+ }
|
|
|
+ // 选中乐器
|
|
|
+ if (selectedInstrument.id) {
|
|
|
+ params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
|
|
|
+ params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
|
|
|
+ params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
|
|
|
+ params.showSalePrice = formatPrice(params.salePrice, "ALL");
|
|
|
+ params.discountPrice = formatPrice(
|
|
|
+ params.originalPrice - params.salePrice,
|
|
|
+ "ALL"
|
|
|
+ );
|
|
|
+
|
|
|
+ const prices: any = formatPrice(params.salePrice);
|
|
|
+ params.integerPart = prices.integerPart
|
|
|
+ params.decimalPart = prices.decimalPart
|
|
|
+ }
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ formatSelectGood: params
|
|
|
+ })
|
|
|
+ },
|
|
|
isLogin() {
|
|
|
// 判断是否登录
|
|
|
- if(!app.globalData.isLogin) {
|
|
|
+ if (!app.globalData.isLogin) {
|
|
|
wx.navigateTo({
|
|
|
url: '../login/login',
|
|
|
})
|
|
@@ -154,7 +195,7 @@ Page({
|
|
|
/** 我的订单 */
|
|
|
onOrder() {
|
|
|
// 判断是否登录
|
|
|
- if(!this.isLogin()) {
|
|
|
+ if (!this.isLogin()) {
|
|
|
return
|
|
|
}
|
|
|
wx.navigateTo({
|
|
@@ -174,7 +215,7 @@ Page({
|
|
|
},
|
|
|
onBuyShop() {
|
|
|
// 判断是否登录
|
|
|
- if(!this.isLogin()) {
|
|
|
+ if (!this.isLogin()) {
|
|
|
return
|
|
|
}
|
|
|
this.setData({
|
|
@@ -190,7 +231,7 @@ Page({
|
|
|
// 判断是否登录
|
|
|
const that = this
|
|
|
debounce(function () {
|
|
|
- if(!that.isLogin()) {
|
|
|
+ if (!that.isLogin()) {
|
|
|
return
|
|
|
}
|
|
|
let info = JSON.stringify({
|
|
@@ -245,7 +286,7 @@ Page({
|
|
|
* 生命周期函数--监听页面显示
|
|
|
*/
|
|
|
onShow() {
|
|
|
- if(!this.data.isFromPreviewImage) {
|
|
|
+ if (!this.data.isFromPreviewImage) {
|
|
|
this.onInit()
|
|
|
} else {
|
|
|
this.setData({
|