|
|
@@ -0,0 +1,128 @@
|
|
|
+package com.izouma.zhumj.service.ammeter.yunding;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyuncs.utils.HttpsUtils;
|
|
|
+import com.github.kevinsawicki.http.HttpRequest;
|
|
|
+import com.izouma.zhumj.dto.ammeter.AmmeterInfo;
|
|
|
+import com.izouma.zhumj.enums.AmmeterType;
|
|
|
+import com.izouma.zhumj.exception.BusinessException;
|
|
|
+import com.izouma.zhumj.service.ammeter.AmmeterApi;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service("YD")
|
|
|
+public class YundingApi implements AmmeterApi {
|
|
|
+ private String baseUrl = "https://saas-openapi.dding.net/v2/";
|
|
|
+ private String clientId = "e6de7bbf4ccec3755d8923ff";
|
|
|
+ private String client_secret = "4ad028e9f0a9af61e9213e3ba1f4e382";
|
|
|
+ private String uid = "";
|
|
|
+ private String token = "";
|
|
|
+ private LocalDateTime tokenExpire = LocalDateTime.now();
|
|
|
+
|
|
|
+ private String request(String url, String method, JSONObject jsonObject) {
|
|
|
+
|
|
|
+ HttpRequest request = new HttpRequest(baseUrl + url, method);
|
|
|
+ request.header("Content-Type", "application/json");
|
|
|
+ request.header("User-Agent", "ddingnet-3.0");
|
|
|
+ request.send(jsonObject.toJSONString());
|
|
|
+ String body = request.body();
|
|
|
+ log.info("云丁api请求url: {} \n body: {}", baseUrl + url, body);
|
|
|
+
|
|
|
+ if (body.contains("\"code\":0")) {
|
|
|
+ token = null;
|
|
|
+ return request(url, method, jsonObject);
|
|
|
+ }
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getToken() {
|
|
|
+ if (tokenExpire.isAfter(LocalDateTime.now()) && StringUtils.isNotBlank(token)) {
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("client_id", clientId);
|
|
|
+ jsonObject.put("client_secret", client_secret);
|
|
|
+ JSONObject res = JSON.parseObject(request("access_token", "POST", jsonObject));
|
|
|
+ if (res.getString("access_token") != null) {
|
|
|
+ token = res.getString("access_token");
|
|
|
+ tokenExpire = LocalDateTime.now().plusDays(29);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ throw new BusinessException("token获取失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getHomeInfo(String lockId, String password, String phone) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("access_token", getToken());
|
|
|
+ jsonObject.put("uuid", lockId);
|
|
|
+ JSONObject json = JSON.parseObject(request("get_home_info", "GET", jsonObject));
|
|
|
+ if (json.getInteger("ErrNo") != 0) {
|
|
|
+ throw new BusinessException("密码发放错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void addPassword(String lockId, String password, String phone) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("access_token", getToken());
|
|
|
+ jsonObject.put("home_id", "61029bd06230c96001be9102");
|
|
|
+ jsonObject.put("uuid", lockId);
|
|
|
+ jsonObject.put("phonenumber", phone);
|
|
|
+ jsonObject.put("is_default", 1);
|
|
|
+ jsonObject.put("password", password);
|
|
|
+ jsonObject.put("name", "住户密码");
|
|
|
+ JSONObject json = JSON.parseObject(request("add_password", "POST", jsonObject));
|
|
|
+ if (json.getInteger("ErrNo") != 0) {
|
|
|
+ throw new BusinessException("密码发放错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AmmeterInfo getAmmeterInfo(String id) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("access_token", getToken());
|
|
|
+ jsonObject.put("home_id", "61029bd06230c96001be9102");
|
|
|
+ jsonObject.put("room_id", null);
|
|
|
+ jsonObject.put("uuid", "918e04e277728c8e06ec8ae71df9fac1");
|
|
|
+ JSONObject json = JSON.parseObject(request("get_elemeter_info", "GET", jsonObject));
|
|
|
+
|
|
|
+ return new AmmeterInfo(id, BigDecimal.valueOf(1318), BigDecimal.valueOf(1355), BigDecimal
|
|
|
+ .valueOf(1L), AmmeterType.YD, "云丁", true, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void recharge(String id, BigDecimal amount) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkin(String id, String name, String phone) {
|
|
|
+ addPassword(id, name, phone);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkout(String id) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void switchOff(String id) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void switchOn(String id) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|