huwhois 4 years ago
parent
commit
7ab67594b3

+ 11 - 0
src/main/java/io/renren/common/validator/Assert.java

@ -9,6 +9,9 @@
9 9
package io.renren.common.validator;
10 10
11 11
import io.renren.common.exception.RRException;
12
13
import java.util.regex.Pattern;
14
12 15
import org.apache.commons.lang.StringUtils;
13 16
14 17
/**
@ -29,4 +32,12 @@ public abstract class Assert {
29 32
            throw new RRException(message);
30 33
        }
31 34
    }
35
36
    public static void isNotPhone(String phone) {
37
        String phone_regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
38
        Pattern p = Pattern.compile(phone_regex);
39
        if (phone.length() != 11 || !p.matcher(phone).matches()) {
40
            throw new RRException("手机号格式不正确");
41
        }
42
    }
32 43
}

+ 53 - 7
src/main/java/io/renren/modules/app/controller/AppRegisterController.java

@ -9,13 +9,12 @@
9 9
package io.renren.modules.app.controller;
10 10
11 11
import io.renren.common.utils.R;
12
import io.renren.common.validator.Assert;
12 13
import io.renren.common.validator.ValidatorUtils;
13 14
import io.renren.modules.app.entity.UserEntity;
14 15
import io.renren.modules.app.form.RegisterForm;
15 16
import io.renren.modules.app.service.SmsCodeService;
16 17
import io.renren.modules.app.service.UserService;
17
import io.renren.common.utils.JsonUtils;
18
import io.renren.modules.app.entity.SmsStatusEntity;
19 18
import io.swagger.annotations.Api;
20 19
import io.swagger.annotations.ApiOperation;
21 20
import org.springframework.beans.factory.annotation.Autowired;
@ -41,14 +40,19 @@ public class AppRegisterController {
41 40
    private SmsCodeService smsCodeService;
42 41
43 42
    /**
44
     * 短信验证码
43
     * 注册短信验证码
45 44
     */
46 45
    @PostMapping("/mobilecode")
47 46
    public R mobileCode(String uuid, String phone) {
48
        // 获取短信验证码
49
        String code = smsCodeService.getCode(uuid, phone);
47
        Assert.isNotPhone(phone);
48
49
        UserEntity user = userService.queryByUsername(phone);
50
        if (user != null) {
51
            return R.error("手机号已注册");
52
        }
53
50 54
        try {
51
            String result = smsCodeService.sendCode(code, phone);
55
            String result = smsCodeService.getCode(uuid, phone);
52 56
            return R.ok().put("result", result);
53 57
        } catch (Exception e) {
54 58
            e.printStackTrace();
@ -59,7 +63,7 @@ public class AppRegisterController {
59 63
    @PostMapping("register")
60 64
    @ApiOperation("注册")
61 65
    public R register(@RequestBody RegisterForm form){
62
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode());
66
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode(), form.getPhone());
63 67
		if(!captcha){
64 68
			return R.error("验证码不正确");
65 69
        }
@ -75,4 +79,46 @@ public class AppRegisterController {
75 79
76 80
        return R.ok();
77 81
    }
82
    
83
    /**
84
     * 找回密码短信验证码
85
     */
86
    @PostMapping("/phonecode")
87
    public R phoneCode(String uuid, String phone) {
88
        UserEntity user = userService.queryByUsername(phone);
89
        if (user == null) {
90
            return R.error("手机号未注册");
91
        }
92
93
        try {
94
            String result = smsCodeService.getCode(uuid, phone);
95
            return R.ok().put("result", result);
96
        } catch (Exception e) {
97
            e.printStackTrace();
98
            return R.error();
99
        }
100
    }
101
102
    /**
103
     * 通过手机验证码重置密码
104
     * @param form
105
     * @return
106
     */
107
    @PostMapping("repassword")
108
    @ApiOperation("通过手机验证码重置密码")
109
    public R rePassword(@RequestBody RegisterForm form){
110
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode(), form.getPhone());
111
		if(!captcha){
112
			return R.error("验证码不正确");
113
        }
114
        
115
        //表单校验
116
        ValidatorUtils.validateEntity(form);
117
118
        UserEntity user = userService.queryByUsername(form.getPhone());
119
        Long userId = user.getUserId();
120
        userService.updatePassWord(userId, form.getPassword());
121
122
        return R.ok();
123
    }
78 124
}

+ 0 - 13
src/main/java/io/renren/modules/app/entity/SmsStatusEntity.java

@ -1,13 +0,0 @@
1
package io.renren.modules.app.entity;
2
import lombok.Data;
3
4
/**
5
 * 短信发送返回值实体类
6
 */
7
@Data
8
public class SmsStatusEntity {
9
    private String respCode;
10
    private String respDesc;
11
    private String smsId;
12
    private String[] failList;
13
}

+ 2 - 14
src/main/java/io/renren/modules/app/service/SmsCodeService.java

@ -1,11 +1,8 @@
1 1
package io.renren.modules.app.service;
2 2
3 3
import com.baomidou.mybatisplus.extension.service.IService;
4
import io.renren.common.utils.PageUtils;
5 4
import io.renren.modules.app.entity.SmsCodeEntity;
6 5
7
import java.util.Map;
8
9 6
/**
10 7
 * 短信验证码
11 8
 *
@ -17,7 +14,7 @@ public interface SmsCodeService extends IService<SmsCodeEntity> {
17 14
    /**
18 15
     * 获取短信验证码
19 16
     */
20
    String getCode(String uuid, String phone);
17
    String getCode(String uuid, String phone) throws Exception;
21 18
22 19
    /**
23 20
     * 验证码效验
@ -25,14 +22,5 @@ public interface SmsCodeService extends IService<SmsCodeEntity> {
25 22
     * @param code  验证码
26 23
     * @return  true:成功  false:失败
27 24
     */
28
    boolean validate(String uuid, String code);
29
30
    /**
31
     *  发送短信验证码
32
     * @param code
33
     * @param phone
34
     * @return
35
     * @throws Exception
36
     */
37
	String sendCode(String code, String phone) throws Exception;
25
    boolean validate(String uuid, String code, String phone);
38 26
}

+ 15 - 18
src/main/java/io/renren/modules/app/service/impl/SmsCodeServiceImpl.java

@ -21,7 +21,7 @@ import io.renren.modules.app.service.SmsCodeService;
21 21
public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeDao, SmsCodeEntity> implements SmsCodeService {
22 22
23 23
    @Override
24
    public String getCode(String uuid, String phone){
24
    public String getCode(String uuid, String phone) throws Exception {
25 25
        if (StringUtils.isBlank(uuid)) {
26 26
            throw new RRException("uuid不能为空");
27 27
        }
@ -32,13 +32,23 @@ public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeDao, SmsCodeEntity> i
32 32
        smsCodeEntity.setPhone(phone);
33 33
        smsCodeEntity.setCode(code);
34 34
        smsCodeEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), ConfigConstant.EXPIRE_TIME));
35
35
        
36
        // 发送验证码
37
        StringBuilder sb = new StringBuilder();
38
		sb.append("accountSid").append("=").append(ConfigConstant.ACCOUNT_SID);
39
		sb.append("&to").append("=").append(phone);
40
        sb.append("&param").append("=").append(URLEncoder.encode(code + "," + ConfigConstant.EXPIRE_TIME, "UTF-8"));
41
		sb.append("&templateid").append("=").append(ConfigConstant.TEMPLATE_ID);
42
		String body = sb.toString() + HttpRequestUtil.createCommonParam(ConfigConstant.ACCOUNT_SID, ConfigConstant.AUTH_TOKEN);
43
		String result = HttpRequestUtil.post(ConfigConstant.BASE_URL, body);
44
		System.out.println(result);
45
        
36 46
        this.save(smsCodeEntity);
37
        return code;
47
        return result;
38 48
    }
39 49
40 50
    @Override
41
    public boolean validate(String uuid, String code) {
51
    public boolean validate(String uuid, String code, String phone) {
42 52
        SmsCodeEntity smsCodeEntity = this.getOne(new QueryWrapper<SmsCodeEntity>().eq("uuid", uuid));
43 53
        if(smsCodeEntity == null){
44 54
            return false;
@ -47,22 +57,9 @@ public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeDao, SmsCodeEntity> i
47 57
        //删除验证码
48 58
        this.removeById(uuid);
49 59
50
        if(smsCodeEntity.getCode().equalsIgnoreCase(code) && smsCodeEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
60
        if(smsCodeEntity.getCode().equalsIgnoreCase(code) && smsCodeEntity.getPhone().equalsIgnoreCase(phone) && smsCodeEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
51 61
            return true;
52 62
        }
53 63
        return false;
54 64
    }
55
56
    @Override
57
    public String sendCode(String code, String phone) throws Exception {
58
        StringBuilder sb = new StringBuilder();
59
		sb.append("accountSid").append("=").append(ConfigConstant.ACCOUNT_SID);
60
		sb.append("&to").append("=").append(phone);
61
        sb.append("&param").append("=").append(URLEncoder.encode(code + "," + ConfigConstant.EXPIRE_TIME, "UTF-8"));
62
		sb.append("&templateid").append("=").append(ConfigConstant.TEMPLATE_ID);
63
		String body = sb.toString() + HttpRequestUtil.createCommonParam(ConfigConstant.ACCOUNT_SID, ConfigConstant.AUTH_TOKEN);
64
		String result = HttpRequestUtil.post(ConfigConstant.BASE_URL, body);
65
		System.out.println(result);
66
        return result;
67
    }
68 65
}

+ 8 - 10
src/main/java/io/renren/modules/app/service/impl/UserServiceImpl.java

@ -20,7 +20,6 @@ import io.renren.modules.app.dao.UserDao;
20 20
import io.renren.modules.app.entity.UserEntity;
21 21
import io.renren.modules.app.form.LoginForm;
22 22
import io.renren.modules.app.service.UserService;
23
import org.apache.commons.codec.digest.DigestUtils;
24 23
import org.apache.commons.lang.RandomStringUtils;
25 24
import org.apache.shiro.crypto.hash.Sha256Hash;
26 25
import org.springframework.stereotype.Service;
@ -40,7 +39,6 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
40 39
		UserEntity user = queryByUsername(form.getUsername());
41 40
		Assert.isNull(user, "账号或密码错误");
42 41
		//密码错误
43
		// if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
44 42
		if(!user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())){
45 43
			throw new RRException("账号或密码错误");
46 44
		}
@ -59,6 +57,14 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
59 57
		this.save(user); 
60 58
	}
61 59
60
	@Override
61
	public int updatePassWord(Long id, String password) {
62
		String salt = RandomStringUtils.randomAlphanumeric(20);
63
		password = new Sha256Hash(password, salt).toHex();
64
		
65
		return baseMapper.updatePassWord(id, password, salt);
66
	}
67
	
62 68
	@Override
63 69
	public List<Map<String, Object>> queryOrderList(Long memberId) {
64 70
		return baseMapper.queryOrderList(memberId);
@ -68,12 +74,4 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
68 74
	public Map<String, Object> queryOrderInfoByAttendersId(Long attendersId) {
69 75
		return baseMapper.queryOrderInfoByAttendersId(attendersId);
70 76
	}
71
72
	@Override
73
	public int updatePassWord(Long id, String password) {
74
		String salt = RandomStringUtils.randomAlphanumeric(20);
75
		password = new Sha256Hash(password, salt).toHex();
76
77
		return baseMapper.updatePassWord(id, password, salt);
78
	}
79 77
}