Emmmmm 今天Leader不在家,悄悄給大家分享一个微信公众号网页开发,以及获取用户信息开发。

今天leader给了一个公众号投票的需求

1、每个微信只能投一票
2、不能重复投票。防止刷票。
3、可以正常分享页面。

我觉得这些需求对我这个牛(菜)逼(鸟)轰(一)轰(个)的程序员来说不是个什么大问题。

哈哈哈哈哈  好不吹牛逼了。来先说思路。

 
微信开发文档----https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842/
我不喜欢太多字。你们仔鸡看吧。

先画一个牛城图。。。

整体的精髓都在图上了。原创不易,转载请说明出处。谢谢谢谢。哪里不懂可以直接联系我

用户授权并获取code

参数说明
appid=APPID(公众号唯一标识)
redirect_uri=REDIRECT_URI(授权后重定向的回调链接地址)
response_type=code(返回类型,无需更改)
scope=SCOPE(snsapi_base ,不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo 弹出授权页面,可通过openid拿到昵称、性别、所在地。)
state=STATE(重定向后会带上state参数,开发者可以填写任意参数值)
#wechat_redirect(无需更改)

地址实例(虽是测试号,但我还是隐藏部分信息)红色字体需要根据实际更改。
https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxx&redirect_uri=www.sohu.com&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect

上代码吧。


 package cn.builder.controller.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
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.springframework.web.servlet.ModelAndView;

import cn.builder.controller.base.BaseController;
import cn.builder.entity.Page;
import cn.builder.service.activity.ActivityService;
import cn.builder.util.AppUtil;
import cn.builder.util.DateUtil;
import cn.builder.util.Jurisdiction;
import cn.builder.util.PageData;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController {

	@Resource(name="activityService")
	private ActivityService activityService;
	
	private String menuUrl ="activity/getVoteList.do";
	/**
	 * 获取用户code值
	 * 
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCodeUrl")
	public void getCode(HttpServletResponse response, HttpServletRequest request) throws Exception {
		String url = WeChatUtil.getCodeUrl("http://cnbuilder.cn/activity/voteList");
		response.sendRedirect(url);
	}

	/**
	 * 音频列表以及用户是否投票
	 * 
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/voteList")
	public ModelAndView UserLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = this.getModelAndView();
		// 1.获取code
		String code = request.getParameter("code");
		Map<String, Object> map =new HashMap<>();
		try {
			  map = WeChatUtil.getOpenId(code);
		} catch (Exception e) {
			response.sendRedirect("http://cnbuilder.cn/activity/getCodeUrl");
			return null;
		}
		
		if (code==null||CollectionUtils.isEmpty(map)||map==null) {
			response.sendRedirect("http://cnbuilder.cn/activity/getCodeUrl");
			return null;
		}
		// 2.根据code换取openid
		String openId = (String) map.get("openId");

		PageData pd = new PageData();
		pd.put("u_uuid", openId);
		PageData vote_record = activityService.userIsExistence(pd);
		if (vote_record == null) {
			// 3.然后通过openid获取用户信息
			// 3.1先获取accessToken
			String accessToken = (String) map.get("accessToken");
			// 3.2获取用户信息
			JSONObject userInfo = WeChatUtil.getUserInfo2(openId, accessToken);
			pd.put("u_name", userInfo.get("nickname"));
			pd.put("u_image", userInfo.get("headimgurl"));
			pd.put("u_register_time", DateUtil.getTime());
			activityService.saveUserInfo(pd);
		}
		// 查询所有投票数据
		略。。。。
		 //返回页面
		mv.setViewName("activity/activity");
		
		return mv;
		}
 }
 //工具类
 package cn.cnbuilder.controller.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cnbuilder.dao.redis.RedisDao;
import cn.cnbuilder.entity.AccessToken;
import cn.cnbuilder.entity.TicketJson;
import cn.cnbuilder.util.MyX509TrustManager;
import cn.cnbuilder.util.PageData;
import cn.cnbuilder.util.Tools;
import cn.cnbuilder.util.wxpay.Sign;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 * 公众平台通用接口工具类
 */
public class WeChatUtil {
	
	@Resource(name = "redisDaoImpl")
	private RedisDao redisDaoImpl;
	
	private static Logger log = LoggerFactory.getLogger(WeChatUtil.class);

	// 公众号的appid和secretid
	public static String WEIXIN_APPID = "xxxxxxxxxxxx";
	public static String WEIXIN_APPSECRET = "xxxxxxxxxxxxxx";

	// 获取access_token的接口地址(GET) 限200(次/天)
	public final static String access_Token_Url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ WEIXIN_APPID + "&secret=" + WEIXIN_APPSECRET;
	// 拉取用户信息(需scope为 snsapi_userinfo) ACCESS_TOKEN 是网页授权的ACCESS_TOKEN
	public final static String user_Info_Url_login = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID";

	// 获取用户基本信息(包括UnionID机制)
	public final static String user_Info_Url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

	// 用户同意授权,获取code
	public final static String scope_Code_Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
			+ WEIXIN_APPID
			+ "&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
	// 通过code换取网页授权access_token
	public final static String scope_OpenId_Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
			+ WEIXIN_APPID + "&secret=" + WEIXIN_APPSECRET + "&code=CODE&grant_type=authorization_code";
	// 由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
	public final static String refresh_token_Url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="
			+ WEIXIN_APPID + "&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";

	// 获取用户code值
	public final static String get_code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
			+ WEIXIN_APPID
			+ "&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

	//获取jsticket
	public final static String get_jsticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

	
	
	/**
	 * 通过code获取网页授权 和用户openid
	 * 
	 * @param code
	 * @return
	 */
	public static Map<String, Object> getOpenId(String code) {
		Map<String, Object> resMap = new HashMap<>();
		String openId = null;
		String accessToken = null;
		String refreshToken = null;
		String url = scope_OpenId_Url.replace("CODE", code);
		JSONObject jsonObject = httpRequest(url, "POST", null);
		log.info("WeChatUtil getOpenId=" + jsonObject);
		if (null != jsonObject) {
			if (!jsonObject.containsKey("errcode")) {
				openId = jsonObject.getString("openid");
				accessToken = jsonObject.getString("access_token");
				refreshToken = jsonObject.getString("refresh_token");
				resMap.put("openId", openId);
				resMap.put("accessToken", accessToken);
				resMap.put("refresh_token", refreshToken);
				return resMap;
			} else {
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.info("通过code换取网页授权失败errorCode:{" + errorCode + "},errmsg:{" + errorMsg + "}");
				System.out.println("通过code换取网页授权失败errorCode:{" + errorCode + "},errmsg:{" + errorMsg + "}");
			 
			}
		}
		return resMap;
		
	}

	/**
	 * 通过openId和accessToken获取当前用户的基本信息
	 * 
	 * @param openId
	 * @param accessToken
	 * @return
	 */
	public static JSONObject getUserInfo2(String openId, String accessToken) {
		String url = user_Info_Url.replace("OPENID", openId).replace("ACCESS_TOKEN", accessToken);
		JSONObject jsonObject = httpRequest(url, "POST", null);
		log.info("WeChatUtil getUserInfo=" + jsonObject);
		return jsonObject;
	}

	/**
	 * 通过appId和appSecretId获取accessToken
	 * 
	 * @date 2018年3月6日
	 * @return
	 */
	public static String getAccessToken() {
		String url = access_Token_Url.replace("WEIXIN_APPID", WEIXIN_APPID).replace("WEIXIN_APPSECRET",
				WEIXIN_APPSECRET);
		JSONObject jsonObject = httpRequest(url, "GET", null);
		log.info("WeChatUtil getAccessToken=" + jsonObject);
		return jsonObject.getString("access_token");
	}

	/**
	 * 获取用户code值
	 */
	public static String getCodeUrl(String redirect_uri) {
		String url = get_code_url.replace("REDIRECT_URI", redirect_uri);
		return url;

	}

	/**
	 * 刷新token有效期
	 * 
	 * @date 2018年3月6日
	 * @return
	 */
	public static String refreshToken(String REFRESH_TOKEN) {
		String url = refresh_token_Url.replace("REFRESH_TOKEN", REFRESH_TOKEN);
		JSONObject jsonObject = httpRequest(url, "GET", null);
		log.info("WeChatUtil refreshToken=" + jsonObject);
		return jsonObject.getString("access_token");
	}
	
	/**
	 * 获取jsticket
	 * 
	 * @date 2018年3月6日
	 * @return
	 */
	public static String getJsTicket(String accessToken) {
		String url = get_jsticket.replace("ACCESS_TOKEN", accessToken);
		JSONObject jsonObject = httpRequest(url, "GET", null);
		return jsonObject.getString("ticket");
	}
	
	/**
	 * URL编码(utf-8)
	 * 
	 * @param source
	 * @return
	 */
	public static String urlEncodeUTF8(String source) {
		String result = source;
		try {
			result = java.net.URLEncoder.encode(source, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			log.error("urlEncodeUTF8出现异常!\n" + e.getMessage());
		}
		return result;
	}

	public static String getWEIXIN_APPID() {
		return WEIXIN_APPID;
	}

	public static void setWEIXIN_APPID(String wEIXIN_APPID) {
		WEIXIN_APPID = wEIXIN_APPID;
	}

	public static String getWEIXIN_APPSECRET() {
		return WEIXIN_APPSECRET;
	}

	public static void setWEIXIN_APPSECRET(String wEIXIN_APPSECRET) {
		WEIXIN_APPSECRET = wEIXIN_APPSECRET;
	}

	/**
	 * 发起https请求并获取结果
	 * 
	 * @param requestUrl    请求地址
	 * @param requestMethod 请求方式(GET、POST)
	 * @param outputStr     提交的数据
	 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
	 */
	public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();
		try {
			// 创建SSLContext对象,并使用我们指定的信任管理器初始化
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();
			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);
			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			// 设置请求方式(GET/POST)
			httpUrlConn.setRequestMethod(requestMethod);
			if ("GET".equalsIgnoreCase(requestMethod))
				httpUrlConn.connect();
			// 当有数据需要提交时
			if (null != outputStr) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				// 注意编码格式,防止中文乱码
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			// 将返回的输入流转换成字符串
			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			// 释放资源
			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (ConnectException ce) {
			ce.printStackTrace();
			log.error("Weixin server connection timed out.");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("https request error:{}", e);
		}
		return jsonObject;
	}

	public static Map<String, String> sign(String jsapi_ticket, String url) {
		Map<String, String> ret = new HashMap<String, String>();
		String nonce_str = create_nonce_str();
		String timestamp = create_timestamp();
		String string1;
		String signature = "";

		// 注意这里参数名必须全部小写,且必须有序
		string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url;
		System.out.println(string1);

		try {
			MessageDigest crypt = MessageDigest.getInstance("SHA-1");
			crypt.reset();
			crypt.update(string1.getBytes("UTF-8"));
			signature = byteToHex(crypt.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		ret.put("url", url);
		ret.put("jsapi_ticket", jsapi_ticket);
		ret.put("nonceStr", nonce_str);
		ret.put("timestamp", timestamp);
		ret.put("signature", signature);

		return ret;
	}

	private static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}

	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}
 
	
	public static void main(String[] args) {

		// 注意 URL 一定要动态获取,不能 hardcode
		String url = "http://gwchsk.imwork.net/wechat/order/test.html";
		String accessToken = getAccessToken();
		String jsTicket = getJsTicket(accessToken);
		Map<String, String> ret = sign(jsTicket,url);
		for (Map.Entry entry : ret.entrySet()) {
			System.out.println(entry.getKey() + "=" + entry.getValue());
		}
	}

}
 

上面就是一套完整的微信网页推送流程,有什么不懂就联系我。


鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。 微信: 支付宝:


感谢一路支持我的人。。。。。
Love me and hold me
QQ:69673804(16年老号)
EMAIL:69673804@qq.com
友链交换
如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。

------------


网站名称:KingYiFan’S Blog
网站地址:http://blog.cnbuilder.cn
网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
网站Logo/头像:http://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg