feat: 用户接口修改
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
||||
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@@ -12,7 +13,10 @@ import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.utils.RedisUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -29,11 +33,16 @@ public class UserController {
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@GetMapping("/login")
|
||||
public Object login(@RequestParam(name = "code") String code, @RequestParam(name = "rawdata") String rawData, @RequestParam(name = "signature") String signature
|
||||
) {
|
||||
public Object login(@RequestParam(name = "code") String code,
|
||||
@RequestParam(name = "rawdata") String rawData,
|
||||
@RequestParam(name = "signature") String signature,
|
||||
@RequestParam(name = "encryptedData") String encryptedData,
|
||||
@RequestParam(name = "iv") String iv) {
|
||||
WxMaJscode2SessionResult session;
|
||||
WxMaUserInfo userInfo;
|
||||
try {
|
||||
session = wxMaService.getUserService().getSessionInfo(code);
|
||||
userInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(), encryptedData, iv);
|
||||
} catch (WxErrorException e) {
|
||||
return JSON.toJSON(Result.error(e.getMessage()));
|
||||
}
|
||||
@@ -49,6 +58,8 @@ public class UserController {
|
||||
user.setOpenid(session.getOpenid());
|
||||
user.setNickname(raw.getString("nickname"));
|
||||
user.setAvatarUrl(raw.getString("avatarUrl"));
|
||||
user.setGender(raw.getString("gender"));
|
||||
user.setAdmin(false);
|
||||
userMapper.insert(user);
|
||||
}
|
||||
map.put("token", StpUtil.getTokenValue());
|
||||
@@ -56,4 +67,14 @@ public class UserController {
|
||||
WxMaConfigHolder.remove();
|
||||
return JSON.toJSON(Result.ok(map));
|
||||
}
|
||||
|
||||
@GetMapping("/validate")
|
||||
public Object validate() {
|
||||
try {
|
||||
StpUtil.checkLogin();
|
||||
} catch (Exception e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,8 @@ public class User {
|
||||
private String openid;
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private boolean isAdmin;
|
||||
private String phoneNum;
|
||||
private String birthday;
|
||||
private String gender;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user