Procházet zdrojové kódy

微信小程序登录优化

guomengjiao před 4 měsíci
rodič
revize
f46d0dac0d

+ 15 - 3
ruoyi-api/src/main/java/com/ruoyi/api/controller/user/ApiUserLoginController.java

@@ -58,7 +58,7 @@ public class ApiUserLoginController extends AbstractApiController {
     @ApiOperation("获取小程序openId")
     @PostMapping("/user/getOpenId")
     public R<Map<String, Object>> getOpenId(@RequestBody WxUserDto dto) {
-        String openId = userService.getOpenId(dto.getCode(), dto.getAppId());
+        String openId = userService.getOpenId(dto.getCode(), dto.getClientType());
         Map<String, Object> stringObjectHashMap = new HashMap<>();
         stringObjectHashMap.put("openId", openId);
         return R.ok(stringObjectHashMap);
@@ -71,7 +71,7 @@ public class ApiUserLoginController extends AbstractApiController {
     @ApiOperation("小程序授权登录")
     @PostMapping("/user/authorization/login")
     public R<Map<String, Object>> loginAuthorization(@RequestBody  WxUserDto dto) {
-        User user = userService.loginAuthorization(dto.getOpenId(), dto.getAppId());
+        User user = userService.loginAuthorization(dto.getOpenId(), dto.getClientType());
         HashMap<String, Object> result = new HashMap<>();
         result.put("token", loginSuccess(user, false, null, dto.getClientType()));
         return R.ok(result);
@@ -83,12 +83,24 @@ public class ApiUserLoginController extends AbstractApiController {
     @ApiOperation("小程序获取手机号")
     @PostMapping("/user/phone")
     public R<Map<String, Object>> wxPhone(@RequestBody WxUserDto dto) {
-        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxUserService.getPhoneByCode(dto.getCode(), dto.getAppId());
+        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxUserService.getPhoneByCode(dto.getCode(), dto.getClientType());
         HashMap<String, Object> result = new HashMap<>();
         result.put("phone", wxMaPhoneNumberInfo.getPhoneNumber());
         return R.ok(result);
     }
 
+    /**
+     * 支付宝解密手机号
+     */
+    @ApiOperation("支付宝解密手机号")
+    @PostMapping("/user/phone/alipay")
+    public R<Map<String, Object>> alipayPhone(@RequestBody WxUserDto dto) {
+        String phone = userService.getPhoneByCode(dto.getEncryptedData());
+        HashMap<String, Object> result = new HashMap<>();
+        result.put("phone", phone);
+        return R.ok(result);
+    }
+
     /**
      * 注册用户,小程序授权且获取了手机号
      */

+ 5 - 0
ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/BusinessApplyBo.java

@@ -65,6 +65,11 @@ public class BusinessApplyBo extends BaseEntity {
      */
     @ApiModelProperty(value = "上级商家邀请码", required = true)
     private String parentBusinessCode;
+
+    /**
+     * 上级商家ID
+     */
+    private Long parentBusinessId;
     /**
      * 营业执照图片(多张)
      */

+ 5 - 0
ruoyi-business/src/main/java/com/ruoyi/business/domain/vo/BusinessApplyVo.java

@@ -354,6 +354,11 @@ public class BusinessApplyVo {
     @ApiModelProperty(value = "签名", required = true)
     private String signImage;
 
+    /**
+     * 代理审核状态
+     */
+    @ApiModelProperty(value = "代理审核状态", required = true)
+    private Integer agentAuditStatus;
     /**
      * 代理审核时间
      */

+ 2 - 6
ruoyi-business/src/main/java/com/ruoyi/business/service/impl/BusinessApplyServiceImpl.java

@@ -192,6 +192,7 @@ public class BusinessApplyServiceImpl implements IBusinessApplyService {
             if (ObjectUtil.isNull(businessVo)) {
                 throw new ServiceException(BusinessApplyExceptionEnum.PARENT_BUSINESS_NOT_EXISTS);
             }
+            bo.setParentBusinessId(businessVo.getBusinessId());
         }
         BusinessApply businessApply = baseMapper.selectOne(new LambdaQueryWrapper<BusinessApply>().eq(BusinessApply::getUserId, bo.getUserId()).last("limit 1"));
         BusinessApply saveOrUpdate;
@@ -267,15 +268,11 @@ public class BusinessApplyServiceImpl implements IBusinessApplyService {
         if (!AuditStatus.AUDIT_PASS.getCode().equals(businessApply.getAgentAuditStatus())) {
             throw new ServiceException(BusinessApplyExceptionEnum.AGENT_NOT_AUDIT_PASS);
         }
-        AgentAccountCodeVo agentAccountCodeVo = agentAccountCodeService.availableByAccountCode(bo.getAccountCode());
+        AgentAccountCodeVo agentAccountCodeVo = agentAccountCodeService.availableByAccountCode(businessApply.getAccountCode());
         if (ObjectUtil.isNull(agentAccountCodeVo)) {
             throw new ServiceException(BusinessApplyExceptionEnum.ACCOUNT_CODE_NOT_EXISTS);
         }
         Agent agent = agentService.loadById(agentAccountCodeVo.getAgentId(), true);
-        BusinessVo businessVo = businessService.queryByInviteCode(bo.getParentBusinessCode());
-        if (ObjectUtil.isNull(businessVo)) {
-            throw new ServiceException(BusinessApplyExceptionEnum.PARENT_BUSINESS_NOT_EXISTS);
-        }
         Long businessId = businessService.queryIdByUserId(businessApply.getUserId());
         if (AuditStatus.AUDIT_PASS.getCode().equals(bo.getAuditStatus())) {
             //通过
@@ -284,7 +281,6 @@ public class BusinessApplyServiceImpl implements IBusinessApplyService {
             businessBo.setRelationUserId(businessApply.getUserId());
             businessBo.setAgentId(agentAccountCodeVo.getAgentId());
             businessBo.setParentAgentId(agent.getParentAgentId());
-            businessBo.setParentBusinessId(businessVo.getBusinessId());
             businessId = businessService.insertOrUpdate(businessBo);
             //更新账户码
             AgentAccountCodeBo updateCodeBo = new AgentAccountCodeBo();

+ 61 - 0
ruoyi-common/src/main/java/com/ruoyi/common/enums/ClientType.java

@@ -0,0 +1,61 @@
+package com.ruoyi.common.enums;
+
+import com.baomidou.mybatisplus.annotation.EnumValue;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
+
+import java.util.Objects;
+
+/**
+ * 1-商城小程序
+ * 2-微信支付小程序
+ * 3-支付宝支付小程序
+ * 4-app
+ */
+public enum ClientType implements IIntegerEnum {
+
+    SHOP_MINI_APP(1, "商城小程序"),
+    WECHAT_PAY_MINI_APP(2, "微信支付小程序"),
+    ALI_PAY_MINI_APP(3, "支付宝支付小程序"),
+    APP(4, "app"),
+    ;
+
+    @EnumValue
+    @JsonValue
+    private Integer code;
+
+    private String msg;
+
+    ClientType(Integer code, String msg) {
+        this.code = code;
+        this.msg = msg;
+    }
+
+    public void setCode(Integer code) {
+        this.code = code;
+    }
+
+    @Override
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    @Override
+    public Integer getCode() {
+        return code;
+    }
+
+    @JsonCreator
+    public static ClientType getByCode(int code) {
+        for (ClientType value : ClientType.values()) {
+            if (Objects.equals(code, value.getCode())) {
+                return value;
+            }
+        }
+        return null;
+    }
+}

+ 4 - 2
ruoyi-user/src/main/java/com/ruoyi/user/service/IUserService.java

@@ -122,9 +122,9 @@ public interface IUserService {
      */
     User changeUserIntegral(User user, Integer integral, Boolean isAdd);
 
-    String getOpenId(String code, String appId);
+    String getOpenId(String code, Integer clientType);
 
-    User loginAuthorization(String openId, String appId);
+    User loginAuthorization(String openId, Integer clientType);
 
     void personalDataUpdate(PersonalDataBo bo, Long userId);
 
@@ -178,4 +178,6 @@ public interface IUserService {
     List<Long> queryIdList(UserBo userBo);
 
     String queryHeadPhotoById(Long userId);
+
+    String getPhoneByCode(String encryptedData);
 }

+ 32 - 9
ruoyi-user/src/main/java/com/ruoyi/user/service/impl/UserServiceImpl.java

@@ -11,6 +11,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.alipay.api.AlipayApiException;
 import com.alipay.api.AlipayClient;
 import com.alipay.api.DefaultAlipayClient;
+import com.alipay.api.internal.util.AlipayEncrypt;
 import com.alipay.api.request.AlipaySystemOauthTokenRequest;
 import com.alipay.api.request.AlipayUserInfoShareRequest;
 import com.alipay.api.response.AlipaySystemOauthTokenResponse;
@@ -25,6 +26,7 @@ import com.google.zxing.qrcode.QRCodeWriter;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.domain.PageQuery;
 import com.ruoyi.common.core.page.TableDataInfo;
+import com.ruoyi.common.enums.ClientType;
 import com.ruoyi.common.enums.ExceptionEnum;
 import com.ruoyi.common.enums.InviteType;
 import com.ruoyi.common.exception.ServiceException;
@@ -50,6 +52,7 @@ import com.ruoyi.weixin.config.WxMaProperties;
 import com.ruoyi.weixin.domain.WxUserDto;
 import com.ruoyi.weixin.service.WxUserService;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -67,6 +70,7 @@ import java.util.stream.Collectors;
  * @author wuchao
  * @date 2022-03-03
  */
+@Slf4j
 @RequiredArgsConstructor
 @Service
 public class UserServiceImpl implements IUserService {
@@ -248,10 +252,11 @@ public class UserServiceImpl implements IUserService {
     public User authorization(WxUserDto wxUserDto) {
         String openId = wxUserDto.getOpenId();
 
-        WxMaProperties.Config config = wxMaProperties.getConfigs().get(0);
         Integer code = UserThirdType.WX_MINI_PROGRAM.getCode();
-        if (!wxUserDto.getAppId().equals(config.getAppid())) {
+        if (ClientType.WECHAT_PAY_MINI_APP.getCode().equals(wxUserDto.getClientType())) {
             code = UserThirdType.WX_CASHIER_MINI_PROGRAM.getCode();
+        } else if (ClientType.ALI_PAY_MINI_APP.getCode().equals(wxUserDto.getClientType())) {
+            code = UserThirdType.ALI_MINI_PROGRAM.getCode();
         }
         //查询此openid是否绑定过用户
         UserThirdIdentity userThirdIdentity = userThirdIdentityMapper.selectOne(
@@ -370,20 +375,25 @@ public class UserServiceImpl implements IUserService {
     }
 
     @Override
-    public String getOpenId(String code, String appId) {
-        return  wxUserService.getOpenIdByCode(code, appId);
+    public String getOpenId(String code, Integer clientType) {
+        if (ClientType.ALI_PAY_MINI_APP.getCode().equals(clientType)) {
+            //支付宝
+            return getAlipayIdByCode(code);
+        }
+        return wxUserService.getOpenIdByCode(code, clientType);
     }
 
     @Override
-    public User loginAuthorization(String openId, String appId) {
+    public User loginAuthorization(String openId, Integer clientType) {
         if (StringUtils.isEmpty(openId)) {
             throw new ServiceException(UserExceptionEnum.USER_THIRD_ID_NOT_EXISTS);
         }
         //查询此openid是否绑定过用户
-        WxMaProperties.Config config = wxMaProperties.getConfigs().get(0);
-        Integer code = UserThirdType.WX_MINI_PROGRAM.getCode();
-        if (!appId.equals(config.getAppid())) {
-            code = UserThirdType.WX_CASHIER_MINI_PROGRAM.getCode();
+        Integer code = UserThirdType.WX_CASHIER_MINI_PROGRAM.getCode();
+        if (ClientType.ALI_PAY_MINI_APP.getCode().equals(clientType)) {
+            code = UserThirdType.ALI_MINI_PROGRAM.getCode();
+        } else if (ClientType.SHOP_MINI_APP.getCode().equals(clientType)) {
+            code = UserThirdType.WX_MINI_PROGRAM.getCode();
         }
         UserThirdIdentity userThirdIdentity = getUserThirdIdentity(code, openId, null);
         if (ObjectUtil.isNull(userThirdIdentity)) {
@@ -674,6 +684,19 @@ public class UserServiceImpl implements IUserService {
         return ObjectUtil.isNotNull(user) ? user.getHeadPhoto() : "";
     }
 
+    @Override
+    public String getPhoneByCode(String encryptedData) {
+        try {
+            String decryptContent = AlipayEncrypt.decryptContent(encryptedData, "AES", aliPayConfig.getAlipayPublicKey(), "utf-8");
+            JSONObject jsonObject = JSONObject.parseObject(decryptContent);
+            String phone = jsonObject.getString("mobile");
+            return StringUtils.isNotEmpty(phone) ? phone : jsonObject.getString("phoneNumber");
+        } catch (Exception e) {
+            log.error("支付宝解密手机号异常", e);
+        }
+        return null;
+    }
+
     private String generateUniqueMemberCode(Long userId) {
         //生成唯一码 用户不会重复 6-8位
         String userIdPart = String.valueOf(userId);

+ 0 - 3
ruoyi-weixin/ruoyi-weixin-miniapp/src/main/java/com/ruoyi/weixin/domain/WxUserDto.java

@@ -31,9 +31,6 @@ public class WxUserDto implements Serializable {
     @NotBlank(message = "openId不能为空", groups = {RegGroup.class})
     private String openId;
 
-    @NotBlank(message = "appId不能为空", groups = {RegGroup.class})
-    private String appId;
-
     //微信用户信息
     /**
      * 昵称

+ 23 - 8
ruoyi-weixin/ruoyi-weixin-miniapp/src/main/java/com/ruoyi/weixin/service/WxUserService.java

@@ -4,15 +4,19 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
+import com.ruoyi.common.enums.ClientType;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.weixin.config.WxMaConfiguration;
+import com.ruoyi.weixin.config.WxMaProperties;
 import com.ruoyi.weixin.domain.WxUserDto;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.util.List;
+
 /**
  * @author wc
  */
@@ -20,6 +24,9 @@ import org.springframework.stereotype.Service;
 @Service
 public class WxUserService {
 
+    @Resource
+    private WxMaProperties wxMaProperties;
+
     /**
      * 获取openid
      *
@@ -30,7 +37,7 @@ public class WxUserService {
         if (StringUtils.isBlank(wxUserDto.getCode())) {
             return null;
         }
-        final WxMaService wxService = WxMaConfiguration.getMaService(wxUserDto.getAppId());
+        final WxMaService wxService = WxMaConfiguration.getMaService(getAppId(wxUserDto.getClientType()));
 
         try {
             WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(wxUserDto.getCode());
@@ -53,11 +60,11 @@ public class WxUserService {
      * @param code
      * @return
      */
-    public String getOpenIdByCode(String code, String appId) {
+    public String getOpenIdByCode(String code, Integer clientType) {
         if (StringUtils.isBlank(code)) {
             return null;
         }
-        final WxMaService wxService = WxMaConfiguration.getMaService(appId);
+        final WxMaService wxService = WxMaConfiguration.getMaService(getAppId(clientType));
 
         try {
             WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
@@ -76,7 +83,7 @@ public class WxUserService {
      * @return
      */
     public WxMaUserInfo getInfo(WxUserDto dto) {
-        final WxMaService wxService = WxMaConfiguration.getMaService(dto.getAppId());
+        final WxMaService wxService = WxMaConfiguration.getMaService(getAppId(dto.getClientType()));
 
         // 用户信息校验
         if (!wxService.getUserService().checkUserInfo(dto.getSessionKey(), dto.getRawData(), dto.getSignature())) {
@@ -94,7 +101,7 @@ public class WxUserService {
      * @return
      */
     public WxMaPhoneNumberInfo getPhone(WxUserDto dto) {
-        final WxMaService wxService = WxMaConfiguration.getMaService(dto.getAppId());
+        final WxMaService wxService = WxMaConfiguration.getMaService(getAppId(dto.getClientType()));
 
         // 用户信息校验
         if (!wxService.getUserService().checkUserInfo(dto.getSessionKey(), dto.getRawData(), dto.getSignature())) {
@@ -111,8 +118,8 @@ public class WxUserService {
      * @param code
      * @return
      */
-    public WxMaPhoneNumberInfo getPhoneByCode(String code, String appId) {
-        final WxMaService wxService = WxMaConfiguration.getMaService(appId);
+    public WxMaPhoneNumberInfo getPhoneByCode(String code, Integer clientType) {
+        final WxMaService wxService = WxMaConfiguration.getMaService(getAppId(clientType));
         WxMaPhoneNumberInfo newPhoneNoInfo = null;
         try {
 //            newPhoneNoInfo = wxService.getUserService().getNewPhoneNoInfo(code);
@@ -123,4 +130,12 @@ public class WxUserService {
 
         return newPhoneNoInfo;
     }
+
+    private String getAppId(Integer clientType) {
+        List<WxMaProperties.Config> configs = wxMaProperties.getConfigs();
+        if (ClientType.WECHAT_PAY_MINI_APP.getCode().equals(clientType)) {
+            return configs.get(1).getAppid();
+        }
+        return configs.get(0).getAppid();
+    }
 }