浏览代码

注册,登录

李天标 3 年之前
父节点
当前提交
2b8c4f3905

+ 12 - 0
pom.xml

@@ -134,6 +134,18 @@
 			<artifactId>fastjson</artifactId>
 			<version>1.2.75</version>
 		</dependency>
+		<!-- Spring Component End -->
+		<dependency>
+			<groupId>org.json</groupId>
+			<artifactId>json</artifactId>
+			<version>20200518</version>
+		</dependency>
+		<!--邮箱-->
+		<dependency>
+			<groupId>javax.mail</groupId>
+			<artifactId>mail</artifactId>
+			<version>1.4.7</version>
+		</dependency>
 		<!-- hutool -->
 		<dependency>
 			<groupId>cn.hutool</groupId>

+ 5 - 7
src/main/java/com/szwl/controller/DemoController.java

@@ -17,11 +17,7 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
 
@@ -46,8 +42,10 @@ public class DemoController {
 
     @ApiOperation(value = "测试更新")
     @PostMapping("/update")
-    public ResponseModel<?> testUpdate(TAdmin entity) {
-        return R.ok(tAdminService.saveOrUpdate(entity));
+    public ResponseModel<?> testUpdate(@RequestBody TAdmin entity) {
+        int a = 0;
+//        return R.ok(tAdminService.saveOrUpdate(entity));
+        return R.ok();
     }
 
 

+ 153 - 0
src/main/java/com/szwl/controller/TAdminController.java

@@ -0,0 +1,153 @@
+package com.szwl.controller;
+
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.szwl.constant.ResponseCodesEnum;
+import com.szwl.model.bo.R;
+import com.szwl.model.bo.ResponseModel;
+import com.szwl.model.entity.TAdmin;
+import com.szwl.model.entity.TMessageCode;
+import com.szwl.service.TAdminService;
+import com.szwl.service.TMessageCodeService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * <p>
+ * 短信验证码 前端控制器
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+@Api(value = "/tAdmin", tags = {"账户"})
+@RestController
+@RequestMapping("/tAdmin")
+public class TAdminController {
+    @Autowired
+    TAdminService tAdminService;
+    @Autowired
+    TMessageCodeService tMessageCodeService;
+
+    @ApiOperation(value = "注册")
+    @PostMapping("/save")
+    public ResponseModel<?> save(@RequestBody TAdmin admin) {
+        if(StringUtils.isEmpty(admin.getUsername())||StringUtils.isEmpty(admin.getName())||StringUtils.isEmpty(admin.getPassword())){
+            return R.fail(ResponseCodesEnum.A0100,"数据有空!");
+        }
+        if(admin.getIfForeign().equals("0")){
+            //国内用户注册
+            if(StringUtils.isEmpty(admin.getPhone())){
+                return R.fail(ResponseCodesEnum.A0100,"手机号为空!");
+            }
+        }else {
+            //国外用户注册
+            if(StringUtils.isEmpty(admin.getEmail())){
+                return R.fail(ResponseCodesEnum.A0100,"邮箱为空!");
+            }
+        }
+        if (StringUtils.isEmpty(admin.getCode())){
+            return R.fail(ResponseCodesEnum.A0100,"验证码为空!");
+        }
+        //校验是否有重复的
+        LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+        query.eq(TAdmin::getUsername,admin.getUsername());
+        List<TAdmin> list = tAdminService.list(query);
+        if(list.size()>0){
+            return R.fail(ResponseCodesEnum.A0201,"用户登录名已存在");
+        }
+        LambdaQueryWrapper<TAdmin> query1 = Wrappers.lambdaQuery();
+        if(admin.getIfForeign().equals("0")){
+            query1.eq(TAdmin::getPhone,admin.getPhone());
+        }else {
+            query1.eq(TAdmin::getEmail,admin.getEmail());
+        }
+        List<TAdmin> list1 = tAdminService.list(query1);
+        if(list1.size()>0){
+            return R.fail(ResponseCodesEnum.A0203,"用户手机/邮箱已存在");
+        }
+        //校验验证码是否正确
+        LambdaQueryWrapper<TMessageCode> query2 = Wrappers.lambdaQuery();
+        if(admin.getIfForeign().equals("0")){
+            query2.eq(TMessageCode::getPhone,admin.getPhone());
+        }else {
+            query2.eq(TMessageCode::getPhone,admin.getEmail());
+        }
+        query2.eq(TMessageCode::getType,"0");//0,代表注册验证码
+        query2.eq(TMessageCode::getStatus,"0");
+        List<TMessageCode> messageCodeList = tMessageCodeService.list(query2);
+        if(messageCodeList.size()>0){
+            TMessageCode tMessageCode = messageCodeList.get(messageCodeList.size() - 1);
+            if(!tMessageCode.getCode().equals(admin.getCode())){
+                return R.fail(ResponseCodesEnum.A0002,"验证码错误");
+            }
+            tMessageCode.setStatus("1");
+            admin.setCreateDate(new Date());
+            admin.setModifyDate(new Date());
+            admin.setIsAdmined(false);
+            admin.setIsEnabled(true);
+            admin.setLoginFailureCount(0);
+            admin.setIsLocked(false);
+            admin.setPassword(DigestUtils.md5Hex(admin.getPassword()));
+            boolean b = tAdminService.save(admin);
+            tMessageCode.setModifyDate(new Date());
+            tMessageCodeService.saveOrUpdate(tMessageCode);
+            return R.ok(b);
+        }else {
+            return R.fail(ResponseCodesEnum.A0002,"没有找到验证码");
+        }
+
+    }
+    @ApiOperation(value = "登录")
+    @PostMapping("/login")
+    public ResponseModel<?> login(String username,String password) {
+        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
+            return R.fail(ResponseCodesEnum.A0001,"参数有空");
+        }
+        //验证用户名登录
+        LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+        query.eq(TAdmin::getUsername,username);
+        query.eq(TAdmin::getPassword,password);
+        List<TAdmin> list = tAdminService.list(query);
+        if(list.size()>0){
+            return R.ok(list.get(0));
+        }
+        //验证手机登录
+        LambdaQueryWrapper<TAdmin> query1 = Wrappers.lambdaQuery();
+        query1.eq(TAdmin::getPhone,username);
+        query1.eq(TAdmin::getPassword,password);
+        List<TAdmin> list1 = tAdminService.list(query1);
+        if(list1.size()>0){
+            return R.ok(list1.get(0));
+        }
+        return R.fail(ResponseCodesEnum.A0001,"密码或用户名错误");
+    }
+    @ApiOperation(value = "修改密码")
+    @PostMapping("/updatePassword")
+    public ResponseModel<?> updatePassword(String username,String password) {
+        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
+            return R.fail(ResponseCodesEnum.A0001,"参数有空");
+        }
+        //查找用户名
+        LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+        query.eq(TAdmin::getUsername,username);
+        List<TAdmin> list = tAdminService.list(query);
+        if(list.size()>0){
+            TAdmin admin = list.get(0);
+            admin.setPassword(password);
+            boolean b = tAdminService.saveOrUpdate(admin);
+            return R.ok(b);
+        }
+
+        return R.fail(ResponseCodesEnum.A0001,"修改失败");
+    }
+}
+

+ 142 - 0
src/main/java/com/szwl/controller/TMessageCodeController.java

@@ -0,0 +1,142 @@
+package com.szwl.controller;
+
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.szwl.constant.ResponseCodesEnum;
+import com.szwl.model.bo.R;
+import com.szwl.model.bo.ResponseModel;
+import com.szwl.model.entity.TAdmin;
+import com.szwl.model.entity.TMessageCode;
+import com.szwl.service.TAdminService;
+import com.szwl.service.TMessageCodeService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * <p>
+ * 短信验证码 前端控制器
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+@Api(value = "/tMessageCode", tags = {"验证码"})
+@RestController
+@RequestMapping("/tMessageCode")
+public class TMessageCodeController {
+    @Autowired
+    TAdminService tAdminService;
+    @Autowired
+    TMessageCodeService tMessageCodeService;
+    @ApiOperation(value = "发送注册验证码")
+    @PostMapping("/sentRegisterCode")
+    public ResponseModel<?> sentRegisterCode(String ifForeign,String phoneOrEmail) {
+        if(ifForeign.equals("0")){
+            if(StringUtils.isEmpty(phoneOrEmail)){
+                return R.fail(ResponseCodesEnum.A0100,"手机号为空!");
+            }
+            //检测是否已有手机号注册
+            LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+            query.eq(TAdmin::getPhone,phoneOrEmail);
+            List<TAdmin> list = tAdminService.list(query);
+            if(list.size()>0){
+                return R.fail(ResponseCodesEnum.A0202,"用户手机号已存在");
+            }
+            //国内发送短信
+            String result = tMessageCodeService.sentMessage("0", phoneOrEmail);
+            return R.ok(result);
+        }else {
+            if(StringUtils.isEmpty(phoneOrEmail)){
+                return R.fail(ResponseCodesEnum.A0100,"邮箱为空!");
+            }
+            //检测是否已有邮箱注册
+            LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+            query.eq(TAdmin::getEmail,phoneOrEmail);
+            List<TAdmin> list = tAdminService.list(query);
+            if(list.size()>0){
+                return R.fail(ResponseCodesEnum.A0207,"用户邮箱已存在");
+            }
+            //国外发邮件
+            String result = tMessageCodeService.sentEmail("0",phoneOrEmail);
+            return R.ok(result);
+        }
+    }
+    @ApiOperation(value = "发送忘记密码验证码")
+    @PostMapping("/sentForgetCode")
+    public ResponseModel<?> sentForgetCode(String ifForeign,String username,String phoneOrEmail) {
+        if(StringUtils.isEmpty(username)){
+            return R.fail(ResponseCodesEnum.A0100,"用户名为空!");
+        }
+        if(ifForeign.equals("0")){
+            if(StringUtils.isEmpty(phoneOrEmail)){
+                return R.fail(ResponseCodesEnum.A0100,"手机号为空!");
+            }
+            //检测是否已有手机号注册
+            LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+            query.eq(TAdmin::getPhone,phoneOrEmail);
+            query.eq(TAdmin::getUsername,username);
+            List<TAdmin> list = tAdminService.list(query);
+            if(list.size()<=0){
+                return R.fail(ResponseCodesEnum.A0001,"登录名或手机号出错");
+            }
+            //国内发送短信
+            String result = tMessageCodeService.sentMessage("1", phoneOrEmail);
+            return R.ok(result);
+        }else {
+            if(StringUtils.isEmpty(phoneOrEmail)){
+                return R.fail(ResponseCodesEnum.A0100,"邮箱为空!");
+            }
+            //检测是否已有邮箱注册
+            LambdaQueryWrapper<TAdmin> query = Wrappers.lambdaQuery();
+            query.eq(TAdmin::getEmail,phoneOrEmail);
+            query.eq(TAdmin::getUsername,username);
+            List<TAdmin> list = tAdminService.list(query);
+            if(list.size()<=0){
+                return R.fail(ResponseCodesEnum.A0001,"登录名或邮箱出错");
+            }
+            //国外发邮件
+            String result = tMessageCodeService.sentEmail("1",phoneOrEmail);
+            return R.ok(result);
+        }
+    }
+    @ApiOperation(value = "校验验证码")
+    @PostMapping("/checkForgetCode")
+    public ResponseModel<?> checkForgetCode(String ifForeign,String code,String phoneOrEmail) {
+        if(StringUtils.isEmpty(code)){
+            return R.fail(ResponseCodesEnum.A0100,"验证码为空!");
+        }
+        LambdaQueryWrapper<TMessageCode> query2 = Wrappers.lambdaQuery();
+        if(ifForeign.equals("0")){
+            query2.eq(TMessageCode::getPhone,phoneOrEmail);
+        }else {
+            query2.eq(TMessageCode::getPhone,phoneOrEmail);
+        }
+        query2.eq(TMessageCode::getType,"1");//1,忘记密码验证码
+        query2.eq(TMessageCode::getStatus,"0");
+        List<TMessageCode> messageCodeList = tMessageCodeService.list(query2);
+        if(messageCodeList.size()>0){
+            TMessageCode tMessageCode = messageCodeList.get(messageCodeList.size() - 1);
+            if(!tMessageCode.getCode().equals(code)){
+                return R.fail(ResponseCodesEnum.A0002,"验证码错误");
+            }else {
+                tMessageCode.setStatus("1");
+                tMessageCode.setModifyDate(new Date());
+                tMessageCodeService.saveOrUpdate(tMessageCode);
+                return R.ok();
+            }
+        }else {
+            return R.fail(ResponseCodesEnum.A0002,"没有找到验证码");
+        }
+    }
+}
+

+ 16 - 0
src/main/java/com/szwl/mapper/TMessageCodeMapper.java

@@ -0,0 +1,16 @@
+package com.szwl.mapper;
+
+import com.szwl.model.entity.TMessageCode;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ * 短信验证码 Mapper 接口
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+public interface TMessageCodeMapper extends BaseMapper<TMessageCode> {
+
+}

+ 23 - 0
src/main/java/com/szwl/mapper/xml/TMessageCodeMapper.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.szwl.mapper.TMessageCodeMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.szwl.model.entity.TMessageCode">
+        <id column="id" property="id" />
+        <result column="create_date" property="createDate" />
+        <result column="modify_date" property="modifyDate" />
+        <result column="admin_id" property="adminId" />
+        <result column="phone" property="phone" />
+        <result column="code" property="code" />
+        <result column="type" property="type" />
+        <result column="status" property="status" />
+        <result column="note" property="note" />
+    </resultMap>
+
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+        id, create_date, modify_date, admin_id, phone, code, type, status, note
+    </sql>
+
+</mapper>

+ 1 - 1
src/main/java/com/szwl/model/entity/TAdmin.java

@@ -81,7 +81,7 @@ public class TAdmin implements Serializable {
     @ApiModelProperty(value = "是否退款;")
     private String isRefund;
 
-    @ApiModelProperty(value = "是否国外;")
+    @ApiModelProperty(value = "是否国外;0:国内,1:国外")
     private String ifForeign;
 
     @ApiModelProperty(value = "是否开启远程开关机")

+ 50 - 0
src/main/java/com/szwl/model/entity/TMessageCode.java

@@ -0,0 +1,50 @@
+package com.szwl.model.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import java.util.Date;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.io.Serializable;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * <p>
+ * 短信验证码
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value="TMessageCode对象", description="短信验证码")
+public class TMessageCode implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    private Date createDate;
+
+    private Date modifyDate;
+
+    private Long adminId;
+
+    private String phone;
+
+    @ApiModelProperty(value = "短信验证码内容")
+    private String code;
+
+    @ApiModelProperty(value = "验证码类型;0,注册;1,忘记密码验证码")
+    private String type;
+
+    @ApiModelProperty(value = "是否使用;0,未使用;1,已使用")
+    private String status;
+
+    private String note;
+
+
+}

+ 151 - 0
src/main/java/com/szwl/model/utils/MailUtil.java

@@ -0,0 +1,151 @@
+package com.szwl.model.utils;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+import javax.mail.*;
+import javax.mail.internet.*;
+import java.util.Properties;
+
+/**
+ * 阿里云邮件发送
+ */
+public class MailUtil {
+    private static final String ALIDM_SMTP_HOST = "smtp.mxhichina.com";
+//    private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
+    private static final int ALIDM_SMTP_PORT = 25;// 或80
+
+    // 发件人的账号 和 密码
+    private String user;
+    private String password;
+
+    public MailUtil() {
+//        this("AfterSalesInfo@sunzee.net", "Sz123456");
+        this("magic_candy_sunzee@sunzee.net", "Sz123456");
+    }
+
+    public MailUtil(String user, String password) {
+        this.user = user;
+        this.password = password;
+    }
+
+//    public static void main(String[] args) {
+//        new MailUtil().send("520283710@qq.com", "测试1", "nihao显示");
+        //new MailUtil().send("1161588342@qq.com", "测试1", "市劳动纠纷联赛积分了","C:/Users/guo/Desktop/Proguard.xml");
+//    }
+
+    /**
+     * 发送邮件
+     *
+     * @param toEmail 收件人邮箱地址
+     * @param subject 邮件标题
+     * @param content 邮件内容 可以是html内容
+     */
+    public void send(String toEmail, String subject, String content) {
+        Session session = loadMailSession();
+        // session.setDebug(true);
+        // 创建邮件消息
+        MimeMessage message = new MimeMessage(session);
+        try {
+            // 设置发件人
+            message.setFrom(new InternetAddress(user));
+            Address[] a = new Address[1];
+            a[0] = new InternetAddress(user);
+            message.setReplyTo(a);
+            // 设置收件人
+            InternetAddress to = new InternetAddress(toEmail);
+            message.setRecipient(MimeMessage.RecipientType.TO, to);
+            // 设置邮件标题
+            message.setSubject(subject);
+            // 设置邮件的内容体
+            message.setContent(content, "text/html;charset=UTF-8");
+            // 发送邮件
+            Transport.send(message);
+        } catch (MessagingException e) {
+            String err = e.getMessage();
+            // 在这里处理message内容, 格式是固定的
+            System.out.println(err);
+        }
+    }
+
+
+    /**
+     * 发送邮件 带附件
+     *
+     * @param toEmail    收件人邮箱地址
+     * @param subject    邮件标题
+     * @param content    邮件内容 可以是html内容
+     * @param attachPath 附件路径
+     */
+    public void send(String toEmail, String subject, String content, String attachPath) {
+        Session session = loadMailSession();
+
+        MimeMessage mm = new MimeMessage(session);
+        try {
+            //发件人
+            mm.setFrom(new InternetAddress(user));
+            //收件人
+            mm.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); // 设置收件人
+            // mm.setRecipient(Message.RecipientType.CC, new
+            // InternetAddress("XXXX@qq.com")); //设置抄送人
+            //标题
+            mm.setSubject(subject);
+            //内容
+            Multipart multipart = new MimeMultipart();
+            //body部分
+            BodyPart contentPart = new MimeBodyPart();
+            contentPart.setContent(content, "text/html;charset=utf-8");
+            multipart.addBodyPart(contentPart);
+
+            //附件部分
+            BodyPart attachPart = new MimeBodyPart();
+            FileDataSource fileDataSource = new FileDataSource(attachPath);
+            attachPart.setDataHandler(new DataHandler(fileDataSource));
+            attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
+            multipart.addBodyPart(attachPart);
+
+            mm.setContent(multipart);
+            Transport.send(mm);
+        } catch (Exception e) {
+            String err = e.getMessage();
+            // 在这里处理message内容, 格式是固定的
+            System.out.println(err);
+        }
+
+    }
+
+    private Session loadMailSession() {
+        try {
+            // 配置发送邮件的环境属性
+            final Properties props = new Properties();
+            // 表示SMTP发送邮件,需要进行身份验证
+            props.put("mail.smtp.auth", "true");
+            props.put("mail.smtp.host", ALIDM_SMTP_HOST);
+            // props.put("mail.smtp.port", ALIDM_SMTP_PORT);
+            // 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
+            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+            props.put("mail.smtp.socketFactory.port", "465");
+            props.put("mail.smtp.port", "465");
+            // 发件人的账号
+            props.put("mail.user", user);
+            // 访问SMTP服务时需要提供的密码
+            props.put("mail.password", password);
+            // 构建授权信息,用于进行SMTP进行身份验证
+            Authenticator authenticator = new Authenticator() {
+                @Override
+                protected PasswordAuthentication getPasswordAuthentication() {
+                    // 用户名、密码
+                    String userName = props.getProperty("mail.user");
+                    String password = props.getProperty("mail.password");
+                    return new PasswordAuthentication(userName, password);
+                }
+            };
+            // 使用环境属性和授权信息,创建邮件会话
+            return Session.getInstance(props, authenticator);
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println("mail session is null");
+        }
+        return null;
+    }
+
+}

+ 212 - 0
src/main/java/com/szwl/model/utils/YunPianSms.java

@@ -0,0 +1,212 @@
+package com.szwl.model.utils;
+
+
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import org.json.JSONObject;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by x on 2017
+ */
+public final class YunPianSms {
+    //查账户信息的http地址
+    private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";
+
+    //智能匹配模版发送接口的http地址
+    private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
+
+    //模板发送接口的http地址
+    private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
+
+    //发送语音验证码接口的http地址
+    private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";
+
+    //编码格式。发送编码格式统一用UTF-8
+    private static String ENCODING = "UTF-8";
+
+    public static JSONObject sendSms(String phone, String content) throws IOException, URISyntaxException {
+
+        //修改为您的apikey.apikey可在官网(http://www.yuanpian.com)登录后获取
+        String apikey = "07784f5fedb508046c841b391005b7de";
+
+        //修改为您要发送的手机号
+        String mobile = URLEncoder.encode(phone, ENCODING);
+
+        /**************** 查账户信息调用示例 *****************/
+//        System.out.println(YunPianSms.getUserInfo(apikey));
+
+        /**************** 使用智能匹配模版接口发短信(推荐) *****************/
+        //设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
+        String text = content;
+        //发短信调用示例
+        String result = YunPianSms.sendSms(apikey, text, mobile);
+        JSONObject resultJson = new JSONObject(result);
+        System.out.println("短信发送结果:" + resultJson.get("msg"));
+        return resultJson;
+        /**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模版接口) *****************/
+        //设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
+//        long tpl_id = 1;
+        //设置对应的模板变量值
+
+//        String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
+//                + URLEncoder.encode("1234", ENCODING) + "&"
+//                + URLEncoder.encode("#company#",ENCODING) + "="
+//                + URLEncoder.encode("云片网",ENCODING);
+        //模板发送的调用示例
+//        System.out.println(tpl_value);
+//        System.out.println(YunPianSms.tplSendSms(apikey, tpl_id, tpl_value, mobile));
+
+        /**************** 使用接口发语音验证码 *****************/
+//        String code = "1234";
+        //System.out.println(YunPianSms.sendVoice(apikey, mobile ,code));
+    }
+
+
+    /**
+     * 取账户信息
+     *
+     * @return json格式字符串
+     * @throws IOException
+     */
+
+    public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("apikey", apikey);
+        return post(URI_GET_USER_INFO, params);
+    }
+
+    /**
+     * 智能匹配模版接口发短信
+     *
+     * @param apikey apikey
+     * @param text    短信内容
+     * @param mobile  接受的手机号
+     * @return json格式字符串
+     * @throws IOException
+     */
+
+    public static String sendSms(String apikey, String text, String mobile) throws IOException {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("apikey", apikey);
+        params.put("text", text);
+        params.put("mobile", mobile);
+        return post(URI_SEND_SMS, params);
+    }
+
+    /**
+     * 通过模板发送短信(不推荐)
+     *
+     * @param apikey    apikey
+     * @param tpl_id     模板id
+     * @param tpl_value  模板变量值
+     * @param mobile     接受的手机号
+     * @return json格式字符串
+     * @throws IOException
+     */
+
+    public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("apikey", apikey);
+        params.put("tpl_id", String.valueOf(tpl_id));
+        params.put("tpl_value", tpl_value);
+        params.put("mobile", mobile);
+        return post(URI_TPL_SEND_SMS, params);
+    }
+
+    /**
+     * 通过接口发送语音验证码
+     *
+     * @param apikey apikey
+     * @param mobile 接收的手机号
+     * @param code   验证码
+     * @return
+     */
+
+    public static String sendVoice(String apikey, String mobile, String code) {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("apikey", apikey);
+        params.put("mobile", mobile);
+        params.put("code", code);
+        return post(URI_SEND_VOICE, params);
+    }
+
+    /**
+     * 基于HttpClient 4.3的通用POST方法
+     *
+     * @param url       提交的URL
+     * @param paramsMap 提交<参数,值>Map
+     * @return 提交响应
+     */
+
+    public static String post(String url, Map<String, String> paramsMap) {
+        CloseableHttpClient client = HttpClients.createDefault();
+        String responseText = "";
+        CloseableHttpResponse response = null;
+        try {
+            HttpPost method = new HttpPost(url);
+            if (paramsMap
+                    != null) {
+                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
+                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
+                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
+                    paramList.add(pair);
+                }
+                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
+            }
+            response = client.execute(method);
+            HttpEntity entity = response.getEntity();
+            if (entity != null) {
+                responseText = EntityUtils.toString(entity);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                response.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return responseText;
+    }
+
+    public static void main(String[] args) {
+
+        try {
+
+            //String content="【申泽智能】您好,机器二次取棍异常报警,机器名:MG301测试机,设备号:aaa863010031925055";
+            String tpl_value = URLEncoder.encode("#content#", ENCODING) + "=" +
+                    URLEncoder.encode("二次", ENCODING) + "&" + URLEncoder.encode(
+                    "#name#", ENCODING) + "=" + URLEncoder.encode("MG机",
+                    ENCODING) + "&" + URLEncoder.encode("#clientId#", ENCODING) + "=" + URLEncoder.encode("aaa8635", ENCODING);
+            //您好,机器#content#,机器名:#name#,设备号:#clientId#
+            System.out.println(tpl_value.length());
+            String content = "您好,机器二次取棍异常警告,机器名:MG301测试机,设备号:aaa863010031925055";
+            System.out.println(content.length());
+            //String result = tplSendSms("07784f5fedb508046c841b391005b7de", 2890698L, tpl_value, "15875317659");
+
+            String result = sendSms("07784f5fedb508046c841b391005b7de", content, "15875317659");
+            JSONObject resultJson = new JSONObject(result);
+            System.out.println("短信发送结果:" + resultJson.get("msg"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+
+    }
+}

+ 24 - 0
src/main/java/com/szwl/service/TMessageCodeService.java

@@ -0,0 +1,24 @@
+package com.szwl.service;
+
+import com.szwl.model.entity.TMessageCode;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ * 短信验证码 服务类
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+public interface TMessageCodeService extends IService<TMessageCode> {
+
+    /*
+    * 发送短信验证码
+    * */
+    String sentMessage(String type, String phone);
+    /*
+     * 发送邮箱验证码
+     * */
+    String sentEmail(String type, String email);
+}

+ 127 - 0
src/main/java/com/szwl/service/impl/TMessageCodeServiceImpl.java

@@ -0,0 +1,127 @@
+package com.szwl.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.szwl.model.entity.TMessageCode;
+import com.szwl.mapper.TMessageCodeMapper;
+import com.szwl.model.utils.MailUtil;
+import com.szwl.model.utils.YunPianSms;
+import com.szwl.service.TMessageCodeService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+/**
+ * <p>
+ * 短信验证码 服务实现类
+ * </p>
+ *
+ * @author wuhs
+ * @since 2022-04-14
+ */
+@Service
+public class TMessageCodeServiceImpl extends ServiceImpl<TMessageCodeMapper, TMessageCode> implements TMessageCodeService {
+
+    @Autowired
+    TMessageCodeMapper tMessageCodeMapper;
+
+    private static final String appid = "07784f5fedb508046c841b391005b7de";
+    /*
+     * 发送短信验证码
+     * */
+    @Override
+    public String sentMessage(String type, String phone) {
+        String result = null;
+        String retu = null;
+        try {
+            String code = addCode();
+            result = YunPianSms.sendSms(appid, getMessage(type, code), phone);
+            JSONObject sult = JSONObject.parseObject(result);
+            Object msg = sult.get("msg");
+            Object mobile = sult.get("mobile");
+            retu = (msg+":"+mobile).toString();
+            if(mobile!=null){
+                TMessageCode tMessageCode = new TMessageCode();
+                tMessageCode.setType(type);
+                tMessageCode.setCode(code);
+                tMessageCode.setPhone(phone);
+                tMessageCode.setModifyDate(new Date());
+                tMessageCode.setCreateDate(new Date());
+                tMessageCode.setStatus("0");
+                int insert = tMessageCodeMapper.insert(tMessageCode);
+            }
+
+            int a= 0;
+        } catch (Exception e) {
+
+        }
+
+        return retu;
+    }
+    /*
+     * 发送邮箱验证码
+     * */
+    @Override
+    public String sentEmail(String type, String email) {
+        String code = addCode();
+//        String subject = "Verification code message";
+        String subject =getSubject(type);
+        StringBuffer contnet = new StringBuffer();
+        getContnet(type,code);
+//        String str1="Dear customer<br>" +
+//                "<br>" +
+//                "Hello, your verification code is:";
+//        String str2=",please enter it within 3 minutes.";
+//        contnet.append(str1).append(code).append(str2);
+        new MailUtil().send(email,subject,contnet.toString());
+        TMessageCode tMessageCode = new TMessageCode();
+        tMessageCode.setType(type);
+        tMessageCode.setCode(code);
+        tMessageCode.setPhone(email);
+        tMessageCode.setModifyDate(new Date());
+        tMessageCode.setCreateDate(new Date());
+        tMessageCode.setStatus("0");
+        tMessageCodeMapper.insert(tMessageCode);
+        return "success";
+    }
+    //邮件标题
+    private String getSubject(String type) {
+        String subject = "";
+        if(type.equals("0")||type.equals("1")){
+            subject = "Verification code message";
+        }
+        return subject;
+    }
+    //邮件内容
+    private String getContnet(String type, String code) {
+        StringBuffer contnet = new StringBuffer();
+        if(type.equals("0")||type.equals("1")){
+
+            String str1="Dear customer<br>" +
+                    "<br>" +
+                    "Hello, your verification code is:";
+            String str2=",please enter it within 3 minutes.";
+            contnet.append(str1).append(code).append(str2);
+        }
+        return contnet.toString();
+    }
+    //生成6位随机数字
+    private String addCode() {
+        int code = (int) ((Math.random() * 9 + 1) * 100000);
+        String retu = String.valueOf(code);
+        return retu;
+    }
+
+    private String getMessage(String type, String code) {
+        String message = "";
+        if(StringUtils.isNotEmpty(type)&&type.equals("0")){
+            message = "您好,您的验证码是"+code+",请于3分钟内输入。";
+        }
+        if(StringUtils.isNotEmpty(type)&&type.equals("1")){
+            message = "您好,您的验证码是"+code+",请于3分钟内输入。";
+        }
+        return message;
+    }
+}

+ 1 - 1
src/test/java/com/szwl/AutoGeneratorTests.java

@@ -47,7 +47,7 @@ class AutoGeneratorTests {
 		strategyConfig
 //				.setCapitalMode(true)//设置全局大写命名
 				.setInclude(new String[]{
-						"t_admin"
+						""
 				})//只会生成该表
 				.setEntityLombokModel(true)//实体类生成之后自动添加lombok注解
 				.setNaming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略