| | |
| | | |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.domain.R; |
| | | |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.TTenantResp; |
| | | import com.ruoyi.common.core.domain.entity.TSysUserResp; |
| | | import com.ruoyi.common.core.domain.model.LoginBody; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.core.domain.model.LoginUserApplet; |
| | | import com.ruoyi.common.core.utils.HttpUtils; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.framework.web.service.SysLoginService; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.model.TSysAppUser; |
| | | import com.ruoyi.system.service.TSysAppUserService; |
| | | import com.ruoyi.system.utils.wx.body.resp.Code2SessionRespBody; |
| | | import com.ruoyi.system.utils.wx.body.resq.Code2SessionResqBody; |
| | | import com.ruoyi.system.utils.wx.model.WeixinProperties; |
| | |
| | | private RestTemplate wxRestTemplate; |
| | | @Resource |
| | | private RedisService redisService; |
| | | |
| | | /** |
| | | * 上传文件存储在本地的根路径 |
| | | */ |
| | | // @Value("${file.upload.location}") |
| | | // private String localFilePath; |
| | | |
| | | @Resource |
| | | private TSysAppUserService sysAppUserService; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Autowired |
| | | private SysLoginService loginService; |
| | | /** |
| | | * 账号密码登录 |
| | | * |
| | | * @param loginBody 登录信息 |
| | | * @return 结果 |
| | | */ |
| | | @ApiOperation(value = "账号密码登录",notes = "管理员账号密码登录") |
| | | @PostMapping("/login") |
| | | public AjaxResult login(@RequestBody LoginBody loginBody) |
| | | { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | // 生成令牌 |
| | | LoginUser loginUser = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | loginBody.getUuid()); |
| | | ajax.put(Constants.TOKEN, tokenService.createToken(loginUser)); |
| | | List<SysRole> roles = loginUser.getUser().getRoles(); |
| | | if(CollectionUtils.isEmpty(roles)){ |
| | | return AjaxResult.error("请关联角色!"); |
| | | } |
| | | if(roles.get(0).getStatus() == 1){ |
| | | return AjaxResult.error("该账号角色已被禁用!"); |
| | | } |
| | | return ajax; |
| | | } |
| | | |
| | | @ApiOperation(value = "通过code获得openid,获取用户信息",tags = {"微信小程序登录"}) |
| | | @PostMapping("/openIdByJsCode") |
| | | public R<Map<String, Object>> openIdByJsCode(@RequestBody AppletUserEncrypteData data) { |
| | | log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode()); |
| | | WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService); |
| | | Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode())); |
| | | String openid = body.getOpenid(); |
| | | String sessionKey = body.getSessionKey(); |
| | | // 用户信息解密 数据验签 |
| | | // if (StringUtils.isNotBlank(data.getSignature())) { |
| | | // WxUtils.verifySignature(data.getRawData(), sessionKey, data.getSignature()); |
| | | // /** |
| | | // * 账号密码登录 |
| | | // * |
| | | // * @param loginBody 登录信息 |
| | | // * @return 结果 |
| | | // */ |
| | | // @ApiOperation(value = "账号密码登录",notes = "管理员账号密码登录") |
| | | // @PostMapping("/login") |
| | | // public AjaxResult login(@RequestBody LoginBody loginBody) |
| | | // { |
| | | // AjaxResult ajax = AjaxResult.success(); |
| | | // // 生成令牌 |
| | | // LoginUser loginUser = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | // loginBody.getUuid()); |
| | | // ajax.put(Constants.TOKEN, tokenService.createToken(loginUser)); |
| | | // List<SysRole> roles = loginUser.getUser().getRoles(); |
| | | // if(CollectionUtils.isEmpty(roles)){ |
| | | // return AjaxResult.error("请关联角色!"); |
| | | // } |
| | | if(StringUtils.isEmpty(data.getEncryptedData()) || StringUtils.isEmpty(data.getIv())){ |
| | | return R.fail("已拒绝授权"); |
| | | } |
| | | AppletUserDecodeData appletUserDecodeData = WxUtils.encryptedData(data.getEncryptedData(), sessionKey, data.getIv()); |
| | | appletUserDecodeData.setOpenId(openid); |
| | | // 先使用openId和当前手机号进行查询 |
| | | // TTenant tenant = tTenantService.getOne(Wrappers.lambdaQuery(TTenant.class) |
| | | // .and(e->e.eq(TTenant::getOpenId, appletUserDecodeData.getOpenId()).or() |
| | | // .eq(TTenant::getPhone, appletUserDecodeData.getPhoneNumber()))); |
| | | // if (tenant==null){ |
| | | // if(roles.get(0).getStatus() == 1){ |
| | | // return AjaxResult.error("该账号角色已被禁用!"); |
| | | // } |
| | | // return ajax; |
| | | // } |
| | | // |
| | | // @ApiOperation(value = "通过code获得openid,获取用户信息",tags = {"微信小程序登录"}) |
| | | // @PostMapping("/openIdByJsCode") |
| | | // public R<Map<String, Object>> openIdByJsCode(@RequestBody AppletUserEncrypteData data) { |
| | | // log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode()); |
| | | // WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService); |
| | | // Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode())); |
| | | // String openid = body.getOpenid(); |
| | | // String sessionKey = body.getSessionKey(); |
| | | // // 用户信息解密 数据验签 |
| | | //// if (StringUtils.isNotBlank(data.getSignature())) { |
| | | //// WxUtils.verifySignature(data.getRawData(), sessionKey, data.getSignature()); |
| | | //// } |
| | | // if(StringUtils.isEmpty(data.getEncryptedData()) || StringUtils.isEmpty(data.getIv())){ |
| | | // return R.fail("已拒绝授权"); |
| | | // } |
| | | // AppletUserDecodeData appletUserDecodeData = WxUtils.encryptedData(data.getEncryptedData(), sessionKey, data.getIv()); |
| | | // appletUserDecodeData.setOpenId(openid); |
| | | // // 先使用openId和当前手机号进行查询 |
| | | // TSysAppUser sysAppUser = sysAppUserService.getOne(Wrappers.lambdaQuery(TSysAppUser.class) |
| | | // .and(e->e.eq(TSysAppUser::getOpenId, appletUserDecodeData.getOpenId()).or() |
| | | // .eq(TSysAppUser::getPhone, appletUserDecodeData.getPhoneNumber()))); |
| | | // if (sysAppUser==null){ |
| | | //// appUser.setTenantAttributes(); |
| | | //// appUser.setTenantType(); |
| | | // tenant = new TTenant(); |
| | | // tenant.setPhone(appletUserDecodeData.getPhoneNumber()); |
| | | // tenant.setAccount(appletUserDecodeData.getPhoneNumber()); |
| | | // tenant.setPassword(SecurityUtils.encryptPassword(appletUserDecodeData.getPhoneNumber().substring(5))); |
| | | // tenant.setOpenId(appletUserDecodeData.getOpenId()); |
| | | // sysAppUser = new TSysAppUser(); |
| | | // sysAppUser.setPhone(appletUserDecodeData.getPhoneNumber()); |
| | | //// sysAppUser.setAccount(appletUserDecodeData.getPhoneNumber()); |
| | | //// sysAppUser.setPassword(SecurityUtils.encryptPassword(appletUserDecodeData.getPhoneNumber().substring(5))); |
| | | // sysAppUser.setOpenId(appletUserDecodeData.getOpenId()); |
| | | // // 手机号中间四位替换为* |
| | | // tenant.setResidentName(appletUserDecodeData.getPhoneNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")); |
| | | // tTenantService.save(tenant); |
| | | // sysAppUser.setNickName(appletUserDecodeData.getPhoneNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")); |
| | | // sysAppUserService.save(sysAppUser); |
| | | // } |
| | | // LoginUserApplet loginUserApplet = new LoginUserApplet(); |
| | | // TTenantResp tTenantResp = new TTenantResp(); |
| | | // BeanUtils.copyProperties(tenant, tTenantResp); |
| | | // tTenantResp.setResidentName(appletUserDecodeData.getPhoneNumber()); |
| | | // loginUserApplet.setUser(tTenantResp); |
| | | // loginUserApplet.setUserId(tenant.getId()); |
| | | Map<String, Object> tokenInfos = new HashMap<>(); |
| | | // TSysUserResp sysUserResp = new TSysUserResp(); |
| | | // BeanUtils.copyProperties(sysAppUser, sysUserResp); |
| | | // loginUserApplet.setUser(sysUserResp); |
| | | // loginUserApplet.setUserId(sysAppUser.getId()); |
| | | // Map<String, Object> tokenInfos = new HashMap<>(); |
| | | // tokenInfos.put("token",tokenService.createTokenApplet(loginUserApplet)); |
| | | // tokenInfos.put("info",loginUserApplet); |
| | | return R.ok(tokenInfos); |
| | | } |
| | | // return R.ok(tokenInfos); |
| | | // } |
| | | |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 敏感词检测 |
| | | * @param content |
| | | * @param openid |
| | | * @return |
| | | */ |
| | | @PostMapping("/sensitiveWordDetection") |
| | | public R<Boolean> sensitiveWordDetection (@RequestParam("content") String content, @RequestParam("openid") String openid){ |
| | | WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService); |
| | | String accessToken = appletTools.getAccessToken(""); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("content", content); |
| | | jsonObject.put("version", 2); |
| | | jsonObject.put("scene", 2); |
| | | jsonObject.put("openid", openid); |
| | | String post = HttpUtils.post("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken, jsonObject.toString()); |
| | | JSONObject object = JSONObject.parseObject(post); |
| | | Integer errcode = object.getInteger("errcode"); |
| | | if(0 != errcode){ |
| | | throw new RuntimeException(object.getString("errmsg")); |
| | | } |
| | | JSONArray detail = object.getJSONArray("detail"); |
| | | for (int i = 0; i < detail.size(); i++) { |
| | | JSONObject jsonObject1 = detail.getJSONObject(i); |
| | | Integer errcode1 = jsonObject1.getInteger("errcode"); |
| | | if(0 == errcode1){ |
| | | String suggest = jsonObject1.getString("suggest"); |
| | | Integer label = jsonObject1.getInteger("label"); |
| | | String keyword = jsonObject1.getString("keyword"); |
| | | Integer prob = jsonObject1.getInteger("prob"); |
| | | if(("risky".equals(suggest) || "review".equals(suggest)) && 100 != label && com.ruoyi.common.utils.StringUtils.isNotEmpty(keyword) && 80 <= prob){ |
| | | return R.ok(true); |
| | | } |
| | | } |
| | | } |
| | | return R.ok(false); |
| | | } |
| | | // /** |
| | | // * 敏感词检测 |
| | | // * @param content |
| | | // * @param openid |
| | | // * @return |
| | | // */ |
| | | // @PostMapping("/sensitiveWordDetection") |
| | | // public R<Boolean> sensitiveWordDetection (@RequestParam("content") String content, @RequestParam("openid") String openid){ |
| | | // WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService); |
| | | // String accessToken = appletTools.getAccessToken(""); |
| | | // JSONObject jsonObject = new JSONObject(); |
| | | // jsonObject.put("content", content); |
| | | // jsonObject.put("version", 2); |
| | | // jsonObject.put("scene", 2); |
| | | // jsonObject.put("openid", openid); |
| | | // String post = HttpUtils.post("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken, jsonObject.toString()); |
| | | // JSONObject object = JSONObject.parseObject(post); |
| | | // Integer errcode = object.getInteger("errcode"); |
| | | // if(0 != errcode){ |
| | | // throw new RuntimeException(object.getString("errmsg")); |
| | | // } |
| | | // JSONArray detail = object.getJSONArray("detail"); |
| | | // for (int i = 0; i < detail.size(); i++) { |
| | | // JSONObject jsonObject1 = detail.getJSONObject(i); |
| | | // Integer errcode1 = jsonObject1.getInteger("errcode"); |
| | | // if(0 == errcode1){ |
| | | // String suggest = jsonObject1.getString("suggest"); |
| | | // Integer label = jsonObject1.getInteger("label"); |
| | | // String keyword = jsonObject1.getString("keyword"); |
| | | // Integer prob = jsonObject1.getInteger("prob"); |
| | | // if(("risky".equals(suggest) || "review".equals(suggest)) && 100 != label && com.ruoyi.common.utils.StringUtils.isNotEmpty(keyword) && 80 <= prob){ |
| | | // return R.ok(true); |
| | | // } |
| | | // } |
| | | // } |
| | | // return R.ok(false); |
| | | // } |
| | | } |