Browse Source

add PC扫码登录

he2802 3 năm trước cách đây
mục cha
commit
4fba710d33

+ 17 - 1
zhongzheng-api/src/main/java/com/zhongzheng/controller/wx/WxLoginController.java

@@ -82,7 +82,7 @@ public class WxLoginController
 
     @ApiOperation("小程序校验PC登录二维码")
     @PostMapping("/scan_login_check")
-    public AjaxResult scan_login_check(@RequestParam("scanCode") String scanCode)
+    public AjaxResult scanLoginCheck(@RequestParam("scanCode") String scanCode)
     {
         ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
         String key = "SCAN_LOGIN_"+scanCode;
@@ -93,9 +93,25 @@ public class WxLoginController
         }else{
             return AjaxResult.error("校验码不存在或已过期 ");
         }
+    }
 
+    @ApiOperation("PC获取登录二维码地址")
+    @GetMapping("/pc_login_url")
+    public AjaxResult pcLoginUrl()
+    {
+        String url = wxLoginService.pcLoginUrl();
+        return AjaxResult.success(url);
     }
 
+    @ApiOperation("PC检查小程序是否扫码登录")
+    @GetMapping("/check_pc_login")
+    public AjaxResult checkPcLogin(@RequestParam("scanCode") String scanCode)
+    {
+        Map<String,Object> map = wxLoginService.checkPcLogin(scanCode);
+        return AjaxResult.success(map);
+    }
+
+
     /**
      * 公众号登录方法
      *

+ 2 - 0
zhongzheng-api/src/main/resources/application-dev.yml

@@ -109,6 +109,8 @@ wx:
         key: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         appsecret: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         notifyUrl: http://42.192.164.187:19005/wx/pay/callback
+    scanLogin:
+        host: https://testm.xyyxt.net/
 
 certificate:
     host: http://192.168.1.38:8000/

+ 2 - 0
zhongzheng-api/src/main/resources/application-pre.yml

@@ -109,6 +109,8 @@ wx:
         key: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         appsecret: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         notifyUrl: http://120.79.166.78:19009/wx/pay/callback
+    scanLogin:
+        host: https://testm.xyyxt.net/
 
 certificate:
     host: http://192.168.1.38:8000/

+ 2 - 0
zhongzheng-api/src/main/resources/application-prod.yml

@@ -121,6 +121,8 @@ wx:
         key: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         appsecret: GdXyPxYjZx1234123yJzXgDxYpXyjpx9
         notifyUrl: https://api.xyyxt.net/wx/pay/callback
+    scanLogin:
+        host: https://m.xyyxt.net/
 
 certificate:
     host: https://m.xyyxt.net/

+ 12 - 0
zhongzheng-common/src/main/java/com/zhongzheng/common/utils/ToolsUtils.java

@@ -139,4 +139,16 @@ public class ToolsUtils {
         }
     }
 
+    public static String getCharAndNumr(int length) {
+        Random random = new Random();
+        StringBuffer valSb = new StringBuffer();
+        String charStr = "0123456789abcdefghijklmnopqrstuvwxyz";
+        int charLength = charStr.length();
+        for (int i = 0; i < length; i++) {
+            int index = random.nextInt(charLength);
+            valSb.append(charStr.charAt(index));
+        }
+        return valSb.toString();
+    }
+
 }

+ 42 - 0
zhongzheng-framework/src/main/java/com/zhongzheng/framework/web/service/WxLoginService.java

@@ -7,12 +7,14 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.zhongzheng.common.constant.Constants;
+import com.zhongzheng.common.core.redis.RedisCache;
 import com.zhongzheng.common.exception.CustomException;
 
 import com.zhongzheng.common.utils.*;
 import com.zhongzheng.common.utils.http.HttpUtils;
 import com.zhongzheng.common.utils.ip.IpUtils;
 import com.zhongzheng.modules.alisms.service.IAliSmsService;
+import com.zhongzheng.modules.course.domain.CourseMenu;
 import com.zhongzheng.modules.course.mapper.CourseSectionMapper;
 import com.zhongzheng.modules.user.bo.UserAddBo;
 import com.zhongzheng.modules.user.domain.User;
@@ -33,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.annotation.Resource;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 登录校验方法
@@ -59,6 +62,9 @@ public class WxLoginService
     @Value("${wx.gzh.appid}")
     private String gzh_appid;
 
+    @Value("${wx.scanLogin.host}")
+    private String scanLoginHost;
+
     @Value("${wx.gzh.appsecret}")
     private String gzh_appsrcret;
 
@@ -79,6 +85,9 @@ public class WxLoginService
     @Autowired
     private IAliSmsService iSmsService;
 
+    @Autowired
+    private RedisCache redisCache;
+
     @Autowired
     private UserSchoolInfoMapper userSchoolInfoMapper;
 
@@ -94,6 +103,39 @@ public class WxLoginService
         return map;
     }
 
+    public String pcLoginUrl() {
+        String scanCode = ToolsUtils.getCharAndNumr(6);
+        String key = "SCAN_LOGIN_"+scanCode;
+        redisCache.setCacheObject(key, -1,30, TimeUnit.SECONDS);//30秒锁定
+        return  scanLoginHost+"pc/login/"+scanCode;
+    }
+
+    /**
+     * PC检查是否小程序登录成功
+     * @param scanCode
+     * @return
+     */
+    public Map<String,Object> checkPcLogin(String scanCode) {
+        String key = "SCAN_LOGIN_"+scanCode;
+        Long userId = redisCache.getCacheObject(key);
+        if(Validator.isNotEmpty(userId)&&userId.longValue()>0L){
+            User user = iUserService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId, userId).last("limit 1"));
+            if(Validator.isEmpty(user)){
+                throw new CustomException("暂未登录");
+            }
+            ClientLoginUser loginUser = new ClientLoginUser();
+            loginUser.setUser(user);
+            Map<String,Object> map = new HashMap<>();
+            map.put(Constants.TOKEN,wxTokenService.createToken(loginUser));
+            map.put("user_account",loginUser.getUser().getUserAccount());
+            map.put("full_info",Validator.isEmpty(user.getIdCard())?false:true); //是否完善身份信息
+            return map;
+        }
+        else{
+            throw new CustomException("暂未登录");
+        }
+    }
+
     @Transactional(rollbackFor = Exception.class)
     public Map<String,Object> login(WxLoginBody loginBody) {
         User user = getWxUnionIdUser(loginBody);