123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- // index.ts
- import { api_shopInstruments, api_shopProduct } from "../../api/login";
- import { debounce, formatPrice } from '../../utils/util'
- // 获取应用实例
- const app = getApp<IAppOption>()
- // pages/orders/orders.ts
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- firstImgList: [
- 'https://oss.dayaedu.com/ktyq/1739181226082.png',
- 'https://oss.dayaedu.com/ktyq/1739181253580.png',
- 'https://oss.dayaedu.com/ktyq/1739181268714.png',
- 'https://oss.dayaedu.com/ktyq/1739181284457.png',
- 'https://oss.dayaedu.com/ktyq/1739181316779.png',
- 'https://oss.dayaedu.com/ktyq/1739181329436.png'
- ],
- firstCurrent: 0,
- imgList: [
- 'https://oss.dayaedu.com/ktyq/1732610921517.png',
- 'https://oss.dayaedu.com/ktyq/1732610940095.png',
- 'https://oss.dayaedu.com/ktyq/1732610952376.png',
- 'https://oss.dayaedu.com/ktyq/1732610965625.png',
- // 'https://oss.dayaedu.com/ktyq/1731664269098.png',
- ],
- goodsImgList: [
- 'https://oss.dayaedu.com/ktyq/1739182504757.png',
- 'https://oss.dayaedu.com/ktyq/1739182521080.png',
- 'https://oss.dayaedu.com/ktyq/1739182535430.png',
- 'https://oss.dayaedu.com/ktyq/1739182557976.png',
- 'https://oss.dayaedu.com/ktyq/1739182545221.png'
- ],
- current: 0,
- autoplay: false,
- interval: 5000,
- duration: 500,
- popupShow: false,
- instrumentList: [] as any,
- list: [] as any,
- isOverSaled: false, // 是否所有商品都没有库存
- smallGoods: {}, // 最小金额商品
- selected: {} as any,
- selectInstrumentId: '', // 选中的乐器
- selectedInstrument: {} as any,
- formatSelectGood: {
- typeName: '',
- showSalePrice: '', // 显示的现价
- originalPrice: 0, // 原价
- salePrice: 0, // 现价
- discountPrice: '' // 已省
- } as any, // 格式化所有选中的数据
- showService: false,
- isFromPreviewImage: false,
- showBuyer: true, // 收益人
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad() {
- this.onInit()
- },
- /**
- * 获取基础信息
- */
- async onInit() {
- try {
- const result = await api_shopInstruments({ appId: app.globalData.appId })
- const instrumentList = result.data.data || []
- instrumentList.forEach((item: any) => {
- item.showSalePrice = formatPrice(item.salePrice || 0, 'ALL')
- })
- const { data } = await api_shopProduct({ appId: app.globalData.appId });
- const list = data.data || []
- let selected: any = {}
- let isOverSaled = true // 是否销售完
- // 最少金额商品
- let smallGoods: any = {}
- list.forEach((item: any) => {
- item.originalPrice = formatPrice(item.originalPrice, "ALL");
- item.showSalePrice = formatPrice(item.salePrice, "ALL");
- item.typeName = this.formatPeriod(item.num, item.period);
- item.discountPrice = formatPrice(
- item.originalPrice - item.salePrice,
- "ALL"
- );
- const prices: any = formatPrice(item.salePrice)
- item.integerPart = prices.integerPart
- item.decimalPart = prices.decimalPart
- if (item.stockNum > 0) {
- isOverSaled = false
- if (!selected.id) {
- selected = item
- }
- }
- // 获取最小金额
- if(smallGoods?.salePrice) {
- smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item
- } else {
- smallGoods = item
- }
- });
- if (isOverSaled) {
- // 没有可购买商品则默认选中第一个商品
- selected = list[0]
- }
- this.setData({
- list,
- instrumentList, // 乐器列表
- isOverSaled,
- selected,
- smallGoods,
- selectInstrumentId: '',
- selectedInstrument: {}
- }, () => {
- this.onFormatGoods()
- })
- } catch (e) {
- console.log(e, 'e')
- }
- },
- // 格式化类型
- 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 || {}
- }, () => {
- this.onFormatGoods()
- })
- },
- /** 选中乐器 */
- onSelectInstrument(e: any) {
- const { dataset } = e.currentTarget;
- if (dataset.id === this.data.selectInstrumentId) {
- this.setData({
- selectInstrumentId: '',
- selectedInstrument: {}
- }, () => {
- this.onFormatGoods()
- })
- } else {
- const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
- this.setData({
- selectInstrumentId: dataset.id,
- selectedInstrument: item || {}
- }, () => {
- this.onFormatGoods()
- })
- }
- },
- onFirstChange(e: any) {
- const detail = e.detail;
- if (detail.source === 'touch' || detail.source == 'autoplay') {
- this.setData({
- firstCurrent: detail.current
- })
- }
- },
- // 事件处理函数
- changeSwiper(e: any) {
- const detail = e.detail;
- 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) {
- wx.navigateTo({
- url: '../login/login',
- })
- return false
- }
- return true
- },
- /** 我的订单 */
- onOrder() {
- // 判断是否登录
- if (!this.isLogin()) {
- return
- }
- wx.navigateTo({
- url: '../orders/orders',
- })
- },
- /** 客服 */
- onService() {
- this.setData({
- showService: true
- })
- },
- changePop(event: { detail: any }) {
- this.setData({
- showService: event.detail
- })
- },
- 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}`,
- success: () => {
- that.setData({
- popupShow: false
- })
- }
- })
- }, 500)()
- },
- onSelectBuyer() {
- this.setData({
- showBuyer: true
- })
- },
- onCloseBuyer(e: {detail: any}) {
- this.setData({
- showBuyer: e.detail
- })
- },
- onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
- wx.previewImage({
- current: e.currentTarget.dataset.src,
- urls: this.data.imgList,
- success: () => {
- this.setData({
- isFromPreviewImage: true
- })
- }
- })
- },
- onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
- wx.previewImage({
- current: e.currentTarget.dataset.src,
- urls: this.data.goodsImgList,
- success: () => {
- this.setData({
- isFromPreviewImage: true
- })
- }
- })
- },
- onPreivewGoods(e: { currentTarget: { dataset: any } }) {
- wx.previewImage({
- current: e.currentTarget.dataset.src,
- urls: [e.currentTarget.dataset.src],
- success: () => {
- this.setData({
- isFromPreviewImage: true
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- if (!this.data.isFromPreviewImage) {
- this.onInit()
- } else {
- this.setData({
- isFromPreviewImage: false
- })
- }
- },
- onShareAppMessage() {
- return {
- title: '器乐数字AI工具',
- path: '/pages/index/index',
- imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
- }
- },
- onShareTimeline() {
- return {
- title: '器乐数字AI工具',
- path: '/pages/index/index',
- imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
- }
- }
- })
|