Explorar el Código

微信小程序登录

wangliang hace 1 año
padre
commit
785086f3d9

+ 0 - 12
sop-api/src/main/java/com/qmth/sop/server/auth/WxappAccessTokenService.java

@@ -1,12 +0,0 @@
-package com.qmth.sop.server.auth;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.qmth.boot.core.solar.model.WxappAccessToken;
-import com.qmth.sop.business.entity.TWxappAccessToken;
-
-public interface WxappAccessTokenService extends IService<TWxappAccessToken> {
-
-    WxappAccessToken getWxappAccessToken();
-
-    void saveWxappAccessToken(String accessToken, Long expiresTime);
-}

+ 0 - 38
sop-api/src/main/java/com/qmth/sop/server/auth/impl/WxappAccessTokenServiceImpl.java

@@ -1,38 +0,0 @@
-package com.qmth.sop.server.auth.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.qmth.boot.core.solar.model.WxappAccessToken;
-import com.qmth.sop.business.entity.TWxappAccessToken;
-import com.qmth.sop.business.mapper.TWxappAccessTokenMapper;
-import com.qmth.sop.server.auth.WxappAccessTokenService;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
-@Service
-public class WxappAccessTokenServiceImpl extends ServiceImpl<TWxappAccessTokenMapper, TWxappAccessToken> implements WxappAccessTokenService {
-    private static WxappAccessToken wt;
-
-    @Override
-    public WxappAccessToken getWxappAccessToken() {
-        if (wt != null) {
-            return wt;
-        }
-        TWxappAccessToken e = this.getById(1L);
-        wt = new WxappAccessToken();
-        wt.setAccessToken(e.getAccessToken());
-        wt.setExpireTime(e.getExpiresTime());
-        return wt;
-    }
-
-    @Transactional
-    @Override
-    public void saveWxappAccessToken(String accessToken, Long expiresTime) {
-        wt.setAccessToken(accessToken);
-        wt.setExpireTime(expiresTime);
-        TWxappAccessToken e = this.getById(1L);
-        e.setAccessToken(accessToken);
-        e.setExpiresTime(expiresTime);
-        this.updateById(e);
-    }
-
-}

+ 0 - 42
sop-business/src/main/java/com/qmth/sop/business/entity/TWxappAccessToken.java

@@ -1,42 +0,0 @@
-package com.qmth.sop.business.entity;
-
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-
-import java.io.Serializable;
-
-/**
- * <p>
- * 微信token表
- * </p>
- *
- * @author wangliang
- * @since 2023-09-15
- */
-@ApiModel(value = "TWxappAccessToken对象", description = "微信token表")
-public class TWxappAccessToken implements Serializable {
-
-    private static final long serialVersionUID = 1L;
-
-    @ApiModelProperty(value = "token")
-    private String accessToken;
-
-    @ApiModelProperty(value = "过期时间")
-    private Long expiresTime;
-
-    public String getAccessToken() {
-        return accessToken;
-    }
-
-    public void setAccessToken(String accessToken) {
-        this.accessToken = accessToken;
-    }
-
-    public Long getExpiresTime() {
-        return expiresTime;
-    }
-
-    public void setExpiresTime(Long expiresTime) {
-        this.expiresTime = expiresTime;
-    }
-}

+ 16 - 0
sop-business/src/main/java/com/qmth/sop/business/entity/TWxappInfo.java

@@ -1,5 +1,8 @@
 package com.qmth.sop.business.entity;
 
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.contant.SystemConstant;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 
@@ -17,6 +20,10 @@ import java.io.Serializable;
 public class TWxappInfo implements Serializable {
     private static final long serialVersionUID = 1L;
 
+    @ApiModelProperty(value = "主键")
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long id;
+
     @ApiModelProperty(value = "openId")
     private String openId;
 
@@ -28,10 +35,19 @@ public class TWxappInfo implements Serializable {
     }
 
     public TWxappInfo(String openId, String phone) {
+        this.id = SystemConstant.getDbUuid();
         this.openId = openId;
         this.phone = phone;
     }
 
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
     public String getOpenId() {
         return openId;
     }

+ 0 - 16
sop-business/src/main/java/com/qmth/sop/business/mapper/TWxappAccessTokenMapper.java

@@ -1,16 +0,0 @@
-package com.qmth.sop.business.mapper;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.qmth.sop.business.entity.TWxappAccessToken;
-
-/**
- * <p>
- * 微信token表 Mapper 接口
- * </p>
- *
- * @author wangliang
- * @since 2023-09-15
- */
-public interface TWxappAccessTokenMapper extends BaseMapper<TWxappAccessToken> {
-
-}

+ 0 - 16
sop-business/src/main/java/com/qmth/sop/business/service/TWxappAccessTokenService.java

@@ -1,16 +0,0 @@
-package com.qmth.sop.business.service;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.qmth.sop.business.entity.TWxappAccessToken;
-
-/**
- * <p>
- * 微信token表 服务类
- * </p>
- *
- * @author wangliang
- * @since 2023-09-15
- */
-public interface TWxappAccessTokenService extends IService<TWxappAccessToken> {
-
-}

+ 1 - 1
sop-business/src/main/java/com/qmth/sop/business/service/TWxappInfoService.java

@@ -42,4 +42,4 @@ public interface TWxappInfoService extends IService<TWxappInfo> {
      * @return
      */
     LoginResult wxappLoginByEncryptedData(String loginCode, String encryptedData, String iv) throws NoSuchAlgorithmException;
-}
+}

+ 0 - 20
sop-business/src/main/java/com/qmth/sop/business/service/impl/TWxappAccessTokenServiceImpl.java

@@ -1,20 +0,0 @@
-package com.qmth.sop.business.service.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.qmth.sop.business.entity.TWxappAccessToken;
-import com.qmth.sop.business.mapper.TWxappAccessTokenMapper;
-import com.qmth.sop.business.service.TWxappAccessTokenService;
-import org.springframework.stereotype.Service;
-
-/**
- * <p>
- * 微信token表 服务实现类
- * </p>
- *
- * @author wangliang
- * @since 2023-09-15
- */
-@Service
-public class TWxappAccessTokenServiceImpl extends ServiceImpl<TWxappAccessTokenMapper, TWxappAccessToken> implements TWxappAccessTokenService {
-
-}

+ 9 - 26
sop-business/src/main/java/com/qmth/sop/business/service/impl/TWxappInfoServiceImpl.java

@@ -3,7 +3,7 @@ package com.qmth.sop.business.service.impl;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.qmth.boot.core.solar.model.WxappAccessToken;
+import com.qmth.boot.core.solar.model.WxappPhoneNumber;
 import com.qmth.boot.core.solar.model.WxappSession;
 import com.qmth.boot.core.solar.service.SolarService;
 import com.qmth.sop.business.bean.result.LoginResult;
@@ -18,8 +18,6 @@ import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.AppSourceEnum;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
 import com.qmth.sop.common.util.ByteUtil;
-import com.qmth.sop.common.util.HttpUtil;
-import com.qmth.sop.common.util.JacksonUtil;
 import com.qmth.sop.common.util.ShaUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -31,7 +29,9 @@ import javax.crypto.Cipher;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 import java.security.NoSuchAlgorithmException;
-import java.util.*;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 
 /**
  * <p>
@@ -67,7 +67,6 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
         WxappSession wxappSession = getAuthorization(loginCode);
         String openId = ByteUtil.toHexAscii(ShaUtils.encode(wxappSession.getOpenId()));
         TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openId));
-//        Optional.ofNullable(tWxappInfo).orElseThrow(() -> ParameterExceptions.OPENID_NOT_FOUND);
         Optional.ofNullable(tWxappInfo).orElseThrow(() -> ExceptionResultEnum.WXAPP_OPENID_NO_DATA.exception());
         List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, tWxappInfo.getPhone()));
         if (CollectionUtils.isEmpty(sysUserList)) {
@@ -96,26 +95,10 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
         SysConfig sysConfigWxappId = commonCacheService.addSysConfigCache(SystemConstant.WXAPP_APP_ID);
         Optional.ofNullable(sysConfigWxappId).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置微信小程序appid"));
 
-        SysConfig sysConfigWxappUrl = commonCacheService.addSysConfigCache(SystemConstant.WXAPP_PHONE_BIND_NEW_URL);
-        Optional.ofNullable(sysConfigWxappUrl).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置微信小程序手机绑定url"));
+        WxappPhoneNumber wxappPhoneNumber = solarService.getWxappPhoneNumber(sysConfigWxappId.getConfigValue(), phoneCode);
+        Optional.ofNullable(wxappPhoneNumber).orElseThrow(() -> ExceptionResultEnum.WXAPP_PHONE_NO_DATA.exception());
 
-        Map<String, Object> params = new HashMap<>();
-        params.put(SystemConstant.CODE, phoneCode);
-        String ret;
-        try {
-            WxappAccessToken wxappAccessToken = solarService.getWxappAccessToken(sysConfigWxappId.getConfigValue());
-            Optional.ofNullable(wxappAccessToken).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未查询到微信accessToken"));
-
-            ret = HttpUtil.postJson(sysConfigWxappUrl.getConfigValue() + wxappAccessToken.getAccessToken(), JacksonUtil.parseJson(params), null, null, false);
-        } catch (Exception e) {
-            throw ExceptionResultEnum.ERROR.exception("登录失败");
-        }
-        JSONObject jo = JSONObject.parseObject(ret);
-        if (Objects.nonNull(jo.getInteger(SystemConstant.ERR_CODE)) && jo.getInteger(SystemConstant.ERR_CODE).intValue() != 0) {
-            throw ExceptionResultEnum.ERROR.exception("登录失败," + jo.getString(SystemConstant.ERR_MSG));
-        }
-        String phone = jo.getJSONObject(SystemConstant.PHONE_INFO).getString(SystemConstant.PURE_PHONE_NUMBER);
-        List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, phone));
+        List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, wxappPhoneNumber.getPurePhoneNumber()));
         if (CollectionUtils.isEmpty(sysUserList)) {
             throw ExceptionResultEnum.ERROR.exception("用户不存在");
         }
@@ -129,7 +112,7 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
         WxappSession wxappSession = getAuthorization(loginCode);
         String openId = ByteUtil.toHexAscii(ShaUtils.encode(wxappSession.getOpenId()));
         TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openId));
-        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openId, phone) : tWxappInfo;
+        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openId, wxappPhoneNumber.getPurePhoneNumber()) : tWxappInfo;
         tWxappInfoService.saveOrUpdate(tWxappInfo);
         return sysUserService.login(sysUser.getPassword(), sysUser, AppSourceEnum.SYSTEM);
     }
@@ -146,7 +129,7 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
     @Transactional
     public LoginResult wxappLoginByEncryptedData(String loginCode, String encryptedData, String iv) throws NoSuchAlgorithmException {
         WxappSession wxappSession = getAuthorization(loginCode);
-        String openId = ByteUtil.toHexAscii(ShaUtils.sha256(wxappSession.getOpenId()));
+        String openId = ByteUtil.toHexAscii(ShaUtils.encode(wxappSession.getOpenId()));
         JSONObject jo = decrypt(encryptedData, iv, wxappSession.getSessionKey());
         String phone = jo.getString(SystemConstant.PURE_PHONE_NUMBER);
         List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, phone));

+ 12 - 1
sop-business/src/main/resources/db/log/wangliang_update_log.sql

@@ -1192,4 +1192,15 @@ WHERE id=121;
 
 INSERT INTO sys_role
 (id, org_id, name, enable, `type`, create_id, create_time, update_id, update_time, default_role, interpret)
-VALUES(11, NULL, '乙方总负责人', 1, 'THIRD_PMO', NULL, NULL, NULL, NULL, 1, '系统角色');
+VALUES(11, NULL, '乙方总负责人', 1, 'THIRD_PMO', NULL, NULL, NULL, NULL, 1, '系统角色');
+
+--2023.9.19update
+DROP TABLE IF EXISTS t_wxapp_access_token;
+
+ALTER TABLE t_wxapp_info DROP PRIMARY KEY;
+
+ALTER TABLE t_wxapp_info ADD id BIGINT
+    COMMENT '主键' NOT NULL first;
+
+ALTER TABLE t_wxapp_info ADD CONSTRAINT pkt_wxapp_info
+    PRIMARY KEY (id);

+ 0 - 5
sop-business/src/main/resources/mapper/TWxappAccessTokenMapper.xml

@@ -1,5 +0,0 @@
-<?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.qmth.sop.business.mapper.TWxappAccessTokenMapper">
-
-</mapper>

+ 2 - 0
sop-common/src/main/java/com/qmth/sop/common/enums/ExceptionResultEnum.java

@@ -97,6 +97,8 @@ public enum ExceptionResultEnum {
 
     WXAPP_OPENID_NO_DATA(HttpStatus.INTERNAL_SERVER_ERROR, 5000035, "微信用户未绑定"),
 
+    WXAPP_PHONE_NO_DATA(HttpStatus.INTERNAL_SERVER_ERROR, 5000036, "微信手机号获取失败"),
+
     /**
      * 401
      */