333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
import { BaseSms } from "../../entity/basicInfo/user/baseSms";
import { SysUser } from "../../entity/sys/core/sysUser";
import moment = require("moment");
 
/**
 * 用户中心
 */
export default class UserController extends BaseController {
  //#region 修改密码发送验证码
  /**
   * 注册发送验证码
   */
  @Post()
  public async sendForGetValidateCode() {
    try {
      //#region 校验数据
      //验证手机号不能为空
      if (!this.body.mobile) {
        this.info.result = false;
        this.info.msg = "您没有输入手机号!";
        this.ctx.body = this.info;
        return;
      }
      //验证手机号格式是否正确
      let tegexText = /^(134[012345678]\d{7}|1[345789][012356789]\d{8})$/g;
      if (!tegexText.test(this.body.mobile)) {
        this.info.result = false;
        this.info.statusCode = 500;
        this.info.state = "ConsignorApp_MobileFormatError";
        this.info.msg = "手机号不正确";
        this.ctx.body = this.info;
        return;
      }
 
      let clientInfo = await this.dbRead.findOne(BaseConsignor, {
        mobile: this.body.mobile
      });
      if (!clientInfo) {
        this.info.result = false;
        this.info.msg = "您输入的手机号在系统中不存在!";
        this.ctx.body = this.info;
        return;
      }
 
      //#endregion
 
      // 发送验证码
      let mobile = this.body.mobile;
      //let templateId = "321297";//带路网验证码模板
      let randKey = 999999 * Math.random() + 100000;
      let params = {
        "#code#": randKey
      };
      this.info = await this.ctx.service.utils.smsHelper.sendYPSMS(mobile, params, 11111);
 
      if (this.info.result) {
        let sms = new BaseSms();
        sms.keyCode = this.body.mobile;
        sms.validCode = randKey.toString();
        sms.validType = "api验证";
        sms.isUsed = 0;
        //记录验证码
        await this.dbWrite.save(sms);
        this.info.result = true;
        this.info.statusCode = 200;
        this.info.state = "ValidateCodeSuccess";
        this.info.msg = "发送成功";
      } else {
        this.info.result = false;
        this.info.statusCode = 500;
        this.info.state = "fail";
        this.info.msg = "发送失败";
      }
      this.ctx.body = this.info;
      return;
    } catch (ex) {
      let msg = "出现异常:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      this.ctx.body = this.info;
      return;
    }
  }
  //#endregion
 
  //#region 忘记密码-手机修改密码
  @Post()
  public async phoneUpdatePwd() {
    try {
      //校验数据
      if (!this.body.mobile) {
        this.info.result = false;
        this.info.msg = "手机号不能为空!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.ValidateCode) {
        this.info.result = false;
        this.info.msg = "验证码不能为空!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "请输入密码!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.RepeatPassWord) {
        this.info.result = false;
        this.info.msg = "请确认密码!";
        this.ctx.body = this.info;
        return;
      }
      if (this.body.RepeatPassWord != this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "两次密码不一致!";
        this.ctx.body = this.info;
        return;
      }
 
      var userInfo = await this.dbRead.findOne(SysUser, {
        mobile: this.body.mobile
      });
      if (userInfo == null) {
        this.info.result = false;
        this.info.msg = "手机号未注册!";
        this.ctx.body = this.info;
        return;
      }
      //解密验证码
 
      var smsInfo = await this.dbRead.findOne(BaseSms, {
        where: {
          keyCode: this.body.mobile,
          isUsed: 0
        },
        order: {
          sms_Id: "DESC"
        }
      });
      if (smsInfo != null && smsInfo.validCode == this.body.ValidateCode) {
        smsInfo.isUsed = 1;
        await this.dbWrite.save(smsInfo);
        var span = moment(new Date()).diff(moment(smsInfo.createDate), "minutes");
        if (span > 10) {
          //十分钟后超时
          this.info.result = false;
          this.info.msg = "验证已失效!";
          this.ctx.body = this.info;
          return;
        }
      } else {
        this.info.result = false;
        this.info.msg = "验证不正确!";
        this.ctx.body = this.info;
        return;
      }
 
      //修改密码
      let userPwd = this.ctx.helper.md5EncodingSalt(this.body.userPwd);
      await this.dbWrite.update(SysUser, userInfo.user_Id, {
        userPwd: userPwd
      });
      this.info.result = true;
      this.info.msg = "修改成功!";
      this.ctx.body = this.info;
      return;
    } catch {
      this.info.result = false;
      this.info.msg = "修改失败!";
      this.ctx.body = this.info;
      return;
    }
  }
  //#endregion
 
  //#region 超级管理员后端修改密码
  @Post()
  public async modifyPwd() {
    try {
      //校验数据
      if (!this.body.mobile) {
        this.info.result = false;
        this.info.msg = "手机号不能为空!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "请输入密码!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.RepeatPassWord) {
        this.info.result = false;
        this.info.msg = "请确认密码!";
        this.ctx.body = this.info;
        return;
      }
      if (this.body.RepeatPassWord != this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "两次密码不一致!";
        this.ctx.body = this.info;
        return;
      }
 
      var userInfo = await this.dbRead.findOne(SysUser, {
        mobile: this.body.mobile
      });
      if (userInfo == null) {
        this.info.result = false;
        this.info.msg = "手机号未注册!";
        this.ctx.body = this.info;
        return;
      }
      //修改密码
      let userPwd = this.ctx.helper.md5EncodingSalt(this.body.userPwd);
      await this.dbWrite.update(SysUser, userInfo.user_Id, {
        userPwd: userPwd
      });
      this.info.result = true;
      this.info.msg = "修改成功!";
      this.ctx.body = this.info;
    } catch {
      this.info.result = false;
      this.info.msg = "修改失败!";
      this.ctx.body = this.info;
      return;
    }
  }
  //#endregion
}