Parcourir la source

解決:微信和用户账号绑定关系不是一对一

pull/385/head
suqin il y a 10 mois
Parent
révision
85278c0884
7 fichiers modifiés avec 124 ajouts et 49 suppressions
  1. +19
    -2
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java
  2. +0
    -5
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/query/InspectionQuery.java
  3. +3
    -1
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/enums/UserCodeEnum.java
  4. +14
    -1
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java
  5. +12
    -24
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/InspectionServiceImpl.java
  6. +53
    -16
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java
  7. +23
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/vo/WxAuthorizeVo.java

+ 19
- 2
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java Voir le fichier

@@ -20,11 +20,11 @@ public class WxController {
private IWxService iWxService;

//根据code获取openId
@GetMapping("/getOpenId/{code}")
/* @GetMapping("/getOpenId/{code}")
public JsonResult getOpenId(@PathVariable("code") String code) {
log.info("进入获取openId接口");
return iWxService.openid(code);
}
}*/

@PostMapping("/send/message")
public JsonResult sendMessage(@RequestBody WxMessageRequest wxMessageRequest) {
@@ -32,4 +32,21 @@ public class WxController {
return iWxService.sendMessage(wxMessageRequest);
}

//根据code获取openId且判断用户是否授权
@GetMapping("/getOpenId/{code}")
public JsonResult checkAuthorize(@PathVariable("code") String code) {
log.info("进入获取openId接口");
return iWxService.checkAuthorize(code);
}

/**
* 微信授权
* @param openId
* @return
*/
@GetMapping("/bind/{openId}")
public JsonResult bind(@PathVariable("openId") String openId) {
log.info("进入获取openId接口");
return iWxService.bind(openId);
}
}

+ 0
- 5
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/query/InspectionQuery.java Voir le fichier

@@ -66,11 +66,6 @@ public class InspectionQuery extends BaseQuery {
*/
private String time;

/**
* 结束日期(传入)
*/
// private String endTime;

/**
* 日期,日期类型
*/

+ 3
- 1
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/enums/UserCodeEnum.java Voir le fichier

@@ -14,7 +14,9 @@ public enum UserCodeEnum {
USER_NAME_IS_NULL(1240102, "登录用户名为空"),
OLD_PASSWORD_IS_ERROR(1240103, "密码错误"),
PASSWORD_REVISE_FAIL(1240104, "密码修改失败"),
PASSWORD_REVISE_SUCCESS(1240105, "密码修改成功");
PASSWORD_REVISE_SUCCESS(1240105, "密码修改成功"),
USER_ID_IS_NULL(1240106, "登录用户id为空"),
USER_0PENID_IS_NULL(1240107, "用户openID为空");

/**
* 错误码

+ 14
- 1
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java Voir le fichier

@@ -14,7 +14,7 @@ public interface IWxService {
* @param code
* @return
*/
JsonResult openid(String code);
// JsonResult openid(String code);

/**
* 发送消息
@@ -23,4 +23,17 @@ public interface IWxService {
*/
JsonResult sendMessage(WxMessageRequest wxMessageRequest);

/**
* 根据code获取openId且判断用户是否授权
* @param code
* @return
*/
JsonResult checkAuthorize(String code);

/**
* 微信授权
* @param openId
* @return
*/
JsonResult bind(String openId);
}

+ 12
- 24
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/InspectionServiceImpl.java Voir le fichier

@@ -74,34 +74,24 @@ public class InspectionServiceImpl implements IInspectionService {
if (null == query.getLimit() && null == query.getPage()) {
throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
}
//登录用户信息
User user = CurrentUserUtil.getUserInfo();
Integer type = user.getDataPermission();
if (null == type) {
throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
}
String tenantId = user.getTenantId();
query.setTenantId(tenantId);

//初始部门id
String deptIdInt = query.getDeptId();
//开始、结束时间
if (StringUtils.isNotEmpty(query.getTime())) {
Date startTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, query.getTime() + " 00:00:00");
Date endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, query.getTime() + " 23:59:59");
query.setStartTimeDate(startTime);
query.setEndTimeDate(endTime);
}

//获取当前部门对应的巡检任务
//获取分页数据
IPage<Inspection> page = new Page<>(query.getPage(), query.getLimit());
IPage<InspectionInfoVo> pageDataVo = new Page<>(query.getPage(), query.getLimit());
Date startTime = null;
Date endTime = null;
if (StringUtils.isNotEmpty(query.getTime())) {
startTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, query.getTime() + " 00:00:00");
endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, query.getTime() + " 23:59:59");
}
query.setStartTimeDate(startTime);
query.setEndTimeDate(endTime);

List<InspectionInfoVo> inspectionInfoVoList = new ArrayList<>();
//用户角色判断 1超级管理员 2部门管理员 3普通用户
if (DataPermissionEnum.ALL.getCode() == type) {
if (DataPermissionEnum.ALL.getCode() == user.getDataPermission()) {
query.setDeptId(""); // 要查所有部门的, 因此不通过deptId过滤
IPage<InspectionInfoVo> IPageData = inspectionMapper.queryPage(page, query);
IPageData.getRecords().stream().forEach(x -> {
@@ -129,16 +119,14 @@ public class InspectionServiceImpl implements IInspectionService {
for (InspectionInfoVo record : records) {
inspectionInfoVoList.add(record);
}
//构建返回结果对象
pageDataVo.setPages(IPageData.getPages());
pageDataVo.setCurrent(IPageData.getCurrent());
pageDataVo.setSize(IPageData.getSize());
pageDataVo.setTotal(IPageData.getTotal());


}
//登录用户角色为部门管理员或普通用户
if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == type || DataPermissionEnum.DEPT.getCode() == type) {
if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission() || DataPermissionEnum.DEPT.getCode() == user.getDataPermission()) {
// 根据部门id获取此部门id和所有下级部门id封装list集合
List<String> deptLists = deptMapper.selectAllChildListById(query.getDeptId());
if (StringUtils.isEmpty(deptLists)) {
@@ -175,11 +163,11 @@ public class InspectionServiceImpl implements IInspectionService {
inspectionInfoVoList.add(record);
}

//构建返回结果对象
pageDataVo.setPages(pageData.getPages());
pageDataVo.setCurrent(pageData.getCurrent());
pageDataVo.setSize(pageData.getSize());
pageDataVo.setTotal(pageData.getTotal());

}
}
Map<Integer, AirportLineVo> airportLineMap = this.getAirportLineMap(inspectionInfoVoList);

+ 53
- 16
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java Voir le fichier

@@ -20,6 +20,7 @@ import com.tuoheng.miniprogram.param.WxSendMessageParam;
import com.tuoheng.miniprogram.service.IWxService;
import com.tuoheng.miniprogram.utils.CurrentUserUtil;
import com.tuoheng.miniprogram.utils.GetOpenIdUtil;
import com.tuoheng.miniprogram.vo.WxAuthorizeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
@@ -58,7 +59,7 @@ public class WxServiceImpl implements IWxService {
* @return
*/
@Override
public JsonResult openid(String code) {
public JsonResult checkAuthorize(String code) {
String userId = CurrentUserUtil.getUserId();
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret);
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") ||
@@ -71,28 +72,64 @@ public class WxServiceImpl implements IWxService {
log.error("获取openid失败:", openidResult);
return JsonResult.error("获取openid失败");
}
//根据当前登录用户id查询授权表是否存在一条授权数据

//默认为false未授权(对应标识1 弹出授权页面给用户)
boolean flag = false;
//根据当前登录用户id和vx查询授权表是否存在一条授权数据
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery()
.eq(UserAuthorize::getUserId, userId)
.eq(UserAuthorize::getOpenId,openid)
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode()));
if (ObjectUtil.isNull(beforeUserAuthorize)) {
UserAuthorize userAuthorize = new UserAuthorize();
if (StringUtils.isNotEmpty(openid)) {
//查询出来openId数据入库
userAuthorize.setOpenId(openid);
}
userAuthorize.setUserId(userId);
userAuthorize.setCreateTime(DateUtils.now());
if (StringUtils.isNotEmpty(userId)) {
userAuthorize.setCreateUser(userId);

//有: 该vx和当前账号绑定过 置为true (对应标识2)
if (!ObjectUtil.isNull(beforeUserAuthorize)) {
flag = true;
}
int result = userAuthorizeMapper.insert(userAuthorize);

if (result <= 0) {
return JsonResult.error("授权数据入库失败");
WxAuthorizeVo wxAuthorizeVo = new WxAuthorizeVo();
wxAuthorizeVo.setIsAuthroize(flag);
wxAuthorizeVo.setOpenId(openid);
return JsonResult.success(wxAuthorizeVo);
}

@Override
public JsonResult bind(String openId) {
String userId = CurrentUserUtil.getUserId();
if(StringUtils.isEmpty(openId)){
return JsonResult.error(UserCodeEnum.USER_ID_IS_NULL.getCode(), UserCodeEnum.USER_ID_IS_NULL.getMsg());
}
if(StringUtils.isEmpty(userId)){
return JsonResult.error(UserCodeEnum.USER_0PENID_IS_NULL.getCode(), UserCodeEnum.USER_0PENID_IS_NULL.getMsg());
}
//openId入库
//根据当前登录用户id查询授权表是否存在一条授权数据
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery()
.eq(UserAuthorize::getUserId, userId)
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode()));
if (ObjectUtil.isNotNull(beforeUserAuthorize)) {

beforeUserAuthorize.setOpenId(openId);
beforeUserAuthorize.setUserId(userId);
beforeUserAuthorize.setUpdateUser(userId);
beforeUserAuthorize.setUpdateTime(DateUtils.now());
int count = userAuthorizeMapper.updateById(beforeUserAuthorize);
if (count <= 0) {
return JsonResult.error("用户更新绑定微信账号失败");
}
return JsonResult.success();
}

UserAuthorize userAuthorize = new UserAuthorize();
userAuthorize.setOpenId(openId);
userAuthorize.setUserId(userId);
userAuthorize.setCreateUser(userId);
userAuthorize.setCreateTime(DateUtils.now());
int result = userAuthorizeMapper.insert(userAuthorize);

if (result <= 0) {
return JsonResult.error("授权数据入库失败");
}
//关联登录用户,已经做过授权 修改授权标识
//修改authorize为已授权到用户表中
User user = CurrentUserUtil.getUserInfo();
if (ObjectUtil.isEmpty(user)) {
return JsonResult.error(UserCodeEnum.USER_IS_NULL.getCode(), UserCodeEnum.USER_IS_NULL.getMsg());

+ 23
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/vo/WxAuthorizeVo.java Voir le fichier

@@ -0,0 +1,23 @@
package com.tuoheng.miniprogram.vo;


import lombok.Data;

/**
* 微信授权登录判断类
*
* @Author suqin
* @Date 2023/01/09
*/
@Data
public class WxAuthorizeVo {
/**
* 是否授权 true授权过 false未授权
*/
private Boolean isAuthroize;

/**
* 微信唯一用户标识
*/
private String openId;
}

Chargement…
Annuler
Enregistrer