ApiTokenService.java
1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.huaheng.api.xinyi.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huaheng.api.xinyi.constant.XinYiConstant;
import com.huaheng.api.xinyi.domian.ResultEntity;
import com.huaheng.api.xinyi.domian.TokenEntity;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.http.HttpUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* 获取API访问的身份认证Token信息
* @author Enzo Cotter
* @date 2019/12/20
*/
@Service
public class ApiTokenService {
@Resource
private TokenService tokenService;
public String apiToken(){
/* 清空token表*/
QueryWrapper<TokenEntity> queryWrapper = new QueryWrapper<>();
tokenService.remove(queryWrapper);
String url = XinYiConstant.XINYI_HOST+"xygerp/ald/auth/token";
Map<String, Object> map = new HashMap<>();
map.put("userName", "XYG_ALIWMS");
map.put("password", "xinyiboli868");
map.put("language", "ZHS");
map.put("wxLoginCode", "");
String result = HttpUtils.sendJsonPost(JSON.toJSONString(map), url);
System.out.println(result);
ResultEntity<TokenEntity> resultEntity = JSON.parseObject(result, new TypeReference<ResultEntity<TokenEntity>>(){});
if(!resultEntity.getOk() ){
throw new ServiceException("服务器处理失败");
} else if (!"0".equals(resultEntity.getCode())) {
throw new ServiceException("服务器响应错误");
} else if (!tokenService.insertSelective(resultEntity.getData())){
throw new BusinessException("保存失败");
}
return "success";
}
}