在微信公众号中我们会进行投票,那么投票我们该如何实现此功能呢?实现投票需要访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。
目录
2 第二步:通过code换取网页授权access_token
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
我们通过此地址进行访问
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
以下是详细代码
public function index(){
$appid="这里是个人APPID";
$redirect_uri=urlencode('http://www.qidoudouzts.top/vote/index.php/home/index/getcode');
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
}
function getcode(){
$code=$_GET['code'];
$json=$this->access_token($code);
// echo $json;
$arr=json_decode($json,true);
$arr=array_change_key_case($arr,CASE_LOWER);
if (isset($arr['access_token']) && isset($arr['openid'])) {
$this->userinfo($arr['access_token'],$arr['openid']);
//保存回话
session('openid',$arr['openid']);
//跳转
$this->redirect('Vote/index');//这里要跳到投票界面,来进行投票
}else{
echo "获取出错".$json;
}
}
(投票)voteController控制器
function index(){
$data= M('group')->select();//从表中取出被投票成员
$this->assign('data',$data);
$this->display();
}
public function save_vote($groupid){
$openid=session('openid');
$where['openid']=session('openid');
// $openid="oOPIO1Z3kCGUQ-yQJnMFsk08qfqw";
// $where['openid']=$openid;
$where['groupid']=$groupid;
$ret=$this->buildwhere(3,$openid,$groupid);
$data=M('tou')->where($where)->find();
if (empty($data)) {
M('tou')->add(array('openid'=>$openid,'groupid'=>$groupid,'vote_time'=>time()));
$this->ajaxReturn(array('errcode'=>0,'msg'=>"投票成功"));
}else{
$this->ajaxReturn(array('errcode'=>1,'msg'=>$ret['rule']));
}
}
前台代码
<!DOCTYPE html>
<html>
<head>
<title>jQuery WeUI</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
">
<link rel="stylesheet" href="__PUBLIC__/lib/weui.min.css">
<link rel="stylesheet" href="__PUBLIC__/css/jquery-weui.css">
<link rel="stylesheet" href="__PUBLIC__/demos/css/demos.css">
</head>
<body ontouchstart>
<div class="header">投票界面</div>
<div class="weui-search-bar" id="searchBar">
<form class="weui-search-bar__form" action="#">
<div class="weui-search-bar__box">
<i class="weui-icon-search"></i>
<input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required="">
<a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
</div>
<label class="weui-search-bar__label" id="searchText" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);">
<i class="weui-icon-search"></i>
<span>搜索</span>
</label>
</form>
<a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
</div>
<volist name="data" id="row">
<div class="weui-panel__bd">
<a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">
<div class="weui-media-box__hd">
<img class="weui-media-box__thumb" src="__PUBLIC__/demos/images/avatar.jpg " alt="">
</div>
<div class="weui-media-box__bd">
<h4 class="weui-media-box__title">选手编号{$row.groupid}</h4>
<h4 class="weui-media-box__title">选手组号{$row.groupname}</h4>
<p class="weui-media-box__desc">
<!-- <a href="javascript:;" class="weui-btn weui-btn_primary">投我</a> -->
<input type="button" οnclick="vote('{$row.groupid}')" value="投我"> </p>
</div>
</a>
</volist>
</div>
<script src="__PUBLIC__/lib/jquery-2.1.4.js"></script>
<script src="__PUBLIC__/lib/fastclick.js"></script>
<script>
function vote(groupid){
$.post("{:U('save_vote')}", {groupid: groupid}, function(data ) {
if (data.errcode=='0') {
alert("投票成功",'success');
}else{
alert(data.msg);
}
},'json');
}
</script>
<script src="__PUBLIC__/js/jquery-weui.js"></script>
</body>
</html>