本帖最后由 ppgjx 于 2022-12-13 18:18 编辑
[Asm] 纯文本查看 复制代码 package com.api.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import com.api.common.result.PageReq;
import com.api.common.result.PageResult;
import com.api.common.result.Result;
import com.api.common.user.*;
import com.api.constant.RedisKey;
import com.api.constant.ResultCode;
import com.api.dao.ApiUserDao;
import com.api.dao.UserFlowDao;
import com.api.entity.ApiUserEntity;
import com.api.entity.UserFlowEntity;
import com.api.execption.ResRunException;
import com.api.service.ApiUserService;
import com.api.service.UserFlowService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* 用户表(ApiUser)表控制层
*
* [url=home.php?mod=space&uid=686208]@AuThor[/url] makejava
* [url=home.php?mod=space&uid=441028]@Since[/url] 2022-12-04 17:58:36
*/
@RestController
@Api(tags = "admin")
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
/**
* 服务对象
*/
@Resource
private ApiUserService ApiUserService;
@Resource
private ApiUserDao apiUserDao;
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 添加用户月
* [url=home.php?mod=space&uid=952169]@Param[/url] req
* @return
*/
@RequestMapping(value = "/addBalance",method = RequestMethod.POST)
@ApiOperation(value = "添加用户余额")
@SaCheckLogin
@Transactional
public Result addBalance(@RequestBody @Validated AddBalanceReq req) {
Integer balance = req.getBalance();
String account = req.getAccount();
ApiUserEntity user = StpUtil.getSession().getModel("user",ApiUserEntity.class);
if(!user.getAccount().equals("admin")){
throw new ResRunException(ResultCode.ERR,"非管理员");
}
ApiUserEntity apiUserEntity = apiUserDao.selectOneByAccount(account);
if(null == apiUserEntity){
throw new ResRunException(ResultCode.ERR,"用户不存在");
}
apiUserEntity.setBalance(apiUserEntity.getBalance() + balance);
apiUserDao.updateById(apiUserEntity);
//添加流水
UserFlowEntity userFlowEntity = new UserFlowEntity();
userFlowEntity.setUserId(user.getId());
userFlowEntity.setType(UserFlowEntity.流水_收入);
userFlowEntity.setAmount(balance);
userFlowEntity.setExchangeDesc("充值");
userFlowEntity.setTradeNo("");
userFlowEntity.setCreateTime(new Date());
userFlowEntity.setUpdateTime(new Date());
userFlowDao.insert(userFlowEntity);
return Result.success();
}
@Resource
private UserFlowDao userFlowDao;
@Resource
private ApiUserService apiUserService;
@RequestMapping(value = "/getUserList",method = RequestMethod.GET)
@ApiOperation(value = "获取用户列表")
@SaCheckLogin
public PageResult<Page<ApiUserEntity>> getUserList(PageReq req,ApiUserEntity apiUser) {
ApiUserEntity user = StpUtil.getSession().getModel("user",ApiUserEntity.class);
if(!user.getAccount().equals("admin")){
throw new ResRunException(ResultCode.ERR,"非管理员");
}
Page<ApiUserEntity> search = apiUserService.search(req, apiUser);
return PageResult.success(search);
}
}
比如这段代码 每次都要判断是不是管理员 有没有一种方法在这个类中不管调用哪个接口都会走这个方法 这个方法像拦截器一样可以允许请求通过 但只是在这个类中有效 我知道拦截器和过滤器可以实现这个需求 但是我不想用 就想问spring有没有这种注解过或者方法可以做到这样 php里面就有一个init方法
|