Commit 1be3410a7e693cfdfcec6b722d586bc93166f6de
1 parent
ac3bd266f2
Exists in
master
全局异常处理,修改用户表结构
Showing 7 changed files with 270 additions and 7 deletions
- talkonlineweb/pom.xml
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java
- talkonlineweb/src/main/resources/application.yml
- talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java
talkonlineweb/pom.xml
View file @
1be3410
| ... | ... | @@ -131,6 +131,12 @@ |
| 131 | 131 | </exclusion> |
| 132 | 132 | </exclusions> |
| 133 | 133 | </dependency> |
| 134 | + <!-- 环信 --> | |
| 135 | +<!-- <dependency>--> | |
| 136 | +<!-- <groupId>com.easemob.im</groupId>--> | |
| 137 | +<!-- <artifactId>im-sdk-core</artifactId>--> | |
| 138 | +<!-- <version>0.3.0</version>--> | |
| 139 | +<!-- </dependency>--> | |
| 134 | 140 | </dependencies> |
| 135 | 141 | |
| 136 | 142 | <dependencyManagement> |
| ... | ... | @@ -142,6 +148,20 @@ |
| 142 | 148 | <type>pom</type> |
| 143 | 149 | <scope>import</scope> |
| 144 | 150 | </dependency> |
| 151 | +<!-- <dependency>--> | |
| 152 | +<!-- <groupId>io.netty</groupId>--> | |
| 153 | +<!-- <artifactId>netty-bom</artifactId>--> | |
| 154 | +<!-- <version>4.1.59.Final</version>--> | |
| 155 | +<!-- <type>pom</type>--> | |
| 156 | +<!-- <scope>import</scope>--> | |
| 157 | +<!-- </dependency>--> | |
| 158 | +<!-- <dependency>--> | |
| 159 | +<!-- <groupId>io.projectreactor</groupId>--> | |
| 160 | +<!-- <artifactId>reactor-bom</artifactId>--> | |
| 161 | +<!-- <version>2020.0.4</version>--> | |
| 162 | +<!-- <type>pom</type>--> | |
| 163 | +<!-- <scope>import</scope>--> | |
| 164 | +<!-- </dependency>--> | |
| 145 | 165 | </dependencies> |
| 146 | 166 | </dependencyManagement> |
| 147 | 167 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java
View file @
1be3410
| 1 | 1 | package com.lyms.talkonlineweb.controller; |
| 2 | 2 | |
| 3 | +import com.alibaba.fastjson.JSONArray; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 3 | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | 6 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 5 | 7 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 6 | 8 | |
| 7 | 9 | |
| ... | ... | @@ -10,20 +12,19 @@ |
| 10 | 12 | import com.lyms.talkonlineweb.result.CheckResult; |
| 11 | 13 | import com.lyms.talkonlineweb.service.LymsUserService; |
| 12 | 14 | import com.lyms.talkonlineweb.util.Constant; |
| 15 | +import com.lyms.talkonlineweb.util.HXService; | |
| 13 | 16 | import com.lyms.talkonlineweb.util.JwtUtils; |
| 14 | 17 | import io.jsonwebtoken.Claims; |
| 15 | 18 | import lombok.extern.log4j.Log4j2; |
| 16 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.beans.factory.annotation.Value; | |
| 17 | 21 | import org.springframework.util.DigestUtils; |
| 18 | 22 | import org.springframework.validation.BindingResult; |
| 19 | 23 | import org.springframework.validation.annotation.Validated; |
| 20 | 24 | import org.springframework.web.bind.annotation.*; |
| 21 | 25 | |
| 22 | 26 | import javax.servlet.http.HttpSession; |
| 23 | -import java.util.Date; | |
| 24 | -import java.util.HashMap; | |
| 25 | -import java.util.List; | |
| 26 | -import java.util.Map; | |
| 27 | +import java.util.*; | |
| 27 | 28 | import java.util.concurrent.TimeUnit; |
| 28 | 29 | |
| 29 | 30 | @RestController |
| ... | ... | @@ -38,6 +39,16 @@ |
| 38 | 39 | @Autowired |
| 39 | 40 | private LymsUserMapper lymsUserMapper; |
| 40 | 41 | |
| 42 | + @Autowired | |
| 43 | + private HXService hxService; | |
| 44 | + | |
| 45 | + @Value("${hx.APPKEY}") | |
| 46 | + private String appKey; | |
| 47 | + @Value("${hx.ClientID}") | |
| 48 | + private String clientId; | |
| 49 | + @Value("${hx.ClientSecret}") | |
| 50 | + private String clientSecret; | |
| 51 | + | |
| 41 | 52 | @RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS}) |
| 42 | 53 | public BaseResponse login(String username, String passwd){ |
| 43 | 54 | // QueryWrapper queryWrapper= Wrappers.query(LymsUser.class); |
| 44 | 55 | |
| ... | ... | @@ -52,11 +63,23 @@ |
| 52 | 63 | LymsUser user=uLst.get(0); |
| 53 | 64 | String JWT = JwtUtils.createJWT("1", user.getLogin(), Constant.JWT_TTL); |
| 54 | 65 | tMap.put("token",JWT); |
| 66 | + | |
| 67 | + if(user.getHxid()==null){ | |
| 68 | + JSONObject json=hxService.addUser(user.getLogin(),Constant.COMMON_PASSWD); | |
| 69 | + JSONArray rArr=json.getJSONArray("entities"); | |
| 70 | + if(rArr.size()>0){ | |
| 71 | + user.setHxid(rArr.getJSONObject(0).getString("uuid")); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + lymsUserService.saveOrUpdate(user); | |
| 55 | 75 | user.setPasswd(null); |
| 56 | 76 | tMap.put("user",user); |
| 57 | 77 | } |
| 58 | 78 | baseResponse.setObject(tMap); |
| 59 | 79 | baseResponse.setErrorcode(0); |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 60 | 83 | return baseResponse; |
| 61 | 84 | } |
| 62 | 85 | |
| ... | ... | @@ -112,6 +135,7 @@ |
| 112 | 135 | |
| 113 | 136 | boolean f=lymsUserService.saveOrUpdate(user); |
| 114 | 137 | baseResponse.setErrorcode(f==true?0:1); |
| 138 | +// hxAddUser(user); | |
| 115 | 139 | } catch (Exception e) { |
| 116 | 140 | log.error(e.getMessage()); |
| 117 | 141 | e.printStackTrace(); |
| ... | ... | @@ -121,6 +145,11 @@ |
| 121 | 145 | |
| 122 | 146 | return baseResponse; |
| 123 | 147 | } |
| 148 | + | |
| 149 | +// public void hxAddUser(LymsUser user) { | |
| 150 | +// EMUser emUser=emService.user().get(user.getLogin()).block(); | |
| 151 | +// | |
| 152 | +// } | |
| 124 | 153 | |
| 125 | 154 | /** |
| 126 | 155 | * 删除用户 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsUser.java
View file @
1be3410
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java
View file @
1be3410
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java
View file @
1be3410
| 1 | +package com.lyms.talkonlineweb.util; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 5 | +import lombok.extern.log4j.Log4j2; | |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | +import org.springframework.beans.factory.annotation.Value; | |
| 8 | +import org.springframework.context.annotation.Bean; | |
| 9 | +import org.springframework.http.HttpEntity; | |
| 10 | +import org.springframework.http.HttpHeaders; | |
| 11 | +import org.springframework.http.HttpMethod; | |
| 12 | +import org.springframework.http.ResponseEntity; | |
| 13 | +import org.springframework.stereotype.Component; | |
| 14 | +import org.springframework.web.client.RestTemplate; | |
| 15 | + | |
| 16 | +import java.util.ArrayList; | |
| 17 | +import java.util.HashMap; | |
| 18 | +import java.util.List; | |
| 19 | +import java.util.Map; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * 环信相关API | |
| 23 | + */ | |
| 24 | +@Component | |
| 25 | +@Log4j2 | |
| 26 | +public class HXService { | |
| 27 | + | |
| 28 | + @Value("${hx.APPKEY}") | |
| 29 | + private String appKey; | |
| 30 | + @Value("${hx.ClientID}") | |
| 31 | + private String clientId; | |
| 32 | + @Value("${hx.ClientSecret}") | |
| 33 | + private String clientSecret; | |
| 34 | + | |
| 35 | + @Value("${hx.org_name}") | |
| 36 | + private String orgName; | |
| 37 | + @Value("${hx.app_name}") | |
| 38 | + private String appName; | |
| 39 | + | |
| 40 | + | |
| 41 | + private RestTemplate restTemplate=new RestTemplate(); | |
| 42 | + private ResponseEntity<String> resp; | |
| 43 | + | |
| 44 | +// @Autowired | |
| 45 | +// private RestTemplate restTemplate; | |
| 46 | + | |
| 47 | +// @Bean | |
| 48 | +// public EMService getEMService(){ | |
| 49 | +// EMProperties properties = EMProperties.builder() | |
| 50 | +// .setAppkey(appKey) | |
| 51 | +// .setClientId(clientId) | |
| 52 | +// .setClientSecret(clientSecret) | |
| 53 | +// .build(); | |
| 54 | +// | |
| 55 | +// EMService service = new EMService(properties); | |
| 56 | +// return service; | |
| 57 | +// } | |
| 58 | + | |
| 59 | + public String getUrl(){ | |
| 60 | + return String.format("http://a1.easemob.com/%s/%s/",orgName,appName); | |
| 61 | + } | |
| 62 | + public String getToken(){ | |
| 63 | + String token=""; | |
| 64 | + Map<String,Object> param=new HashMap<>(); | |
| 65 | + param.put("grant_type","client_credentials"); | |
| 66 | + param.put("client_id",clientId); | |
| 67 | + param.put("client_secret",clientSecret); | |
| 68 | + | |
| 69 | + ResponseEntity<String> resp=restTemplate.postForEntity(getUrl()+"token",param,String.class); | |
| 70 | + System.out.println(resp); | |
| 71 | + if(resp.getStatusCodeValue()==200) { | |
| 72 | + token= JSON.parseObject(resp.getBody()).getString("access_token"); | |
| 73 | + } | |
| 74 | + return token; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public JSONObject getUser(String username){ | |
| 78 | + JSONObject user=new JSONObject(); | |
| 79 | + String token=getToken(); | |
| 80 | + HttpHeaders headers=new HttpHeaders(); | |
| 81 | + headers.add("Authorization","Bearer "+token); | |
| 82 | + HttpEntity param=new HttpEntity(headers); | |
| 83 | + resp=restTemplate.exchange(getUrl()+"users/"+username, HttpMethod.GET,param,String.class); | |
| 84 | + if (resp.getStatusCodeValue()==200){ | |
| 85 | + user= JSON.parseObject(resp.getBody()); | |
| 86 | + } | |
| 87 | + return user; | |
| 88 | + } | |
| 89 | + | |
| 90 | + public JSONObject addUser(String username,String passwd){ | |
| 91 | + JSONObject rs=new JSONObject(); | |
| 92 | + Map<String,Object> param=new HashMap<>(); | |
| 93 | + HttpHeaders headers=new HttpHeaders(); | |
| 94 | + | |
| 95 | + headers.add("Authorization","Bearer "+getToken()); | |
| 96 | + List lParam=new ArrayList(); | |
| 97 | + param.clear(); | |
| 98 | + param.put("username",username); | |
| 99 | + param.put("password",passwd); | |
| 100 | + lParam.add(param); | |
| 101 | + HttpEntity entity=new HttpEntity(param,headers); | |
| 102 | + resp=restTemplate.postForEntity(getUrl()+"users",entity,String.class); | |
| 103 | + if (resp.getStatusCodeValue()==200){ | |
| 104 | + rs= JSON.parseObject(resp.getBody()); | |
| 105 | + } | |
| 106 | + log.info(rs); | |
| 107 | + return rs; | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * 删除用户 | |
| 112 | + * @param username 环信用户ID | |
| 113 | + * @return | |
| 114 | + */ | |
| 115 | + public JSONObject delUser(String username){ | |
| 116 | + JSONObject user=new JSONObject(); | |
| 117 | + String token=getToken(); | |
| 118 | + HttpHeaders headers=new HttpHeaders(); | |
| 119 | + headers.add("Authorization","Bearer "+token); | |
| 120 | + HttpEntity param=new HttpEntity(headers); | |
| 121 | + resp=restTemplate.exchange(getUrl()+"users/"+username, HttpMethod.DELETE,param,String.class); | |
| 122 | + if (resp.getStatusCodeValue()==200){ | |
| 123 | + user= JSON.parseObject(resp.getBody()); | |
| 124 | + } | |
| 125 | + return user; | |
| 126 | + } | |
| 127 | + | |
| 128 | + public JSONObject sendMsg(String[] target,String msgType,String msgContent,String from){ | |
| 129 | + JSONObject rs=new JSONObject(); | |
| 130 | + Map<String,Object> param=new HashMap<>(); | |
| 131 | + HttpHeaders headers=new HttpHeaders(); | |
| 132 | + | |
| 133 | + headers.add("Authorization","Bearer "+getToken()); | |
| 134 | + List lParam=new ArrayList(); | |
| 135 | + param.put("target",target); | |
| 136 | + | |
| 137 | + Map<String,Object> msg=new HashMap<>(); | |
| 138 | + msg.put("type",msgType); | |
| 139 | + msg.put("msg",msgContent); | |
| 140 | + | |
| 141 | + param.put("msg",msg); | |
| 142 | + param.put("from",from); | |
| 143 | + | |
| 144 | + lParam.add(param); | |
| 145 | + HttpEntity entity=new HttpEntity(param,headers); | |
| 146 | + resp=restTemplate.postForEntity(getUrl()+"messages",entity,String.class); | |
| 147 | + if (resp.getStatusCodeValue()==200){ | |
| 148 | + rs= JSON.parseObject(resp.getBody()); | |
| 149 | + } | |
| 150 | + log.info(rs); | |
| 151 | + return rs; | |
| 152 | + } | |
| 153 | + | |
| 154 | + | |
| 155 | +} |
talkonlineweb/src/main/resources/application.yml
View file @
1be3410
| ... | ... | @@ -23,4 +23,11 @@ |
| 23 | 23 | excludePath: login,test1,test2 |
| 24 | 24 | uploadPath: /data/talkonline/upload/ |
| 25 | 25 | imgUrlPre: https://dev-talk.healthbaby.com.cn/upload/ |
| 26 | + | |
| 27 | +#环信配置 | |
| 28 | +hx.APPKEY: 1135210903239178#demo | |
| 29 | +hx.ClientID: YXA65kqQUy4pQaqHb3pwXwfnrA | |
| 30 | +hx.ClientSecret: YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w | |
| 31 | +hx.org_name: 1135210903239178 | |
| 32 | +hx.app_name: demo |
talkonlineweb/src/test/java/com/lyms/talkonlineweb/TalkonlinewebApplicationTests.java
View file @
1be3410
| 1 | 1 | package com.lyms.talkonlineweb; |
| 2 | 2 | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 3 | 5 | import com.lyms.talkonlineweb.mapper.LymsUserMapper; |
| 4 | 6 | import com.lyms.talkonlineweb.service.LymsUserService; |
| 5 | 7 | import org.junit.jupiter.api.Test; |
| 6 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | 9 | import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.http.HttpEntity; | |
| 11 | +import org.springframework.http.HttpHeaders; | |
| 12 | +import org.springframework.http.ResponseEntity; | |
| 13 | +import org.springframework.web.client.RestTemplate; | |
| 14 | +import org.springframework.web.servlet.function.ServerRequest; | |
| 8 | 15 | |
| 16 | +import java.util.ArrayList; | |
| 17 | +import java.util.HashMap; | |
| 9 | 18 | import java.util.List; |
| 19 | +import java.util.Map; | |
| 10 | 20 | |
| 11 | -@SpringBootTest | |
| 21 | +//@SpringBootTest | |
| 12 | 22 | class TalkonlinewebApplicationTests { |
| 13 | 23 | |
| 14 | 24 | @Autowired |
| ... | ... | @@ -26,6 +36,47 @@ |
| 26 | 36 | }); |
| 27 | 37 | System.out.println("############################"); |
| 28 | 38 | lymsUserService.list().forEach(e-> System.out.println(e)); |
| 39 | + } | |
| 40 | + | |
| 41 | +// @Test | |
| 42 | +// void testHx(){ | |
| 43 | +// EMProperties properties = EMProperties.builder() | |
| 44 | +// .setAppkey("1135210903239178#demo") | |
| 45 | +// .setClientId("YXA65kqQUy4pQaqHb3pwXwfnrA") | |
| 46 | +// .setClientSecret("YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w") | |
| 47 | +// .build(); | |
| 48 | +// | |
| 49 | +// EMService service = new EMService(properties); | |
| 50 | +// service.user().create("test122234","12345"); | |
| 51 | +// } | |
| 52 | + | |
| 53 | + @Test | |
| 54 | + void testRest(){ | |
| 55 | + RestTemplate restTemplate=new RestTemplate(); | |
| 56 | + String url="http://a1.easemob.com/1135210903239178/demo/"; | |
| 57 | + Map<String,Object> param=new HashMap<>(); | |
| 58 | + param.put("grant_type","client_credentials"); | |
| 59 | + param.put("client_id","YXA65kqQUy4pQaqHb3pwXwfnrA"); | |
| 60 | + param.put("client_secret","YXA6RZaIr68MPPLcvqUnYxpqEkAEj6w"); | |
| 61 | + | |
| 62 | + ResponseEntity<String> resp=restTemplate.postForEntity(url+"token",param,String.class); | |
| 63 | + System.out.println(resp); | |
| 64 | + if(resp.getStatusCodeValue()==200){ | |
| 65 | + | |
| 66 | + String token=JSON.parseObject(resp.getBody()).getString("access_token"); | |
| 67 | + | |
| 68 | + System.out.println("token="+token); | |
| 69 | + HttpHeaders headers=new HttpHeaders(); | |
| 70 | + headers.add("Authorization","Bearer "+token); | |
| 71 | + List lParam=new ArrayList(); | |
| 72 | + param.clear(); | |
| 73 | + param.put("username","test110"); | |
| 74 | + param.put("password","test110"); | |
| 75 | + lParam.add(param); | |
| 76 | + HttpEntity entity=new HttpEntity(param,headers); | |
| 77 | + resp=restTemplate.postForEntity(url+"users",entity,String.class); | |
| 78 | + System.out.println(resp); | |
| 79 | + } | |
| 29 | 80 | } |
| 30 | 81 | |
| 31 | 82 | } |