|
@@ -0,0 +1,114 @@
|
|
|
+package com.fjhx.customer.service.customer.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.Header;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.customer.entity.xiaoman.po.XiaomanConfig;
|
|
|
+import com.fjhx.customer.entity.xiaoman.vo.XiaoManTokenVO;
|
|
|
+import com.fjhx.customer.mapper.xiaoman.XiaomanConfigMapper;
|
|
|
+import com.fjhx.customer.service.xiaoman.XiaomanConfigService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 小满配置表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class XiaomanConfigServiceImpl extends ServiceImpl<XiaomanConfigMapper, XiaomanConfig> implements XiaomanConfigService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getToken(XiaomanConfig config) {
|
|
|
+ getXiaomanToken(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshToken(XiaomanConfig config) {
|
|
|
+ refreshXiaomanToken(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新小满token
|
|
|
+ *
|
|
|
+ * @param config
|
|
|
+ */
|
|
|
+ private void refreshXiaomanToken(XiaomanConfig config) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("client_id", config.getClientId());
|
|
|
+ jsonObject.put("client_secret", config.getClientSecret());
|
|
|
+ jsonObject.put("grant_type", "refresh_token");
|
|
|
+ jsonObject.put("refresh_token", config.getRefreshToken());
|
|
|
+ XiaoManTokenVO token = getToken(config, jsonObject.toJSONString(), config.getRefreshTokenUrl());
|
|
|
+ updateConfig(config, token);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取小满token
|
|
|
+ *
|
|
|
+ * @param config
|
|
|
+ */
|
|
|
+ private void getXiaomanToken(XiaomanConfig config) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("client_id", config.getClientId());
|
|
|
+ jsonObject.put("client_secret", config.getClientSecret());
|
|
|
+ jsonObject.put("grant_type", "password");
|
|
|
+ jsonObject.put("scope", config.getScope());
|
|
|
+ jsonObject.put("username", config.getUsername());
|
|
|
+ jsonObject.put("password", config.getPassword());
|
|
|
+ String body = jsonObject.toJSONString();
|
|
|
+ XiaoManTokenVO token = getToken(config, body, config.getTokenUrl());
|
|
|
+ updateConfig(config, token);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateConfig(XiaomanConfig config, XiaoManTokenVO token) {
|
|
|
+ config.setAccessToken(token.getAccessToken());
|
|
|
+ config.setRefreshToken(token.getRefreshToken());
|
|
|
+ config.setExpireTime(DateUtil.offsetSecond(DateUtil.date(), token.getExpiresIn()));
|
|
|
+ config.setLastTokenTime(DateUtil.date());
|
|
|
+ this.updateById(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static XiaoManTokenVO getToken(XiaomanConfig config, String body, String url){
|
|
|
+ String s = HttpUtil.buildBasicAuth(config.getUsername(), config.getPassword(), Charset.forName("UTF-8"));
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put(Header.AUTHORIZATION.getValue(), s);
|
|
|
+ log.info("请求地址{},请求参数:{}", url, body);
|
|
|
+ String respond = HttpUtil.createPost(url).timeout(5000).body(body).charset(Charset.forName("UTF-8")).addHeaders(headers).execute().body();
|
|
|
+ log.info("请求返回结果:{}", respond);
|
|
|
+
|
|
|
+ XiaoManTokenVO xiaoManTokenVO;
|
|
|
+ try {
|
|
|
+ xiaoManTokenVO = JSON.parseObject(respond, XiaoManTokenVO.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求小满token失败,返回结果:{}", respond);
|
|
|
+ throw new RuntimeException("请求小满token失败");
|
|
|
+ }
|
|
|
+ return xiaoManTokenVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public XiaomanConfig getConfig() {
|
|
|
+ List<XiaomanConfig> list = this.list();
|
|
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
+ throw new RuntimeException("小满配置表数据为空");
|
|
|
+ } else if (list.size() > 1) {
|
|
|
+ throw new RuntimeException("小满配置表数据大于1条");
|
|
|
+ }
|
|
|
+ return list.get(0);
|
|
|
+ }
|
|
|
+}
|