审计记录

src/com/w1hd/zzhnc/controller/wx/WxMiniController.java 9.1 KB
zxt@theyeasy.com committed
1 2 3 4 5 6 7
package com.w1hd.zzhnc.controller.wx;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
zxt@theyeasy.com committed
8
import java.util.List;
zxt@theyeasy.com committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.theyeasy.weixin.model.WxMiniSessionInfo;
import org.theyeasy.weixin.service.WxMiniService;
import org.theyeasy.weixin.util.WxMiniUtil;

zxt@theyeasy.com committed
25
import com.beust.jcommander.internal.Maps;
zxt@theyeasy.com committed
26 27 28
import com.w1hd.zzhnc.controller.pc.BaseController;
import com.w1hd.zzhnc.dao.FansDao;
import com.w1hd.zzhnc.model.Articles;
zxt@theyeasy.com committed
29
import com.w1hd.zzhnc.model.Banner;
zxt@theyeasy.com committed
30
import com.w1hd.zzhnc.model.Fans;
zxt@theyeasy.com committed
31
import com.w1hd.zzhnc.model.Goods;
zxt@theyeasy.com committed
32 33
import com.w1hd.zzhnc.service.ActivityService;
import com.w1hd.zzhnc.service.ArticleService;
zxt@theyeasy.com committed
34
import com.w1hd.zzhnc.service.BannerService;
zxt@theyeasy.com committed
35
import com.w1hd.zzhnc.service.FansService;
zxt@theyeasy.com committed
36
import com.w1hd.zzhnc.service.GoodsService;
zxt@theyeasy.com committed
37
import com.w1hd.zzhnc.util.RedisUtil;
zxt@theyeasy.com committed
38
import com.w1hd.zzhnc.vo.MiniHomeVO;
zxt@theyeasy.com committed
39 40 41 42 43 44 45 46 47 48 49 50 51
import com.w1hd.zzhnc.vo.Vo_msg;

@Controller
@RequestMapping("/wxmini")
public class WxMiniController extends BaseController {

	@Autowired
	FansService fansService;
	@Autowired
	WxMiniService wxMiniService;

	@Autowired
	ArticleService articleService;
zxt@theyeasy.com committed
52

zxt@theyeasy.com committed
53 54
	@Autowired
	ActivityService activityService;
zxt@theyeasy.com committed
55

zxt@theyeasy.com committed
56 57 58
	@Autowired
	FansDao fansDao;

zxt@theyeasy.com committed
59
	@RequestMapping(value="/wxmsg",method=RequestMethod.GET)
zxt@theyeasy.com committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	public void WxMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");

		PrintWriter out = response.getWriter();
		if (!WxMiniUtil.checkSignature(signature, timestamp, nonce)) {
			out.print("sorry,该方法只处理微信推送的消息!");
			System.out.println("sorry,该方法只处理微信推送的消息!");
			out.close();
			return;

		}

		// 微信消息接入验证echostr
		if (null != echostr && echostr.length() > 0) {
			System.out.println("check ok,echostr=" + echostr);
			out.print(echostr);
			out.close();
			return;
		}

		// 处理用户发送来的消息
		WxMiniUtil wxMiniUtil = new WxMiniUtil();
		String responseMsg = wxMiniService.processRequest(request);
		if (null == response || responseMsg.length() <= 0)
			responseMsg = "success";

		// 响应微信服务器
		out.print(responseMsg);
		out.close();

		System.out.println("回复给微信的消息:" + responseMsg);

	}

zxt@theyeasy.com committed
97
	@RequestMapping(value="/login",method=RequestMethod.POST) // 购房助手登录 double lng,double lat
zxt@theyeasy.com committed
98
	public @ResponseBody Vo_msg login(@RequestParam(value = "code") String code,
zxt@theyeasy.com committed
99 100 101
			@RequestParam(value = "shareId") Integer shareId, @RequestParam(value = "nickname") String nickname,
			@RequestParam(value = "logo") String logo, @RequestParam(value = "lng") double lng,
			@RequestParam(value = "lat") double lat) {
zxt@theyeasy.com committed
102 103 104 105
		logger.info("小程序登录:code=" + code);
		// 取openid
		WxMiniSessionInfo sessionInfo = WxMiniUtil.jscode2session(code);
		// 添加粉丝记录
zxt@theyeasy.com committed
106
		Fans fans = fansService.addFans(sessionInfo, shareId, nickname, logo, lng, lat);
zxt@theyeasy.com committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		if (fans != null) {
			Map<String, Object> map = new HashMap<>();
			map.put("miniOpenId", sessionInfo.getOpenid());
			return new Vo_msg(0, map);
		} else {
			return new Vo_msg(-1, "登录失败,服务器异常");
		}
	}

	// 获取小程序二维码码1
	@RequestMapping(value = "/getminicode1", method = RequestMethod.POST)
	public @ResponseBody Vo_msg getMiniCode1(String path, int width) {
		String result = WxMiniUtil.getMiniCode1(path, width, true, 0, 0, 0);
		return new Vo_msg(0, result);
	}

	// 获取小程序二维码码2
	@RequestMapping(value = "/getminicode2", method = RequestMethod.POST)
	public @ResponseBody Vo_msg getMiniCode2(String scene, String page, int width) {
		try {
			scene = URLEncoder.encode("lcc", "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		String result = WxMiniUtil.getMiniCode2(scene, page, width, true, 0, 0, 0);
		return new Vo_msg(0, result);
	}

	// 获取小程序二维码码1
	@RequestMapping(value = "/getminicode3", method = RequestMethod.POST)
	public @ResponseBody Vo_msg getMiniCode3(String path, int width) {
		String result = WxMiniUtil.getMiniCode3(path, width);
		return new Vo_msg(0, result);
	}

	// 强制刷新Token
zxt@theyeasy.com committed
144
	@RequestMapping(value = "/refresh_token",method = RequestMethod.POST)
zxt@theyeasy.com committed
145 146 147 148 149 150 151
	public @ResponseBody String refreshToken(String pwd) {
		if (!pwd.equals("nmdzpsmhs"))
			return "error pwd";
		return WxMiniUtil.refreshToken();
	}

	// 文章列表
zxt@theyeasy.com committed
152
	@RequestMapping(value = "/getArticlesList",method = RequestMethod.GET)
zxt@theyeasy.com committed
153 154 155 156 157 158 159
	@ResponseBody
	public Object getArticlesList(Integer page, Integer pagesize) {

		return new Vo_msg(0, articleService.getArticlesList(page, pagesize, ""));
	}

	// 增加文章浏览数
zxt@theyeasy.com committed
160
	@RequestMapping(value = "/addArticleViewCount",method = RequestMethod.GET)
zxt@theyeasy.com committed
161 162 163 164 165 166 167
	@ResponseBody
	public Object addArticleViewCount(Integer id) {
		articleService.addArticleViewCount(id);
		return new Vo_msg(0, "ok");
	}

	// 获取
zxt@theyeasy.com committed
168
	@RequestMapping(value = "/getArticle",method=RequestMethod.GET)
zxt@theyeasy.com committed
169 170 171 172 173 174 175 176 177 178
	@ResponseBody
	public Object getArticle(Integer id) {
		Articles article = articleService.getArticle(id);
		if (article != null) {
			return new Vo_msg(0, article);
		} else {
			return new Vo_msg(-1, "获取失败,数据为空");
		}
	}

zxt@theyeasy.com committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	// //用户隐藏了小程序时调用此接口,将会向用户发送一个小程序卡片,一天只发送一次。
	// @RequestMapping(value = "/leave")
	// @ResponseBody
	// public String leaveMini(@RequestParam(value = "fansId") Integer fansId) {
	// if (fansId<1) return "Failed: fansId < 1";
	//
	// Activity activitySetting = activityService.getActivitySetting();
	// if (activitySetting != null && activitySetting.getStatus() ==
	// ActivityStatus.进行中.getIndex() ) //抽奖活动进行中
	// {
	// Fans fans = fansDao.selectByPrimaryKey(fansId);
	// if (null==fans) return "Failed:fans is null";
	//
	// //一天最多发送一次
	// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
	// Date last = fans.getLastSendProgram();
	// if(last!=null &&
	// fmt.format(last).equals(fmt.format(DateTime.now().toDate()))) return "faild:
	// allowed send one time everyday.";
	//
	// //发送小程序卡片
	// String media_id = RedisUtil.get("zzhnc_leave_program_media_id");
	// String thumb_url = RedisUtil.get("zzhnc_leave_program_thumb_url");
	//
	// if (null==media_id || media_id.length()<1 || null==thumb_url ||
	// thumb_url.length()<1)
	// {
	// //获取默认首页的海报
	// Example example = new Example(Postertemplet.class);
	// example.createCriteria().andEqualTo("postertype",
	// 2).andEqualTo("deleted",false);
	// java.util.List<Postertemplet> posterList =
	// postertempletDao.selectByExample(example);
	// if (posterList.size() < 1) return "failed: Postertemplet Img not exists!";
	// thumb_url = posterList.get(0).getImgurl();
	//
	// Vo_msg imgMsg = WxMiniUtil.uploadImage(thumb_url, false);
	// if (imgMsg.code==0)
	// {
	// media_id = imgMsg.data.toString();
	// RedisUtil.set("zzhnc_leave_program_media_id",media_id,60*60);
	// RedisUtil.set("zzhnc_leave_program_thumb_url",thumb_url,60*60);
	// }
	// else
	// {
	// return "failed: upload media img failed,errmsg=" + imgMsg.msg;
	// }
	// }
	//
	//
	// String result = WxMiniUtil.sendCustoMiniprogrampage(fans.getMiniopenid(),
	// "买房就找万小二","pages/openAnimation/openAnimation", thumb_url, media_id);
	// System.out.println("粉丝离开小程序时发送小程序卡片:" + result);
	// fans.setLastSendProgram(DateTime.now().toDate());
	// fansDao.updateByPrimaryKeySelective(fans);
	// return result;
	// }
	// return "ok";
	// }
	//
	// 清除小程序卡片的media_id的方法
zxt@theyeasy.com committed
240
	@RequestMapping(value = "/cleanProgramMedia",method=RequestMethod.GET)
zxt@theyeasy.com committed
241 242
	@ResponseBody
	public String cleanProgramMedia(@RequestParam(value = "code") String code) {
zxt@theyeasy.com committed
243 244
		if (!code.equals("nmdzpsmhs"))
			return "error pwd";
zxt@theyeasy.com committed
245 246 247 248
		RedisUtil.remove("zzhnc_leave_program_media_id");
		RedisUtil.remove("zzhnc_leave_program_thumb_url");
		return "ok";
	}
zxt@theyeasy.com committed
249 250 251 252 253 254 255

	@Autowired
	BannerService bannerService;

	@Autowired
	GoodsService goodsService;

zxt@theyeasy.com committed
256
	@RequestMapping(value = "/index",method=RequestMethod.GET)
zxt@theyeasy.com committed
257 258 259 260 261 262
	@ResponseBody
	public Object index(@RequestParam String openId) {
		try {
			Map<String, Object> result = Maps.newHashMap();
			List<Banner> bannerList = bannerService.getAll();
			List<Articles> homeData = articleService.getHomeData();
zxt@theyeasy.com committed
263
			List<Goods> homeData2 = goodsService.getHomeData();
zxt@theyeasy.com committed
264
			result.put("banner", bannerList);
zxt@theyeasy.com committed
265 266
			List<MiniHomeVO> data = MiniHomeVO.getData(homeData, homeData2);
			result.put("data", data);
zxt@theyeasy.com committed
267 268 269 270 271 272 273
			return new Vo_msg(0, result);
		} catch (Exception e) {
			return new Vo_msg(-1, "系统繁忙");
		}

	}

zxt@theyeasy.com committed
274
}