微信投票:(设计网页,网页授权获取access_token,设计投票方式)
第一步:先要网页授权获取code:在IndexController.class.php中:
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$appid='wx8de5dc1fbeac393c';
$redirect_uri = urlencode('http://yuanyue.top/vote/index.php/home/index/validUser');
$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);
}
第二步:通过code换取网页授权获取access_token并拉取用户信息:
public function validUser(){
$code=$_GET["code"];
// echo $code;
$json = $this->access_token($code);
$arr = json_decode($json,true);//json->array
$arr = array_change_key_case($arr,CASE_LOWER);//将数组中的键转化为小写
if(isset($arr['access_token']) && isset($arr['openid'])){
$this->getUserInfo($arr['access_token'],$arr['openid']);
}else{
echo '获取access_token出错'.$json;
}
}
private function access_token($code){
$appid = "wx8de5dc1fbeac393c";
$appsecret = "878c4252f00904a06fbc78b75b96b057";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
// return $url;
$ret = https_request($url);
return $ret;
}
第三步:拉取用户信息(利用openid):
private function getUserInfo($access_token,$openid){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid";
$json = https_request($url);
// echo $json;
$userinfo_array = json_decode($json,true);
$userinfo = array_change_key_case($userinfo_array,CASE_LOWER);
$fans = M('fans');
$data = $fans->where("openid='{$userinfo['openid']}'")->find();
if(empty($data)){
$ret = $fans->add($userinfo);
if($ret){
$this->display('Vote/index');
}else{
echo 'error';
}
}else{
$this->display('Vote/index');
}
}
第四步:保存投票信息,并添加到数据库中:在VoteController.class.php:
<?php
namespace Home\Controller;
use Think\Controller;
class VoteController extends Controller{
public function index(){
$this->display();
}
public function vote(){
$data = M('group')->select();
// print_r($data);
// exit;
$this->assign('data',$data);
$this->display();
}
public function save_vote($groupid){
$openid = "oJUeY1Ul_RKOkh3cGzkZB8lbxgBc";
$where['openid'] = $openid;
$where['groupid'] = $groupid;
$data = M('vote')->where($where)->find();
if(empty($data)){
M('vote')->add(array('openid'=>$openid,'groupid'=>$groupid,'vote_time'=>time()));
$this->ajaxReturn(array('errcode'=>0,'msg'=>'投票成功,谢谢参与'));
}else{
$this->ajaxReturn(array('errcode'=>1,'msg'=>'您已经投过票,每个队只能投一次'));
}
}
第五步:写投票方式,分为4种不同的条件:
private function buildwhere($type,$openid,$groupid){
//查询条件
$where = array();
$y = date("Y");
$m = date("m");
$d = date("d");
//将今天开始的年月日时分秒,转换成unix时间戳(开始示例:2015-10-10 00:00:00)
$todayStart = mktime(0,0,0,$m,$d,$y);
$todayEnd = mktime(23,59,59,$m,$d,$y);
switch ($type) {
case 1:
//每队仅能投一次.
$where['openid'] = $openid;
$where['groupid'] = $groupid;
break;
case 2:
//仅能投一次
$where['openid'] = $openid;
break;
case 3:
//给每个队一天仅能投一次
$where['openid'] = $openid;
$where['groupid'] = $groupid;
$where['vote_time'] = array('between',"$todayStart,$todayEnd");
break;
case 4:
//每天仅能投一次.
$where['openid'] = $openid;
$where['vote_time'] = array('between',"$todayStart,$todayEnd");
break;
default:
break;
}
return $where;
}
}
?>