|
@@ -9,6 +9,12 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
|
import work.baiyun.chronicdiseaseapp.model.vo.GetOpenidRequest;
|
|
import work.baiyun.chronicdiseaseapp.model.vo.GetOpenidRequest;
|
|
|
import work.baiyun.chronicdiseaseapp.service.WeChatService;
|
|
import work.baiyun.chronicdiseaseapp.service.WeChatService;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.PermissionGroup;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.UserInfoMapper;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.model.po.UserInfo;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.service.TokenService;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -19,6 +25,12 @@ public class WeChatController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private WeChatService weChatService;
|
|
private WeChatService weChatService;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private UserInfoMapper userInfoMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TokenService tokenService;
|
|
|
|
|
+
|
|
|
@RequestMapping("/")
|
|
@RequestMapping("/")
|
|
|
public R<?> hello() {
|
|
public R<?> hello() {
|
|
|
return R.success(200, "Hello from Spring backend!");
|
|
return R.success(200, "Hello from Spring backend!");
|
|
@@ -29,13 +41,42 @@ public class WeChatController {
|
|
|
if (req == null || req.getCode() == null || req.getCode().isEmpty()) {
|
|
if (req == null || req.getCode() == null || req.getCode().isEmpty()) {
|
|
|
return R.fail(400, "Missing code parameter");
|
|
return R.fail(400, "Missing code parameter");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // role is required now
|
|
|
|
|
+ if (req.getRole() == null) {
|
|
|
|
|
+ return R.fail(400, "Missing role parameter");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
String openid = weChatService.getOpenId(req.getCode());
|
|
String openid = weChatService.getOpenId(req.getCode());
|
|
|
- if (openid != null) {
|
|
|
|
|
- Map<String, String> data = new HashMap<>();
|
|
|
|
|
- data.put("openid", openid);
|
|
|
|
|
- return R.success(200, "ok", data);
|
|
|
|
|
- } else {
|
|
|
|
|
|
|
+ if (openid == null) {
|
|
|
return R.fail(500, "Failed to get openid");
|
|
return R.fail(500, "Failed to get openid");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // map incoming role int to PermissionGroup enum
|
|
|
|
|
+ PermissionGroup pg;
|
|
|
|
|
+ try {
|
|
|
|
|
+ pg = PermissionGroup.fromCode(req.getRole());
|
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
|
+ return R.fail(400, "Invalid role parameter");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查找是否存在 role + wx_openid 的用户
|
|
|
|
|
+ QueryWrapper<UserInfo> qw = new QueryWrapper<>();
|
|
|
|
|
+ qw.eq("role", pg.getCode()).eq("wx_openid", openid);
|
|
|
|
|
+ UserInfo ui = userInfoMapper.selectOne(qw);
|
|
|
|
|
+ if (ui == null) {
|
|
|
|
|
+ // create new user info with provided role and openid
|
|
|
|
|
+ ui = new UserInfo();
|
|
|
|
|
+ ui.setRole(pg);
|
|
|
|
|
+ ui.setWx_openid(openid);
|
|
|
|
|
+ userInfoMapper.insert(ui);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // generate or update token
|
|
|
|
|
+ String token = tokenService.createToken(ui.getId());
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> data = new HashMap<>();
|
|
|
|
|
+ data.put("token", token);
|
|
|
|
|
+ return R.success(200, "ok", data);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|