Browse Source

[feat] 增加小程序登录、门户登录短信验证等代码

limeng 5 months ago
parent
commit
0552b98da8

+ 6 - 0
application-webadmin/pom.xml

@@ -98,6 +98,12 @@
             <version>3.3.3</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>wx-java-miniapp-spring-boot-starter</artifactId>  <!-- 微信登录(小程序) -->
+            <version>4.6.0</version>
+        </dependency>
+
         <!--        <dependency>-->
 <!--            <groupId>com.anji-plus</groupId>-->
 <!--            <artifactId>spring-boot-starter-captcha</artifactId>-->

+ 32 - 10
application-webadmin/src/main/java/com/tourism/webadmin/app/website/controller/LoginToWebsiteController.java

@@ -1,5 +1,8 @@
 package com.tourism.webadmin.app.website.controller;
 
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.WxMaUserService;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.dev33.satoken.annotation.SaIgnore;
 import cn.dev33.satoken.session.SaSession;
 import cn.dev33.satoken.stp.StpUtil;
@@ -46,6 +49,7 @@ import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import jakarta.validation.Valid;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang3.StringUtils;
 import org.dromara.sms4j.api.entity.SmsResponse;
 import org.dromara.sms4j.core.factory.SmsFactory;
@@ -96,6 +100,8 @@ public class LoginToWebsiteController {
     private SessionCacheHelper cacheHelper;
     @Autowired
     private UpDownloaderFactory upDownloaderFactory;
+    @Autowired
+    private WxMaService wxMaService;
 
     private static final String SHOW_NAME_FIELD = "showName";
     private static final String HEAD_IMAGE_URL_FIELD = "headImageUrl";
@@ -201,7 +207,8 @@ public class LoginToWebsiteController {
     @PostMapping("/doLoginBySms")
     public ResponseResult<JSONObject> doLoginBySms(
             @MyRequestBody String loginMoblie,
-            @MyRequestBody String smsCode) throws UnsupportedEncodingException
+            @MyRequestBody String smsCode,
+            @MyRequestBody String countryCode) throws UnsupportedEncodingException
     {
         // 校验短信验证码
         String smsCodeKey = CacheConstants.getSmsCodeKey(loginMoblie);
@@ -220,6 +227,8 @@ public class LoginToWebsiteController {
             tourUser.setLoginName(loginMoblie);
             tourUser.setUserStatus(TourUserStatus.STATUS_NORMAL);
             tourUser.setMobile(loginMoblie);
+            tourUser.setCountryCode(StringUtils.isBlank(countryCode)? "86" : countryCode);
+            tourUser.setPassword(passwordEncoder.encode("123456"));
             tourUser = tourUserService.saveNew(tourUser);
         }
         // 生成token
@@ -228,18 +237,31 @@ public class LoginToWebsiteController {
     }
 
     /**
-     * 小程序登录,给出AK、SK、解析密钥、登录
+     * 小程序根据phoneCode登录
      */
     @SaIgnore
     @OperationLog(type = SysOperationLogType.LOGIN, saveResponse = false)
-    @PostMapping("/doLoginByWxMini")
-    public ResponseResult<JSONObject> doLoginByWxMini(
-            @MyRequestBody String code,
-            @MyRequestBody String encryptedData,
-            @MyRequestBody String iv) throws UnsupportedEncodingException {
-
-//        return loginToWebsiteService.doLoginByWxMini(code, encryptedData, iv);
-        return null;
+    @GetMapping("/doLoginByWxMini")
+    public ResponseResult<JSONObject> doLoginByWxMini(String phoneCode) throws WxErrorException {
+        log.info("小程序获取手机号code:" + phoneCode);
+        WxMaUserService userService = wxMaService.getUserService();
+        WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(phoneCode);
+
+        log.info("小程序获取手机号信息:" + phoneNoInfo);
+        TourUser tourUser = tourUserService.getOne(Wrappers.<TourUser>lambdaQuery().eq(TourUser::getMobile, phoneNoInfo.getPhoneNumber()));
+        // 如果用户信息不存在,则新建
+        if(tourUser == null){
+            tourUser = new TourUser();
+            tourUser.setLoginName(phoneNoInfo.getPhoneNumber());
+            tourUser.setUserStatus(TourUserStatus.STATUS_NORMAL);
+            tourUser.setMobile(phoneNoInfo.getPhoneNumber());
+            tourUser.setPassword(passwordEncoder.encode("123456"));
+            tourUser.setCountryCode(phoneNoInfo.getCountryCode());
+            tourUser = tourUserService.saveNew(tourUser);
+        }
+        // 生成token
+        JSONObject jsonObject = this.buildTourLoginDataAndLogin(tourUser);
+        return ResponseResult.success(jsonObject);
     }
 
 

+ 4 - 0
application-webadmin/src/main/java/com/tourism/webadmin/back/dto/TourUserDto.java

@@ -100,4 +100,8 @@ public class TourUserDto {
     @Schema(description = "LIKE模糊搜索字符串")
     private String searchString;
 
+    /**
+     * 国家区号。
+     */
+    private String countryCode;
 }

+ 6 - 0
application-webadmin/src/main/java/com/tourism/webadmin/back/model/TourUser.java

@@ -85,6 +85,12 @@ public class TourUser extends BaseModel {
     private Date birthday;
 
     /**
+     * 国家代码。
+     */
+    @TableField(value = "country_code")
+    private String countryCode;
+
+    /**
      * 逻辑删除标记字段(1: 正常 -1: 已删除)。
      */
     @TableLogic

+ 6 - 0
application-webadmin/src/main/java/com/tourism/webadmin/back/vo/TourUserVo.java

@@ -90,4 +90,10 @@ public class TourUserVo extends BaseVo {
      */
     @Schema(description = "sex 全局字典关联数据")
     private Map<String, Object> sexDictMap;
+
+    /**
+     *  国家代码
+     */
+    @Schema(description = "国家代码")
+    private String countryCode;
 }

+ 26 - 0
application-webadmin/src/main/resources/application-dev.yml

@@ -205,3 +205,29 @@ sa-token:
     timeout: 10s
   is-read-header: true
   is-read-cookie: false
+sms:
+  config-type: yaml
+  blends:
+    huawei:
+      #华为短信appKey
+      accessKeyId: 7aUa9993Ggq76rRK0Bb35vZb5NCq
+      #华为短信appSecret
+      accessKeySecret: vk2BGZr08FHjkwbBAGjhO4qKwTbM
+      #短信签名
+      signature: 华为云短信测试
+      #通道号
+      sender: 1069368924410007823
+      #模板ID 如果使用自定义模板发送方法可不设定
+      template-id: c4ef7de4affd4f1ca4c71cf7e62a024e
+      #华为回调地址,如不需要可不设置或为空
+      statusCallBack:
+      #华为分配的app请求地址
+      url: https://smsapi.cn-north-4.myhuaweicloud.com:443
+wx:
+  miniapp: # 小程序配置(必填),参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-miniapp-spring-boot-starter/README.md 文档
+    appid: wx2d0bc678042f0817
+    secret: e7963a0242de3ea651578874a12e33c7
+    config-storage:
+      type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
+      key-prefix: wa # Redis Key 的前缀
+      http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台

+ 28 - 2
application-webadmin/src/main/resources/application-prod.yml

@@ -69,7 +69,7 @@ application:
   sessionExpiredSeconds: 86400
   # 是否排他登录。
   excludeLogin: false
-  hostIpPort: 1.94.207.143:8082
+  hostIpPort: https://www.xiaoyaotravel.com/api
 
 # 这里仅仅是一个第三方配置的示例,如果没有接入斯三方系统,
 # 这里的配置项也不会影响到系统的行为,如果觉得多余,也可以手动删除。
@@ -204,4 +204,30 @@ sa-token:
     # 连接超时时间
     timeout: 10s
   is-read-header: true
-  is-read-cookie: false
+  is-read-cookie: false
+sms:
+  config-type: yaml
+  blends:
+    huawei:
+      #华为短信appKey
+      accessKeyId: 7aUa9993Ggq76rRK0Bb35vZb5NCq
+      #华为短信appSecret
+      accessKeySecret: vk2BGZr08FHjkwbBAGjhO4qKwTbM
+      #短信签名
+      signature: 华为云短信测试
+      #通道号
+      sender: 1069368924410007823
+      #模板ID 如果使用自定义模板发送方法可不设定
+      template-id: c4ef7de4affd4f1ca4c71cf7e62a024e
+      #华为回调地址,如不需要可不设置或为空
+      statusCallBack:
+      #华为分配的app请求地址
+      url: https://smsapi.cn-north-4.myhuaweicloud.com:443
+wx:
+  miniapp: # 小程序配置(必填),参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-miniapp-spring-boot-starter/README.md 文档
+    appid: wx63c280fe3248a3e7
+    secret: 6f270509224a7ae1296bbf1c8cb97aed
+    config-storage:
+      type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
+      key-prefix: wa # Redis Key 的前缀
+      http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台