添加common下用户注册登录
This commit is contained in:
@@ -152,15 +152,15 @@ public class BookLabelAndMarketController {
|
||||
@RequestMapping("/getNotToLabelList")
|
||||
public R getNotToLabelList(@RequestBody Map params){
|
||||
LambdaQueryWrapper<ShopProduct> wrapper = new LambdaQueryWrapper();
|
||||
if (!"".equals(params.get("goodsType").toString())){
|
||||
if (params.containsKey("goodsType")&&!"".equals(params.get("goodsType").toString())){
|
||||
wrapper.eq(ShopProduct::getGoodsType,params.get("goodsType").toString());
|
||||
}
|
||||
if (!"".equals(params.get("bookLabelId").toString())){
|
||||
if (params.containsKey("bookLabelId")&&!"".equals(params.get("bookLabelId").toString())){
|
||||
String sql = "select product_id from shop_product_to_book_label where del_flag = 0 and book_label_id = "+params.get("bookLabelId");
|
||||
wrapper.notInSql(ShopProduct::getProductId,sql);
|
||||
}
|
||||
if (!"".equals(params.get("bookMarketId").toString())){
|
||||
String sql = "select product_id from shop_product_to_book_market where del_flag = 0 and book_market_id = "+params.get("bookLabelId");
|
||||
if (params.containsKey("bookMarketId")&&!"".equals(params.get("bookMarketId").toString())){
|
||||
String sql = "select product_id from shop_product_to_book_market where del_flag = 0 and book_market_id = "+params.get("bookMarketId");
|
||||
wrapper.notInSql(ShopProduct::getProductId,sql);
|
||||
}
|
||||
Page<ShopProduct> page = productService.page(new Page<>(
|
||||
|
||||
@@ -1,14 +1,291 @@
|
||||
package com.peanut.modules.common.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peanut.common.utils.MD5Utils;
|
||||
import com.peanut.common.utils.MailUtil;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.modules.common.entity.MyUserEntity;
|
||||
import com.peanut.modules.common.service.MyUserService;
|
||||
import com.peanut.modules.sys.service.SysUserTokenService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("common/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private MyUserService userService;
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
@Autowired
|
||||
private SysUserTokenService sysUserTokenService;
|
||||
|
||||
/**
|
||||
* 常规注册 发短信验证码
|
||||
*/
|
||||
@RequestMapping("/sms/sendcode")
|
||||
public R registerSms(@RequestParam("phone") String phone, @RequestParam(required = false) String areaCode) throws Exception {
|
||||
int areacode=0;
|
||||
if(areaCode==null||areaCode.equals("")){
|
||||
areacode=0;
|
||||
}else{
|
||||
areacode = Integer.valueOf(areaCode);
|
||||
}
|
||||
//验证一分钟内是否已经发过
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
|
||||
if (!StringUtils.isEmpty(redisCode)) {
|
||||
long l = Long.parseLong(redisCode.split("_")[1]);
|
||||
if (System.currentTimeMillis() - l < 60000) {
|
||||
//60s 内不能再发
|
||||
return R.error(500,"短信验证码频率过高,请稍后再试!");
|
||||
}
|
||||
}
|
||||
//生成随机五位数
|
||||
Random random = new Random();
|
||||
String i = random.nextInt(99999) + "";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int j = 0; j < 5 - i.length(); j++) {
|
||||
sb.append("0");
|
||||
}
|
||||
i = sb.toString() + i;
|
||||
String code = i + "_"+System.currentTimeMillis();
|
||||
//redis 缓存验证码
|
||||
redisTemplate.opsForValue().set("RegistCode"+phone,code,5, TimeUnit.MINUTES);
|
||||
//发送
|
||||
userService.sendCodeForRegister(phone,code,areacode);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 常规注册 发送邮箱验证码
|
||||
*/
|
||||
@RequestMapping("/getMailCaptcha")
|
||||
public R getMailCaptcha(String email) throws Exception {
|
||||
//验证一分钟内是否已经发过
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode" + email);
|
||||
if (!StringUtils.isEmpty(redisCode)) {
|
||||
long l = Long.parseLong(redisCode.split("_")[1]);
|
||||
if (System.currentTimeMillis() - l < 60000) {
|
||||
//60s 内不能再发
|
||||
return R.error(500,"验证码频率过高,请稍后再试!");
|
||||
}
|
||||
}
|
||||
//生成随机五位数
|
||||
Random random = new Random();
|
||||
String code = random.nextInt(99999) + "";
|
||||
String timeCode = code + "_"+System.currentTimeMillis();
|
||||
//redis 缓存验证码
|
||||
redisTemplate.opsForValue().set("RegistCode"+email,timeCode,5, TimeUnit.MINUTES);
|
||||
//发送
|
||||
return MailUtil.sendMail("疯子读书邮箱验证码",code,email);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户电话号
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/updateUserTel")
|
||||
public R updateUserTel(@RequestBody Map<String,Object> map){
|
||||
String phone = map.get("phone").toString();
|
||||
String code = map.get("code").toString();
|
||||
Integer id = Integer.valueOf(map.get("id").toString());
|
||||
//查询是否存在当前手机号
|
||||
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
||||
wrapper.eq(MyUserEntity::getTel,phone);
|
||||
MyUserEntity userEntity = userService.getOne(wrapper);
|
||||
if (userEntity == null) {
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode"+phone);
|
||||
if(StringUtils.isEmpty(redisCode)){
|
||||
return R.error("验证码已过期,请重试");
|
||||
}
|
||||
String lcode = redisCode.split("_")[0];
|
||||
if (!lcode.equals(code)) {
|
||||
return R.error("短信验证码不符!");
|
||||
}
|
||||
MyUserEntity userInfo = userService.getById(id);
|
||||
userInfo.setTel(phone);
|
||||
userService.updateById(userInfo);
|
||||
return R.ok();
|
||||
}else {
|
||||
return R.error("手机号已被绑定!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户邮件
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/updateUserEmail")
|
||||
public R updateUserEmail(@RequestBody Map<String,Object> map){
|
||||
String email = map.get("email").toString();
|
||||
String code = map.get("code").toString();
|
||||
Integer id = Integer.valueOf(map.get("id").toString());
|
||||
//查询是否存在当前邮箱
|
||||
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
||||
wrapper.eq(MyUserEntity::getEmail,email);
|
||||
MyUserEntity userEntity = userService.getOne(wrapper);
|
||||
if (userEntity == null) {
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode"+email);
|
||||
if(StringUtils.isEmpty(redisCode)){
|
||||
return R.error("验证码已过期,请重试");
|
||||
}
|
||||
String lcode = redisCode.split("_")[0];
|
||||
if (!lcode.equals(code)) {
|
||||
return R.error("验证码不符!");
|
||||
}
|
||||
MyUserEntity userInfo = userService.getById(id);
|
||||
userInfo.setEmail(email);
|
||||
userService.updateById(userInfo);
|
||||
return R.ok();
|
||||
}else {
|
||||
return R.error("邮箱已被绑定!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号验证码密码注册
|
||||
*/
|
||||
@RequestMapping("/register")
|
||||
public R register(@RequestParam("tel") String tel,
|
||||
@RequestParam("code") String code,
|
||||
@RequestParam("password") String password){
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode" + tel);
|
||||
if (StringUtils.isEmpty(redisCode)){
|
||||
return R.error(500,"短信验证码已过期,请重试");
|
||||
}
|
||||
String lcode = redisCode.split("_")[0];
|
||||
if (!lcode.equals(code)) {
|
||||
return R.error(500,"短信验证码不符!");
|
||||
}
|
||||
MyUserEntity user = userService.getBaseMapper().selectOne(new QueryWrapper<MyUserEntity>().eq("tel", tel));
|
||||
if(!ObjectUtil.isEmpty(user)){
|
||||
return R.error(500,"该手机号已经注册!");
|
||||
}
|
||||
String saltMD5 = MD5Utils.getSaltMD5(password);
|
||||
MyUserEntity myUserEntity = new MyUserEntity();
|
||||
myUserEntity.setTel(tel);
|
||||
myUserEntity.setPassword(saltMD5);
|
||||
userService.save(myUserEntity);
|
||||
R r = sysUserTokenService.createToken(myUserEntity.getId());
|
||||
return R.ok("注册成功").put("userInfo",myUserEntity).put("token",r);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号验证码注册或登录
|
||||
*/
|
||||
@RequestMapping("/registerOrLogin")
|
||||
public R register(String tel, String code) {
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode" + tel);
|
||||
System.out.println(redisCode);
|
||||
if (StringUtils.isEmpty(redisCode)){
|
||||
return R.error(500,"验证码已过期,请重试");
|
||||
}
|
||||
String lcode = redisCode.split("_")[0];
|
||||
if (!lcode.equals(code)) {
|
||||
return R.error(500,"验证码不符!");
|
||||
}
|
||||
//查询是否存在当前用户
|
||||
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
||||
if (tel.contains("@")){
|
||||
wrapper.eq(MyUserEntity::getEmail,tel);
|
||||
}else {
|
||||
wrapper.eq(MyUserEntity::getTel,tel);
|
||||
}
|
||||
MyUserEntity userEntity = userService.getOne(wrapper);
|
||||
if (userEntity == null) {
|
||||
// 用户不存在则创建用户 注册成功
|
||||
MyUserEntity myUserEntity = new MyUserEntity();
|
||||
if (tel.contains("@")){
|
||||
myUserEntity.setEmail(tel);
|
||||
}else {
|
||||
myUserEntity.setTel(tel);
|
||||
}
|
||||
userService.save(myUserEntity);
|
||||
R r = sysUserTokenService.createToken(myUserEntity.getId());
|
||||
return R.ok("注册成功").put("userInfo",myUserEntity).put("token",r);
|
||||
}else {
|
||||
R r = sysUserTokenService.createToken(userEntity.getId());
|
||||
return R.ok("登录成功!").put("userInfo",userEntity).put("token",r);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号密码登录
|
||||
*/
|
||||
@RequestMapping("/login")
|
||||
public R login(@RequestParam("phone") String phone,
|
||||
@RequestParam("password") String password) {
|
||||
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
|
||||
if (phone.contains("@")) {
|
||||
wrapper.eq(MyUserEntity::getEmail,phone);
|
||||
}else {
|
||||
wrapper.eq(MyUserEntity::getTel,phone);
|
||||
}
|
||||
//防止多账号报错
|
||||
MyUserEntity userEntity = userService.getOne(wrapper);
|
||||
if (userEntity == null) {
|
||||
return R.error(500,"用户不存在!");
|
||||
}else {
|
||||
if (userEntity.getPassword() == null|| userEntity.getPassword().equals("")) {
|
||||
return R.error(500,"当前未设置密码,请使用验证码登录!");
|
||||
}else {
|
||||
if (MD5Utils.getSaltverifyMD5(password,userEntity.getPassword())){
|
||||
R r = sysUserTokenService.createToken(userEntity.getId());
|
||||
return R.ok("登陆成功!").put("userInfo",userEntity).put("token",r);
|
||||
}else {
|
||||
return R.error(500,"密码不正确,请重试!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置密码
|
||||
*/
|
||||
@RequestMapping("/setPassword")
|
||||
public R setPassword(@RequestParam("phone") String phone,
|
||||
@RequestParam("password") String password,
|
||||
@RequestParam("code") String code) {
|
||||
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
|
||||
System.out.println(redisCode);
|
||||
if (StringUtils.isEmpty(redisCode)){
|
||||
return R.error(500,"验证码已过期,请重试");
|
||||
}
|
||||
String lcode = redisCode.split("_")[0];
|
||||
if (!lcode.equals(code)) {
|
||||
return R.error(500,"验证码不符!");
|
||||
}
|
||||
//查询是否存在当前用户
|
||||
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
if (phone.contains("@")){
|
||||
wrapper.eq(MyUserEntity::getEmail, phone);
|
||||
}else {
|
||||
wrapper.eq(MyUserEntity::getTel, phone);
|
||||
}
|
||||
MyUserEntity userEntity = userService.getOne(wrapper);
|
||||
if (userEntity == null) {
|
||||
return R.error(500,"当前用户不存在!");
|
||||
}
|
||||
String saltMD5 = MD5Utils.getSaltMD5(password);
|
||||
userEntity.setPassword(saltMD5);
|
||||
userService.updateById(userEntity);
|
||||
return R.ok("成功");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.peanut.modules.common.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.peanut.modules.common.entity.MyUserEntity;
|
||||
|
||||
public interface MyUserService extends IService<MyUserEntity> {
|
||||
|
||||
void sendCodeForRegister(String phone, String code,Integer areaCode) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.peanut.modules.common.service.impl;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.peanut.modules.app.config.SMSConfig;
|
||||
import com.peanut.modules.app.config.Sample;
|
||||
import com.peanut.modules.common.dao.MyUserDao;
|
||||
import com.peanut.modules.common.entity.MyUserEntity;
|
||||
import com.peanut.modules.common.service.MyUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service("myUserService")
|
||||
public class MyUserServiceImpl extends ServiceImpl<MyUserDao, MyUserEntity> implements MyUserService {
|
||||
|
||||
@Autowired
|
||||
private SMSConfig smsConfig;
|
||||
|
||||
@Override
|
||||
public void sendCodeForRegister(String phone, String code, Integer areaCode) throws Exception {
|
||||
String scode = code.split("_")[0];
|
||||
sendCode(phone,scode,areaCode);
|
||||
}
|
||||
|
||||
private void sendCode(String phone, String code, Integer areaCode) throws Exception {
|
||||
com.aliyun.dysmsapi20170525.Client client = Sample.createClient(smsConfig.getAccessKeyId(),smsConfig.getAccessKeySecret());
|
||||
String tem;
|
||||
if(areaCode!=null&&areaCode>0&&areaCode!=86){
|
||||
tem = smsConfig.getSTemplateCode();
|
||||
phone = areaCode+phone;
|
||||
}else{
|
||||
tem = smsConfig.getTemplateCode();
|
||||
}
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setSignName(smsConfig.getSingName())
|
||||
.setTemplateCode(tem)
|
||||
.setPhoneNumbers(phone)
|
||||
.setTemplateParam("{\"code\":\""+ code +"\"}");
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
SendSmsResponseBody body = sendSmsResponse.getBody();
|
||||
System.out.println(body.getMessage());
|
||||
} catch (TeaException error) {
|
||||
// 如有需要,请打印 error
|
||||
Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 如有需要,请打印 error
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user