2025-09-17 16:11:59 +08:00
|
|
|
package com.cdzy.activity.controller;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.cdzy.activity.model.JsonResult;
|
2025-09-22 09:47:31 +08:00
|
|
|
import com.cdzy.activity.model.User;
|
|
|
|
|
import com.cdzy.activity.service.UserService;
|
2025-09-17 16:11:59 +08:00
|
|
|
import com.cdzy.activity.uitls.VerifyUtil;
|
2025-09-22 09:47:31 +08:00
|
|
|
import com.mybatisflex.core.query.QueryWrapper;
|
2025-09-17 16:11:59 +08:00
|
|
|
import jakarta.annotation.Resource;
|
2025-09-22 10:00:08 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2025-09-17 16:11:59 +08:00
|
|
|
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;
|
|
|
|
|
|
2025-09-22 09:47:31 +08:00
|
|
|
@Resource
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
2025-09-17 16:11:59 +08:00
|
|
|
/**
|
|
|
|
|
* 用户微信无感登录。
|
|
|
|
|
*
|
|
|
|
|
* @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")));
|
|
|
|
|
}
|
2025-09-22 09:47:31 +08:00
|
|
|
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);
|
2025-09-17 16:11:59 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 10:00:08 +08:00
|
|
|
/**
|
|
|
|
|
* 用户微信无感登录。
|
|
|
|
|
*
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 16:11:59 +08:00
|
|
|
}
|