Your Name 4 年 前
コミット
e8966c8e43
共有1 個のファイルを変更した37 個の追加3 個の削除を含む
  1. 37
    3
      src/main/java/com/shigongli/controller/LoginController.java

+ 37
- 3
src/main/java/com/shigongli/controller/LoginController.java ファイルの表示

@@ -1,18 +1,21 @@
1 1
 package com.shigongli.controller;
2 2
 
3
+import cn.binarywang.wx.miniapp.api.WxMaService;
4
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
3 5
 import com.shigongli.common.*;
6
+import com.shigongli.constants.StatusConstant;
4 7
 import com.shigongli.entity.SysUser;
8
+import com.shigongli.entity.TaPerson;
5 9
 import com.shigongli.service.ISysUserService;
10
+import com.shigongli.service.ITaPersonService;
6 11
 import me.chanjar.weixin.mp.api.WxMpService;
7 12
 import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
8 13
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
9 14
 import org.springframework.beans.factory.annotation.Autowired;
10
-import com.shigongli.vo.LoginParam;
11 15
 import io.swagger.annotations.Api;
12 16
 import io.swagger.annotations.ApiOperation;
13 17
 import io.swagger.annotations.ApiParam;
14 18
 import org.springframework.web.bind.annotation.PostMapping;
15
-import org.springframework.web.bind.annotation.RequestBody;
16 19
 import org.springframework.web.bind.annotation.RequestParam;
17 20
 import org.springframework.web.bind.annotation.RestController;
18 21
 
@@ -27,6 +30,9 @@ public class LoginController extends BaseController {
27 30
     @Autowired
28 31
     ISysUserService iSysUserService;
29 32
 
33
+    @Autowired
34
+    ITaPersonService iTaPersonService;
35
+
30 36
     @Autowired
31 37
     WxUtils wxUtils;
32 38
 
@@ -38,7 +44,7 @@ public class LoginController extends BaseController {
38 44
      */
39 45
     @PostMapping("/mp/login")
40 46
     @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
41
-    public ResponseBean login(@ApiParam("登录参数") @RequestParam("code") String code) throws Exception {
47
+    public ResponseBean mpLogin(@ApiParam("登录参数") @RequestParam("code") String code) throws Exception {
42 48
         WxMpService mpService = wxUtils.getMpService();
43 49
         WxMpOAuth2AccessToken wxMpOAuth2AccessToken = mpService.oauth2getAccessToken(code);
44 50
         WxMpUser wxMpUser = mpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
@@ -63,4 +69,32 @@ public class LoginController extends BaseController {
63 69
 
64 70
         return ResponseBean.success(sysUser);
65 71
     }
72
+
73
+    /**
74
+     * 小程序登录
75
+     * @param code
76
+     * @return
77
+     * @throws Exception
78
+     */
79
+    @PostMapping("/ma/login")
80
+    @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
81
+    public ResponseBean maLogin(@ApiParam("登录参数") @RequestParam("code") String code) throws Exception {
82
+        WxMaService maService = wxUtils.getMaService();
83
+        WxMaJscode2SessionResult sessionInfo = maService.getUserService().getSessionInfo(code);
84
+
85
+        TaPerson taPerson = iTaPersonService.getByOpenId(sessionInfo.getOpenid());
86
+        if (null == taPerson || StatusConstant.DELETE.equals(taPerson.getStatus())) {
87
+            taPerson = new TaPerson();
88
+            taPerson.setOpenid(sessionInfo.getOpenid());
89
+            iTaPersonService.save(taPerson);
90
+        }
91
+
92
+        // 生成 token
93
+        Map<String, Object> claims = new HashMap<>();
94
+        claims.put("personId", taPerson.getPersonId());
95
+        String jws = JWTUtils.encode(claims);
96
+        taPerson.setToken(jws);
97
+
98
+        return ResponseBean.success(taPerson);
99
+    }
66 100
 }