杨磊
4 天以前 ef37c59e055a990ce247b265b27d3fcef430a243
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
<template>
  <div>
    <div class="content">
      <div class="information">
        <div v-if="userInfo.icon">
          <span>头像:</span>
          <span><img class="usericon" :src="userInfo.icon" /></span>
        </div>
        <div>
          <span>昵称:</span>
          <span>{{ userInfo.name }}</span>
        </div>
        <div>
          <span>微信认证:</span>
          <span v-if="weChatState" class="primaryTxt">已认证</span>
          <span v-else class="warningTxt">未认证</span>
          <p v-if="!weChatState" class="modify" @click="goBindWeChat">去认证</p>
        </div>
        <div>
          <span>绑定手机:</span>
          <span v-if="userInfo.phoneNumber">{{ userInfo.phoneNumber }}</span>
          <span v-if="!userInfo.phoneNumber" class="warningTxt">未绑定</span>
          <p
            v-if="userInfo.phoneNumber"
            class="modify"
            @click="
              () => {
                isPlace = true;
              }
            "
          >
            更换手机号
          </p>
          <p
            v-if="!userInfo.phoneNumber"
            class="modify"
            @click="
              () => {
                isPlace = true;
              }
            "
          >
            绑定手机号
          </p>
        </div>
      </div>
      <div class="wrapImg">
        <img src="" class="autoImg" alt="" />
      </div>
    </div>
    <!-- 更换手机号模态框 -->
    <el-dialog
      title="绑定手机号"
      :visible="isPlace"
      width="600px"
      :before-close="handleClose"
    >
      <el-form
        :key="formKey"
        :model="replacePhone"
        ref="form"
        label-position="left"
        :rules="rules"
        label-width="130px"
      >
        <el-form-item label="手机号:" prop="phone">
          <el-input
            v-model="replacePhone.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="图形验证码:" prop="imgCode">
          <div class="flex">
            <el-input
              v-model="replacePhone.imgCode"
              placeholder="请输入图形验证码"
            ></el-input>
            <div class="imgCodeBox">
              <img :src="imgCode" alt="" @click="getImgCapcha" />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="短信验证码:" prop="verificationCode">
          <div class="flex">
            <el-input
              v-model="replacePhone.verificationCode"
              placeholder="请输入短信验证码"
            ></el-input>
            <div class="imgCodeBox">
              <el-button
                style="width: 100%"
                @click="getVerifyCode"
                :disabled="countDown != 0"
                >{{
                  countDown == 0
                    ? "获取短信验证码"
                    : "验证码(" + countDown + "s)"
                }}</el-button
              >
            </div>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="replace">确 定</el-button>
      </span>
    </el-dialog>
    <div class="title">
      用户类型
      <span> THE USER TYPES </span>
    </div>
    <div class="content">
      <div class="information">
        <div>
          <span>用户类型:</span>
          <span>{{ userInfo.role == "Teacher" ? "教师" : "学生" }}</span>
        </div>
        <div>
          <span>教师认证:</span>
          <span class="primaryTxt" v-if="userInfo.role == 'Teacher'"
            >已认证</span
          >
          <span v-else>未认证</span>
          <p
            class="modify"
            v-if="userInfo.role == 'Teacher'"
            @click="toTeacherRegister"
          >
            修改信息
          </p>
        </div>
        <div v-if="userInfo.role == 'Teacher'">
          <span style="width: 150px">电子书剩余申请次数:</span>
          <span> {{ electronicSampleBookapplyNum }}次 </span>
        </div>
        <div v-if="userInfo.role == 'Teacher'">
          <span style="width: 150px">纸质书剩余申请次数:</span>
          <span> {{ paperSampleBookapplyNum }}次 </span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["userInfo"]),
  },
  data() {
    return {
      isPlace: false,
      formKey: 0, // 解决form 表单,验证表单状态不清空问题
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
        imgCode: [
          { required: true, message: "请输入图形验证码", trigger: "blur" },
          { min: 4, max: 4, message: "请输入 4 位验证码", trigger: "blur" },
        ],
        verificationCode: [
          { required: true, message: "请输入短信验证码", trigger: "blur" },
        ],
      },
      imgCode: "",
      replacePhone: {
        phone: "",
        imgCode: "",
        verificationCode: "",
      },
      countDown: 0, // 验证码倒计时秒数
      weChatState: false, // 微信认证状态
      electronicSampleBookapplyNum: 0,
      paperSampleBookapplyNum: 0,
    };
  },
  created() {
    this.getImgCapcha();
    this.getElectronicSampleBookapplyNum();
    this.getPaperSampleBookapplyNum();
    this.getWechatAuthenticationState();
    this.getUserInfo();
    
    if (!this.userInfo.phoneNumber) {
      this.$confirm(
        "检测到您还未绑定手机号,绑定手机号后当前账号即可使用手机号进行登录!",
        "温馨提示",
        {
          confirmButtonText: "绑定手机号",
          cancelButtonText: "关闭",
          type: "warning",
        }
      )
        .then(() => {
          this.isPlace = true;
        })
        .catch(() => {});
    }
  },
  methods: {
    toTeacherRegister() {
      this.$parent.changeSelected("1");
    },
 
    getUserInfo() {
      this.MG.identity.getCurrentAppUser().then((res) => {
        if (res) {
          let teacherRole = res.roleLinks.find(
            (item) => item.role.refCode == "teacher"
          );
          let teacherInfo = res.infoList.find(
            (item) => item.type == "teacherInfo"
          );
          let wechatInfo = res.infoList.find((item) => item.type == "WeChat");
          let studentInfo = res.infoList.find((item) => item.type == "Default");
          let phoneInfo = res.secretList.find(
            (item) => item.type == "MobilePhone"
          );
          if (teacherRole && teacherInfo) {
            let data = {};
            try {
              data = JSON.parse(teacherInfo.data);
            } catch (error) {
              data = {};
            }
            this.$store.dispatch("setUserInfo", {
              ...data,
              name: data.fullName,
              phoneNumber: phoneInfo?.credential,
              role: "Teacher",
              roleId: teacherRole.role.id,
            });
          } else if (wechatInfo) {
            this.$store.dispatch("setUserInfo", {
              ...wechatInfo,
              phoneNumber: phoneInfo?.credential,
              role: "Student",
            });
          } else if (studentInfo) {
            this.$store.dispatch("setUserInfo", {
              ...studentInfo,
              phoneNumber: phoneInfo?.credential,
              role: "Student",
            });
          }
        }
      });
    },
    replace() {
      this.$refs.form.validate((val) => {
        let query = {
          phoneNumber: this.replacePhone.phone,
          phoneCaptcha: this.replacePhone.verificationCode,
        };
 
        this.MG.identity.userSetPhoneNumber(query).then((res) => {
          if (res == "验证码过期或错误") {
            this.$message({
              message: res + ",请稍后重试",
              type: "error",
            });
          } else if (res == "此手机号码已被其它账号绑定") {
            this.$message({
              message: res + ",请更换其他手机号。",
              type: "error",
            });
          } else {
            this.$message({
              message: res,
              type: "success",
            });
            this.$store.dispatch("setUserInfo", {
              ...this.userInfo,
              phoneNumber: this.replacePhone.phone,
            });
            this.isPlace = false;
          }
        });
      });
    },
    // 获取图形验证码
    getImgCapcha() {
      this.MG.identity.getImgCode().then((res) => {
        this.imgCode = "data:image/png;base64," + res;
      });
    },
    // 发送短信验证码
    getVerifyCode() {
      let validateFieldList = [];
      this.$refs.form.validateField(["phone", "imgCode"], (val) => {
        // 由于 validateField 验证会触发多次,当每次验证通过后都往数组里添加一条数据。
        if (!val) {
          validateFieldList.push("通过");
        }
        // 此处 2 为验证字段的数量
        if (validateFieldList.length == 2) {
          this.MG.identity
            .getPhoneCode({
              phoneNumber: this.replacePhone.phone,
              imageCaptcha: this.replacePhone.imgCode,
              appRefCode: this.config.appRefCode,
            })
            .then((res) => {
              if (
                res == "验证码发送成功" ||
                res == "验证码发送频繁请稍后再试"
              ) {
                this.$message({
                  message: res,
                  type: "success",
                });
                // 开启短信验证倒计时
                this.getSecond(90);
              } else {
                this.$message({
                  message: res,
                  type: "error",
                });
                this.getImgCapcha();
              }
            });
        }
      });
    },
    // 验证码倒计时
    getSecond(time) {
      if (!this.timer) {
        this.countDown = time;
        this.timer = setInterval(() => {
          this.countDown--;
          if (this.countDown == 0) {
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    handleClose(done) {
      // 恢复默认值
      for (const key in this.replacePhone) {
        this.replacePhone[key] = "";
      }
      this.formKey++;
      this.isPlace = false;
    },
    // 获取微信认证状态
    getWechatAuthenticationState() {
      this.MG.identity.checkBuildingWeChat({}).then((res) => {
        if (res) {
          this.weChatState = true;
        } else {
          this.weChatState = false;
          this.$confirm(
            "您还未绑定微信,为了您能正常使用中国农业大学出版社公众号样书申请服务,请使用微信扫码绑定!",
            "温馨提示",
            {
              confirmButtonText: "去扫码绑定微信",
              cancelButtonText: "关闭",
              type: "warning",
            }
          )
            .then(() => {
              this.goBindWeChat();
            })
            .catch(() => {});
        }
      });
    },
    goBindWeChat() {
      this.$router.push({
        path: "/bindWeChat",
      });
    },
    //电子书剩余申请次数
    getElectronicSampleBookapplyNum() {
      this.MG.app
        .getTicketResult({
          ticketRefCodeOrGuid: "electronicSampleBookapplyNum",
          roleId: this.userInfo.roleId,
        })
        .then((res) => {
          this.electronicSampleBookapplyNum = res.totalCount - res.usedCount;
        });
    },
    //纸质书剩余申请次数
    getPaperSampleBookapplyNum() {
      this.MG.app
        .getTicketResult({
          ticketRefCodeOrGuid: "paperSampleBookapplyNum",
          roleId: this.userInfo.roleId,
        })
        .then((res) => {
          this.paperSampleBookapplyNum = res.totalCount - res.usedCount;
        });
    },
  },
};
</script>
<style lang="less" scoped>
.content {
  padding: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .wrapImg {
    position: relative;
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }
  .information {
    display: inline-block;
    color: #666;
    div {
      span {
        display: inline-block;
        width: 130px;
        line-height: 50px;
      }
      & > span:nth-child(2) {
        color: #333;
      }
      p {
        display: inline-block;
      }
    }
    .modify {
      color: #e50021;
      border-bottom: 1px solid #e50021;
      cursor: pointer;
    }
  }
}
.imgCodeBox {
  margin-left: 20px;
  height: 40px;
  width: 140px;
  cursor: pointer;
  img {
    width: 140px;
    height: 100%;
  }
}
.title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 1.8px;
  line-height: 36px;
  padding-left: 40px;
  background: #d8f7e6;
  line-height: 60px;
  color: #00873c;
  span {
    font-size: 16px;
    font-weight: 500;
  }
}
 
.usericon {
  width: 60px;
  height: 60px;
  vertical-align: middle;
  border-radius: 50%;
}
</style>