| | |
| | | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | |
| | | |
| | | import java.util.Map; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.moral.api.entity.AppUser; |
| | | import com.moral.api.exception.BusinessException; |
| | | import com.moral.api.mapper.AppUserMapper; |
| | | import com.moral.api.service.UserService; |
| | | import com.moral.api.utils.WechatUtils; |
| | | |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.constant.ResultMessage; |
| | | |
| | | |
| | | @Slf4j |
| | | @Api(tags = {"小程序用户管理"}) |
| | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | @Autowired |
| | | private AppUserMapper appUserMapper; |
| | | |
| | | @ApiOperation(value = "登陆信息", notes = "登陆信息") |
| | | @PostMapping("login") |
| | | @ApiOperation(value = "登陆信息") |
| | | @PostMapping("logins") |
| | | public ResultMessage login(@RequestBody Map<String, Object> parameters) { |
| | | if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), |
| | |
| | | } |
| | | |
| | | |
| | | @PostMapping("/wx/login") |
| | | @ApiOperation(value = "小程序登陆", notes = "小程序登陆") |
| | | public ResultMessage userLogin(@RequestParam(value = "code", required = false) String code, |
| | | @RequestParam(value = "rawData", required = false) String rawData, |
| | | @RequestParam(value = "signature", required = false) String signature) { |
| | | // 用户非敏感信息:rawData |
| | | // 签名:signature |
| | | JSONObject rawDataJson = JSON.parseObject(rawData); |
| | | // 1.接收小程序发送的code |
| | | // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code |
| | | JSONObject SessionKeyOpenId = WechatUtils.getSessionKeyOrOpenId(code); |
| | | // 3.接收微信接口服务 获取返回的参数 |
| | | String openid = SessionKeyOpenId.getString("openid"); |
| | | String sessionKey = SessionKeyOpenId.getString("session_key"); |
| | | |
| | | // 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey) |
| | | String signature2 = DigestUtils.sha1Hex(rawData + sessionKey); |
| | | if (!signature.equals(signature2)) { |
| | | // return ResultMessage.ok().message("签名校验失败"); |
| | | // return ResultMessage.ok("签名校验失败"); |
| | | throw new BusinessException("签名校验失败"); |
| | | } |
| | | // 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库; |
| | | LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery(); |
| | | lqw.eq(AppUser::getOpenId, openid); |
| | | // User user = userService.getOne(lqw); |
| | | AppUser user = appUserMapper.selectOne(lqw); |
| | | |
| | | if (user == null) { |
| | | // 用户信息入库 |
| | | String nickName = rawDataJson.getString("nickName"); |
| | | String avatarUrl = rawDataJson.getString("avatarUrl"); |
| | | user = new AppUser(); |
| | | user.setOpenId(Integer.parseInt(openid)); |
| | | user.setAvatarUrl(avatarUrl); |
| | | user.setNickName(nickName); |
| | | appUserMapper.insert(user); |
| | | } |
| | | return ResultMessage.ok(user); |
| | | @GetMapping("/wx/login") |
| | | @ApiOperation(value = "小程序登陆") |
| | | public ResultMessage userLogin(@RequestParam(value = "code") String code |
| | | ) { |
| | | Map<String, Object> result = userService.wxLogin(code); |
| | | return ResultMessage.ok(result); |
| | | } |
| | | |
| | | @GetMapping("/wx/exit") |
| | | @ApiOperation(value = "小程序退出") |
| | | public ResultMessage updateUserId(@RequestParam @ApiParam(value = "userId",name = "用户主键") Integer userId) { |
| | | userService.updateUserId(userId); |
| | | return ResultMessage.ok(); |
| | | } |
| | | |
| | | } |