This commit is contained in:
D 2024-03-02 00:28:24 +08:00
parent da9c38f476
commit f13712509e
6 changed files with 155 additions and 105 deletions

View File

@ -1,10 +1,24 @@
package com.ruoyi.oauth.phone.controller;
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 com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.oauth.phone.service.DySmsService;
@RestController
@Anonymous
@RequestMapping("/oauth/phone")
public class DySmsController {
@Autowired
public DySmsService dySmsService;
@PostMapping("/login")
public String postMethodName() {
dySmsService.doLogin("17854126030");
return null;
}
}

View File

@ -6,12 +6,14 @@ import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import com.ruoyi.oauth.phone.enums.DySmsTemplate;
import com.ruoyi.oauth.phone.service.DySmsService;
import com.ruoyi.oauth.phone.utils.DySmsUtil;
import com.ruoyi.system.service.ISysUserService;
@ -31,7 +33,7 @@ public class DySmsServiceImpl implements DySmsService {
private TokenService tokenService;
public static String generateRandomString(int n) {
String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String characters = "0123456789"; //ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
StringBuilder result = new StringBuilder();
Random random = new Random();
@ -55,7 +57,9 @@ public class DySmsServiceImpl implements DySmsService {
throw new ServiceException("该手机号未绑定用户");
}
try {
dySmsUtil.sendSms(null, null, phone);
JSONObject templateParams = new JSONObject();
templateParams.put("code", code);
dySmsUtil.sendSms(DySmsTemplate.Test_TEMPLATE_CODE, templateParams, phone);
redisCache.setCacheObject("phone_codes_login" + phone, code, 1, TimeUnit.MINUTES);
} catch (Exception e) {
e.printStackTrace();

View File

@ -76,8 +76,11 @@ public class DySmsUtil {
try {
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
logger.info("短信接口返回的数据----------------");
logger.info(sendSmsResponse.toString());
if (sendSmsResponse.getBody().getCode().equals("OK")) {
logger.info("短信接口返回的数据---", sendSmsResponse.getBody().getMessage());
} else {
logger.error("短信接口返回的数据---", sendSmsResponse.getBody().getMessage());
}
} catch (TeaException error) {
// 错误 message
System.out.println(error.getMessage());

View File

@ -23,6 +23,11 @@
<artifactId>ruoyi-common</artifactId>
</dependency>
<!-- 系统框架 -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-framework</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -16,10 +16,14 @@ import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.online.domain.OnlineMb;
import com.ruoyi.online.service.IOnlineMbService;
import com.ruoyi.online.utils.SqlMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@ -31,9 +35,13 @@ public class OnLineController extends BaseController {
private SqlSessionFactory sqlSessionFactory;
@Autowired
private IOnlineMbService onlineMbService;
@Resource(name = "ss")
private PermissionService permissionService;
@RequestMapping("/api/**")
public Object api(@RequestParam(required = false) HashMap<String,Object> params,@RequestBody(required = false) HashMap<String,Object> data,HttpServletRequest request,HttpServletResponse response) {
public Object api(@RequestParam(required = false) HashMap<String, Object> params,
@RequestBody(required = false) HashMap<String, Object> data, HttpServletRequest request,
HttpServletResponse response) {
OnlineMb selectOnlineMb = new OnlineMb();
selectOnlineMb.setPath(request.getRequestURI().replace("/online/api", ""));
selectOnlineMb.setMethod(request.getMethod());
@ -44,28 +52,50 @@ public class OnLineController extends BaseController {
if (params != null) {
params.keySet().forEach(key -> {
Matcher matcher = pattern.matcher(key);
if(matcher.find()){
if (matcher.find()) {
object_params.put(matcher.group(1), object.get(key));
}else{
} else {
object.put(key, params.get(key));
}
});
}
if (data != null) {
if(data.containsKey("params")){
if (data.containsKey("params")) {
object_params.putAll((HashMap<String, Object>) object.get("params"));
data.remove("params");
}
object.putAll(data);
}
object.put("params", object_params);
List<OnlineMb> selectOnlineMbList = onlineMbService.selectOnlineMbList(selectOnlineMb);
if (selectOnlineMbList.size() == 0) {
return error("没有相关接口");
return AjaxResult.error("没有资源"+selectOnlineMb.getPath());
} else if (selectOnlineMbList.size() > 1) {
return error("存在多个接口");
return AjaxResult.error(500,"系统错误,在线接口重复");
} else {
OnlineMb onlineMb = selectOnlineMbList.get(0);
boolean permissionFlag = true;
if (onlineMb.getPermissionType() != null) {
switch (onlineMb.getPermissionType()) {
case "hasPermi" -> permissionFlag = permissionService.hasPermi(onlineMb.getPermissionValue());
case "lacksPermi" -> permissionFlag = !permissionService.lacksPermi(onlineMb.getPermissionValue());
case "hasAnyPermi" -> permissionFlag = permissionService.hasAnyPermi(onlineMb.getPermissionValue());
case "hasRole" -> permissionFlag = permissionService.hasRole(onlineMb.getPermissionValue());
case "lacksRole" -> permissionFlag = !permissionService.lacksRole(onlineMb.getPermissionValue());
case "hasAnyRoles" -> permissionFlag = permissionService.hasAnyRoles(onlineMb.getPermissionValue());
}
}
if (!permissionFlag) {
return AjaxResult.error(403,"没有权限,请联系管理员授权");
}
if (onlineMb.getDeptId() != null && onlineMb.getDeptId().equals("1")) {
object.put("deptId", SecurityUtils.getDeptId());
}
if (onlineMb.getUserId() != null && onlineMb.getUserId().equals("1")) {
object.put("userId", SecurityUtils.getUserId());
}
String sql = "<script>\n" + onlineMb.getSql() + "\n</script>";
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlMapper sqlMapper = new SqlMapper(sqlSession);
@ -75,7 +105,7 @@ public class OnLineController extends BaseController {
case "selectOne" -> success(sqlMapper.selectOne(sql, object));
case "update" -> toAjax(sqlMapper.update(sql, object));
case "delete" -> toAjax(sqlMapper.delete(sql, object));
default -> error("错误的执行器");
default -> AjaxResult.error(500,"系统错误,执行器错误");
};
}
}

View File

@ -15,213 +15,207 @@ import io.swagger.v3.oas.annotations.media.Schema;
* @date 2024-01-26
*/
@Schema(description = "mybatis在线接口对象")
public class OnlineMb extends BaseEntity
{
public class OnlineMb extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 主键 */
@Schema(defaultValue = "主键")
private Long mbId;
/** 标签名 */
@Schema(defaultValue = "标签名")
@Excel(name = "标签名")
private String tag;
/** 标签id */
@Schema(defaultValue = "标签id")
@Excel(name = "标签id")
private String tagId;
/** 参数类型 */
@Schema(defaultValue = "参数类型")
@Excel(name = "参数类型")
private String parameterType;
/** 结果类型 */
@Schema(defaultValue = "结果类型")
@Excel(name = "结果类型")
private String resultMap;
/** sql语句 */
@Schema(defaultValue = "sql语句")
@Excel(name = "sql语句")
private String sql;
/** 请求路径 */
@Schema(defaultValue = "请求路径")
@Excel(name = "请求路径")
private String path;
/** 请求方式 */
@Schema(defaultValue = "请求方式")
@Excel(name = "请求方式")
private String method;
/** 响应类型 */
@Schema(defaultValue = "响应类型")
@Excel(name = "响应类型")
private String resultType;
/** 执行器 */
@Schema(defaultValue = "执行器")
@Excel(name = "执行器")
private String actuator;
/** 是否需要userId */
@Schema(defaultValue = "是否需要userId")
@Excel(name = "是否需要userId")
private String userId;
/** 是否需要deptId */
@Schema(defaultValue = "是否需要deptId")
@Excel(name = "是否需要deptId")
private String deptId;
public void setMbId(Long mbId)
{
/** 许可类型 */
@Schema(defaultValue = "许可类型")
@Excel(name = "许可类型")
private String permissionType;
/** 许可值 */
@Schema(defaultValue = "许可值")
@Excel(name = "许可值")
private String permissionValue;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getPermissionType() {
return permissionType;
}
public void setPermissionType(String permissionType) {
this.permissionType = permissionType;
}
public String getPermissionValue() {
return permissionValue;
}
public void setPermissionValue(String permissionValue) {
this.permissionValue = permissionValue;
}
public void setMbId(Long mbId) {
this.mbId = mbId;
}
public Long getMbId()
{
public Long getMbId() {
return mbId;
}
public void setTag(String tag)
{
public void setTag(String tag) {
this.tag = tag;
}
public String getTag()
{
public String getTag() {
return tag;
}
public void setTagId(String tagId)
{
public void setTagId(String tagId) {
this.tagId = tagId;
}
public String getTagId()
{
public String getTagId() {
return tagId;
}
public void setParameterType(String parameterType)
{
public void setParameterType(String parameterType) {
this.parameterType = parameterType;
}
public String getParameterType()
{
public String getParameterType() {
return parameterType;
}
public void setResultMap(String resultMap)
{
public void setResultMap(String resultMap) {
this.resultMap = resultMap;
}
public String getResultMap()
{
public String getResultMap() {
return resultMap;
}
public void setSql(String sql)
{
public void setSql(String sql) {
this.sql = sql;
}
public String getSql()
{
public String getSql() {
return sql;
}
public void setPath(String path)
{
public void setPath(String path) {
this.path = path;
}
public String getPath()
{
public String getPath() {
return path;
}
public void setMethod(String method)
{
public void setMethod(String method) {
this.method = method;
}
public String getMethod()
{
public String getMethod() {
return method;
}
public void setResultType(String resultType)
{
public void setResultType(String resultType) {
this.resultType = resultType;
}
public String getResultType()
{
public String getResultType() {
return resultType;
}
public void setActuator(String actuator)
{
public void setActuator(String actuator) {
this.actuator = actuator;
}
public String getActuator()
{
public String getActuator() {
return actuator;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("mbId", getMbId())
.append("tag", getTag())
.append("tagId", getTagId())
.append("parameterType", getParameterType())
.append("resultMap", getResultMap())
.append("sql", getSql())
.append("path", getPath())
.append("method", getMethod())
.append("resultType", getResultType())
.append("actuator", getActuator())
.toString();
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("mbId", getMbId())
.append("tag", getTag())
.append("tagId", getTagId())
.append("parameterType", getParameterType())
.append("resultMap", getResultMap())
.append("sql", getSql())
.append("path", getPath())
.append("method", getMethod())
.append("resultType", getResultType())
.append("actuator", getActuator())
.append("userId",getUserId())
.append("deptId",getDeptId())
.append("permissionType",getPermissionType())
.append("permissionValue",getPermissionValue())
.toString();
}
}