ios
litian
2024-04-16 d441735db86da8db28a87caca533d10e0493ae87
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
const app = getApp();
import SparkMD5 from 'spark-md5'
import tool from "../../../assets/js/toolClass.js";
import {
  getTopicMsgCmsItemFile
} from '../../../assets/js/middleGround/tool.js';
import FormData from '../../../utils/formdata/index.js';
import {
  loginInfo
} from '../../../assets/js/login';
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    teacherInfo: {
      schoolName: '', //学校名称
      fullName: '', //姓名
      positionalTitle: '', //职称
      courseName: '', //任课教程
      phone: '', //联系电话
      telphone: '', //座机
      email: '', //联系邮箱
      detailedAddress: '', //通讯地址
      relevantCertificates: [], //相关证件
      state: '', //审核状态默认待审核
      agree: false
    },
    teachPosts: [],
    teachText: '',
    teachVisible: false,
    phoneError: false,
    telphoneError: false,
    emailError: false,
    topicMessageList: [],
    topicId: "",
    worksInfo: [],
    userId: '',
    imgPics: [],
    file: "",
    imgs: [],
    protocolTxt: '', //教师协议
    protocolShow: false,
    editState: true,
    reasonTxt: "",
    reasonTxtShow: false,
    skeletonLoding: true,
    isIos: wx.getSystemInfoSync().platform === 'ios',
    keyboardHeight: 0
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    //获取职称
    this.getpositionalTitle();
    this.getAgreement();
    // 检查登录状态
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        if (data) {
          this.getUserRole()
        }
      })
    } else {
      this.getUserRole()
    }
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
 
  },
  getUserRole() {
    app.MG.identity.getCurrentAppUser().then((res) => {
      if (res) {
        this.getType()
        this.setData({
          userId: res.userId,
          skeletonLoding: true,
        })
        let nickNameData = res.infoList.find((item) => item.type == 'nickName')
        let teacherRole = res.roleLinks.find((item) => item.role.refCode == 'teacher')
        let teacherInfos = res.infoList.find((item) => item.type == 'teacherInfo')
        let WeChatInfo = res.infoList.find((item) => item.type === "WeChat");
        let defaultUser = {};
        if (teacherRole && teacherInfos) {
          defaultUser = {
            ...teacherInfos,
            nickName: nickNameData && JSON.parse(nickNameData.data).nickName ? JSON.parse(nickNameData.data).nickName : WeChatInfo ? WeChatInfo.name : '',
            icon: nickNameData && JSON.parse(nickNameData.data).icon != "" ? JSON.parse(nickNameData.data).icon : WeChatInfo ? WeChatInfo.icon : '',
            userId: res.userId,
            role: 'Teacher',
            roleId: teacherRole.role.id,
          }
 
        } else if (WeChatInfo) {
          defaultUser = {
            ...WeChatInfo,
            nickName: nickNameData && JSON.parse(nickNameData.data).nickName ? JSON.parse(nickNameData.data).nickName : WeChatInfo.name,
            icon: nickNameData && JSON.parse(nickNameData.data).icon != "" ? JSON.parse(nickNameData.data).icon : WeChatInfo.icon,
            userId: res.userId,
          }
        }
        wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser));
      }
    })
  },
  //获取字段
  getType() {
    app.MG.resource.getCmsTypeByRefCode({
      refCodes: ['jsek_teacherCertification']
    }).then((res) => {
      this.setData({
        worksInfo: res[0].cmsTypeLinks[0].children,
      })
      this.newGetTeacherInfo()
    })
 
  },
  //获取教师状态
  newGetTeacherInfo() {
    let that = this;
    const data = {
      start: 0,
      size: 10,
      topicIdOrRefCode: 'teacherRoleApproval',
      appRefCode: app.config.appRefCode,
      sort: {
        type: 'Desc',
        field: 'CreateDate'
      }
    }
    app.MG.ugc.getTopicMessageList(data).then((res) => {
      try {
        that.setData({
          imgPics: [],
        })
        const resData = res.datas.find((i) => i.appUserCreator.userId === that.data.userId)
        if (resData) {
          if (resData.state == 'WaitAudit') {
            that.setData({
              editState: false,
            })
          } else {
            that.setData({
              editState: true,
            })
          }
          let info = getTopicMsgCmsItemFile(that.data.worksInfo, resData.cmsItemDataList)
          that.setData({
            'teacherInfo.fullName': info.fullName,
            'teacherInfo.schoolName': info.schoolName,
            'teacherInfo.positionalTitle': info.positionalTitle,
            'teacherInfo.courseName': info.courseName,
            'teacherInfo.phone': info.phone,
            'teacherInfo.telphone': info.telphone ? info.telphone : '',
            'teacherInfo.email': info.email,
            'teacherInfo.detailedAddress': info.detailedAddress ? info.detailedAddress : '',
            'teacherInfo.relevantCertificates': info.relevantCertificates,
            'teacherInfo.agree': true,
            'teacherInfo.state': resData.state,
            topicId: resData.id,
            topicMessageList: resData.cmsItemDataList,
            teachText: info.positionalTitle,
            skeletonLoding: false,
          })
          if (resData.feedBack != null) {
            that.setData({
              reasonTxt: JSON.parse(resData.feedBack).reason,
            })
          }
          if (that.data.teacherInfo.relevantCertificates.length > 0) {
            let arr = [];
            if (typeof that.data.teacherInfo.relevantCertificates == 'object') {
              that.data.teacherInfo.relevantCertificates.forEach((ele) => {
                let imgObj = {
                  md5: ele.file.md5,
                  linkType: 'LinkFile',
                  linkProtectType: 'Public',
                  url: app.config.requestCtx + `/file/GetPreViewImage?md5=` + ele.md5,
                  imgUrl: ele.file.imgUrl
                }
                arr.push(imgObj);
                that.setData({
                  imgPics: arr,
                });
              })
            } else {
              let imgObj = {
                md5: that.data.teacherInfo.relevantCertificates,
                linkType: 'LinkFile',
                linkProtectType: 'Public',
                url: app.config.requestCtx + `/file/GetPreViewImage?md5=` + that.data.teacherInfo.relevantCertificates,
                imgUrl: app.config.requestCtx + `/file/GetPreViewImage?md5=` + that.data.teacherInfo.relevantCertificates,
              }
              arr.push(imgObj);
              that.setData({
                imgPics: arr,
              });
            }
          }
 
        } else {
          that.setData({
            skeletonLoding: false,
          });
        }
      } catch (error) {}
    })
  },
  //获取职称
  getpositionalTitle() {
    const data = {
      refCodes: ['positionalTitle']
    }
    app.MG.store.getProductTypeField(data).then((res) => {
      try {
        let list = res[0]
        let options = JSON.parse(list.config).option
        options.forEach((item) => {
          item.label = item.name;
        })
        this.setData({
          teachPosts: options,
        })
      } catch (error) {
 
      }
    })
  },
  getAgreement() {
    let query = {
      path: 'jsek_protocol',
      fields: {
        content: []
      }
    }
    app.MG.resource.getItem(query).then((res) => {
      try {
        const data = res.datas.find((e) => e.refCode == 'jsek_teacherCertificationAgreement')
        this.setData({
          protocolTxt: data ? data.content : '暂无协议',
        })
      } catch (error) {
        this.setData({
          protocolTxt: '暂无协议',
        })
      }
    })
  },
  //学校名称
  onSchoolNameInput(e) {
    this.setData({
      "teacherInfo.schoolName": e.detail.value,
    });
  },
  //姓名
  onFullNameInput(e) {
    this.setData({
      "teacherInfo.fullName": e.detail.value,
    });
  },
  // 任教课程
  onCourseNameInput(e) {
    this.setData({
      "teacherInfo.courseName": e.detail.value,
    });
  },
  //详细地址
  onAddressInput(e) {
    this.setData({
      "teacherInfo.detailedAddress": e.detail.value,
    });
  },
 
 
  //选择职称
  onTeachPicker() {
    this.setData({
      teachVisible: true
    });
  },
  onPickerChange(e) {
    const {
      label
    } = e.detail;
    this.setData({
      teachVisible: false,
      teachText: label[0],
      'teacherInfo.positionalTitle': label[0],
    });
  },
  onPickerCancel(e) {
    this.setData({
      'teacherInfo.positionalTitle': '',
      teachVisible: false,
    });
  },
  //输入手机号
  onPhoneInput(e) {
    const {
      phoneError
    } = this.data;
    if (e.detail.value) {
      const isPhoneNumber = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(e.detail.value);
      if (phoneError === isPhoneNumber) {
        this.setData({
          phoneError: !isPhoneNumber,
          "teacherInfo.phone": e.detail.value,
        });
      }
    } else {
      this.setData({
        phoneError: false,
      });
    }
  },
  //输入座机
  onTelphoneInput(e) {
    const {
      telphoneError
    } = this.data;
    if (e.detail.value) {
      const isPhoneNumber = /^0\d{2}-\d{8}$|^0\d{3}-\d{7}$/.test(e.detail.value);
      if (telphoneError === isPhoneNumber) {
        this.setData({
          telphoneError: !isPhoneNumber,
          "teacherInfo.telphone": e.detail.value,
        });
      }
    } else {
      this.setData({
        telphoneError: false,
      });
    }
  },
  onEmailInput(e) {
    const {
      emailError
    } = this.data;
    if (e.detail.value) {
      const isPhoneNumber = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(e.detail.value);
      if (emailError === isPhoneNumber) {
        this.setData({
          emailError: !isPhoneNumber,
          "teacherInfo.email": e.detail.value,
        });
      }
    } else {
      this.setData({
        emailError: false,
      });
    }
  },
  // 上传图片事件
  chooseImg(e) {
    var that = this;
    if (that.data.imgPics.length == 4) {
      wx.showToast({
        title: "工作证文件不超过5个!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath也可以作为img标签的src属性显示图片
        wx.getFileSystemManager().readFile({
          filePath: res.tempFiles[0].tempFilePath, //选择图片返回的相对路径
          // encoding: 'binary', //编码格式
          success: ress => {
            //成功的回调
            let spark = new SparkMD5.ArrayBuffer();
            spark.append(ress.data);
            let md5 = spark.end(false);
            if (!that.data.imgPics.find((item) => item.md5 == md5)) {
              let formData = new FormData();
              formData.append('Md5', md5);
              formData.append('FileName', md5);
              formData.append('FileType', res.tempFiles[0].fileType);
              formData.appendFile("file", res.tempFiles[0].tempFilePath);
              const data = formData.getData();
              let _token = wx.getStorageSync(app.config.tokenKey);
              let header = {};
              if (_token == null) {
                header["Authorization"] = `Basic ${Base64.encode(website.clientId + ":" + website.clientSecret)}`;
              } else {
                header["Authorization"] = `Bearer ` + _token;
              }
              new Promise((resolve, reject) => {
                wx.request({
                  url: app.config.requestCtx + '/file/api/ApiUpload',
                  method: 'POST',
                  header: {
                    'content-type': data.contentType,
                    ...header
                  },
                  data: data.buffer,
                  success(res1) {
                    if (res1.statusCode == 200) {
                      resolve(res1.data);
                      if (res1.data) {
                        let arr = [];
                        let imgObj = {
                          md5: md5,
                          linkType: 'LinkFile',
                          linkProtectType: 'Public',
                          url: app.config.requestCtx + `/file/GetPreViewImage?md5=` + md5,
                          imgUrl: res.tempFiles[0].tempFilePath
                        }
                        arr = [...that.data.imgPics, ...[imgObj]];
 
                        that.setData({
                          imgPics: arr,
                        });
                      }
                    } else {
                      reject('运行时错误,请稍后再试');
                    }
                  }
                })
              })
            } else {
              wx.showToast({
                title: "当前文件已上传,请勿重复操作!",
                icon: 'none',
                duration: 1000,
              })
            }
          }
        })
 
      }
    });
  },
  // 删除图片 
  deleteImg(e) {
    var imgs = this.data.imgPics;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgPics: imgs
    });
  },
  // 预览现场图片 
  previewImg(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    let imgs = [];
    this.data.imgPics.forEach(item => {
      imgs.push(item.url)
    })
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  onChange() {
    if (this.data.teacherInfo.agree) {
      this.setData({
        'teacherInfo.agree': false,
      });
    } else {
      this.setData({
        'teacherInfo.agree': true,
      });
    }
 
  },
  //查看教师认证服务
  viewContent() {
    this.setData({
      protocolShow: true,
    });
  },
  onVisibleChange(e) {
    this.setData({
      protocolShow: e.detail.visible,
      reasonTxtShow: e.detail.visible
    });
  },
  onCloseProtocol() {
    this.setData({
      protocolShow: false,
    });
  },
 
  //查看为通过原因
  previewReason() {
    this.setData({
      reasonTxtShow: true,
    });
  },
 
  onCloseReasonTxt() {
    this.setData({
      reasonTxtShow: false,
    });
  },
 
  submit() {
    let that = this;
    if (!that.data.teacherInfo.schoolName) {
      wx.showToast({
        title: "请填写学校名称!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (!that.data.teacherInfo.fullName) {
      wx.showToast({
        title: "请填写真实姓名!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (!that.data.teacherInfo.courseName) {
      wx.showToast({
        title: "请填写任教课程!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (!that.data.teacherInfo.phone) {
      wx.showToast({
        title: "请填写联系电话!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (!that.data.teacherInfo.email) {
      wx.showToast({
        title: "请填写邮箱!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (that.data.imgPics.length == 0) {
      wx.showToast({
        title: "请上传工作证!",
        icon: 'none',
        duration: 1000,
      })
      return
    }
    if (that.data.teacherInfo.agree) {
      if (that.data.topicMessageList.length > 0) {
        let dataRequests = tool.UpdateworksDataBytool(
          that.data.worksInfo,
          that.data.topicMessageList,
          that.data.teacherInfo,
          that.data.imgPics
        );
        const data = {
          description: '',
          icon: '',
          id: that.data.topicId,
          topicIdOrRefCode: 'teacherRoleApproval',
          name: that.data.teacherInfo.fullName + '',
          content: '',
          state: 'WaitAudit',
          type: 'teacherRegister',
          newDataRequests: dataRequests.newData,
          updateDataRequests: dataRequests.updateData,
          delDataRequest: {
            ids: []
          }
        }
        let basicInfo = JSON.parse(JSON.stringify(that.data.teacherInfo))
        delete basicInfo.worksInfo
        delete basicInfo.state
        const userInfo = {
          requests: [{
            data: JSON.stringify(basicInfo),
            name: that.data.teacherInfo.fullName + '',
            type: 'newTeacherInfo'
          }]
        }
        app.MG.identity.setAppUserInfo(userInfo).then((res) => {
          if (res) {
            app.MG.ugc.updateTopicMessage(data).then(() => {
              if (res !== false) {
                wx.showToast({
                  title: "提交成功!请等待审核...",
                  icon: 'none',
                  duration: 1000,
                })
                that.getUserRole()
                // that.newGetTeacherInfo()
              }
            })
          }
        })
      } else {
        const data = {
          topicIdOrRefCode: 'teacherRoleApproval',
          name: that.data.teacherInfo.fullName + '',
          content: '',
          state: 'WaitAudit',
          type: 'teacherRegister',
          cmsTypeRefCode: 'jsek_teacherCertification',
          newDataListRequest: tool.worksDataBytool(that.data.worksInfo, that.data.teacherInfo, that.data.imgPics)
        }
 
        let basicInfo = JSON.parse(JSON.stringify(that.data.teacherInfo))
        delete basicInfo.worksInfo
        delete basicInfo.state
        const userInfo = {
          requests: [{
            data: JSON.stringify(basicInfo),
            name: that.data.teacherInfo.fullName + '',
            type: 'teacherInfo'
          }]
        }
        app.MG.identity.setAppUserInfo(userInfo).then((res) => {
          app.MG.ugc.newTopicMessage(data).then(() => {
            if (res !== false) {
              wx.showToast({
                title: "提交成功!请等待审核...",
                icon: 'none',
                duration: 1000,
              })
              // that.newGetTeacherInfo()
              that.getUserRole()
            }
          })
        })
      }
    } else {
      wx.showToast({
        title: "请同意《教师认证服务条款》!",
        icon: 'none',
        duration: 1000,
      })
    }
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (wx.getStorageSync(app.config.tokenKey)) {
      this.getUserRole()
    }
  },
  // 监听页面软键盘弹起手动推动页面
  bindkeyboardheightchange(e) {
    // 只对ios 处理
    if (!this.data.isIos === 'ios') {
      return
    }
    // 键盘高度
    const height = e.detail.height;
    const className = e.target.dataset.class;
    if (height === 0) {
      this.scrollToInput(0);
      return;
    }
    try {
      this.createSelectorQuery()
        .select(`.${className}`)
        .boundingClientRect((res) => {
          // 可使用窗口高度
          const windowHeight = wx.getSystemInfoSync().windowHeight;
          // 除去键盘的剩余高度
          let restHeight = windowHeight - height;
          // 元素左下角坐标
          let bottom = res.bottom;
          // 只有当元素被软键盘覆盖的时候才上推页面
          if (bottom <= restHeight) return;
          // 现阶段需要滚动的大小
          let scrollTop = bottom - restHeight;
          this.scrollToInput(height, scrollTop);
        })
        .exec();
    } catch (error) {}
  },
 
  // 获取页面滚动条位置
  getScrollOffset() {
    return new Promise((resolve) => {
      try {
        wx.createSelectorQuery()
          .selectViewport()
          .scrollOffset((res) => {
            resolve(res.scrollTop);
          })
          .exec();
      } catch (error) {
        resolve(0);
      }
    });
  },
 
  // 监听页面软键盘弹起手动推动页面
  scrollToInput(keyboardHeight, scrollTop) {
    this.setData({
      keyboardHeight,
    });
    if (scrollTop) {
      try {
        this.getScrollOffset().then((lastScrollTop) => {
          wx.pageScrollTo({
            // 如果已经存在滚动,在此基础上继续滚
            scrollTop: lastScrollTop ? lastScrollTop + scrollTop : scrollTop,
            duration: 300,
          });
        });
      } catch (error) {}
    }
  },
  changeParam(e) {
    this.scrollToInput(0);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
 
  }
})