闫增涛
2024-04-02 9f916f98c6cc6827c84da18fbc9a5e2cacbefc0a
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
import {
  worksDataBytool
} from "../../../../assets/js/toolClass.js";
const app = getApp();
Page({
  data: {
    userInfo: null,
    active: 0,
    description: "", //申请规则
    teacherInfo: {
      workInfo: null,
      schoolName: "",
      courseName: "",
      detailedAddress: ""
    },
    contactInfo: {
      fullName: "",
      phone: "",
      detailedAddress: ""
    },
    phoneError: false,
    paperBookList: [], //样书单纸质书
    electronicBookList: [], //样书单电子书
    ebookCount: 0,
    paperBookCount: 0,
    bookList: [],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync(app.config.userInfoKey)) {
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync(app.config.userInfoKey))
      })
      if (this.data.userInfo.role == 'Teacher') {
        this.setData({
          'teacherInfo.schoolName': JSON.parse(this.data.userInfo.data).schoolName,
          'teacherInfo.courseName': JSON.parse(this.data.userInfo.data).courseName,
          'contactInfo.fullName': JSON.parse(this.data.userInfo.data).fullName,
          'contactInfo.phone': JSON.parse(this.data.userInfo.data).phone,
          'contactInfo.detailedAddress': JSON.parse(this.data.userInfo.data).detailedAddress,
        })
      }
      if (wx.getStorageSync("paperBookList")) {
        this.setData({
          paperBookList: wx.getStorageSync("paperBookList"),
          bookList: wx.getStorageSync("paperBookList")
        })
      }
      if (wx.getStorageSync("electronicBookList")) {
        this.setData({
          electronicBookList: wx.getStorageSync("electronicBookList")
        })
      }
      this.getSelectBookCount();
      this.getSelectPaperBookCount();
      this.getPaperType();
    }
  },
 
  tabClick(item) {
    let that = this;
    that.setData({
      bookList: []
    })
    if (item.detail.value == '1') {
      that.setData({
        active: item.detail.value,
        bookList: this.data.electronicBookList
      })
      this.getElectronType();
    } else {
      that.setData({
        active: item.detail.value,
        bookList: this.data.paperBookList
      })
      this.getPaperType();
    }
  },
 
  getElectronType() {
    app.MG.resource.getCmsTypeByRefCode({
      refCodes: ['jsek_electronicSampleBook']
    }).then((res) => {
      this.setData({
        description: res[0].description,
        'teacherInfo.workInfo': res[0].cmsTypeLinks[0].children,
      })
    })
  },
  getPaperType() {
    app.MG.resource.getCmsTypeByRefCode({
      refCodes: ['jsek_paperVersionSampleBook']
    }).
    then((res) => {
      if (res && res.length > 0) {
        this.setData({
          description: res[0].description,
          'teacherInfo.workInfo': res[0].cmsTypeLinks[0].children,
        })
      }
    })
  },
  //获取当前已申请次数(电子)
  getSelectBookCount() {
    let that = this;
    app.MG.app.getTicketResult({
        ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
        roleId: that.data.userInfo.roleId
      })
      .then((res) => {
        let num = res.totalCount - res.usedCount;
        that.setData({
          ebookCount: num
        })
      })
  },
  //获取当前已申请次数(纸质)
  getSelectPaperBookCount() {
    let that = this;
    app.MG.app.getTicketResult({
        ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
        roleId: that.data.userInfo.roleId
      })
      .then((res) => {
        let count = res.totalCount - res.usedCount;
        that.setData({
          paperBookCount: count,
        })
      })
  },
  //姓名
  onFullNameInput(e) {
    this.setData({
      "contactInfo.fullName": e.detail.value,
    });
  },
  //输入手机号
  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,
          "contactInfo.phone": e.detail.value,
        });
      }
    } else {
      this.setData({
        phoneError: false,
      });
    }
  },
  onAddressInput() {
    this.setData({
      "contactInfo.detailedAddress": e.detail.value,
    });
  },
 
 
  submit() {
    let that = this;
    if (that.data.bookList.length > 0) {
      if (that.data.active == '1' && that.data.ebookCount == 0) {
        wx.showToast({
          title: "您的电子样书申请次数已用尽,若要继续,请联系管理员!",
          icon: 'none',
          duration: 1000,
        })
        return
      }
      if (that.data.active == '0' && that.data.paperBookCount == 0) {
        wx.showToast({
          title: "您的纸质样书申请次数已用尽,若要继续,请联系管理员!",
          icon: 'none',
          duration: 1000,
        })
        return
      }
      let data = {}
      let applyInfo = {}
      if (that.data.active == '1') {
        data = {
          topicIdOrRefCode: 'applyBook',
          name: that.data.userInfo.name || '',
          content: JSON.stringify(that.data.bookList),
          state: 'WaitAudit',
          type: 'applyForEbook',
          cmsTypeRefCode: 'jsek_electronicSampleBook',
          newDataListRequest: worksDataBytool(that.data.teacherInfo.workInfo, that.data.teacherInfo)
        }
        applyInfo = {
          ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
          roleId: that.data.userInfo.roleId,
          refType: 'applyBook'
        }
      } else {
        if (!(that.data.contactInfo.fullName && that.data.contactInfo.phone && that.data.contactInfo.detailedAddress)) {
          wx.showToast({
            title: "请将收货人信息填写完整!",
            icon: 'none',
            duration: 1000,
          })
          return
        }
        const mailAddress = JSON.stringify(that.data.contactInfo)
        that.setData({
          'teacherInfo.detailedAddress': mailAddress
        })
 
        data = {
          topicIdOrRefCode: 'applyEntityBook',
          name: that.data.userInfo.name || '',
          content: JSON.stringify(that.data.bookList),
          state: 'WaitAudit',
          type: 'applyForPbook',
          cmsTypeRefCode: 'jsek_paperVersionSampleBook',
          newDataListRequest: worksDataBytool(that.data.teacherInfo.workInfo, that.data.teacherInfo)
        }
        applyInfo = {
          ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
          roleId: that.data.userInfo.roleId,
          refType: 'applyEntityBook'
        }
      }
      app.MG.ugc.newTopicMessage(data).then((res) => {
        if (res) {
          app.MG.app.useTicket(applyInfo).then(() => {
            if (that.data.active == '1') {
              that.setData({
                electronicBookList: [],
                bookList: [],
              })
              wx.removeStorageSync("electronicBookList")
            } else {
              that.setData({
                paperBookList: [],
                bookList: [],
              })
              wx.removeStorageSync("paperBookList")
            }
            wx.showModal({
              title: '提示!', //提示的标题
              content: '您的样书申请已提交,管理员审核中!申请状态在【个人中心—样书申请】中查询。', //提示的内容
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: "/packagePersonal/pages/sampleBooks/index?stateTab=1&tabActive=" + that.data.active,
                  });
                } else if (res.cancel) {}
              }
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: "您还未选择图书加入申请单!",
        icon: 'none',
        duration: 1000,
      })
    }
 
  },
 
  onDelete(e) {
    let that = this;
    const item = e.currentTarget.dataset.book;
    wx.showModal({
      title: '提示!', //提示的标题
      content: '确定要删除该书吗?', //提示的内容
      success: function (res) {
        if (res.confirm) {
          if (that.data.active == '1') {
            let eList = that.data.electronicBookList;
            eList.forEach((obj, index) => {
              if (obj.id == item.id) {
                eList.splice(index, 1);
              }
            })
            that.setData({
              bookList: eList
            })
            wx.setStorageSync("electronicBookList", eList);
          } else {
            let pList = that.data.paperBookList;
            pList.forEach((obj, index) => {
              if (obj.id == item.id) {
                pList.splice(index, 1);
              }
            })
            that.setData({
              bookList: pList
            })
            wx.setStorageSync("paperBookList", pList);
          }
        } else if (res.cancel) {}
      }
    })
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
 
  }
})