index.js 7.51 KB
Newer Older
张杰's avatar
张杰 committed
1
// pages/add_record/index.js
2 3 4

const req = require('../../api/teacher.js')
const app = getApp()
张杰's avatar
张杰 committed
5 6 7 8 9 10
Page({

  /**
   * 页面的初始数据
   */
  data: {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    radioValues: [{
        'value': '饮食',
        'code': 'diet',
        'selected': true
      },
      {
        'value': '睡觉',
        'code': 'sleep',
        'selected': false
      },
      {
        'value': 'WC',
        'code': 'wc',
        'selected': false
      },
张杰's avatar
张杰 committed
26 27
    ],
    clazz: [],
28 29 30 31 32
    studentIds:[],
    controlIndex: 0,
    categoryCode: ['diet_morning', 'diet_afternoon', 'diet_evening', 'diet_morning_more', 'diet_afternoon_more', 'diet_water', 'diet_milk'],
    categorys: [],
    typeIndex: 0,
张杰's avatar
张杰 committed
33
    inputModel: [
34

张杰's avatar
张杰 committed
35
    ],
36 37 38 39 40 41 42 43 44
    selectPic: [],
    recordTypes: app.globalData.recordTypes,
    eatCellTags: {},

    //获取到的数据
    eatRecord:[],
    sleepRecord:[],
    wcRecord:[]

张杰's avatar
张杰 committed
45 46 47 48 49 50 51 52 53

  },
  //添加图片
  addPicture: function(e) {
    var that = this;
    // 选择图片
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
54
      success: function(res) {
张杰's avatar
张杰 committed
55 56 57 58 59 60 61 62 63 64 65
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        for (var i in res.tempFilePaths) {
          that.data.selectPic.push(res.tempFilePaths[i])
        }
        that.setData({
          selectPic: that.data.selectPic
        });
      }
    })
  },

66
  previewPic: function(e) {
张杰's avatar
张杰 committed
67 68 69 70 71
    var current = this.data.selectPic[e.target.dataset.index]
    wx.previewImage({
      current: current,
      urls: this.data.selectPic
    })
张杰's avatar
张杰 committed
72 73

  },
张杰's avatar
张杰 committed
74
  //删除图片
75
  deleteImg: function(e) {
张杰's avatar
张杰 committed
76
    var that = this
77
    that.data.selectPic.splice(e.currentTarget.dataset.index, 1)
张杰's avatar
张杰 committed
78 79 80 81
    that.setData({
      selectPic: that.data.selectPic
    })
  },
82 83
  //选择小分类
  tapType: function(e) {
张杰's avatar
张杰 committed
84 85

    this.setData({
86
      typeIndex: e.target.dataset.index,
张杰's avatar
张杰 committed
87
    })
88 89

    this.getRecord()
张杰's avatar
张杰 committed
90 91
  },

92 93 94 95 96 97 98 99 100 101 102 103
  indexChanged: function(e) {
    // 点中的是组中第个元素
    var index = e.target.dataset.index;

    // 读取原始的数组
    var radioValues = this.data.radioValues;
    for (var i = 0; i < radioValues.length; i++) {
      // 全部改为非选中
      radioValues[i].selected = false;
      // 当前那个改为选中
      radioValues[index].selected = true;
    }
张杰's avatar
张杰 committed
104 105

    // console.log(radioValues)
106 107 108
    // 写回数据
    this.setData({
      radioValues: radioValues,
张杰's avatar
张杰 committed
109
      controlIndex: index
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    });
    // clazz状态
    this.clazzStatus();

    this.getRecord()
  },

  // 初始化分类数据
  configCategory: function() {
    var categorys = this.data.categorys
    for (var i = 0; i < this.data.categoryCode.length; i++) {
      let category = this.data.recordTypes[this.data.categoryCode[i]]
      categorys.push(category)
    }

    this.setData({
      categorys: categorys
    })
  },

  getRecord: function() {
    let studentIds = this.data.studentIds
    console.log(studentIds)
    
    let category = this.data.radioValues[this.data.controlIndex].code
    let type = ''
    if(this.data.controlIndex == 0) {
      type = this.data.categoryCode[this.data.typeIndex]
    }

    req.queryRecordList(category, type, studentIds).then(res => {
      console.log(res)
      var eatCellTags = this.data.eatCellTags
      var optionTag = ['diet_morning', 'diet_afternoon', 'diet_evening', 'diet_morning_more', 'diet_afternoon_more']
      for (var i = 0; i < res.length; i++) {
        var item = res[i]

        if (item.recordItems.length == 0) {
          //没有记录时, 自动生成一条
          var r = {}
          r.recordType = type
          r.recordCategory = category
          item.recordItems.push(r)
        }

        for (var k = 0; k < item.recordItems.length; k++) {
          var r = item.recordItems[k]
          if (r.recordCategory == 'sleep') {
            r.cellType = 'input'
            r.cellUnit = '分钟'
            break
          }

          if (r.recordCategory == 'wc') {
            r.cellType = 'select'
            r.cellOptions = ['pee', 'poo', 'diaper']
            break
          }

          if (r.recordType == 'diet_milk') {
            r.cellType = 'input'
            r.cellUnit = '毫升'
            break
          }
          if (r.recordType == 'diet_water') {
            r.cellType = 'input'
            r.cellUnit = '次'
            break
          }
          for (var j = 0, len = optionTag.length; j < len; j++) {
            var tag = optionTag[j]
            if (r.recordType == tag) {
              r.cellType = 'select'
              r.cellOptions = ['少', '正常', '多']
              break
            }
          }

        }
      }
      if (this.data.controlIndex == 0) {
        this.setData({
          eatRecord: res
        })
      } else if (this.data.controlIndex == 1) {
        this.setData({
          sleepRecord: res
        })
      } else if (this.data.controlIndex == 2) {
        this.setData({
          wcRecord: res
        })
      }
      
    })
  },

  quantityCellTap: function (e) {
    console.log(e.currentTarget.dataset.optionindex)
    let records 
    if (this.data.controlIndex == 0) {
      records = this.data.eatRecord
    } else if (this.data.controlIndex == 1) {
      records = this.data.sleepRecord
    } else if (this.data.controlIndex == 2) {
      records = this.data.wcRecord
    }

    for (var i = 0; i < records.length; i++) {
      var item = records[i].recordItems[0]
      item.optionIndex = e.currentTarget.dataset.optionindex
    }

    if (this.data.controlIndex == 0) {
      this.setData({
        eatRecord: records
      })
    } else if (this.data.controlIndex == 1) {
      this.setData({
        sleepRecord: records
      })
    } else if (this.data.controlIndex == 2) {
      this.setData({
        wcRecord: records
      })
    }

  },

  onLoad: function(options) {
    this.data.studentIds = JSON.parse(options.studentIds)
    // onLoad 比 onReady 更早调用,后者为选中时屏幕闪动一下
    this.clazzStatus();

    //分类数据
    this.configCategory()
    //获取选中的分类的记录
    this.getRecord()



  },
  clazzStatus: function() {
    /* 此方法分别被加载时调用,点击某段时调用 */
    // class样式表如"selected last","selected"
    var clazz = [];
    // 参照数据源
    var radioValues = this.data.radioValues;
    for (var i = 0; i < radioValues.length; i++) {
      // 默认为空串,即普通按钮
      var cls = '';
      // 高亮,追回selected
      if (radioValues[i].selected) {
        cls += 'selected ';
      }
      // 最后个元素, 追加last
      if (i == radioValues.length - 1) {
        cls += 'last ';
      }
      //去掉尾部空格
      cls = cls.replace(/(\s*$)/g, '');
      clazz[i] = cls;
    }
张杰's avatar
张杰 committed
273 274

    // console.log(clazz)
275 276 277 278 279
    // 写回数据
    this.setData({
      clazz: clazz
    });
  },
张杰's avatar
张杰 committed
280 281 282 283

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
284
  onReady: function() {
张杰's avatar
张杰 committed
285 286 287 288 289 290

  },

  /**
   * 生命周期函数--监听页面显示
   */
291
  onShow: function() {
张杰's avatar
张杰 committed
292 293 294 295 296 297

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
298
  onHide: function() {
张杰's avatar
张杰 committed
299 300 301 302 303 304

  },

  /**
   * 生命周期函数--监听页面卸载
   */
305
  onUnload: function() {
张杰's avatar
张杰 committed
306 307 308 309 310 311

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
312
  onPullDownRefresh: function() {
张杰's avatar
张杰 committed
313 314 315 316 317 318

  },

  /**
   * 页面上拉触底事件的处理函数
   */
319
  onReachBottom: function() {
张杰's avatar
张杰 committed
320 321 322 323 324 325

  },

  /**
   * 用户点击右上角分享
   */
326
  onShareAppMessage: function() {
张杰's avatar
张杰 committed
327 328 329

  }
})