index.js 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
let utils = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    index: 0,
    index2: 0,
    array: [],
    array2: [],
    currentSwiper: 0,
    date: '2019-01-01 00:00', //默认时间
    disabled: false, //设置是否能点击 false可以 true不能点击
    startDate: 2019, //最早时间
    endDate: 2025, //最晚时间
    placeholder: '截止时间不得早于当前时间',
    lis_RightInput: "lis_RightInput",
    images: [],
    imagesImg: [],
    imagesJoin: '',
    ranking: '3',
    li2_UpPic2Img2: 'li2_UpPic2Img2',
    homeworkName: '', //作业名称
    textareaValue: '', //作业要求
    start: 0, 
    token: '',  
    fenId: '',
    homeworkId: '',  
    onShareImgs: '',
    state: '',
    getMessage: false,
    baseurl:app.globalData.cdnUrl,
    copy: false,
    test: wx.getStorageSync('testCount')
  },
  quitTest(){
    app.quitTest()
  },

  onLoad: function(options) {
    console.log(app.globalData)
    this.setData({
      test:wx.getStorageSync('testCount')
    })
    console.log(wx.getStorageSync('testCount'))
    wx.showLoading({
      title: '加载中',
    });
    this.getSetting()
    if (options.fenId != "") {
      wx.setStorageSync('fenId', options.fenId)
      let url2 = '/api/teacher/share';
      let parasm2 = {
        share_id: options.fenId
      };
      app.post(url2, parasm2).then(res => {})
    }
    if (options.state == 9) {
      wx.navigateTo({
        url: '/pages/detail/detail?zid=' + options.homeworkId + '&classid=' + options.class_id + '&fenState=' + 7,
      })
    };
    this.addWorkFun();
    var data2 = utils.formatDate(new Date());
    var data3 = utils.formatDate2(new Date());

    this.setData({
      date2: data2,
      date: data3,
      fenId: options.fenId,
      token: wx.getStorageSync('token'),
      state: getApp().globalData.showDialog
    });
    this.bannerFun();
  },
  getSetting() {
    let t = this;
    wx.getSetting({
      withSubscriptions: true,
      success(e) {
        console.log(e)
        var itemSettings = e.subscriptionsSetting.itemSettings;
        if (itemSettings) {
          if (itemSettings['T7885P1LTbkNdyw0IQyK5qZiVYoizhFHDGFRzVCPI3g'] == 'accept' && itemSettings['kYbHUPcGgSp2jL5cCsXN0_sobb-asd4B2YMf4RzSjQ8'] == 'accept') {
            t.setData({
              getMessage: false
            })
          }
        }else{
          t.setData({
            getMessage: true
          })
        }
      }
    })
  },
  navgate(e){
    wx.navigateTo({
      url: '/pages/newsDetail/newsDetail?src='+e.currentTarget.dataset.src,
    })
},
  setPush() {
    let t = this;
    wx.requestSubscribeMessage({
      tmplIds: ['T7885P1LTbkNdyw0IQyK5qZiVYoizhFHDGFRzVCPI3g','kYbHUPcGgSp2jL5cCsXN0_sobb-asd4B2YMf4RzSjQ8'],
      success(res) {
        if (res['T7885P1LTbkNdyw0IQyK5qZiVYoizhFHDGFRzVCPI3g'] == 'accept' || res['kYbHUPcGgSp2jL5cCsXN0_sobb-asd4B2YMf4RzSjQ8'] == 'accept') {
          t.setData({
            getMessage: false
          })
          wx.showModal({
            title: '提示',
            content: '恭喜您订阅成功',
          })
        }
      },
      fail(res) {
        console.log('没进来')
        console.log(res)
      }
    })
  },

  // 轮播图接口
  bannerFun() {
    let t = this;
    let url = "/api/banner/index";
    app.post(url,{
      type: wx.getStorageSync('ident')==1?1:2
    }).then(res => {
      t.setData({
        banner: res.data.msg
      });
    });
    wx.hideLoading();
  },
  inputSubject(e) {
    var date = new Date();
    var day = date.getDate();
    this.setData({
      tempSubValue: e.detail.value,
      homeworkName: e.detail.value + day + '日作业'
    })
  },
  // 老师布置作业显示 班级和科目接口
  addWorkFun() {
    let t = this;
    let url = '/api/teacher/addworkindex';

    app.post(url, {}).then(res => {
      if (res.data.code == 5000) {
        wx.showToast({
          title: '请重新登录后操作',
          icon:'none'
        })
        setTimeout(()=>{
          wx.redirectTo({
            url: '/pages/home/home',
          })
        },1300)

        // wx.showModal({
        //   title: '提示',
        //   content: '您还没有创建班级,请创建后操作!',
        //   showCancel: false,
        //   success: res => {
        //     if (res.confirm) {
        //       wx.navigateTo({
        //         url: '/pages/new_class2/new_class2',
        //       })
        //     }
        //   }
        // })
      } else {
        if(res.data.msg.allClass.length>0){
          res.data.msg.allClass.forEach(item=>{
            item.choose = false
          })
          res.data.msg.allClass[0].choose = true
          t.setData({
            array: res.data.msg.allClass,  
            allClass:res.data.msg.allClass,
            class_ids:[res.data.msg.allClass[0].id],
            className:res.data.msg.allClass[0].name,
            alreadyChoose:[res.data.msg.allClass[0]]
          })
        }


        let arr = [];
        let arrId = [];
        let arr2 = [];
        let arr2Id = [];
        
        if(res.data.msg.allClass.length>0){
          for (let i = 0; i < res.data.msg.allClass.length; i++) {
            arr.push(res.data.msg.allClass[i].name);
            arrId.push(res.data.msg.allClass[i].id);
          }
        }

        arr.push('创建班级');
        if (t.data.state == 9) {
          t.setData({
            array: arr,
            arrId: arrId,
            class_name: arr[t.data.array.length - 1],
            class_id: arrId[arrId.length - 1],
          })
        } else {
          this.setData({
            array: arr,
            arrId: arrId,
            class_name: arr[0],
            class_id: arrId[0],
          })
        }
        for (let i = 0; i < res.data.msg.allSubject.length; i++) {
          arr2.push(res.data.msg.allSubject[i].subject_name);
          arr2Id.push(res.data.msg.allSubject[i].id);
          this.setData({
            array2: arr2,
            arr2Id: arr2Id,
            Subject_Name: arr2[0],
            Subject_Id: arr2Id[0]
          })
        }
        // 作业名称默认值
        var date = new Date();
        var day = date.getDate();
        this.setData({
          homeworkName: this.data.array2[0] + day + '日作业',
        })
      }

    })
  },
  checkboxchange(e){
    console.log(e)
    console.log(e.detail.value);
    let ids = e.detail.value,alreadyArry=[];
    this.data.allClass.forEach((item,index)=>{
      
      // console.log(ids.includes(item.id.toString()))
      if(ids.includes(item.id.toString())){
        alreadyArry.push(item)
      };
    })
    // ids.forEach((item,index)=>{
      
    // })
    this.setData({
      alreadyChoose:alreadyArry,
      classIds:e.detail.value
      // alreadyArry:alreadyArry
    })
  },
  
  makeSure(){
    let class_ids =[],className = [],ids=this.data.classIds;
    this.data.alreadyChoose.forEach(item=>{
      class_ids.push(item.id);
      className.push(item.name);
    })
    this.data.allClass.forEach((item,index)=>{
      if(ids.includes(item.id.toString())){
        item.choose = true
      };
    })
    this.setData({
      class_ids:class_ids,
      className: className.join(','),
      chooseClass: false,
      allClass: this.data.allClass
    })
  },
  cancle(){
    this.setData({
      chooseClass: false
    })
  },
  gonav1() {

  },
  gonav2() {
    wx.redirectTo({
      url: '/pages/homework/homework',
    })
  },
  gonav3() {
    wx.redirectTo({
      url: '/pages/my/my',
    })
  },
  // 布置作业接口
  addWorkFun2(e) {
    wx.showLoading({
      title: '加载中',
    });
    let t = this;
    let url = '/api/teacher/addwork';
    // let url = '/api/teacher/addworks';
    var date = new Date();
    var day = date.getDate();
    let images = '';
    images = this.data.images.map(item=>
      item.replace('https://banjiqiniu.broing.cn','') 
    );
    let params = {
      class_id: t.data.class_id,
      // class_ids: t.data.class_ids.join(','),
      // subject_id: t.data.Subject_Id,
      subject_name: t.data.Subject_Name == '其他' ? t.data.tempSubValue : t.data.Subject_Name,
      class_name: t.data.class_name,
      work_name: t.data.homeworkName,
      endtime: t.data.date,
      workdesc: t.data.textareaValue,
      // images: t.data.imagesJoin,
      images: images,
      afterwork: t.data.ranking
    };
    if (t.data.token == '') {
      wx.redirectTo({
        url: '/pages/home/home?fenId=' + t.data.fenId,
      })
    } else if (t.data.array.length < 2) {
      wx.showToast({
        title: '请创建班级',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/new_class2/new_class2',
        })
      }, 1200)
    } else {
      console.log(t.data.tempSubValue)
      if (t.data.Subject_Name == '其他' && (t.data.tempSubValue == '' || t.data.tempSubValue == undefined)) {
        console.log(t.data.tempSubValue)
        wx.showToast({
          title: '请填写科目',
          icon: 'none'
        });
        return false;
      } else if (t.data.homeworkName == "" || t.data.textareaValue == "") {
        wx.showToast({
          title: '请填写作业要求',
          icon: 'none'
        });
      } else if (t.data.ytime < t.data.date2) {
        wx.showToast({
          title: '截止时间不得早于当前时间',
          icon: 'none'
        });
      } else {
        app.post(url, params).then(res => {
          var date = new Date();
          var day = date.getDate();
          this.homeworkId = res.data.msg;
          if (res.data.code == 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else if (res.data.code == 501) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            t.setData({
              textareaValue: '',
              images: [],
              imagesImg: [],
              li2_UpPic2Img2: 'li2_UpPic2Img2',
              imagesJoin: '',
              Subject_Name: t.data.array2[0] == "其他" ? t.data.tempSubValue : t.data.array2[0],
              homeworkName: (t.data.array2[0] == "其他" ? t.data.tempSubValue : t.data.array2[0]) + day + '日作业',
            });
            wx.showToast({
              title: '发布成功',
              icon: 'success',
              duration: 1200
            })

            wx.requestSubscribeMessage({
              tmplIds: ['T7885P1LTbkNdyw0IQyK5qZiVYoizhFHDGFRzVCPI3g', 'kYbHUPcGgSp2jL5cCsXN0_sobb-asd4B2YMf4RzSjQ8'],
              success(res) {
                if (res['T7885P1LTbkNdyw0IQyK5qZiVYoizhFHDGFRzVCPI3g'] == 'accept' || res['kYbHUPcGgSp2jL5cCsXN0_sobb-asd4B2YMf4RzSjQ8'] == 'accept') {
                  if(t.data.getMessage){
                    wx.showModal({
                      title: '提示',
                      content: '恭喜您订阅成功',
                      success: function (res) {
                        t.setData({
                          getMessage: false
                        })
                        setTimeout(() => {
                          wx.navigateTo({
                            url: '/pages/detail/detail?zid=' + t.data.homeworkId + '&classid=' + t.data.class_id + '&fenState=' + 6,
                          })
                        }, 1200)
                      }
                    })
                  }else{
                    setTimeout(() => {
                      wx.navigateTo({
                        url: '/pages/detail/detail?zid=' + t.data.homeworkId + '&classid=' + t.data.class_id + '&fenState=' + 6,
                      })
                    }, 1200)
                  }

                }else{
                  wx.navigateTo({
                    url: '/pages/detail/detail?zid=' + t.data.homeworkId + '&classid=' + t.data.class_id + '&fenState=' + 6,
                  })
                }
              },
              fail(res) {
                console.log('没进来')
                console.log(res)
              }
            })
            var fenId = wx.getStorageSync('fenId');
            if (fenId != '') {
              let url2 = '/api/teacher/share';
              let parasm2 = {
                share_id: fenId
              };
              app.post(url2, parasm2).then(res => {
                console.log(res)
                console.log(t.data.fenId);
              })
              t.setData({
                fenId: ''
              })
            }
            t.setData({
              homeworkId: res.data.msg,
            });
            // t.onShareImg();
          }
        });
      }
    }
  },

  // 转发图片
  onShareImg() {
    let t = this;
    let url = '/api/shareimg/createimg';
    let params = {
      homework_id: t.data.homeworkId,
      style: 1
    };
    app.post(url, params).then((res) => {
      t.setData({
        onShareImgs: res.data.msg
      })
    })
  },
  //转发
  onShareAppMessage: function(res) {
    let t = this,
      path = '/pages/new_class/new_class?id=' + t.data.homeworkId + '&class_id=' + t.data.class_id + '&where=' + 1;
    // if (t.data.homeworkId) {
    return {
      title: '作业来啦',
      path: '/pages/new_class/new_class',
      imageUrl: t.data.onShareImgs,
      success: function(res) {
        // wx.redirectTo({
        //     url: '/pages/homework/homework',
        // })
      },
      fail: (err) => {
        console.log("转发失败", err);
      }
    }
    // }

  },
  // 前往详情
  // goDetail(){
  //     this.setData({
  //         shareBool: false,
  //         textState:true,
  //         disabled:false
  //     })
  //
  // },
  // 前往奖励设置页面
  goReward() {
    wx.navigateTo({
      url: '/pages/reward/reward',
    })
  },
  // 轮播底部原点
  swiperChange: function(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  // picker 发布班级
  bindPickerChange(e) {
    console.log(e);
    let t = this;
    let index = Number(e.detail.value);
    let name = e.currentTarget.dataset.name;
    t.setData({
      class_name: t.data.array[index],
      class_id: t.data.arrId[index],
      share1: true,
      share2: false,
    });
    if (t.data.class_name == "创建班级") {
      wx.navigateTo({
        url: '/pages/new_class2/new_class2',
      })
    };
    // this.setData({
    //   chooseClass: true
    // })
  },
  // picker 科目选择
  bindPickerChange2(e) {
    let t = this;
    let index2 = Number(e.detail.value);
    // 作业名称默认值
    var date = new Date();
    var day = date.getDate();
    this.setData({
      Subject_Name: t.data.array2[index2],
      Subject_Id: t.data.arr2Id[index2],
      share1: true,
      share2: false,
      homeworkName: this.data.array2[index2] + day + '日作业',
    })
  },
  // bindinput 作业名称
  homeworkFun(e) {
    console.log(e);
    let that = this;
    that.setData({
      homeworkName: e.detail.value,
      share1: true,
      share2: false
    })
    if (e.detail.value.length >= 12) {
      wx.showToast({
        title: '作业名称不得超过12个字',
        icon: 'none'
      })
    }
  },
  // picker 截止时间
  onPickerChange: function(e) {
    let that = this;
    console.log(e);
    let ytime = e.detail.year + e.detail.month + e.detail.day + e.detail.time + e.detail.division;
    let date = e.detail.dateString;
    that.setData({
      ytime: ytime,
      date: date,
      share1: true,
      share2: false
      // lis_RightInput:'lis_RightInput2'
    });
  },
  // 删除图片
  removeFun(e) {
    console.log(e);
    let t = this;
    let ind = e.currentTarget.dataset.ind;
    t.data.imagesImg.splice(ind, 1);
    t.data.images.splice(ind, 1);
    t.setData({
      images: t.data.images,
      imagesImg: t.data.imagesImg,
      imagesJoin: t.data.images.join(',')
    })
  },
  // 作业要求
  textareaFun(e) {
    let t = this;
    t.setData({
      textareaValue: e.detail.value,
      share1: true,
      share2: false
    })
  },
  // 图片上传
  addPicFun() {
    let that = this;
    // let tempFilePaths = [];
    wx.chooseImage({
      // count: 3,
      sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {

        // // 限制最多只能留下3张照片
        // var image = that.data.images = images.length <= 3 ? images : images.slice(0, 3);
        that.setData({
          li2_UpPic2Img2: 'li2_UpPic2Block'
        });
        let successUp = 0; //成功个数
        let failUp = 0; //失败个数
        let i = 0; //第几个
        let tempFilePaths = res.tempFilePaths; //总文件
        let length = res.tempFilePaths.length;
        that.uploadAllfile(tempFilePaths, successUp, failUp, i, length);
      }
    })
  },
  // 上传图片至后台
  uploadAllfile(filePaths, successUp, failUp, i, length) {
    let that = this;
    let url = 'https://banji.w.broing.cn/api/common/upload';
    let headers = {
      "token": wx.getStorageSync("token"),
      'content-type': 'multipart/form-data'
    }
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: url, //仅为示例,非真实的接口地址
      header: headers,
      filePath: filePaths[i],
      name: 'file',
      formData: {
        file: '',
        filetype: 'image'
      },
      success: function(res) {
        let dat = JSON.parse(res.data);
        const imagesImg = that.data.imagesImg.concat(filePaths[i]);
        that.setData({
          images: that.data.images.concat(app.globalData.cdnUrl+dat.msg),
          imagesImg: imagesImg
        }); 
        that.setData({
          imagesJoin: that.data.images.join(',')
        })
      },
      fail: function(res) {

      },
      complete() {
        wx.hideLoading()
        i++;
        // let img = that.data.img
        if (i == length) {
          // console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败!');
        } else { //递归调用uploadDIY函数
          that.uploadAllfile(filePaths, successUp, failUp, i, length);
        }
      }
    })
  },
  onShow: function(e) {
    // this.addWorkFun();
      let homeWorkInfo = wx.getStorageSync('copyHomeWork');
      console.log(homeWorkInfo.copy)
      if(homeWorkInfo.copy){
        this.setData({
          copy: homeWorkInfo.copy,
          date: homeWorkInfo.homeWorkInfo.endtime,
          textareaValue: homeWorkInfo.homeWorkInfo.workdesc,
          images: homeWorkInfo.homeWorkInfo.images[0]==''?[]:homeWorkInfo.homeWorkInfo.images
        })
        this.bindPickerChange()
      }
  },
  onHide: function(){
    wx.removeStorageSync('copyHomeWork')
  },
  onUnload: function(){
    wx.removeStorageSync('copyHomeWork')
  }
})