公司之前推出的大赛类营销活动。考虑到十一送流量营销活动因为网上恶意刷单造成公司在活动期间出现较大损失,这次投票活动要着重考虑网上恶意刷票。调研了网上防刷单实现方式,基本是ip + cookie方式,但并无法真正达到要求,因此考虑借助微信公众平台来实现。通过调研微信开发者平台,pc端我们采用微信网页登陆授权方式,移动端采用微信网页授权方式来实现锁定用户,防止网上的恶意刷单。
微信网页登陆授权api参见
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
微信网页授权api参见
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
pc端实现逻辑如下
页面嵌入微信二维码代码
需引入
if (openid == undefined || openid==null || openid=="") {
var obj = new WxLogin({
id: "login_container",
appid: appid,
scope: "snsapi_login",
redirect_uri: encodeURIComponent(redirect_uri),
state: state,
style: "black",
href: ""
});
//$('#wxauth').css("display", "block");
weixinTPWeixinLogin();
}
java代码-回调函数
/**
* 微信授权登录回调方法
* @Title: weixinGameCallBack
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param code
* @param backurl 设定文件
* @return void 返回类型
* @throws
*/
public void weixinGameCallBack(String code,String backurl){
logger.info("weixinGameCallBack param:code={}",code);
JSONObject json=new JSONObject();
if(StringUtils.isBlank(code)){
json.put("resultcode", "-1001");
json.put("msg", "param code is empty!");
this.renderJSON(json.toString());
return;
}
if(StringUtils.isBlank(backurl)){
json.put("resultcode", "-1002");
json.put("msg", "param backurl is empty!");
this.renderJSON(json.toString());
return;
}
try {
String data = getWxAuthInfo(code);
logger.info("QuickController-weixinGameCallBack ,获取unionid;返回=data"
+ data);
JSONObject dataJson = JSONObject.fromObject(data);
String unionid = dataJson.getString("unionid");
if(StringUtil.isNotBlank(backurl)){
try {
backurl=URLDecoder.decode(backurl, "utf-8");
} catch (UnsupportedEncodingException e) {
logger.error("==URLDecoder decode error:{}",e);
}
}
if(StringUtil.isNotBlank(unionid)){
com.csi.jointforce.common.model.AESCrypt aesCrypt=com.csi.jointforce.common.model.AESCrypt.getInstance();
String aesKey=JFconfigHelper.gameYmlConfigInstance().getAesKey();
logger.info("QuickController openId:{};aesKey:{}",unionid,aesKey);
unionid=URLEncoder.encode(aesCrypt.crypt(unionid,aesKey),"utf-8");
logger.info("QuickController return openId:{};",unionid);
if(backurl.contains("?")){
backurl=backurl+"&openId="+unionid;
}else{
backurl=backurl+"?openId="+unionid;
}
}
this.redirectToUrl(backurl);
} catch (Exception e) {
logger.error("QuickController-weixinGameCallBack Error:{}",e);
json.put("resultcode", "-5000");
json.put("msg", "Unknown error occurred on the server");
this.renderJSON(json.toString());
return;
}
}
/*
* return 根据code 获取openId信息
*/
public String getWxAuthInfo(String code){
JSONObject json;
try {
String appid =getWebAppIdAndSecret(0);
String secret = getWebAppIdAndSecret(1);
String grant_type = WeiXinBindConstant.grant_type;
StringBuffer sb = new StringBuffer(WeiXinBindConstant.WxAuthUrl);
sb.append("appid=" + appid + "&");
sb.append("secret=" + secret + "&");
sb.append("code=" + code + "&");
sb.append("grant_type=" + grant_type);
String apipath = PageUtil.parseUrl(sb.toString());
RestAPIClient rest = RestAPIClient.getInstance();
rest.setPost(false);
json = rest.call(apipath);
return json.toString();
} catch (Exception e) {
logger.info("QuickController-getWxAuthInfo 根据code获取openId信息,出现异常,异常信息为:"+e);
}
return null;
}
这里需要注意微信公众平台(微信网页授权)和微信开发平台(微信网页登陆授权)获取的openid不是一个,这里需要采用UnionID机制,具体参见微信公众平台开发文档。