package com.yihu.jkedu.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yihu.base.ConfigUtil; import com.yihu.utils.ApiUtil; import com.yihu.utils.StringUtil; import com.yihu.wsgw.api.ServiceBus; /** * @ClassName: SubscribeControl * @Description:订阅大类 * @author gqb * @date 2017-8-30 下午2:40:06 * */ @Controller @RequestMapping("/subscribe") public class SubscribeControl { /** * @Title: querySubscribe * @Description: 查询全部订阅大类(接口未提供) * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/querySubscribe") public void querySubscribe(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("categoryLevel", "1"); params.put("userId", userId); String formResult = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Category.getCategoryList", params.toString(), false); JSONObject resultJson = new JSONObject(); JSONObject formResultObj = JSONObject.fromObject(formResult); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); if (code == 10000) {// 成功 JSONObject arrform = formResultObj.getJSONObject("Result"); if(arrform!=null){ resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("Result", arrform); response.getWriter().write(resultJson.toString()); return ; } } resultJson.put("Code", -10000); resultJson.put("Message", msg); resultJson.put("Result", null); response.getWriter().write(resultJson.toString()); return ; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "获取全部的订阅异常").toString()); return ; } } /** * @Title: querySubscribeByUserId * @Description: 查询用户的订阅大类 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/querySubscribeByUserId") public void querySubscribeByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户userId不能为空").toString()); return; } JSONObject params = new JSONObject(); params.put("userId", userId); params.put("pageIndex", 0); params.put("pageSize", 1000); String formResult = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Subscrive.getSubscriveList", params.toString(), false); JSONObject resultJson = new JSONObject(); JSONObject formResultObj = JSONObject.fromObject(formResult); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); if (code == 10000) {// 成功 JSONObject arrform = formResultObj.getJSONObject("Result"); if(arrform!=null){ resultJson.put("Code", 10000); resultJson.put("Message", msg); resultJson.put("Result", arrform); response.getWriter().write(resultJson.toString()); return ; } } resultJson.put("Code", -10000); resultJson.put("Message", msg); resultJson.put("Result", null); response.getWriter().write(resultJson.toString()); return ; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "获取用户的订阅异常").toString()); return ; } } /** * @Title: saveSubscribe * @Description: 保存用户订阅大类 * @param @param request * @param @param response * @param @throws Exception 设定文件 * @return void 返回类型 * @throws */ @RequestMapping(value = "/saveSubscribe") public void saveSubscribe(HttpServletRequest request, HttpServletResponse response) throws Exception { try { response.setContentType("application/json;charset=UTF-8"); String categoryId = StringUtil.isEmpty(request.getParameter("categoryIds")) ? null : request.getParameter("categoryIds"); String categoryName = StringUtil.isEmpty(request.getParameter("categoryNames")) ? null : request.getParameter("categoryNames"); String cName = StringUtil.isEmpty(request.getParameter("cname")) ? null : request.getParameter("cname"); String userId = StringUtil.isEmpty(request.getParameter("userId")) ? null : request.getParameter("userId"); if(StringUtil.isEmpty(userId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户UserId不能为空").toString()); return; } if(StringUtil.isEmpty(cName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "用户CName不能为空").toString()); return; } if(StringUtil.isEmpty(categoryId)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "请至少选择一个感兴趣的分类").toString()); return; } if(StringUtil.isEmpty(categoryName)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "请至少选择一个感兴趣的分类").toString()); return; } JSONObject params = new JSONObject(); params.put("categoryId", categoryId); params.put("categoryName", categoryName); params.put("userId", userId); params.put("cName", cName); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "JkEdu.Subscrive.saveSubscrive", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); JSONObject resultJson = new JSONObject(); if (code == 10000) {// 成功 resultJson.put("Code", 10000); resultJson.put("Message", msg); }else{ resultJson.put("Code", code); resultJson.put("Message", msg); } response.getWriter().write(resultJson.toString()); return; } catch (Exception e) { e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-14444, "保存用户的订阅异常").toString()); return; } } }