2025-09-22 10:00:08 +08:00

73 lines
2.2 KiB
Java

package com.cdzy.activity.controller;
import com.alibaba.fastjson2.JSONObject;
import com.cdzy.activity.model.JsonResult;
import com.cdzy.activity.model.User;
import com.cdzy.activity.service.UserService;
import com.cdzy.activity.uitls.VerifyUtil;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
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;
/**
* 微信用户控制层。
*
* @author attiya
* @since 2025-09-17
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private VerifyUtil verifyUtil;
@Resource
private UserService userService;
/**
* 用户微信无感登录。
*
* @param code 微信登录返回的code
* @return 登陆结果
*/
@RequestMapping("login")
public JsonResult<?> silentLogin(@RequestParam(name ="js_code") String code) {
JSONObject result = verifyUtil.wechatAuthority(code);
if (result == null) {
return JsonResult.failed("微信登录失败");
}
if (result.containsKey("errcode")) {
return JsonResult.failed(String.format("微信登录失败 %s", result.getString("errmsg")));
}
String openid = result.getString("openid");
QueryWrapper queryWrapper = QueryWrapper.create()
.eq(User::getWxOpenId, openid);
User user = userService.getOne(queryWrapper);
if (user == null) {
user = new User();
user.setWxOpenId(openid);
userService.save(user);
}
return JsonResult.success("微信登录成功", openid);
}
/**
* 用户微信无感登录。
*
* @param openId 微信登陆openId
* @return 登陆结果
*/
@GetMapping("info")
public JsonResult<?> info(@RequestParam("openId")String openId) {
QueryWrapper queryWrapper = QueryWrapper.create()
.eq(User::getWxOpenId, openId);
User user = userService.getOne(queryWrapper);
return JsonResult.success(user);
}
}