Нет описания

AuthCtrl.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. package com.sharemao.web.wx.ctrl;
  2. import java.net.URLEncoder;
  3. import java.util.Arrays;
  4. import java.util.Map;
  5. import javax.servlet.http.HttpSession;
  6. import org.apache.commons.codec.digest.DigestUtils;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.nianzai.base.mvc.Context;
  12. import com.nianzai.base.mvc.annotation.Controller;
  13. import com.nianzai.base.mvc.annotation.Path;
  14. import com.nianzai.base.mvc.view.SimpleJsonView;
  15. import com.nianzai.base.mvc.view.TextView;
  16. import com.nianzai.util.CodecUtil;
  17. import com.nianzai.util.DateTimeUtil;
  18. import com.nianzai.util.PropertiesUtil;
  19. import com.nianzai.util.Tools;
  20. import com.sharemao.web.wx.Constants;
  21. import com.sharemao.web.wx.util.GzhUtil;
  22. @Controller("/user")
  23. public class AuthCtrl
  24. {
  25. private static final Log log = LogFactory.getLog(AuthCtrl.class);
  26. /**
  27. * 首页 - 判断用户微信是否已绑定账号,如已绑定,返回我的主页界面,如没有绑定,返回登陆界面
  28. * @param context
  29. * @return
  30. */
  31. @Path(value="/index.do")
  32. public TextView index(Context context)
  33. {
  34. HttpSession session = context.getRequest().getSession();
  35. Map<String, String> para = context.getParas();
  36. log.info("para=="+para);
  37. String cursn=para.get("sn");
  38. session.setAttribute("cursn", cursn);//当前设备编号
  39. String openid = (String) session.getAttribute("openid"); // 获取用户微信openid
  40. String accessToken="";
  41. String wxCode = para.get("code");
  42. if(Tools.isEmpty(openid) && !GzhUtil.isEmpty(wxCode))
  43. {
  44. Map<String,String> m=GzhUtil.getOpenId(wxCode,Constants.appId,Constants.appSecret);
  45. openid=m.get("openId");
  46. accessToken=m.get("accessToken");
  47. session.setAttribute("openid", openid);
  48. session.setAttribute("accessToken", accessToken);
  49. }
  50. log.info("wxCode="+wxCode+",openid="+openid);
  51. // if ("o2P0j59YcTmvpZurZWK2fQHAPXJk".equals(openid)||"o2P0j5wNH9HBD5mDHo9of1-Lyuxo".equals(openid)){
  52. if ("null".equals(cursn)){
  53. cursn = "";
  54. }
  55. String url="http://xcwx.jimi.link/user/indexForOld.do?&openid=" + openid+"&sn="+cursn;
  56. url = URLEncoder.encode(url);
  57. log.info("url="+url);
  58. String path = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb06cf14f96dd7149&redirect_uri=" + url + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
  59. return new TextView(path);
  60. // }
  61. //
  62. // para.put("openid", openid);
  63. // GzhUtil.toM3Sign(para);
  64. // String u=Constants.m3IntAddr + "/user/wxIndex.do";
  65. // String result = GzhUtil.sendPost(u, para);
  66. // log.info("url="+u+",result="+result);
  67. // JSONObject resJson = JSONObject.parseObject(result);
  68. // int resCode = resJson.getIntValue("code");
  69. // if(resCode == 0)
  70. // {
  71. // JSONObject dataJson = resJson.getJSONObject("data");
  72. // String phonenum=dataJson.getString("phonenum");
  73. // if(Tools.isEmpty(phonenum))
  74. // {
  75. // context.getRequest().setAttribute("sn", cursn);
  76. // return new TextView("/activation.jsp");
  77. // }
  78. // else
  79. // {
  80. // session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID
  81. // session.setAttribute("phonenum", phonenum); // 用户账户
  82. // String defaultdev =dataJson.getString("defaultdev");
  83. // if(Tools.isEmpty(defaultdev))
  84. // defaultdev=cursn;
  85. // session.setAttribute("defaultdev", defaultdev); // 默认设备
  86. // if(Tools.isEmpty(defaultdev)) {
  87. // return new TextView("/device/getDevInfo.do");
  88. // } else {
  89. // return new TextView("/consumer/getPkgsByDevId.do");
  90. // }
  91. // }
  92. // }
  93. // else
  94. // {
  95. // context.getRequest().setAttribute("sn", cursn);
  96. // String lt=DateTimeUtil.longtime();
  97. // lt=lt.substring(0,lt.length()-2);
  98. // context.getRequest().setAttribute("sign", CodecUtil.str2md5("znz"+cursn+"125226"+lt));
  99. // return new TextView("/activation.jsp");
  100. // }
  101. }
  102. /**
  103. * 用户登陆界面,登陆后将微信号自动绑定到用户账号
  104. * @param context
  105. * @return
  106. */
  107. @Path(value="/login.do")
  108. public SimpleJsonView login(Context context)
  109. {
  110. HttpSession session = context.getRequest().getSession();
  111. Map<String, String> para = context.getParas();
  112. String openid = (String) session.getAttribute("openid"); // 获取用户微信openid
  113. para.put("openid", openid);
  114. GzhUtil.toM3Sign(para);
  115. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxLogin.do", para);
  116. JSONObject resJson = JSONObject.parseObject(result);
  117. int resCode = resJson.getIntValue("code");
  118. if(resCode == 0)
  119. {
  120. JSONObject dataJson = resJson.getJSONObject("data");
  121. session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID
  122. session.setAttribute("phonenum", dataJson.getString("phonenum")); // 用户账户
  123. session.setAttribute("defaultdev", dataJson.getString("defaultdev")); // 默认设备
  124. return new SimpleJsonView("OK");
  125. }
  126. return new SimpleJsonView(resJson.getString("msg"));
  127. }
  128. /**
  129. * 用户注册,注册完成后,微信号将自动绑定该账号
  130. * @param context
  131. * @return
  132. */
  133. @Path(value="/register.do")
  134. public SimpleJsonView register(Context context)
  135. {
  136. HttpSession session = context.getRequest().getSession();
  137. Map<String, String> para = context.getParas();
  138. String openid = (String) session.getAttribute("openid"); // 获取用户微信openid
  139. para.put("openid", openid);
  140. String devid ="";
  141. if(para.get("sn")!=null)
  142. devid=para.get("sn");
  143. else
  144. {
  145. if(session.getAttribute("cursn")!=null)
  146. devid=(String)session.getAttribute("cursn");
  147. }
  148. para.put("devid", devid);
  149. para.put("testval", "5120");
  150. GzhUtil.toM3Sign(para);
  151. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxRegister2.do", para);
  152. log.info(result);
  153. JSONObject resJson = JSONObject.parseObject(result);
  154. int resCode = resJson.getIntValue("code");
  155. if(resCode == 0)
  156. {
  157. JSONObject dataJson = resJson.getJSONObject("data");
  158. session.setAttribute("appuid", dataJson.getString("appuid")); // 用户ID
  159. session.setAttribute("phonenum", dataJson.getString("phonenum")); // 用户账户
  160. session.setAttribute("defaultdev", dataJson.getString("defaultdev")); // 默认设备
  161. if(!Tools.isEmpty(devid))
  162. session.setAttribute("defaultdev", devid);
  163. return new SimpleJsonView("OK");
  164. }
  165. return new SimpleJsonView(resJson.getString("msg"));
  166. }
  167. /**
  168. * 忘记密码
  169. * @param context
  170. * @return
  171. */
  172. @Path(value="/forgetPass.do")
  173. public SimpleJsonView forgetPass(Context context)
  174. {
  175. Map<String, String> para = context.getParas();
  176. GzhUtil.toM3Sign(para);
  177. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/upPass.do", para);
  178. JSONObject resJson = JSONObject.parseObject(result);
  179. int resCode = resJson.getIntValue("code");
  180. if(resCode == 0){
  181. return new SimpleJsonView("修改成功!");
  182. }
  183. return new SimpleJsonView(resJson.getString("msg"));
  184. }
  185. /**
  186. * 跳转至我的账户界面
  187. * @param context
  188. * @return
  189. */
  190. @Path(value="/myacc.do")
  191. public TextView myAccount(Context context)
  192. {
  193. HttpSession session = context.getRequest().getSession();
  194. String appuid = String.valueOf(session.getAttribute("appuid"));
  195. if(GzhUtil.isEmpty(appuid))
  196. return new TextView(Constants.loginUrl);
  197. context.getRequest().setAttribute("phonenum", String.valueOf(session.getAttribute("phonenum")));
  198. return new TextView("/wxuser.jsp");
  199. }
  200. /**
  201. * 注销
  202. * @param context
  203. * @return
  204. */
  205. @Path(value="/logout.do")
  206. public TextView logout(Context context)
  207. {
  208. HttpSession session = context.getRequest().getSession();
  209. Map<String, String> para = context.getParas();
  210. GzhUtil.toM3Sign(para);
  211. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/wxLogout.do", para);
  212. JSONObject resJson = JSONObject.parseObject(result);
  213. int resCode = resJson.getIntValue("code");
  214. if(resCode == 0){
  215. session.removeAttribute("appuid");
  216. return new TextView(Constants.loginUrl);
  217. }
  218. return new TextView("/login/login.jsp");
  219. }
  220. /**
  221. * 获取收货地址列表
  222. * @param context
  223. * @return
  224. */
  225. @Path(value="/getAddr.do")
  226. public TextView getAddr(Context context)
  227. {
  228. HttpSession session = context.getRequest().getSession();
  229. Map<String, String> para = context.getParas();
  230. String appuid = (String) session.getAttribute("appuid");
  231. if(GzhUtil.isEmpty(appuid))
  232. return new TextView(Constants.loginUrl);
  233. para.put("appuid", appuid);
  234. GzhUtil.toM3Sign(para);
  235. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getAddr.do", para);
  236. JSONObject resJson = JSONObject.parseObject(result);
  237. JSONArray addrL = new JSONArray();
  238. int resCode = resJson.getIntValue("code");
  239. if(resCode == 0){
  240. JSONObject dataJson = resJson.getJSONObject("data");
  241. if(dataJson.containsKey("addr")){
  242. addrL = dataJson.getJSONArray("addr");
  243. }
  244. }
  245. context.getRequest().setAttribute("addrs", addrL);
  246. context.getRequest().setAttribute("type", para.get("type"));
  247. return new TextView("/address.jsp");
  248. }
  249. /**
  250. * 获取单条收货地址数据
  251. * @param context
  252. * @return
  253. */
  254. @Path(value="/getSelAddr.do")
  255. public TextView getSelAddr(Context context)
  256. {
  257. Map<String, String> para = context.getParas();
  258. GzhUtil.toM3Sign(para);
  259. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/getAddrById.do", para);
  260. JSONObject resJson = JSONObject.parseObject(result);
  261. int resCode = resJson.getIntValue("code");
  262. JSONObject dataJson = new JSONObject();
  263. if(resCode == 0)
  264. dataJson = resJson.getJSONObject("data");
  265. context.getRequest().setAttribute("addr", dataJson);
  266. return new TextView("/editadd.jsp");
  267. }
  268. /**
  269. * 新增收货地址
  270. * @param context
  271. * @return
  272. */
  273. @Path(value="/addAddr.do")
  274. public SimpleJsonView addAddr(Context context)
  275. {
  276. HttpSession session = context.getRequest().getSession();
  277. String appuid = (String) session.getAttribute("appuid");
  278. if(GzhUtil.isEmpty(appuid))
  279. return new SimpleJsonView(Constants.noLoginMsg);
  280. Map<String, String> para = context.getParas();
  281. para.put("appuid", appuid);
  282. GzhUtil.toM3Sign(para);
  283. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/addAddr.do", para);
  284. JSONObject resJson = JSONObject.parseObject(result);
  285. int resCode = resJson.getIntValue("code");
  286. if(resCode == 0)
  287. return new SimpleJsonView("OK");
  288. return new SimpleJsonView(resJson.getString("msg"));
  289. }
  290. /**
  291. * 编辑收货地址
  292. * @param context
  293. * @return
  294. */
  295. @Path(value="/editAddr.do")
  296. public SimpleJsonView editAddr(Context context)
  297. {
  298. HttpSession session = context.getRequest().getSession();
  299. String appuid = (String) session.getAttribute("appuid");
  300. if(GzhUtil.isEmpty(appuid))
  301. return new SimpleJsonView(Constants.noLoginMsg);
  302. Map<String, String> para = context.getParas();
  303. para.put("appuid", appuid);
  304. GzhUtil.toM3Sign(para);
  305. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/editAddr.do", para);
  306. JSONObject resJson = JSONObject.parseObject(result);
  307. int resCode = resJson.getIntValue("code");
  308. if(resCode == 0)
  309. return new SimpleJsonView("OK");
  310. return new SimpleJsonView(resJson.getString("msg"));
  311. }
  312. /**
  313. * 删除收货地址
  314. * @param context
  315. * @return
  316. */
  317. @Path(value="/delAddr.do")
  318. public SimpleJsonView delAddr(Context context)
  319. {
  320. HttpSession session = context.getRequest().getSession();
  321. String appuid = (String) session.getAttribute("appuid");
  322. if(GzhUtil.isEmpty(appuid))
  323. return new SimpleJsonView(Constants.noLoginMsg);
  324. Map<String, String> para = context.getParas();
  325. para.put("appuid", appuid);
  326. GzhUtil.toM3Sign(para);
  327. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/delAddr.do", para);
  328. JSONObject resJson = JSONObject.parseObject(result);
  329. int resCode = resJson.getIntValue("code");
  330. if(resCode == 0)
  331. return new SimpleJsonView("OK");
  332. return new SimpleJsonView(resJson.getString("msg"));
  333. }
  334. /**
  335. * 修改默认收货地址
  336. * @param context
  337. * @return
  338. */
  339. @Path(value="/setDefAddr.do")
  340. public SimpleJsonView setDefAddr(Context context)
  341. {
  342. HttpSession session = context.getRequest().getSession();
  343. String appuid = (String) session.getAttribute("appuid");
  344. if(GzhUtil.isEmpty(appuid))
  345. return new SimpleJsonView(Constants.noLoginMsg);
  346. Map<String, String> para = context.getParas();
  347. para.put("appuid", appuid);
  348. GzhUtil.toM3Sign(para);
  349. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/setDefAddr.do", para);
  350. JSONObject resJson = JSONObject.parseObject(result);
  351. int resCode = resJson.getIntValue("code");
  352. if(resCode == 0)
  353. return new SimpleJsonView("OK");
  354. return new SimpleJsonView(resJson.getString("msg"));
  355. }
  356. /**
  357. * 获取用户所有订单
  358. * @param context
  359. * @return
  360. */
  361. @Path(value="/getAllOrder.do")
  362. public TextView getAllOrder(Context context)
  363. {
  364. HttpSession session = context.getRequest().getSession();
  365. String appuid = (String) session.getAttribute("appuid");
  366. if(GzhUtil.isEmpty(appuid))
  367. return new TextView(Constants.loginUrl);
  368. Map<String, String> para = context.getParas();
  369. para.put("appuid", appuid);
  370. GzhUtil.toM3Sign(para);
  371. //获取商品订单
  372. String pdtRes = GzhUtil.sendPost(Constants.m3IntAddr + "/device/getOrders.do", para);
  373. JSONObject pdtOrder = JSONObject.parseObject(pdtRes);
  374. if(pdtOrder.getIntValue("code") == 0){
  375. JSONArray pdtOrds = pdtOrder.getJSONArray("data");
  376. context.getRequest().setAttribute("pdtorder", pdtOrds);
  377. }
  378. //获取套餐订单
  379. String pkgRes = GzhUtil.sendPost(Constants.m3IntAddr + "/package/getOrders.do", para);
  380. JSONObject pkgOrder = JSONObject.parseObject(pkgRes);
  381. if(pdtOrder.getIntValue("code") == 0){
  382. JSONArray pkgOrds = pkgOrder.getJSONArray("data");
  383. context.getRequest().setAttribute("pkgorder", pkgOrds);
  384. }
  385. return new TextView("/order/allorder.jsp");
  386. }
  387. /**
  388. * 关于我们
  389. * @param context
  390. * @return
  391. */
  392. @Path(value="/aboutUs.do")
  393. public TextView aboutUs(Context context)
  394. {
  395. Map<String, String> para = context.getParas();
  396. GzhUtil.toM3Sign(para);
  397. String result = GzhUtil.sendPost(Constants.m3IntAddr + "/user/aboutus.do", para);
  398. JSONObject resJson = JSONObject.parseObject(result);
  399. int resCode = resJson.getIntValue("code");
  400. if(resCode == 0){
  401. JSONObject dataJson = resJson.getJSONObject("data");
  402. String content = dataJson.getString("content");
  403. String[] contents = content.split("<br>");
  404. context.getRequest().setAttribute("content", contents);
  405. }
  406. context.getRequest().setAttribute("logo", Constants.LOGO_DOWNLOAD);
  407. return new TextView("/aboutus.jsp");
  408. }
  409. /**
  410. * 检查实名
  411. * @param context
  412. * @return
  413. */
  414. @Path(value="/checksm.do")
  415. public TextView checksm(Context context)
  416. {
  417. String realhost=PropertiesUtil.getValue("wx", "realhost");
  418. Map<String, String> para = context.getParas();
  419. GzhUtil.toM3Sign(para);
  420. HttpSession session = context.getRequest().getSession();
  421. String devid ="";
  422. if(session.getAttribute("cursn")!=null)
  423. devid=(String)session.getAttribute("cursn");
  424. else
  425. devid=(String)session.getAttribute("defaultdev");
  426. if(Tools.isEmpty(devid))
  427. return new TextView(realhost+"/sming.jsp?t=0");
  428. int rmode=0;
  429. String url=Constants.m3IntAddr + "/device/getDevRunMode.do?devid="+devid;
  430. String result = GzhUtil.sendPost(url, para);
  431. log.info(result);
  432. JSONObject resJson = JSONObject.parseObject(result);
  433. int resCode = resJson.getIntValue("code");
  434. if(resCode == 0){
  435. JSONObject dataJson = resJson.getJSONObject("data");
  436. rmode=dataJson.getIntValue("rmode");
  437. }
  438. log.info(devid+url+",rmode="+rmode);
  439. //if(rmode<=2)
  440. {
  441. url=Constants.m3IntAddr + "/device/getIccidBySN2.do?devid="+devid+"&mode="+rmode;
  442. // 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"))
  443. // {
  444. // url=Constants.m3IntAddr + "/device/getIccidBySN2.do?devid="+devid+"&mode="+rmode;
  445. // }
  446. // else if(devid.startsWith("160"))
  447. // {
  448. // url=Constants.m3IntAddr + "/device/getIccidBySN3.do?devid="+devid;
  449. // }
  450. // else
  451. // {
  452. // url=Constants.m3IntAddr + "/device/getIccidBySN.do?devid="+devid;
  453. // }
  454. log.info(devid+url);
  455. result = GzhUtil.sendPost(url, para);
  456. resJson = JSONObject.parseObject(result);
  457. String path=realhost+"/sming.jsp?t=0";
  458. resCode = resJson.getIntValue("code");
  459. if(resCode == 0)
  460. {
  461. String phonenum=(String)session.getAttribute("phonenum");
  462. JSONObject dataJson = resJson.getJSONObject("data");
  463. String iccid1 = dataJson.getString("iccid1");
  464. String iccid2 = dataJson.getString("iccid2");
  465. String iccid3 = dataJson.getString("iccid3");
  466. String state1 = dataJson.getString("state1");
  467. String state2 = dataJson.getString("state2");
  468. String state3 = dataJson.getString("state3");
  469. context.getRequest().setAttribute("devid", devid);
  470. if(state1.equals("1"))
  471. path=realhost+"/sming.jsp?t=01&phone="+phonenum;
  472. else if(state2.equals("1"))
  473. path=realhost+"/sming.jsp?t=02&phone="+phonenum;
  474. else if(state3.equals("1"))
  475. path=realhost+"/sming.jsp?t=03&phone="+phonenum;
  476. if(state1.equals("0") && !Tools.isEmpty(iccid1))
  477. path=realhost+"/sming.jsp?t=1&devid="+devid+"&phone="+phonenum;
  478. else if(state2.equals("0") && !Tools.isEmpty(iccid2))
  479. path=realhost+"/sming.jsp?t=2&devid="+devid+"&phone="+phonenum;
  480. else if(state3.equals("0") && !Tools.isEmpty(iccid3))
  481. path=realhost+"/sming.jsp?t=3&devid="+devid+"&phone="+phonenum;
  482. }
  483. return new TextView(path);
  484. }
  485. }
  486. @Path(value="/guide.do")
  487. public TextView guide(Context context)
  488. {
  489. Map<String, String> para = context.getParas();
  490. HttpSession session = context.getRequest().getSession();
  491. String appuid = String.valueOf(session.getAttribute("appuid"));
  492. para.put("appuid", appuid);
  493. GzhUtil.sendPost(Constants.m3IntAddr + "/user/upGuidFlag.do", para);
  494. String devid=para.get("devid");
  495. context.getRequest().setAttribute("devid", devid);
  496. return new TextView("/guide.jsp");
  497. }
  498. }