收藏
回答

是参数问题吗,具体要怎么解决?

一开始编译的时候出现的是五月,但是随便切换一下年或者月,五月就会消失,重新编译就会又出现,是哪里的问题?找了好久也没找到是哪里的问题,求大佬解答一下,谢谢谢谢

//*.js
let app = getApp();
Page({
  data: {
    id:'',//类型id
    footerIndex: 1,
    amtType:'0',//0支出,1收入
    amtTypeArr:['支出','收入'],
    dateType:1,//时间范围1月,2年
    dateTypeIdx:0,//选中的月或年的idx
    chartList:[],
    chartListCount:0,
    yearArr: ['2023','2024'],
    monthArr: [12,11,10,9,8,7,6,5,4,3,2,1],
    year:'',
    beginMonth:'',
    endMonth:''
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        amtType:options.type,
        id:options.id,
        year:options.year,
        beginMonth:options.mon1,
        endMonth:options.mon2
      });
      this.getChartDetail();
    }else{
      let month = new Date().getMonth() + 1;
      let year = new Date().getFullYear();
      this.setData({
        year:year,
        beginMonth:month,
        endMonth:month,
        monthArr:this.data.monthArr.slice(12-month),
        dateTypeIdx:0
      });
      this.initData();
    }
  },
  onShow: function () {
  },
  initData(){
    let _this = this;
    let data = this.data;
    this.setData({
      chartList:[]
    });
    app.getAjax({
      url:'getChartData',
      params:{
        amtType:data.amtType,
        year: data.year-0,
        beginMonth: data.beginMonth,
        endMonth:data.endMonth
      },
      success(res){
        console.log("getChartData:",res);
        let data = res.result.list;
        let count = 0;
        for(var key in data){
          data[key].count = parseInt(data[key].count*100)/100;
          count += data[key].count;
        }
        _this.setData({
          chartList:data,
          chartListCount:count
        });
      }
    });
  },
  getChartDetail(){
    let _this = this;
    let data = this.data;
    app.getAjax({
      url:'getChartDetailData',
      params:{
        id:data.id,
        year:data.year,
        beginMonth:data.beginMonth,
        endMonth:data.endMonth
      },
      success(res){
        console.log("getChartDetailData:",res);
        let data = res.result.data;
        let count = 0;
        for(var key in data){
          count += data[key].bookAmt
        }
        _this.setData({
          chartList: data,
          chartListCount: count
        });
      }
    });
  },
  cliDateRange(e){
    let idx = e.currentTarget.dataset.idx;
    let dateType = this.data.dateType;
    let year,beginMonth,endMonth;
    if(dateType == 1){//月
      this.setData({
        year:new Date().getFullYear(),
        beginMonth:this.data.monthArr[idx],
        endMonth:this.data.monthArr[idx],
        dateTypeIdx:idx
      });
      this.initData();
    }else{//年
      this.setData({
        dateTypeIdx:idx,
        year:this.data.yearArr[idx],
        beginMonth:1,
        endMonth:12
      });
      this.initData();
    }
  },
  toChartDetail(e){
    let id = e.currentTarget.dataset.id;
    let data = this.data;
    app.navigate(`/pages/chart/chart?id=${id}&year=${data.year}&mon1=${data.beginMonth}&mon2=${data.endMonth}&type=${data.amtType}`);
  },
  amtTypeChange(e){//选择账单类型
    if(e.detail.value != this.data.amtType){
      this.setData({
        amtType:e.detail.value
      });
      if(this.data.id){
        this.getChartDetail();
      }else{
        this.initData();
      }
    }
  },
  setDateType(e){//选择日期范围
    let type = e.currentTarget.dataset.type;
    if(type != this.data.dateType){
      let year,beginMonth,endMonth;
      let setDatas = {};
      if(type == 1){
        let date = new Date();
        setDatas.year = date.getFullYear();
        setDatas.beginMonth = date.getMonth()+1;
        setDatas.endMonth = date.getMonth()+1;
        setDatas.monthArr = [12,11,10,9,8,7,6,5,4,3,2,1].slice(12-date.getMonth()+1);
        setDatas.dateTypeIdx = 0;
      }else{
        setDatas.year = new Date().getFullYear();
        setDatas.beginMonth = 1;
        setDatas.endMonth = 12;
        setDatas.dateTypeIdx = 0;
      }
      setDatas.dateType = type;
      this.setData(setDatas);
      this.initData();
    }
  },
  changeFooter(e) {
    app.changeFooter(e);
  }
})

//*.wxml
<view class="chart_header">
  <view wx:if="{{id}}" class="book_type_name">{{chartList.length?chartList[0].bookTypeName:''}}</view>
  <picker wx:else bindchange="amtTypeChange" value="{{index}}" range="{{amtTypeArr}}">
    <view class="amttype_picker">
      {{amtTypeArr[amtType]}}
    </view>
  </picker>
  <view class="date_range_wrapper">
    <text class="{{dateType==1?'active':''}}" catchtap="setDateType" data-type="1">月</text>
    <text class="{{dateType==2?'active':''}}" catchtap="setDateType" data-type="2">年</text>
  </view>
</view>
<view class="padding_260"></view>
<!-- 选择日期 -->
<scroll-view scroll-x class="date_range">
  <text class="date_range_item {{dateTypeIdx==idx?'active':''}}" catchtap="cliDateRange" data-idx="{{idx}}"
    wx:for="{{dateType==1?monthArr:yearArr}}" wx:for-index="idx" wx:key="idx">{{item+(dateType==1?'月':'')}}</text>
</scroll-view>
<!-- 账单列表 -->
<view class="book_list_wrapper clearfix">
  <text class="book_list_title">{{amtType==0?'支出':'收入'}}排行榜(总{{amtType==0?'支出':'收入'}}:{{chartListCount}}元)</text>
  <view class="book_list_item" wx:for="{{chartList}}" wx:for-index="idx" wx:key="idx"
    catchtap="toChartDetail" data-id="{{item._id.bookTypeId}}" >
    <view class="book_list_img_wrapper">
      <image wx:if="{{id}}" src="{{'/utils/icons/'+item.bookTypeIcon+'.png'}}"></image>
      <image wx:else src="{{'/utils/icons/'+item._id.bookTypeIcon+'.png'}}"></image>
    </view>
    <view class="book_list_item_info">
      <view class="book_list_item_top">
        <text class="book_list_item_name">{{id?(item.remark?item.remark:item.bookTypeName):item._id.bookTypeName}} {{formatData.getPercent((id?item.bookAmt:item.count),chartListCount)}}%</text>
        <text class="book_list_item_amt">{{id?item.bookAmt:item.count}}</text>
      </view>
      <view class="book_list_progress" style="width:{{formatData.getPercent((id?item.bookAmt:item.count),chartListCount)}}"></view>
    </view>
  </view>
</view>



<import src="/components/footer.wxml"/>
<template is="footer" data="{{footerIndex}}"></template>


<wxs module="formatData">
  var getPercent = function(data,total){
    var percent = (data / total).toFixed(4)*10000;
    return parseInt(percent)/100;
  }
  module.exports = {
    getPercent:getPercent
  }
</wxs>
最后一次编辑于  2023-05-09
回答关注问题邀请回答
收藏

1 个回答

  • H1HAO 🇨🇳
    H1HAO 🇨🇳
    2023-05-09

    这是你自己的代码写的,你发个代码片段或者相关代码截图啥的

    2023-05-09
    有用
    回复 3
    • ㅤ
      2023-05-09
      抱歉,我这是头一次在这问不知道流程
      2023-05-09
      回复
    • H1HAO 🇨🇳
      H1HAO 🇨🇳
      2023-05-10回复
      检查一下setDatas.monthArr = [12,11,10,9,8,7,6,5,4,3,2,1].slice(12-date.getMonth()+1) 这一段看下打印的啥
      2023-05-10
      回复
    • ㅤ
      发表于移动端
      2023-05-10回复H1HAO 🇨🇳
      打印出来一个数组有12345
      2023-05-10
      回复
登录 后发表内容