|
|
|
|
@ -0,0 +1,53 @@
|
|
|
|
|
package com.flossom.miniProgram.controller;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.flossom.common.core.web.controller.BaseController;
|
|
|
|
|
import com.flossom.common.core.web.domain.AjaxResult;
|
|
|
|
|
import com.flossom.miniProgram.domain.WxCode2SessionRet;
|
|
|
|
|
import com.flossom.miniProgram.utils.MiniProgramUtils;
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
public class LoginController extends BaseController {
|
|
|
|
|
|
|
|
|
|
@PostMapping("/wx/login")
|
|
|
|
|
public AjaxResult getJwt(@RequestParam(value = "code") String code,
|
|
|
|
|
@RequestParam(value = "rawData", required = false) String rawData,
|
|
|
|
|
@RequestParam(value = "signature", required = false) String signature) throws Exception {
|
|
|
|
|
// 1. 使用临时凭证code获取 appi + appsecret + code
|
|
|
|
|
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(MiniProgramUtils.getSessionKeyAndOpenId(code), WxCode2SessionRet.class);
|
|
|
|
|
if (0 != wxCode2SessionRet.getErrcode()) {
|
|
|
|
|
return AjaxResult.error(wxCode2SessionRet.getErrmsg());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
|
|
|
|
|
String signature2 = DigestUtils.sha1Hex(rawData + wxCode2SessionRet.getSession_key());
|
|
|
|
|
if (!signature.equals(signature2)) {
|
|
|
|
|
return AjaxResult.error("签名校验失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库;
|
|
|
|
|
// 用户非敏感信息:rawData
|
|
|
|
|
// 签名:signature
|
|
|
|
|
JSONObject rawDataJson = JSON.parseObject(rawData);
|
|
|
|
|
// LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
|
|
|
|
|
// lqw.eq(User::getOpenId, openid);
|
|
|
|
|
// User user = userService.getOne(lqw);
|
|
|
|
|
// if (user == null) {
|
|
|
|
|
// // 用户信息入库
|
|
|
|
|
// String nickName = rawDataJson.getString("nickName");
|
|
|
|
|
// String avatarUrl = rawDataJson.getString("avatarUrl");
|
|
|
|
|
// user = new User();
|
|
|
|
|
// user.setOpenId(openid);
|
|
|
|
|
// user.setAvatar(avatarUrl);
|
|
|
|
|
// user.setNickName(nickName);
|
|
|
|
|
// userService.save(user);
|
|
|
|
|
// }
|
|
|
|
|
return AjaxResult.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|