package com.sharemao.web.wx.ctrl; import java.net.URLEncoder; import java.util.Arrays; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.nianzai.base.mvc.Context; import com.nianzai.base.mvc.annotation.Controller; import com.nianzai.base.mvc.annotation.Path; import com.nianzai.base.mvc.view.SimpleJsonView; import com.nianzai.base.mvc.view.TextView; import com.nianzai.util.CodecUtil; import com.nianzai.util.DateTimeUtil; import com.nianzai.util.PropertiesUtil; import com.nianzai.util.Tools; import com.sharemao.web.wx.Constants; import com.sharemao.web.wx.util.GzhUtil; @Controller("/user") public class AuthCtrl { private static final Log log = LogFactory.getLog(AuthCtrl.class); /** * 首页 - 判断用户微信是否已绑定账号,如已绑定,返回我的主页界面,如没有绑定,返回登陆界面 * @param context * @return */ @Path(value="/index.do") public TextView index(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); log.info("para=="+para); String cursn=para.get("sn"); session.setAttribute("cursn", cursn);//当前设备编号 String openid = (String) session.getAttribute("openid"); // 获取用户微信openid String accessToken=""; String wxCode = para.get("code"); if(Tools.isEmpty(openid) && !GzhUtil.isEmpty(wxCode)) { Map m=GzhUtil.getOpenId(wxCode,Constants.appId,Constants.appSecret); openid=m.get("openId"); accessToken=m.get("accessToken"); session.setAttribute("openid", openid); session.setAttribute("openidForPay", openid); session.setAttribute("accessToken", accessToken); } log.info("wxCode="+wxCode+",openid="+openid); para.put("openid", openid); GzhUtil.toM3Sign(para); String u=Constants.m3IntAddr + "/user/wxIndex.do"; String result = GzhUtil.sendPost(u, para); log.info("url="+u+",result="+result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String phonenum=dataJson.getString("phonenum"); if(Tools.isEmpty(phonenum)) { context.getRequest().setAttribute("sn", cursn); return new TextView("/activation.jsp"); } else { session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID session.setAttribute("phonenum", phonenum); // 用户账户 String defaultdev =dataJson.getString("defaultdev"); if(Tools.isEmpty(defaultdev)) defaultdev=cursn; session.setAttribute("defaultdev", defaultdev); // 默认设备 /*if(Tools.isEmpty(defaultdev)) { return new TextView("/device/getDevInfo.do"); } else { return new TextView("/consumer/getPkgsByDevId.do"); }*/ return new TextView("/device/getDevInfo.do"); } } else { context.getRequest().setAttribute("sn", cursn); String lt=DateTimeUtil.longtime(); lt=lt.substring(0,lt.length()-2); context.getRequest().setAttribute("sign", CodecUtil.str2md5("znz"+cursn+"125226"+lt)); return new TextView("/activation.jsp"); } } @Path(value="/indexForOld.do") public TextView indexForOld(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); log.info("para=="+para); String cursn=para.get("sn"); session.setAttribute("cursn", cursn);//当前设备编号 String openid=para.get("openid"); // 获取用户微信openid String accessToken=""; String wxCode = para.get("code"); if (Tools.isEmpty(openid)) { para.forEach((k,v)->context.getRequest().setAttribute(k, v)); return new TextView("/auth/index.do"); } session.setAttribute("openid", openid); log.info("old openid="+openid); para.put("openid", openid); String openidNew=""; if(!GzhUtil.isEmpty(wxCode)) { Map m=GzhUtil.getOpenId(wxCode,Constants.appId,Constants.appSecret); openidNew=m.get("openId"); accessToken=m.get("accessToken"); session.setAttribute("openidForPay", openidNew); session.setAttribute("accessToken", accessToken); } log.info("wxCode="+wxCode+",openid="+openidNew); GzhUtil.toM3Sign(para); String u=Constants.m3IntAddr + "/user/wxIndex.do"; String result = GzhUtil.sendPost(u, para); log.info("url="+u+",result="+result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); String phonenum=dataJson.getString("phonenum"); if(Tools.isEmpty(phonenum)) { context.getRequest().setAttribute("sn", cursn); return new TextView("/activation.jsp"); } else { session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID session.setAttribute("phonenum", phonenum); // 用户账户 String defaultdev =dataJson.getString("defaultdev"); if(Tools.isEmpty(defaultdev)) defaultdev=cursn; session.setAttribute("defaultdev", defaultdev); // 默认设备 /*if(Tools.isEmpty(defaultdev)) { return new TextView("/device/getDevInfo.do"); } else { return new TextView("/consumer/getPkgsByDevId.do"); }*/ return new TextView("/device/getDevInfo.do"); } } else { context.getRequest().setAttribute("sn", cursn); String lt=DateTimeUtil.longtime(); lt=lt.substring(0,lt.length()-2); context.getRequest().setAttribute("sign", CodecUtil.str2md5("znz"+cursn+"125226"+lt)); return new TextView("/activation.jsp"); } } /** * 用户登陆界面,登陆后将微信号自动绑定到用户账号 * @param context * @return */ @Path(value="/login.do") public SimpleJsonView login(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String openid = (String) session.getAttribute("openid"); // 获取用户微信openid para.put("openid", openid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxLogin.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID session.setAttribute("phonenum", dataJson.getString("phonenum")); // 用户账户 session.setAttribute("defaultdev", dataJson.getString("defaultdev")); // 默认设备 return new SimpleJsonView("OK"); } return new SimpleJsonView(resJson.getString("msg")); } /** * 用户注册,注册完成后,微信号将自动绑定该账号 * @param context * @return */ @Path(value="/register.do") public SimpleJsonView register(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String openid = (String) session.getAttribute("openid"); // 获取用户微信openid para.put("openid", openid); String devid =""; if(para.get("sn")!=null) devid=para.get("sn"); else { if(session.getAttribute("cursn")!=null) devid=(String)session.getAttribute("cursn"); } para.put("devid", devid); para.put("testval", "5120"); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxRegister2.do", para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) { JSONObject dataJson = resJson.getJSONObject("data"); session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID session.setAttribute("phonenum", dataJson.getString("phonenum")); // 用户账户 session.setAttribute("defaultdev", dataJson.getString("defaultdev")); // 默认设备 if(!Tools.isEmpty(devid)) session.setAttribute("defaultdev", devid); return new SimpleJsonView("OK"); } return new SimpleJsonView(resJson.getString("msg")); } /** * 忘记密码 * @param context * @return */ @Path(value="/forgetPass.do") public SimpleJsonView forgetPass(Context context) { Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/upPass.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0){ return new SimpleJsonView("修改成功!"); } return new SimpleJsonView(resJson.getString("msg")); } /** * 跳转至我的账户界面 * @param context * @return */ @Path(value="/myacc.do") public TextView myAccount(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); if(GzhUtil.isEmpty(appuid)) return new TextView(Constants.loginUrl); context.getRequest().setAttribute("phonenum", String.valueOf(session.getAttribute("phonenum"))); return new TextView("/wxuser.jsp"); } /** * 注销 * @param context * @return */ @Path(value="/logout.do") public TextView logout(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxLogout.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0){ session.removeAttribute("appuid"); return new TextView(Constants.loginUrl); } return new TextView("/login/login.jsp"); } /** * 获取收货地址列表 * @param context * @return */ @Path(value="/getAddr.do") public TextView getAddr(Context context) { HttpSession session = context.getRequest().getSession(); Map para = context.getParas(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new TextView(Constants.loginUrl); para.put("appuid", appuid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); JSONArray addrL = new JSONArray(); int resCode = resJson.getIntValue("code"); if(resCode == 0){ JSONObject dataJson = resJson.getJSONObject("data"); if(dataJson.containsKey("addr")){ addrL = dataJson.getJSONArray("addr"); } } context.getRequest().setAttribute("addrs", addrL); context.getRequest().setAttribute("type", para.get("type")); return new TextView("/address.jsp"); } /** * 获取单条收货地址数据 * @param context * @return */ @Path(value="/getSelAddr.do") public TextView getSelAddr(Context context) { Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getAddrById.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); JSONObject dataJson = new JSONObject(); if(resCode == 0) dataJson = resJson.getJSONObject("data"); context.getRequest().setAttribute("addr", dataJson); return new TextView("/editadd.jsp"); } /** * 新增收货地址 * @param context * @return */ @Path(value="/addAddr.do") public SimpleJsonView addAddr(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new SimpleJsonView(Constants.noLoginMsg); Map para = context.getParas(); para.put("appuid", appuid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/addAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) return new SimpleJsonView("OK"); return new SimpleJsonView(resJson.getString("msg")); } /** * 编辑收货地址 * @param context * @return */ @Path(value="/editAddr.do") public SimpleJsonView editAddr(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new SimpleJsonView(Constants.noLoginMsg); Map para = context.getParas(); para.put("appuid", appuid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/editAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) return new SimpleJsonView("OK"); return new SimpleJsonView(resJson.getString("msg")); } /** * 删除收货地址 * @param context * @return */ @Path(value="/delAddr.do") public SimpleJsonView delAddr(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new SimpleJsonView(Constants.noLoginMsg); Map para = context.getParas(); para.put("appuid", appuid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/delAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) return new SimpleJsonView("OK"); return new SimpleJsonView(resJson.getString("msg")); } /** * 修改默认收货地址 * @param context * @return */ @Path(value="/setDefAddr.do") public SimpleJsonView setDefAddr(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new SimpleJsonView(Constants.noLoginMsg); Map para = context.getParas(); para.put("appuid", appuid); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/setDefAddr.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0) return new SimpleJsonView("OK"); return new SimpleJsonView(resJson.getString("msg")); } /** * 获取用户所有订单 * @param context * @return */ @Path(value="/getAllOrder.do") public TextView getAllOrder(Context context) { HttpSession session = context.getRequest().getSession(); String appuid = (String) session.getAttribute("appuid"); if(GzhUtil.isEmpty(appuid)) return new TextView(Constants.loginUrl); Map para = context.getParas(); para.put("appuid", appuid); GzhUtil.toM3Sign(para); //获取商品订单 String pdtRes = GzhUtil.sendPost(Constants.m3IntAddr + "/device/getOrders.do", para); JSONObject pdtOrder = JSONObject.parseObject(pdtRes); if(pdtOrder.getIntValue("code") == 0){ JSONArray pdtOrds = pdtOrder.getJSONArray("data"); context.getRequest().setAttribute("pdtorder", pdtOrds); } //获取套餐订单 String pkgRes = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getOrders.do", para); JSONObject pkgOrder = JSONObject.parseObject(pkgRes); if(pdtOrder.getIntValue("code") == 0){ JSONArray pkgOrds = pkgOrder.getJSONArray("data"); context.getRequest().setAttribute("pkgorder", pkgOrds); } return new TextView("/order/allorder.jsp"); } /** * 关于我们 * @param context * @return */ @Path(value="/aboutUs.do") public TextView aboutUs(Context context) { Map para = context.getParas(); GzhUtil.toM3Sign(para); String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/aboutus.do", para); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0){ JSONObject dataJson = resJson.getJSONObject("data"); String content = dataJson.getString("content"); String[] contents = content.split("
"); context.getRequest().setAttribute("content", contents); } context.getRequest().setAttribute("logo", Constants.LOGO_DOWNLOAD); return new TextView("/aboutus.jsp"); } /** * 检查实名 * @param context * @return */ @Path(value="/checksm.do") public TextView checksm(Context context) { String realhost=PropertiesUtil.getValue("wx", "realhost"); Map para = context.getParas(); GzhUtil.toM3Sign(para); HttpSession session = context.getRequest().getSession(); String devid =""; if(session.getAttribute("cursn")!=null) devid=(String)session.getAttribute("cursn"); else devid=(String)session.getAttribute("defaultdev"); if(Tools.isEmpty(devid)) return new TextView(realhost+"/sming.jsp?t=0"); int rmode=0; String url=Constants.m3IntAddr + "/device/getDevRunMode.do?devid="+devid; String result = GzhUtil.sendPost(url, para); log.info(result); JSONObject resJson = JSONObject.parseObject(result); int resCode = resJson.getIntValue("code"); if(resCode == 0){ JSONObject dataJson = resJson.getJSONObject("data"); rmode=dataJson.getIntValue("rmode"); } log.info(devid+url+",rmode="+rmode); //if(rmode<=2) { url=Constants.m3IntAddr + "/device/getIccidBySN2.do?devid="+devid+"&mode="+rmode; // if(devid.startsWith("86") || devid.startsWith("1004") || devid.startsWith("1502") || devid.startsWith("1606") || devid.startsWith("1616") || devid.startsWith("1302") || devid.startsWith("35") || devid.startsWith("6")) // { // url=Constants.m3IntAddr + "/device/getIccidBySN2.do?devid="+devid+"&mode="+rmode; // } // else if(devid.startsWith("160")) // { // url=Constants.m3IntAddr + "/device/getIccidBySN3.do?devid="+devid; // } // else // { // url=Constants.m3IntAddr + "/device/getIccidBySN.do?devid="+devid; // } log.info(devid+url); result = GzhUtil.sendPost(url, para); resJson = JSONObject.parseObject(result); String path=realhost+"/sming.jsp?t=0"; resCode = resJson.getIntValue("code"); if(resCode == 0) { String phonenum=(String)session.getAttribute("phonenum"); JSONObject dataJson = resJson.getJSONObject("data"); String iccid1 = dataJson.getString("iccid1"); String iccid2 = dataJson.getString("iccid2"); String iccid3 = dataJson.getString("iccid3"); String state1 = dataJson.getString("state1"); String state2 = dataJson.getString("state2"); String state3 = dataJson.getString("state3"); context.getRequest().setAttribute("devid", devid); if(state1.equals("1")) path=realhost+"/sming.jsp?t=01&phone="+phonenum; else if(state2.equals("1")) path=realhost+"/sming.jsp?t=02&phone="+phonenum; else if(state3.equals("1")) path=realhost+"/sming.jsp?t=03&phone="+phonenum; if(state1.equals("0") && !Tools.isEmpty(iccid1)) path=realhost+"/sming.jsp?t=1&devid="+devid+"&phone="+phonenum; else if(state2.equals("0") && !Tools.isEmpty(iccid2)) path=realhost+"/sming.jsp?t=2&devid="+devid+"&phone="+phonenum; else if(state3.equals("0") && !Tools.isEmpty(iccid3)) path=realhost+"/sming.jsp?t=3&devid="+devid+"&phone="+phonenum; } return new TextView(path); } } @Path(value="/guide.do") public TextView guide(Context context) { Map para = context.getParas(); HttpSession session = context.getRequest().getSession(); String appuid = String.valueOf(session.getAttribute("appuid")); para.put("appuid", appuid); GzhUtil.sendPost(Constants.m3IntAddr + "/user/upGuidFlag.do", para); String devid=para.get("devid"); context.getRequest().setAttribute("devid", devid); return new TextView("/guide.jsp"); } }