Ver Fonte

微信授权登录

chao há 4 anos atrás
pai
commit
9217677f35

+ 23 - 0
src/main/java/com/caimei/www/controller/unlimited/WeChatController.java

@@ -0,0 +1,23 @@
+package com.caimei.www.controller.unlimited;
+
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/9/27
+ */
+public class WeChatController {
+	private static final String REDIRECT_URI = "we_chat/redirect_uri";
+    /**
+     * 微信授权页面路径
+     * @param model
+     * @return
+     */
+    @GetMapping("/we_chat/redirect_uri.html")
+    public String redirectUri(final Model model) {
+        return REDIRECT_URI;
+    }
+}

+ 13 - 0
src/main/resources/static/js/base.js

@@ -1,5 +1,18 @@
 var spiServer = $("#spiServer").val();
 var isPC = ($(window).width()>768);
+var userAgent = navigator.userAgent.toLowerCase();
+var isWeChatBrowser = (userAgent.match(/MicroMessenger/i) === 'micromessenger');
+if (isWeChatBrowser) {
+    // 微信浏览器自动授权登录
+    var urlForCurrent = window.location.href;
+    var urlForWeChat = 'https://www.caimei365.com/we_chat/redirect_uri.html';
+    $.get(spiServer+'/user/authorizationLink?redirectUri='+urlForWeChat, function(r){
+        if(r.code===0 && r.data){
+            sessionStorage.setItem("urlForCurrent", urlForCurrent);
+            window.location.href = r.data;
+        }
+    });
+}
 var globalUserData = '';
 var GLOBAL_TOKEN = 'X-Token';
 var GLOBAL_USER_ID = 0;

+ 23 - 0
src/main/resources/templates/we_chat/redirect_uri.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html lang="zh-CN" xmlns:th="https://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+      xsi:schemaLocation="https://www.thymeleaf.org ">
+<head>
+    <meta charset="UTF-8">
+    <title>微信授权</title>
+</head>
+<body>
+<template th:replace="components/foot-link"></template>
+<script charset="utf-8" type="text/javascript">
+    // 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE
+    var wx_code = getUrlParam('code');
+    var wx_state = getUrlParam('state');
+    $.get(spiServer+'/user/authorizationLogin?code='+wx_code+'&state='+wx_state, function(r){
+        if(r.code===0){
+            window.location.href = sessionStorage.getItem("urlForCurrent");
+        }else{
+            alert(r.msg);
+        }
+    });
+</script>
+</body>
+</html>