index.js 6.54 KB
Newer Older
张杰's avatar
张杰 committed
1
// pages/home/parents/index.js
2 3

const request = require('../../../api/parrent.js')
张杰's avatar
张杰 committed
4 5
const utils = require('../../../utils/util.js')

6
const app = getApp()
7 8


张杰's avatar
张杰 committed
9 10 11 12 13 14
Page({

  /**
   * 页面的初始数据
   */
  data: {
15 16 17
    page: 0,
    pageSize: 15,
    records: [],
18
    recordTypes: app.globalData.recordTypes,
张杰's avatar
张杰 committed
19
    eatStates: app.globalData.eatStates,
张杰's avatar
张杰 committed
20 21
    rightIcon: '/src/img/xiugaimima.png',
    pictureId: "",
张杰's avatar
张杰 committed
22
    userIcon: "/src/img/defaut_teacher_icon.png",
张杰's avatar
张杰 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    selectPic: [],
    student: {},
    //顶部加载提示信息
    hideTopMore: true,
    // 底部加载提示信息
    hideBottomMore: true
  },

  scrollToUpper: function(e) {
    console.log("滚动到顶部")
    this.setData({
      hideTopMore: false
    })

    this.data.page = 0
    this.httpRequest()

  },

  scrollToLower: function(e) {
    this.data.page++
    this.httpRequest()
张杰's avatar
张杰 committed
45 46 47 48 49 50 51
  },

  tapUserInfoImg: function(e) {
    var that = this;
    // 选择图片
    wx.chooseImage({
      count: 1,
张杰's avatar
张杰 committed
52
      sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
张杰's avatar
张杰 committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function(res) {
        //选中的图片路径
        let temPath = res.tempFilePaths[0]
        that.data.selectPic.push(temPath)
        that.setData({
          selectPic: that.data.selectPic
        });

        wx.uploadFile({
          url: utils.uploadPath(),
          filePath: temPath,
          name: 'file',
          header: {
            'access-token': utils.getAccessToken()
          },
          success(res) {
            const resData = JSON.parse(res.data)
            // 获取服务端图片id
            that.data.pictureId = resData.obj
            that.data.userIcon = utils.handleImgShow(resData.obj)
            if (resData.obj) {
              that.setData({
                pictureId: that.data.pictureId,
                userIcon: that.data.userIcon
              })
张杰's avatar
张杰 committed
79 80

              that.updateStudentPicture()
张杰's avatar
张杰 committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            } else {
              throw new Error({
                msg: '图片上传失败'
              })
            }
          },
          fail(err) {
            wx.showToast({
              title: err.msg,
              icon: 'none'
            })
          }
        })

      }
    })
张杰's avatar
张杰 committed
97 98
  },

张杰's avatar
张杰 committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  updateStudentPicture: function() {
    request.updateStudentPicture(this.data.student.id, this.data.pictureId).then(res => {
      wx.showToast({
        title: "上传成功"
      });

    }).catch(function(err) {
      that.getAllStudent(that.data.classId)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      });
    });
  },

张杰's avatar
张杰 committed
115 116 117 118 119 120
  changePwdTap: function(e) {
    wx.navigateTo({
      url: '/pages/reset_psw/index',
    })
  },

张杰's avatar
张杰 committed
121 122
  previewPic: function(e) {
    var current = this.data.records[e.target.dataset.index].arrPic[e.target.dataset.picindex]
张杰's avatar
张杰 committed
123 124 125 126 127 128 129
    wx.previewImage({
      current: current,
      urls: this.data.records[e.target.dataset.index].arrPic
    })

  },

张杰's avatar
张杰 committed
130 131 132
  /**
   * 生命周期函数--监听页面加载
   */
133
  onLoad: function(options) {
张杰's avatar
张杰 committed
134 135
    this.fillStudent()
    this.httpRequest()
张杰's avatar
张杰 committed
136

张杰's avatar
张杰 committed
137
  },
张杰's avatar
张杰 committed
138

张杰's avatar
张杰 committed
139 140
  fillStudent: function() {
    const that = this
张杰's avatar
张杰 committed
141 142 143 144 145 146
    if (app.globalData.login.students.length == 0) {
      wx.showToast({
        title: '无学生信息, 请联系管理员',
        icon: 'none'
      })
      return
张杰's avatar
张杰 committed
147
    }
张杰's avatar
张杰 committed
148 149
    let student = app.globalData.login.students[0]
    var studentId = student.id
张杰's avatar
张杰 committed
150
    this.setData({
张杰's avatar
张杰 committed
151
      student: student,
张杰's avatar
张杰 committed
152
      sex: student.sex
153
    })
张杰's avatar
张杰 committed
154

张杰's avatar
张杰 committed
155 156 157 158 159 160 161
    if (student.picture != null) {
      let pic = utils.handleImgShow(student.picture)
      this.setData({
        userIcon: pic
      })
    }

张杰's avatar
张杰 committed
162 163 164 165 166
  },

  httpRequest: function() {
    const that = this
    request.queryStudentRecord(that.data.page, that.data.pageSize, that.data.student.id).then(res => {
张杰's avatar
张杰 committed
167 168 169 170 171
      console.log(res)
      let resData = res

      var record = resData['records']
      var templist = this.data.records
张杰's avatar
张杰 committed
172 173 174 175 176 177
      if (that.data.page == 0) {
        templist = []
        this.setData({
          hideTopMore: true
        })
      }
张杰's avatar
张杰 committed
178 179 180 181 182 183 184 185
      for (var i = 0; i < record.length; i++) {
        var item = record[i]

        var dateStr = item.recordDate.substr(0, 10)
        let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
        let date = new Date(dateStr);
        var num = date.getDay()
        item.recordDate = dateStr + " " + show_day[date.getDay()]
张杰's avatar
张杰 committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199
        //图片
        var arrPic = []
        let arrIds = []
        if (item.pictures != null && item.pictures != '') {
          arrIds = item.pictures.split(",")
          for (var j = 0; j < arrIds.length; j++) {

            let id = arrIds[j]
            arrPic.push(utils.handleImgShow(id))
          }
        }
        console.log(arrPic)
        item.arrIds = arrIds
        item.arrPic = arrPic
张杰's avatar
张杰 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

        // 饮食
        var dietRecords = item['dietRecord']

        for (var j = 0; j < dietRecords.length; j++) {
          var diet = dietRecords[j]
          diet.z_recordType = that.data.recordTypes[diet.recordType]
          diet.z_eatState = that.data.eatStates[diet.eatState]
          if (diet.recordType == 'diet_milk') {
            diet.z_eatState = diet.value + "毫升"
          }
          if (diet.recordType == 'diet_water') {
            diet.z_eatState = diet.value + "次"
          }
        }
张杰's avatar
张杰 committed
215

张杰's avatar
张杰 committed
216 217 218 219 220
        // 睡觉
        var sleepRecord = item["sleepRecord"]
        for (var j = 0; j < sleepRecord.length; j++) {
          var r = sleepRecord[j]
          r.z_recordType = that.data.recordTypes[r.recordType]
张杰's avatar
张杰 committed
221

张杰's avatar
张杰 committed
222
        }
张杰's avatar
张杰 committed
223

张杰's avatar
张杰 committed
224 225 226 227 228
        // wc
        var wcRecord = item["wcRecord"]
        for (var j = 0; j < wcRecord.length; j++) {
          var r = wcRecord[j]
          r.z_recordType = that.data.recordTypes[r.recordType]
张杰's avatar
张杰 committed
229

张杰's avatar
张杰 committed
230
        }
张杰's avatar
张杰 committed
231

张杰's avatar
张杰 committed
232 233 234
        console.log(item)
        templist.push(item)
      }
张杰's avatar
张杰 committed
235 236


张杰's avatar
张杰 committed
237 238 239 240 241
      this.setData({
        records: templist,
        page: resData['paging']['page']
      })
    })
张杰's avatar
张杰 committed
242 243 244 245 246
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
247
  onReady: function() {
张杰's avatar
张杰 committed
248 249 250 251 252 253

  },

  /**
   * 生命周期函数--监听页面显示
   */
254
  onShow: function() {
张杰's avatar
张杰 committed
255 256 257 258 259 260

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
261
  onHide: function() {
张杰's avatar
张杰 committed
262 263 264 265 266 267

  },

  /**
   * 生命周期函数--监听页面卸载
   */
268
  onUnload: function() {
张杰's avatar
张杰 committed
269 270 271 272 273 274

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
275
  onPullDownRefresh: function() {
张杰's avatar
张杰 committed
276 277 278 279 280 281

  },

  /**
   * 页面上拉触底事件的处理函数
   */
282
  onReachBottom: function() {
张杰's avatar
张杰 committed
283 284 285 286 287 288

  },

  /**
   * 用户点击右上角分享
   */
289
  onShareAppMessage: function() {
张杰's avatar
张杰 committed
290 291 292

  }
})