diff --git a/talkonlineweb/pom.xml b/talkonlineweb/pom.xml index 35a3909..45ddb21 100644 --- a/talkonlineweb/pom.xml +++ b/talkonlineweb/pom.xml @@ -131,6 +131,12 @@ + + + + + + @@ -142,6 +148,20 @@ pom import + + + + + + + + + + + + + + diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java index cb798c9..20e0dbf 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java @@ -1,5 +1,7 @@ package com.lyms.talkonlineweb.controller; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -10,20 +12,19 @@ import com.lyms.talkonlineweb.result.BaseResponse; import com.lyms.talkonlineweb.result.CheckResult; import com.lyms.talkonlineweb.service.LymsUserService; import com.lyms.talkonlineweb.util.Constant; +import com.lyms.talkonlineweb.util.HXService; import com.lyms.talkonlineweb.util.JwtUtils; import io.jsonwebtoken.Claims; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.DigestUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; @RestController @@ -38,6 +39,16 @@ public class UserContoller { @Autowired private LymsUserMapper lymsUserMapper; + @Autowired + private HXService hxService; + + @Value("${hx.APPKEY}") + private String appKey; + @Value("${hx.ClientID}") + private String clientId; + @Value("${hx.ClientSecret}") + private String clientSecret; + @RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS}) public BaseResponse login(String username, String passwd){ // QueryWrapper queryWrapper= Wrappers.query(LymsUser.class); @@ -52,11 +63,23 @@ public class UserContoller { LymsUser user=uLst.get(0); String JWT = JwtUtils.createJWT("1", user.getLogin(), Constant.JWT_TTL); tMap.put("token",JWT); + + if(user.getHxid()==null){ + JSONObject json=hxService.addUser(user.getLogin(),Constant.COMMON_PASSWD); + JSONArray rArr=json.getJSONArray("entities"); + if(rArr.size()>0){ + user.setHxid(rArr.getJSONObject(0).getString("uuid")); + } + } + lymsUserService.saveOrUpdate(user); user.setPasswd(null); tMap.put("user",user); } baseResponse.setObject(tMap); baseResponse.setErrorcode(0); + + + return baseResponse; } @@ -112,6 +135,7 @@ public class UserContoller { boolean f=lymsUserService.saveOrUpdate(user); baseResponse.setErrorcode(f==true?0:1); +// hxAddUser(user); } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); @@ -122,6 +146,11 @@ public class UserContoller { return baseResponse; } +// public void hxAddUser(LymsUser user) { +// EMUser emUser=emService.user().get(user.getLogin()).block(); +// +// } + /** * 删除用户 * @param uid diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java index 0a7408c..6b5017b 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java @@ -48,7 +48,7 @@ public class LymsUser implements Serializable { * 环信id */ @TableField(value = "hxid") - private Integer hxid; + private String hxid; /** * 创建人 diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java index 0ef7aa3..9d1e472 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java @@ -7,4 +7,5 @@ public class Constant { public static final long JWT_TTL=24*60*60*1000;//1天 public static final int ROOT_MENU = 0;//一级菜单 public static final String CUR_USER = "cur_user";//当前用户 + public static final String COMMON_PASSWD = "123456";//通用密码 } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java new file mode 100644 index 0000000..15253f0 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java @@ -0,0 +1,155 @@ +package com.lyms.talkonlineweb.util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 环信相关API + */ +@Component +@Log4j2 +public class HXService { + + @Value("${hx.APPKEY}") + private String appKey; + @Value("${hx.ClientID}") + private String clientId; + @Value("${hx.ClientSecret}") + private String clientSecret; + + @Value("${hx.org_name}") + private String orgName; + @Value("${hx.app_name}") + private String appName; + + + private RestTemplate restTemplate=new RestTemplate(); + private ResponseEntity resp; + +// @Autowired +// private RestTemplate restTemplate; + +// @Bean +// public EMService getEMService(){ +// EMProperties properties = EMProperties.builder() +// .setAppkey(appKey) +// .setClientId(clientId) +// .setClientSecret(clientSecret) +// .build(); +// +// EMService service = new EMService(properties); +// return service; +// } + + public String getUrl(){ + return String.format("http://a1.easemob.com/%s/%s/",orgName,appName); + } + public String getToken(){ + String token=""; + Map param=new HashMap<>(); + param.put("grant_type","client_credentials"); + param.put("client_id",clientId); + param.put("client_secret",clientSecret); + + ResponseEntity resp=restTemplate.postForEntity(getUrl()+"token",param,String.class); + System.out.println(resp); + if(resp.getStatusCodeValue()==200) { + token= JSON.parseObject(resp.getBody()).getString("access_token"); + } + return token; + } + + public JSONObject getUser(String username){ + JSONObject user=new JSONObject(); + String token=getToken(); + HttpHeaders headers=new HttpHeaders(); + headers.add("Authorization","Bearer "+token); + HttpEntity param=new HttpEntity(headers); + resp=restTemplate.exchange(getUrl()+"users/"+username, HttpMethod.GET,param,String.class); + if (resp.getStatusCodeValue()==200){ + user= JSON.parseObject(resp.getBody()); + } + return user; + } + + public JSONObject addUser(String username,String passwd){ + JSONObject rs=new JSONObject(); + Map param=new HashMap<>(); + HttpHeaders headers=new HttpHeaders(); + + headers.add("Authorization","Bearer "+getToken()); + List lParam=new ArrayList(); + param.clear(); + param.put("username",username); + param.put("password",passwd); + lParam.add(param); + HttpEntity entity=new HttpEntity(param,headers); + resp=restTemplate.postForEntity(getUrl()+"users",entity,String.class); + if (resp.getStatusCodeValue()==200){ + rs= JSON.parseObject(resp.getBody()); + } + log.info(rs); + return rs; + } + + /** + * 删除用户 + * @param username 环信用户ID + * @return + */ + public JSONObject delUser(String username){ + JSONObject user=new JSONObject(); + String token=getToken(); + HttpHeaders headers=new HttpHeaders(); + headers.add("Authorization","Bearer "+token); + HttpEntity param=new HttpEntity(headers); + resp=restTemplate.exchange(getUrl()+"users/"+username, HttpMethod.DELETE,param,String.class); + if (resp.getStatusCodeValue()==200){ + user= JSON.parseObject(resp.getBody()); + } + return user; + } + + public JSONObject sendMsg(String[] target,String msgType,String msgContent,String from){ + JSONObject rs=new JSONObject(); + Map param=new HashMap<>(); + HttpHeaders headers=new HttpHeaders(); + + headers.add("Authorization","Bearer "+getToken()); + List lParam=new ArrayList(); + param.put("target",target); + + Map msg=new HashMap<>(); + msg.put("type",msgType); + msg.put("msg",msgContent); + + param.put("msg",msg); + param.put("from",from); + + lParam.add(param); + HttpEntity entity=new HttpEntity(param,headers); + resp=restTemplate.postForEntity(getUrl()+"messages",entity,String.class); + if (resp.getStatusCodeValue()==200){ + rs= JSON.parseObject(resp.getBody()); + } + log.info(rs); + return rs; + } + + +} diff --git a/talkonlineweb/src/main/resources/application.yml b/talkonlineweb/src/main/resources/application.yml index c184bec..5f01404 100644 --- a/talkonlineweb/src/main/resources/application.yml +++ b/talkonlineweb/src/main/resources/application.yml @@ -22,4 +22,11 @@ logging: excludePath: login,test1,test2 uploadPath: /data/talkonline/upload/ -imgUrlPre: https://dev-talk.healthbaby.com.cn/upload/ \ No newline at end of file +imgUrlPre: https://dev-talk.healthbaby.com.cn/upload/ + +#环信配置 +hx.APPKEY: 1135210903239178#demo +hx.ClientID: YXA65kqQUy4pQaqHb3pwXwfnrA +hx.ClientSecret: YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w +hx.org_name: 1135210903239178 +hx.app_name: demo \ No newline at end of file diff --git a/talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java b/talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java index cc408fc..189a3ef 100644 --- a/talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java +++ b/talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java @@ -1,14 +1,24 @@ package com.lyms.talkonlineweb; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.lyms.talkonlineweb.mapper.LymsUserMapper; import com.lyms.talkonlineweb.service.LymsUserService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.function.ServerRequest; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; -@SpringBootTest +//@SpringBootTest class TalkonlinewebApplicationTests { @Autowired @@ -28,4 +38,45 @@ class TalkonlinewebApplicationTests { lymsUserService.list().forEach(e-> System.out.println(e)); } +// @Test +// void testHx(){ +// EMProperties properties = EMProperties.builder() +// .setAppkey("1135210903239178#demo") +// .setClientId("YXA65kqQUy4pQaqHb3pwXwfnrA") +// .setClientSecret("YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w") +// .build(); +// +// EMService service = new EMService(properties); +// service.user().create("test122234","12345"); +// } + + @Test + void testRest(){ + RestTemplate restTemplate=new RestTemplate(); + String url="http://a1.easemob.com/1135210903239178/demo/"; + Map param=new HashMap<>(); + param.put("grant_type","client_credentials"); + param.put("client_id","YXA65kqQUy4pQaqHb3pwXwfnrA"); + param.put("client_secret","YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w"); + + ResponseEntity resp=restTemplate.postForEntity(url+"token",param,String.class); + System.out.println(resp); + if(resp.getStatusCodeValue()==200){ + + String token=JSON.parseObject(resp.getBody()).getString("access_token"); + + System.out.println("token="+token); + HttpHeaders headers=new HttpHeaders(); + headers.add("Authorization","Bearer "+token); + List lParam=new ArrayList(); + param.clear(); + param.put("username","test110"); + param.put("password","test110"); + lParam.add(param); + HttpEntity entity=new HttpEntity(param,headers); + resp=restTemplate.postForEntity(url+"users",entity,String.class); + System.out.println(resp); + } + } + }