jinpengyong
2023-10-20 17c774c9c13febdcff654ffd6bbabd313c37a3ee
chore:补充提交
2 files added
8 files modified
292 ■■■■ changed files
screen-api/pom.xml 6 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/AppUserController.java 63 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/entity/User.java 3 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/enums/AppAllocationStateEnum.java 59 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/query/AppUserCond.java 31 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/vo/allocation/AllocationFindVo.java 3 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/UserService.java 6 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/AllocationServiceImpl.java 20 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java 82 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/WechatUtils.java 19 ●●●● patch | view | raw | blame | history
screen-api/pom.xml
@@ -11,6 +11,12 @@
    <artifactId>screen-api</artifactId>
    <dependencies>
        <!-- http请求工具包依赖 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.moral</groupId>
            <artifactId>screen-common</artifactId>
screen-api/src/main/java/com/moral/api/controller/AppUserController.java
@@ -6,6 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,17 +15,11 @@
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moral.api.entity.AppUser;
import com.moral.api.exception.BusinessException;
import com.moral.api.mapper.AppUserMapper;
import com.moral.api.service.UserService;
import com.moral.api.utils.WechatUtils;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
@Slf4j
@Api(tags = {"小程序用户管理"})
@@ -35,11 +30,10 @@
    @Autowired
    private UserService userService;
    @Autowired
    private AppUserMapper appUserMapper;
    @ApiOperation(value = "登陆信息", notes = "登陆信息")
    @PostMapping("login")
    @ApiOperation(value = "登陆信息")
    @PostMapping("logins")
    public ResultMessage login(@RequestBody Map<String, Object> parameters) {
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
@@ -53,45 +47,12 @@
    }
    @PostMapping("/wx/login")
    @ApiOperation(value = "小程序登陆", notes = "小程序登陆")
    public ResultMessage userLogin(@RequestParam(value = "code", required = false) String code,
                        @RequestParam(value = "rawData", required = false) String rawData,
                        @RequestParam(value = "signature", required = false) String signature) {
        // 用户非敏感信息:rawData
        // 签名:signature
        JSONObject rawDataJson = JSON.parseObject(rawData);
        // 1.接收小程序发送的code
        // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
        JSONObject SessionKeyOpenId = WechatUtils.getSessionKeyOrOpenId(code);
        // 3.接收微信接口服务 获取返回的参数
        String openid = SessionKeyOpenId.getString("openid");
        String sessionKey = SessionKeyOpenId.getString("session_key");
        // 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
        String signature2 = DigestUtils.sha1Hex(rawData + sessionKey);
        if (!signature.equals(signature2)) {
//            return ResultMessage.ok().message("签名校验失败");
//            return ResultMessage.ok("签名校验失败");
            throw new BusinessException("签名校验失败");
        }
        // 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库;
        LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(AppUser::getOpenId, openid);
//        User user = userService.getOne(lqw);
        AppUser user = appUserMapper.selectOne(lqw);
        if (user == null) {
            // 用户信息入库
            String nickName = rawDataJson.getString("nickName");
            String avatarUrl = rawDataJson.getString("avatarUrl");
            user = new AppUser();
            user.setOpenId(Integer.parseInt(openid));
            user.setAvatarUrl(avatarUrl);
            user.setNickName(nickName);
            appUserMapper.insert(user);
        }
        return ResultMessage.ok(user);
    @GetMapping("/wx/login")
    @ApiOperation(value = "小程序登陆")
    public ResultMessage userLogin(@RequestParam(value = "code") String code
                        ) {
        Map<String, Object> result = userService.wxLogin(code);
        return ResultMessage.ok(result);
    }
screen-api/src/main/java/com/moral/api/entity/User.java
@@ -104,6 +104,9 @@
     */
    private String isDelete;
    private String openId;
    @TableField(exist = false)
    private Integer unitCode;
screen-api/src/main/java/com/moral/api/pojo/enums/AppAllocationStateEnum.java
New file
@@ -0,0 +1,59 @@
package com.moral.api.pojo.enums;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.moral.api.exception.BusinessException;
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum AppAllocationStateEnum implements IntegerValueEnum {
    /**
     *待处理
     */
    TO_BE_PROCESSED(20, "待处理"),
    /**
     *待完成
     */
    TO_BE_COMPLETED(30, "待完成"),
    /**
     * 40或50 都是已完成
     */
    COMPLETED(40, "已完成"),
    ;
    @EnumValue
    public  final Integer value;
    public  final String name;
    AppAllocationStateEnum(Integer value, String name) {
        this.value = value;
        this.name = name;
    }
    private static Map<Integer, AllocationExtensionApproveEnum> valueMap = new HashMap<>();
    static {
        for (AllocationExtensionApproveEnum v : AllocationExtensionApproveEnum.values()) {
            valueMap.put(v.value, v);
        }
    }
    @JsonCreator
    public static AllocationExtensionApproveEnum getByValue(Integer value) {
        if (value == null) {
            return null;
        }
        AllocationExtensionApproveEnum result = valueMap.get(value);
        if (result == null) {
            throw new BusinessException("枚举转换异常" + value);
        }
        return result;
    }
}
screen-api/src/main/java/com/moral/api/pojo/query/AppUserCond.java
New file
@@ -0,0 +1,31 @@
package com.moral.api.pojo.query;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import javax.validation.constraints.NotNull;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="AppUserCond - 小程序登陆对象", description="AppUserCond - 小程序登陆对象")
public class AppUserCond implements Serializable {
    @ApiModelProperty(value = "登陆时需要的code")
    @NotNull(message = "code不能为空!")
    private Integer code;
    @ApiModelProperty(value = "用户名")
    private String account;
    @ApiModelProperty(value = "密码")
    private  String password;
}
screen-api/src/main/java/com/moral/api/pojo/vo/allocation/AllocationFindVo.java
@@ -63,6 +63,9 @@
    @ApiModelProperty(value = "能否审批")
    private Integer isApprove;
    @ApiModelProperty(value = "状态名字")
    private String stateName;
    public Integer getResidueDay() {
        int day = this.changeDay;
        if(AllocationApproveEnum.UNDER_RECTIFICATION.value.equals(state)){
screen-api/src/main/java/com/moral/api/service/UserService.java
@@ -9,6 +9,7 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.moral.api.pojo.bo.UserBO;
import com.moral.api.pojo.query.AppUserCond;
/**
 * <p>
@@ -44,8 +45,11 @@
    //根据所传条件获取用户信息
    UserBO selectUserInfo(Map<String, Object> parameters);
    //小程序登陆
    //小程序用户密码登陆
    Map<String, Object> loginSmallRoutine(Map<String, Object> parameters);
    //小程序直接登陆
    Map<String, Object> wxLogin(String code);
}
screen-api/src/main/java/com/moral/api/service/impl/AllocationServiceImpl.java
@@ -640,7 +640,6 @@
    public List<AllocationFindVo> selectSmallRoutine(Integer state,String startTime,String endTime) {
        ArrayList<AllocationFindVo> allocationFindVos = new ArrayList<>();
        LambdaQueryWrapper<Allocation> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Allocation::getEscalationTime);
        //获取用户信息
        List<Integer> list = unitResult();
        if (!ObjectUtils.isEmpty(list)){
@@ -675,16 +674,31 @@
            wrapper.between(Allocation::getEscalationTime,startTime,endTime);
        }
        wrapper.eq(Allocation::getIsDel,0).eq(Allocation::getIsInvalid,0);
        wrapper.orderByDesc(Allocation::getEscalationTime);
        List<Allocation> allocations = allocationMapper.selectList(wrapper);
        for (Allocation allocation : allocations) {
            AllocationFindVo allocationFindVo = new AllocationFindVo();
            BeanUtils.copyProperties(allocation,allocationFindVo);
            Integer state1 = allocationFindVo.getState();
            if (integer==0 || integer==1){
                if (state1==20){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1==30){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1==40 ||state1==50){
                    allocationFindVo.setStateName(AppAllocationStateEnum.COMPLETED.name);
                }
            }else {
                if (state1==20){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1==30){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1==40||state1==50){
                    allocationFindVo.setStateName(AppAllocationStateEnum.COMPLETED.name);
                }
            }
            allocationFindVos.add(allocationFindVo);
        }
        List<AllocationFindVo> allocationFindVo = allocationMapper.selectSmallRoutine(state,startTime,endTime);
        return allocationFindVos;
    }
screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java
@@ -1,11 +1,13 @@
package com.moral.api.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -23,8 +25,10 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.entity.Group;
@@ -34,6 +38,7 @@
import com.moral.api.entity.User;
import com.moral.api.entity.UserGroup;
import com.moral.api.entity.UserLog;
import com.moral.api.exception.BusinessException;
import com.moral.api.mapper.MenuMapper;
import com.moral.api.mapper.ResponsibilityUnitMapper;
import com.moral.api.mapper.UserGroupMapper;
@@ -42,9 +47,12 @@
import com.moral.api.pojo.bo.UserBO;
import com.moral.api.service.ResponsibilityUnitService;
import com.moral.api.service.UserService;
import com.moral.api.utils.HttpClientUtil;
import com.moral.api.utils.OperationLogUtils;
import com.moral.api.utils.WechatUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.AESUtils;
import com.moral.util.ConvertUtils;
import com.moral.util.DateUtils;
@@ -52,6 +60,7 @@
import com.moral.util.RegexUtils;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
import com.sun.org.apache.regexp.internal.RE;
/**
 * <p>
@@ -93,8 +102,10 @@
    }
    @Override
    @Transactional
    public Map<String, Object> loginSmallRoutine(Map<String, Object> parameters) {
        UserBO userBo = selectUserInfo(parameters);
        String openId = parameters.get("openId").toString();
        Map<String, Object> result = new HashMap<>();
        //校验账户
        if (userBo == null) {
@@ -123,6 +134,9 @@
            result.put("msg", ResponseCodeEnum.ACCOUNT_IS_EXPIRE.getMsg());
            return result;
        }
//        JSONObject SessionKeyOpenId = WechatUtils.getSessionKeyOrOpenId(parameters.get("code").toString());
//        String openid = SessionKeyOpenId.getString("openid");
//        String sessionKey = SessionKeyOpenId.getString("session_key");
        ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(userBo.getUnitId());
        //封装用户信息
        Map<String, Object> userInfo = new LinkedHashMap<>();
@@ -136,6 +150,8 @@
        userInfo.put("wechat", userBo.getWechat());
        userInfo.put("expireTime", DateUtils.dateToDateString(userBo.getExpireTime()));
        userInfo.put("isAdmin", userBo.getIsAdmin());
        userInfo.put("openid", openId);
        try {
            //生成token,并存入redis
            String token = TokenUtils.getToken(userBo.getId().toString(), userInfo);
@@ -155,6 +171,72 @@
        userLog.setOrganizationId(userBo.getOrganizationId());
        userLog.setContent("登陆了用户:" + userBo.getAccount());
        userLogMapper.insert(userLog);
        //添加openId到user表中
        User user = userMapper.selectById(userBo.getId());
        user.setOpenId(openId);
        userMapper.updateById(user);
        return result;
    }
    @Override
    public Map<String, Object> wxLogin(String code) {
        Map<String, Object> result = new HashMap<>();
        // 用户非敏感信息:rawData
        // 签名:signature
//        JSONObject rawDataJson = JSON.parseObject(rawData);
        // 1.接收小程序发送的code
        // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
        JSONObject SessionKeyOpenId = WechatUtils.getSessionKeyOrOpenId(code);
        // 3.接收微信接口服务 获取返回的参数
        String openid = SessionKeyOpenId.getString("openid");
        String sessionKey = SessionKeyOpenId.getString("session_key");
        // 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
//        String signature2 = DigestUtils.sha1Hex(rawData + sessionKey);
//        if (!signature.equals(signature2)) {
//           return ResultMessage.ok().message("签名校验失败");
//            return ResultMessage.ok("签名校验失败");
//            throw new BusinessException("签名校验失败");
//        }
        // 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库;
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(User::getOpenId, openid);
        User user = userMapper.selectOne(lqw);
        if (user == null) {
            result.put("code", ResponseCodeEnum.USER_NOT_EXIST.getCode());
            result.put("msg", ResponseCodeEnum.USER_NOT_EXIST.getMsg());
            result.put("openId",openid);
            return result;
            // 用户信息入库
//            String nickName = rawDataJson.getString("nickName");
//            String avatarUrl = rawDataJson.getString("avatarUrl");
        }
        Map<String, Object> userInfo = new LinkedHashMap<>();
        userInfo.put("userId", user.getId());
        userInfo.put("account", user.getAccount());
        userInfo.put("userName", user.getUserName());
        userInfo.put("email", user.getEmail());
        userInfo.put("mobile", user.getMobile());
        userInfo.put("unitId",user.getUnitId());
//        userInfo.put("unName",user.getAreaName());
        userInfo.put("wechat", user.getWechat());
        userInfo.put("expireTime", DateUtils.dateToDateString(user.getExpireTime()));
        userInfo.put("isAdmin", user.getIsAdmin());
        try {
            //生成token,并存入redis
            String token = TokenUtils.getToken(user.getId().toString(), userInfo);
            result.put("token", token);
        } catch (Exception e) {
            log.error("token生成异常:" + e.getMessage());
            result.put("code", ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode());
            result.put("msg", ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg());
            return result;
        }
        result.put("code", ResponseCodeEnum.SUCCESS.getCode());
        result.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
        return result;
    }
screen-api/src/main/java/com/moral/api/utils/WechatUtils.java
@@ -3,19 +3,33 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moral.api.service.UserLogService;
public class WechatUtils {
    public static final String APPID = "yyyy";
    public static final String SECRET = "yyyy";
    public static final String APPID = "wx41f4c3c007545088";
    public static final String SECRET = "9e1a328ad525dd169252a1cc5067a6f3";
    public static JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
@@ -29,6 +43,7 @@
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }
}