在Controller中调用YMP-WeChat模块微信API方法代码实例

@Controller
public class OAuthController {

	@RequestMapping("/oauth/base")
	public IView oauth_base() throws Exception {
		String _url = WeChat.wxOAuthGetCodeURL(false, "state_str");
		Logs.debug(_url);
		return new RedirectView(_url);
	}

	@RequestMapping("/oauth/info")
	public IView oauth_info() throws Exception {
		String _url = WeChat.wxOAuthGetCodeURL(true, "state_str");
		Logs.debug(_url);
		return new RedirectView(_url);
	}

	@RequestMapping("/oauth/redirect")
	public IView redirect(@RequestParam(required = false) String code, @RequestParam(required = false) String state) throws Exception {
		JSONObject _return = new JSONObject();
		_return.put("code", code);
		_return.put("state", state);
		if (code != null) {
			WxOAuthToken _token = WeChat.wxOAuthGetToken(code);
			_return.put("oauth_token", _token);
			WxOAuthUser _info = WeChat.wxOAuthUserGetInfo(_token.getAccessToken(), _token.getOpenId(), null);
			_return.put("user_info", JSON.toJSON(_info));
		}
		return new JsonView(_return);
	}

	@RequestMapping("/menu/create")
	public IView createMenu() throws Exception {
		WxMenu _menu = WxMenu.create()
				.addItem(WxMenuItem.create().setName("消息测试")
						.addItem(WxMenuItem.create()
								.setName("文本消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_TEXT"))
						.addItem(WxMenuItem.create()
								.setName("图片消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_IMAGE"))
						.addItem(WxMenuItem.create()
								.setName("语音消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_VOICE"))
						.addItem(WxMenuItem.create()
								.setName("视频消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_VIDEO")))
				.addItem(WxMenuItem.create().setName("更多消息")
						.addItem(WxMenuItem.create()
								.setName("音乐消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_MUSIC"))
						.addItem(WxMenuItem.create()
								.setName("图文消息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_MSG_NEWS")))
				.addItem(WxMenuItem.create().setName("接口测试")
						.addItem(WxMenuItem.create()
								.setName("用户基本信息")
								.setType(WX_MESSAGE.EVENT_CLICK)
								.setKey("TEST_API_USERINFO"))
						.addItem(WxMenuItem.create()
								.setName("网页授权1")
								.setType(WX_MESSAGE.EVENT_VIEW)
								.setUrl("http://localhost/web/oauth/base"))
						.addItem(WxMenuItem.create()
								.setName("网页授权2")
								.setType(WX_MESSAGE.EVENT_VIEW)
								.setUrl("http://localhost/web/oauth/info"))
						.addItem(WxMenuItem.create()
								.setName("WeixinJS")
								.setType(WX_MESSAGE.EVENT_VIEW)
								.setUrl("http://localhost/web/wxjs"))
						.addItem(WxMenuItem.create()
								.setName("关于")
								.setType(WX_MESSAGE.EVENT_VIEW)
								.setUrl("http://localhost/web/aboutus")));
		//
		Logs.debug("Create New Menu: " + WeChat.wxMenuCreate(_menu));
		return new JsonView(_menu);
	}

	@RequestMapping("/menu/delete")
	public IView deleteMenu() throws Exception {
		Logs.debug("Delete Menu: " + WeChat.wxMenuDelete());
		return new TextView("Delete Success");
	}

	@RequestMapping("/menu/get")
	public IView getMenu() throws Exception {
		WxMenu _menu = WeChat.wxMenuGet();
		return new JsonView(JSON.toJSON(_menu));
	}

	@RequestMapping("/group/get")
	public IView getGroups() throws Exception {
		return new JsonView(WeChat.wxGroupGetList());
	}

	@RequestMapping("/user/get")
	public IView getUser(@RequestParam(required = false) String uid) throws Exception {
		List<WxUser> _results = new ArrayList<WxUser>();
		if (StringUtils.isBlank(uid)) {
			WxFollwersResult _list = WeChat.wxUserGetList("");
			for (String _openId : _list.getOpenIds()) {
				WxUser _u = WeChat.wxUserGetInfo(_openId, null);
				if (_u != null) {
					_results.add(_u);
				}
			}
		} else {
			WxUser _u = WeChat.wxUserGetInfo(uid, null);
			if (_u != null) {
				_results.add(_u);
			}
		}
		return new JsonView(_results);
	}

	@RequestMapping("/message/all")
	public IView sendToAll(@RequestParam String message) throws Exception {
		WxFollwersResult _list = WeChat.wxUserGetList("");
		for (String _openId : _list.getOpenIds()) {
			TextOutMessage _msg = new TextOutMessage(_openId);
			_msg.setContent(message);
			WeChat.wxMessageSendCustom(_msg);
		}
		return new JsonView(_list);
	}

	@RequestMapping("/message/to")
	public IView sendTo(@RequestParam String to, @RequestParam String message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(to);
		_msg.setContent(message);
		return new TextView(WeChat.wxMessageSendCustom(_msg));
	}

	@RequestMapping("/qrcode/create")
	public IView qrcodeCreate(@RequestParam int scene) throws Exception {
		return new JsonView(WeChat.wxQRCodeCreate(scene, 1800));
	}

	@RequestMapping("/qrcode/show")
	public IView qrcodeShow(@RequestParam String ticket) throws Exception {
		return new RedirectView(WeChat.wxQRCodeShowURL(ticket));
	}

	@RequestMapping("/aboutus")
	public IView aboutus() {
		return new JspView("aboutus.jsp");
	}

}



你可能感兴趣的:(微信,YMP,YMP-WeChat)