/**
* 饿了么授权回调在此处响应,主要是获取授权码code
*/
@GetMapping("/elm")
public Object doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<br>OK");
String method=request.getParameter("method");
//响应授权回调
String code="";
String state="";
if("auth_back".equals(method)){
code=request.getParameter("code");
state=request.getParameter("state");
System.out.println(code);
System.out.println(state);
BusinessInfo info= businessInfoService.getBusinessInfoBySigns(state);
// Token to = getToken(true, ElemeConstants.SANDBOX_APP_KEY, ElemeConstants.SANDBOX_APP_SECRET, ElemeConstants.SANDBOX_REDIRECT_URL, code);
Token to = getToken(true, info.getElmAppKey(), info.getElmAppSecret(), ElemeConstants.SANDBOX_REDIRECT_URL, code);
info.setElmAccessToken(to.getAccessToken());
info.setElmExpiresIn(String.valueOf(to.getExpires()));
info.setElmTokenType(to.getTokenType());
info.setElmRefreshToken(to.getRefreshToken());
Date date=new Date();
Calendar todayFirstDate=Calendar.getInstance();
// todayFirstDate.add(Calendar.DATE, 30); //正式环境
todayFirstDate.add(Calendar.DATE, 1);//沙滩环境
Date firstDate = todayFirstDate.getTime();
Calendar FailureTime=Calendar.getInstance();
// FailureTime.add(Calendar.DATE, 35);//正式环境
FailureTime.add(Calendar.DATE, 15);//沙滩环境
Date RefreshTokenFailureTime = FailureTime.getTime();
info.setElmAccessTokenStartTime(date); //开始时间
info.setElmAccessTokenFailureTime(firstDate);//失效时间
info.setElmRefreshTokenStartTime(date);
info.setElmRefreshTokenFailureTime(RefreshTokenFailureTime);
int i= businessInfoService.updateByPrimaryKeySelective(info);
System.out.println(to);
if(i>0 && !"".equals(code)){
response.getWriter().write("<br>饿了么授权成功,可以正常接单啦!");
}else{
response.getWriter().write("<br>饿了么授权失败!");
}
}
response.getWriter().flush();
response.getWriter().close();
return method;
}
public static Token getToken(boolean isSandbox, String appKey, String appSecret, String callbackUrl, String code) {
Config config = new Config(isSandbox, appKey, appSecret);
OAuthClient client = new OAuthClient(config);
return client.getTokenByCode(code, callbackUrl);
}
@RequestMapping("/getAuthUrl")
@ResponseBody
public Map<String,String> getAuthUrl(Long businessId){
Map<String,String> map=new HashMap<String,String>();
BusinessInfo info= businessInfoService.selectByPrimaryKey(businessId);
String url=null;
if(info.getElmAppKey()!=null&&info.getElmAppSecret()!=null){
/* Config config=ElemeUtil.getConfig(true);
url=ElemeUtil.getAuthUrl(config);*/
Config config=getConfig(true,info);
url =getAuthUrl(config,info);
System.out.println(url);
map.put("url", url);
map.put("result", "success");
}else{
map.put("result", "failure");
map.put("msg", "饿了么必传参数不存在!");
}
return map;
}
/**
* 获取配置类
* @param isSandbox 是否沙箱
* @return
*/
public Config getConfig(boolean isSandbox,BusinessInfo info){
Config config=null;
if(isSandbox==true){
// config=new Config(isSandbox,ElemeConstants.SANDBOX_APP_KEY,ElemeConstants.SANDBOX_APP_SECRET);
config=new Config(isSandbox,info.getElmAppKey(),info.getElmAppSecret());
}else{
// TODO 填充正式环境数据
}
return config;
}
/**
* 获取店铺授权URL地址
* @desc 每次调用会产生一个新的授权码 需要记录code作为后续凭证
*/
public String getAuthUrl(Config config,BusinessInfo info){
OAuthClient client = new OAuthClient(config);
// String authUrl = client.getAuthUrl(ElemeConstants.SANDBOX_REDIRECT_URL, "all", "1234");
String authUrl = client.getAuthUrl(ElemeConstants.SANDBOX_REDIRECT_URL, "all", info.getSigns());
return authUrl;
}
/**
* 授权码(企业)模式获取Token
*/
private static Map<String, String> serverTokenTest(String autoCode) {
Map<String, String> map=null;
Token token = client.getTokenByCode(autoCode,ElemeConstants.SANDBOX_REDIRECT_URL);
if (token.isSuccess()) {
map= setTokenInfo(token);
System.out.println(map);
} else {
System.out.println("error_code: " + token.getError());
System.out.println("error_desc: " + token.getError_description());
}
return map;
}
private static Map<String, String> setTokenInfo(Token token) {
if (null != token && token.isSuccess()) {
tokenMap.put("access_token", token.getAccessToken());
tokenMap.put("token_type", token.getTokenType());
tokenMap.put("expires_in", String.valueOf(token.getExpires()));
tokenMap.put("refresh_token", token.getRefreshToken());
}
return tokenMap;
}
public static void getShopInfo(boolean isSandbox, String appKey, String appSecret, Token token, int shopId) {
try {
Config config = new Config(isSandbox, appKey, appSecret);
ShopService shopService = new ShopService(config, token);
OShop shop = shopService.getShop(shopId);
System.out.println("店名:"+shop.getName()+", 地址:"+shop.getAddressText());
} catch(Exception e) {
e.printStackTrace();
}
}