微信公众号第三方平台投票

        在微信公众号中我们会进行投票,那么投票我们该如何实现此功能呢?实现投票需要访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。

目录

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

5 附:检验授权凭证(access_token)是否有效

我们通过此地址进行访问

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>



免费微信投票管理系统源码,独家抽奖功能,微信投票系统适合萌宝大赛,男神女神大赛等,10套模板后台随意切换,支持用户自主上传照片,加粉神器。 这个免费微信投票系统源码可同时支持编号投票和网页直接投票相结合,做到真正用户体验!未关注的用户可以关注后直接回复编号更方便,而已关注的用户可以直接进入活动页面点击投票,更加人性化! 微信公众平台投票系统功能: 全程界面后台自定义程度同类型最高,完全没有任何限制 微信投票源码提供多套投票主题风格可供选择 防刷票功能,后台开启验证码,可微信对话框输入id投票。 微信投票管理系统源码可以自定义可投票地区ip,限制每个ip的投票次数。 新增人数总用户记录查看,独立的第三方统计代码放置位,让你了解投票给你带来的流量、ip等信息! 该微信投票管理系统可以设置投票者每投一票奖励积分 在线报名,报名时间和投票时间自定义,可设置投票期间也可报名 此版本的投票系统源码新增加投票提醒功能(报名者拉票后,他们帮其投票时,报名者会收到提示!某某人帮我投了一票) 源码所有功能开放免费,安装过程中有任何问题,可以联系QQ 3343582009 ,互相探讨交流。每个月都有新升级的微信投票系统完成,供大家免费使用,性能更稳定,欢迎qq交流。
### 关于微信第三方平台微信公众号的集成或开发 #### 微信公众号开发概述 通过符合微信公众号的语法规范,可以实现与在公众号官网后台相同的功能,并能利用一系列支持的接口满足特定的定制化需求[^1]。 #### 功能特性与优势 微信公众号开发允许用户自定义管理和扩展公众号功能,不仅限于官方提供的基础服务。其主要特点是高度自定义性和良好的可扩展性,使得操作流程更为简便,降低了公众号的操作难度。为了充分利用这些特性,在研发初期建议申请一个具备全面权限的公众号账号;如果使用的是未经认证的公众号,则需注意某些高级功能可能无法启用。此外,遵循官方文档中的语法规则至关重要。 #### 技术准备与资源链接 对于技术实施方面,微信公众平台提供了详细的开发者文档作为指导材料。另外,GitHub上的WxJava项目是一个非常有价值的工具集,它包含了众多预构建的消息处理函数和其他实用组件,极大地简化了诸如XML消息解析等工作。该项目地址为:https://github.com/Wechat-Group/WxJava【此处应有脚注但因规则限制省略】 #### 用户身份验证机制 获取用户的`openid`以及完成网页授权是微信应用开发过程中的一个重要环节。这通常涉及到OAuth2.0协议的应用场景之一——即当用户访问某个受保护页面时,系统会引导他们登录并同意授予应用程序一定的权限范围内的个人信息访问权。一旦获得许可,服务器端就可以调用微信API来交换得到该用户的唯一标识符(`openid`)以及其他相关信息[^2]。 ```java // 示例代码片段展示如何发起OAuth2.0请求以获取code参数用于后续换取access_token及openid String url = "https://open.weixin.qq.com/connect/oauth2/authorize"; Map<String, Object> params = new HashMap<>(); params.put("appid", appid); params.put("redirect_uri", redirectUri); params.put("response_type", "code"); params.put("scope", "snsapi_userinfo"); // 或者 snsapi_base 取决于所需权限级别 params.put("state", state); StringBuilder paramStrBuilder = new StringBuilder(); for (Entry<String, Object> entry : params.entrySet()) { paramStrBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } paramStrBuilder.deleteCharAt(paramStrBuilder.length() - 1); // 移除最后一个&字符 HttpServletResponse response.sendRedirect(url + "?" + paramStrBuilder.toString()); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值