package org.jeecg.modules.zl.zlUser.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.zl.util.Method;
import org.jeecg.modules.zl.zlUser.mapper.IndexMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping("/privatePhone")
public class PrivatePhone {
@Resource
private RestTemplate restTemplate;
@Resource
private Method method;
@Resource
private IndexMapper indexMapper;
@RequestMapping("/bindPrivatePhone")
public Result bindPrivatePhone(@RequestBody Map map){
Map<String, Object> rMap = new HashMap<>(3);
JSONObject json = new JSONObject();
String caller = (String) map.get("caller");
String called = (String) map.get("called");
if (caller.equals(called)) {
return Result.ok("无法联系自己");
}
json.put("relationNum", "+86***********");
json.put("callerNum", "+86" + caller);
json.put("calleeNum", "+86" + called);
json.put("duration", "60");
String path = "/rest/caas/relationnumber/partners/v1.0";
Map responseEntity = bindPhone(json, path, "bind");
String resultCode = (String) responseEntity.get("resultcode");
if ("0".equals(resultCode)) {
String subscriptionId = (String) responseEntity.get("subscriptionId");
map.put("subscriptionId", subscriptionId);
if (1 == indexMapper.insertBingRecord(map)) {
rMap.put("relationNum", "***********");
return Result.OK("绑定成功",rMap);
}
return Result.ok("绑定成功");
}
return Result.ok("绑定失败,请稍后重试");
}
public Map bindPhone(JSONObject json, String path, String type) {
String url = "***********************" + path;
String app_key = "**************************";
String app_secret = "**************************";
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization",
"WSSE realm="SDP", profile="UsernameToken", type="Appkey"");
headers.add("Content-Type", "application/json;charset=UTF-8");
headers.add("Accept", "application/json");
headers.add("X-WSSE", buildWsseHeader(app_key, app_secret));
HttpEntity entity = new HttpEntity(json, headers);
String responseEntity;
Map<String, Object> resultMap;
try {
if ("bind".equals(type)) {
responseEntity = restTemplate.postForObject(url, entity, String.class);
} else {
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class);
String body = result.getBody();
Gson gson = new Gson();
resultMap = gson.fromJson(body, new HashMap<>(3).getClass());
return resultMap;
}
} catch (RestClientException e) {
Map<String, String> map = new HashMap<>(3);
map.put("resultcode", "1001");
map.put("resultMsg", "华为云绑定异常,请稍后重试");
return map;
}
Gson gson = new Gson();
Map responseMap = gson.fromJson(responseEntity, HashMap.class);
return responseMap;
}
static String buildWsseHeader(String appKey, String appSecret) {
String time = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(LocalDateTime.now());
String nonce = UUID.randomUUID().toString().replace("-", "");
MessageDigest md;
byte[] passwordDigest = null;
try {
md = MessageDigest.getInstance("SHA-256");
md.update((nonce + time + appSecret).getBytes());
passwordDigest = md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String passwordDigestBase64Str = Base64.getEncoder().encodeToString(passwordDigest);
String WSSE_HEADER_FORMAT = "UsernameToken Username="%s",PasswordDigest="%s",Nonce="%s",Created="%s"";
return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);
}
@RequestMapping(value = "callback", produces = "text/html;charset=UTF-8")
@ResponseBody
public String fCallback(@RequestBody Map<String, Object> map) {
String eventType = (String) map.get("eventType");
Map<String, String> statusInfo = (Map<String, String>) map.get("statusInfo");
if (StringUtils.isBlank(eventType) || null == statusInfo) {
map.put("resultCode", "10003");
map.put("resultMsg", "入参不可为空");
return new JSONObject(map).toString();
}
return callback(map);
}
public String callback(Map<String, Object> map) {
String eventType = (String) map.get("eventType");
Map<String, Object> statusInfo = (Map<String, Object>) map.get("statusInfo");
String sessionId = (String) statusInfo.get("sessionId");
String subscriptionId = (String) statusInfo.get("subscriptionId");
map.put("timestamp", statusInfo.get("timestamp"));
map.put("sessionId", sessionId);
map.put("caller", statusInfo.get("caller"));
map.put("called", statusInfo.get("called"));
map.put("subscriptionId", subscriptionId);
if ("disconnect".equals(eventType)) {
unBindPrivatePhone(subscriptionId);
indexMapper.updateUnBindTime(map);
Map<Object, Object> privateInfo = indexMapper.selectPrivatePhoneInfo(subscriptionId);
if (null != privateInfo) {
long jobId = (long) privateInfo.get("jobId");
String workerUserId = (String) privateInfo.get("workerUserId");
Integer comNum = indexMapper.getComNum(jobId, workerUserId);
if (null==comNum || comNum==0){
indexMapper.insertCommunicate(jobId, workerUserId);
}else {
indexMapper.editComNum(jobId, workerUserId,comNum+1);
}
}
}
return "HTTP/1.1 200 OK ";
}
public Map unBindPrivatePhone(String subscriptionId) {
String path = "/rest/caas/relationnumber/partners/v1.0?subscriptionId=" + subscriptionId;
Map responseEntity = bindPhone(null, path, "unbind");
return responseEntity;
}
}