| | |
| | | package com.ruoyi.web.controller.api; |
| | | |
| | | import cn.hutool.http.HttpUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.ruoyi.common.core.domain.model.LoginBody; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.utils.AliSmsUtil; |
| | | import com.ruoyi.common.utils.NumberUtil; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | |
| | | if(user!=null && user.getStatus()==2){ |
| | | return AjaxResult.error("登录失败,当前账号已被冻结"); |
| | | } |
| | | String openid = getOpenid(loginBody.getJscode()); |
| | | TbUser user1 = tbUserService.getOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getOpenId, openid).ne(TbUser::getStatus,3).eq(TbUser::getIsDelete, 0)); |
| | | if(user1!=null && !loginBody.getPhone().equals(user1.getPhone())){ |
| | | return AjaxResult.error("该微信已绑定其他手机号"); |
| | | } |
| | | if(user==null){ |
| | | TbUser tbUser = new TbUser(); |
| | | tbUser.setPhone(loginBody.getPhone()); |
| | | tbUser.setStatus(1); |
| | | tbUser.setUserName(loginBody.getPhone()); |
| | | tbUser.setInviteId(loginBody.getInviteId()); |
| | | tbUser.setInviteId(loginBody.getInviteUserId()); |
| | | tbUser.setOpenId(openid); |
| | | tbUserService.save(tbUser); |
| | | user = tbUser; |
| | | ajax.put("firstRegister",1); |
| | | }else { |
| | | ajax.put("firstRegister",0); |
| | | if(StringUtils.isEmpty(user.getInviteId()) && !StringUtils.isEmpty(loginBody.getInviteUserId())){ |
| | | user.setInviteId(loginBody.getInviteUserId()); |
| | | } |
| | | if(StringUtils.isEmpty(user.getOpenId())){ |
| | | user.setOpenId(openid); |
| | | } |
| | | tbUserService.updateById(user); |
| | | } |
| | | LoginUser loginUser = new LoginUser(); |
| | | loginUser.setUserId(Long.valueOf(user.getId())); |
| | |
| | | @PostMapping("/openIdByJsCode") |
| | | public AjaxResult openIdByJsCode(@RequestBody AppletUserEncrypteData data) { |
| | | log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode()); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService); |
| | | Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode())); |
| | | String openid = body.getOpenid(); |
| | |
| | | if (user==null){ |
| | | user = new TbUser(); |
| | | user.setPhone(appletUserDecodeData.getPhoneNumber()); |
| | | user.setAvatar(appletUserDecodeData.getAvatarUrl()); |
| | | user.setUserName(appletUserDecodeData.getNickName()); |
| | | user.setAvatar("https://yizhengcheng.oss-cn-chengdu.aliyuncs.com/img/18dfce33c55644d38abdb88b57db906f.png"); |
| | | user.setUserName(appletUserDecodeData.getPhoneNumber()); |
| | | user.setOpenId(appletUserDecodeData.getOpenId()); |
| | | user.setInviteId(data.getInviteUserId()); |
| | | user.setStatus(1); |
| | | tbUserService.save(user); |
| | | ajax.put("firstRegister",1); |
| | | }else { |
| | | ajax.put("firstRegister",0); |
| | | if(StringUtils.isEmpty(user.getInviteId()) && !StringUtils.isEmpty(data.getInviteUserId())){ |
| | | user.setInviteId(data.getInviteUserId()); |
| | | } |
| | | user.setOpenId(appletUserDecodeData.getOpenId()); |
| | | user.updateById(); |
| | | } |
| | | if(user.getStatus()==2){ |
| | | return AjaxResult.error("登录失败,当前账号已被冻结"); |
| | | } |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | |
| | | LoginUser loginUser = new LoginUser(); |
| | | loginUser.setUserId(Long.valueOf(user.getId())); |
| | | loginUser.setUser(new SysUser()); |
| | | ajax.put(Constants.TOKEN, tokenService.createToken(loginUser)); |
| | | ajax.put("username",user.getUserName()); |
| | | ajax.put("avatar",user.getAvatar()); |
| | | |
| | | return ajax; |
| | | } |
| | | |
| | |
| | | String code = NumberUtil.getRandomInteger(6); |
| | | redisService.setCacheObject("login_"+phone, code, 5L, TimeUnit.MINUTES); |
| | | // 发送验证码 |
| | | |
| | | |
| | | try { |
| | | AliSmsUtil.sendSuccessMessage(phone,code); |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "获取协议 1=用户协议,2=隐私协议",tags = {"登录模块"}) |
| | | @GetMapping("/getAgreement/{type}") |
| | | @GetMapping("/getAgreement") |
| | | public R<List<TbAgreement>> getAgreement() { |
| | | List<TbAgreement> list = agreementService.list(new LambdaQueryWrapper<TbAgreement>().in(TbAgreement::getType, 1, 2)); |
| | | return R.ok(list); |
| | | } |
| | | |
| | | |
| | | public String getOpenid(String jscode) { |
| | | String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxConfig.getMemberAppId() + |
| | | "&secret=" + wxConfig.getMemberAppSecret() + |
| | | "&js_code=" + jscode + |
| | | "&grant_type=authorization_code"; |
| | | |
| | | String result = HttpUtil.get(url); |
| | | JSONObject jsonObject = JSONUtil.parseObj(result); |
| | | |
| | | String openid = jsonObject.getStr("openid"); |
| | | |
| | | return openid; |
| | | } |
| | | |
| | | |
| | | } |