package com.xunxin.controller.app.huanxin;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xunxin.controller.system.BaseController;
import com.xunxin.dao.sys.PlatformMutualityManagentDao;
import com.xunxin.util.HttpUtils;
import com.xunxin.util.HttpUtils.UHeader;
import com.xunxin.vo.sys.PageData;
import com.xunxin.vo.sys.PlatformMutualityManagent;
import com.xunxin.web.api.bean.Response;
import com.xunxin.web.api.bean.Router;
/**
* Copyright © 2017 noseparte(Libra) © Like the wind, like rain
* @Author Noseparte
* @Compile 2017年10月24日 -- 下午5:31:38
* @Version 1.0
* @Description 获取环信SDK的返回信息
*/
@Controller
@RequestMapping(value=Router.PATH+Router.Easemob.PATH)
public class EasemobController extends BaseController{
private static final Logger log = Logger.getLogger(EasemobController.class);
private static final String LINKED_URL = "http://www.easemob.com/";
@Autowired
private PlatformMutualityManagentDao platformMutualityManagentDao;
@RequestMapping(value=Router.Easemob.GET_TOKEN,method=RequestMethod.POST)
@ResponseBody
public Response getToken(PageData pd) {
log.info("info-: get huanxin token begin");
Response res = this.getReponse();
try {
PlatformMutualityManagent pm = platformMutualityManagentDao.findOne(pd);
String app_key = pm.getApp_key();
String org_name = app_key.split("#")[0];//组织名称
String app_name = app_key.split("#")[1];//APP名称
String client_id = app_key.split("#")[1];//客户端id
String client_secret = app_key.split("#")[1];//客户端密钥
//post请求路径
String url = LINKED_URL + org_name + "/" + app_name + "/" + "token";
//post请求参数 json
StringBuffer sb = new StringBuffer();
sb.append("grant_type:").append("client_credentials");
sb.append("client_id:").append(client_id);
sb.append("client_secret:").append(client_secret);
String argJson = sb.toString();
//请求头信息
List headerList = new ArrayList();
headerList.add(new UHeader("Content-Type", "application/json"));
//获取环信token
String resp = HttpUtils.getPostResponse(url, argJson, headerList);
JSONObject obj = JSON.parseObject(resp);
//TODO 判断response 状态码
String access_token = obj.getString("access_token"); //token 值
String expires_in = obj.getString("expires_in"); //token 有效时间,以秒为单位,在有效期内不需要重复获取
String application = obj.getString("application"); //token 有效时间,以秒为单位,在有效期内不需要重复获取
//获得返回值 并解析
PageData tokenPd = new PageData<>();
tokenPd.put("access_token", access_token);
tokenPd.put("expires_in", expires_in);
tokenPd.put("application", application);
log.info("infoMsg:- get token success, ok");
return res.success(tokenPd);
} catch (Exception e) {
log.error("errorMsg:- An error occurred when get token!");
return res.failure(e.getMessage());
}
}
}