packagecom.j1.mai.action;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importnet.sf.json.JSONObject;importorg.apache.log4j.Logger;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.j1.base.type.MsgStatus;importcom.j1.mai.model.common.SoaApiBaseAction;importcom.j1.mai.util.PropertyConfigurer;importcom.j1.soa.common.DateUtils;importcom.j1.soa.common.Md5Util;
@Controller
@Scope("request")
@RequestMapping("/storeConsumptionLogin")public class StoreLoginAction extendsSoaApiBaseAction {//final static String url ="http://localhost:8080/httpServer/c_i/common_i";
static Logger LOG = Logger.getLogger(StoreLoginAction.class);
@RequestMapping("/login")publicObject loginStoreConsumption(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value= "empName", required = true) String empName,//用户名
@RequestParam(value = "loginPassWd", required = true) String loginPassWd//密码
) {/*** 调用接口*/Map mapRes = new HashMap();//读取配置文件
String promoteUrl =(String)PropertyConfigurer.getString("storeConsumptiondLoginUrl");
String message= null;
JSONObject jsonObject= null;
message=httpSend(promoteUrl, empName, loginPassWd);try{//解析json字符串
message = message.replaceAll("\\\\", "");
String jsonStr= message.substring(message.indexOf("[") + 1,
message.indexOf("]"));
jsonObject=JSONObject.fromObject(jsonStr);
String responseCode= jsonObject.getString("msg");if (responseCode.equals("用户名或密码错误")) {
mapRes.put("status", 1);
mapRes.put("msg", "loginFalse");
}else{
mapRes.put("status", 0);
mapRes.put("msg", "ok");
}
}catch(Exception e) {
e.printStackTrace();
}finally{/***将操作的个人信息存在Session中,传给前台
*以便于在前台在录入会员的时候传递到后台*/
//操作人名称
String empNameAdd=jsonObject.getString("empName");//操作门店名称
String deptNameAdd=jsonObject.getString("deptName");//门店编码
String deptNo=jsonObject.getString("deptNo");//登录名
String loginName=jsonObject.getString("loginName");//操作人ID
String empId=jsonObject.getString("empId");
request.getSession().setAttribute("empNameAdd", empNameAdd);
request.getSession().setAttribute("deptNameAdd", deptNameAdd);
request.getSession().setAttribute("deptNo",deptNo );
request.getSession().setAttribute("empId", empId);
request.getSession().setAttribute("loginName", loginName);//this.write(request, response);
}
JSON o=(JSON) com.alibaba.fastjson.JSONObject.toJSON(mapRes);
System.out.println("查看=============="+o);returncom.alibaba.fastjson.JSONObject.toJSON(mapRes);
}/*** 发送HTTP请求
*
*@paramurl
*@parampropsMap
* 发送的参数*/
publicString httpSend(String promoteUrl, String empName, String loginPassWd) {
StringBuffer buffer= newStringBuffer();
String responseContent= null;try{
URL url_new= new URL(promoteUrl);//创建连接
HttpURLConnection connection =(HttpURLConnection) url_new
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Accept", "application/json"); //设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); //设置发送数据的格式
connection.connect();//POST请求
DataOutputStream out = newDataOutputStream(
connection.getOutputStream());//utf-8编码 ;
String sign= null;
String time=DateUtils.longToDateAll(System.currentTimeMillis());
String token= "91A1643059824847938125BA0AC0F557"; //token 不产于传送
String format = "json"; //传送方式
String method = "queryTbl_Employee";//调用方法
String sessionKey = "123456789078945";//sessionkey
String up_date = time;//上传日期 yyyy-mm-dd
String version = "1.0.2";//版本号
try{
sign= Md5Util.Bit32(format + method + sessionKey +token+ up_date +version);
}catch(Exception e1) {
e1.printStackTrace();
}
JSONObject obj= newJSONObject();
obj.element("sessionKey", sessionKey);
obj.element("method", method);
obj.element("format", format);
obj.element("up_Date", time);
obj.element("sign", sign);
obj.element("version", version);
JSONObject objbusinessdate= newJSONObject();
objbusinessdate.element("username", empName);//username ,
objbusinessdate.element("password", loginPassWd); //password
obj.element("businessData", objbusinessdate);
System.out.println(obj.toString());
out.writeBytes(obj.toString());
out.flush();//读取响应
int length = (int) connection.getContentLength();//获取长度
System.out.println("length:" +length);if (connection.getResponseCode() !=HttpURLConnection.HTTP_OK) {
System.out.println("网络错误异常!!!!");
}
InputStream in=connection.getInputStream();
BufferedReader rds= new BufferedReader(newInputStreamReader(in,"UTF-8"));
String tempLine=rds.readLine();
StringBuffer tempStr= newStringBuffer();if (tempLine != null) {
tempStr.append(tempLine);
tempLine=rds.readLine();
}
responseContent=tempStr.toString();//BufferedReader rd = new BufferedReader(new InputStreamReader(//connection.getInputStream(), "UTF-8"));//while( (responseMsg = rd.readLine())!=null){//buffer.append(responseMsg);//
//}
out.close();
rds.close();
connection.disconnect();
System.out.println(" Buffer============= " +buffer.toString());returnresponseContent;
}catch(IOException e) {
e.printStackTrace();
}//return buffer.toString();//自定义错误信息
return responseContent; //自定义错误信息
}
}