前端:
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script type="text/javascript"> wx.config({ debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出, appId: "{$appId}", timestamp:"{$timestamp}", nonceStr: "{$nonceStr}", signature:"{$signature}", jsApiList: [//需要使用的JS接口列表,分享默认这几个,如果有其他的功能比如图片上传之类的,需要添加对应api进 'updateAppMessageShareData' ] }); wx.ready(function(){ var shareData = { title:"分享的标题", desc:"描述的内容", link:"http://tp.xxxx.cn", imgUrl:"http://tp.xxxx.cn/static/index/img/img/1.jpg" }; wx.updateAppMessageShareData(shareData); }); wx.error(function(res){ console.log(res.errMsg); }); </script>
后端:
引入jssdk方式
控制器:
// http_curl函数 public function http_curl($url,$type='get',$res='json',$arr='') { /* $url 请求的url $type 请求类型 $res 返回数据类型 $arr post请求参数 */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); } $output = curl_exec($ch); curl_close($ch); if ($res == 'json') { return json_decode($output, true); } }
// 首页进入 分享功能 public function index() { $jssdkObj = new \Jssdk('wxc74b8b385acb6e95','8be8ba94c72533f95bd0ca5b8075b06c'); $res = $jssdkObj->getSignPackage(); $appId = $res['appId']; $timestamp = $res['timestamp']; $nonceStr = $res['nonceStr']; $signature = $res['signature']; $this->assign( array( 'appId'=>$appId, 'timestamp'=>$timestamp, 'nonceStr'=>$nonceStr, 'signature'=>$signature, ) ); // 展示参赛列表 $getlst = Db::name('msg')->where('status',1)->order('piaoshu desc,id desc')->select(); $this->assign('getlst',$getlst); return view(); }
// 参赛功能 public function add() { if(request()->isAjax()) { //1、 获取数据 $data['username'] = input('post.username'); $data['tel'] = input('post.tel'); $data['des'] = input('post.des'); $data['create_time'] = date('Y-m-d H:i', time()); // 时间 $data['ip']=request()->ip();// ip try{ // 获取图片 $file = request()->file('img'); // 配置大小 图片3MB $info = $file->validate(['size'=>1024*1024*3,'ext'=>'jpeg,jpg,png,gif'])->move( '../public/uploads/'); if($info){ // 打开图片 $image = \think\Image::open('../public/uploads/'. $info->getSaveName()); // 裁剪图片等比例缩放 $image->thumb(320, 500,\think\Image::THUMB_SCALING)->save('../public/uploads/'.$info->getSaveName()); //保存图片名称 $fileimg = str_replace("\\","/",$info->getSaveName()); }else{ return json(['status'=>0,'msg'=>$file->getError()]); } }catch (\Exception $e) { return json(['status'=>0,'url'=>'add','msg'=>"没有上传图片"]); } $data['img'] = $fileimg; // 模型添加数据 $indexmodel = new IndexModel(); $result =$indexmodel->save($data); if($result){ // 发送邮件 // sendEmail('444903516@qq.com', '微信活动参与',"姓名:".$data['username']."电话:".$data['tel']); return json(['status'=>1,'url'=>'index','msg'=>'参赛成功']); }else{ //return json(['url'=>'index','msg'=>'参赛失败','status'=>'400']); return json(['status'=>0,'msg'=>'参赛成功']); } } return view(); }
// 投票方法 public function toupiao() { if(request()->isAjax()){ // 1、获取给谁投票id $id = input('post.id'); $oppenid =session::get('userid'); // $oppenid ='appid2'; // 2. 查询当前appid的票数是否已经达到2票 $getps = Db::name('user')->where('userid',$oppenid)->value('ps'); // 3、根据参数人数日期 判断 是否为今天 if($getps <= 1){ // 获取总票数 达到1000无法投票 $getcountres = Db::name('msg')->where('id',$id)->value('piaoshu'); // if($getcountres = 1000){ // return json(['status'=>0,'msg'=>'最高票数啦']); //} // 被投票人的票数+1 $tpres = Db::name('msg')->where('id',$id)->setInc('piaoshu',1); if($tpres){ // 发送邮件 //sendEmail('2065646680@qq.com', '微信邮件',$data['ip']); // 当前微信号人的投票次数+1 Db::name('user')->where('userid',$oppenid)->update(['ps'=>$getps+1]); // 获取总投票数 $renshu = Db::name('msg')->where('id',$id)->value('piaoshu'); return json(['status'=>1,'msg'=>'投票成功啦','numres'=>$renshu]); }else{ return json(['status'=>0,'msg'=>'投票失败']); } }else{ return json(['status'=>0,'msg'=>'每天只能投2票哦']); } } }
<?php namespace app\index\controller; use think\Controller; use think\Db; use think\Request; use app\index\model\Index as IndexModel; use think\facade\Session; // 微信jssdk require '../extend/org/wechat/jssdk.php'; class Index extends Common { public function initialize() { parent::initialize(); } public function share(){ $jssdkObj = new \Jssdk('wxc74b8b385acb6e9','8be8ba94c72533f95bd0ca5b8075b06'); $res = $jssdkObj->getSignPackage(); $appId = $res['appId']; $timestamp = $res['timestamp']; $nonceStr = $res['nonceStr']; $signature = $res['signature']; $res=array( 'appId'=>$appId, 'timestamp'=>$timestamp, 'nonceStr'=>$nonceStr, 'signature'=>$signature, ); exit(json_encode(['code'=>200,'msg'=>'success','data'=>$res])); } // 首页进入 分享功能 public function index() { $jssdkObj = new \Jssdk('wxc74b8b385acb6e95','8be8ba94c72533f95bd0ca5b8075b06c'); $res = $jssdkObj->getSignPackage(); $appId = $res['appId']; $timestamp = $res['timestamp']; $nonceStr = $res['nonceStr']; $signature = $res['signature']; $this->assign( array( 'appId'=>$appId, 'timestamp'=>$timestamp, 'nonceStr'=>$nonceStr, 'signature'=>$signature, ) ); // 展示参赛列表 $getlst = Db::name('msg')->where('status',1)->order('piaoshu desc,id desc')->select(); $this->assign('getlst',$getlst); return view(); } // 详情页 public function shows($id = 0) { $res = Db::name('msg')->where('id',$id)->find(); $this->assign('res',$res); return view(); } // 搜索 public function search(){ if(request()->isPost()){ $data = input('post.str'); $type = input('post.n'); if($type=="number"){ $res = Db::name('msg')->where('id',$data)->select(); }else{ $res = Db::name('msg')->where('username',$data)->select(); } $this->assign('getsearchlst',$res); } return view(); } // 规则 public function gz() { return view(); } // 排名 public function pm() { $getpx = Db::name('msg')->where('status',1)->order('piaoshu desc')->select(); $this->assign('getpx',$getpx); return view(); } // 参赛功能 public function add() { if(request()->isAjax()) { //1、 获取数据 $data['username'] = input('post.username'); $data['tel'] = input('post.tel'); $data['des'] = input('post.des'); $data['create_time'] = date('Y-m-d H:i', time()); // 时间 $data['ip']=request()->ip();// ip try{ // 获取图片 $file = request()->file('img'); // 配置大小 图片3MB $info = $file->validate(['size'=>1024*1024*3,'ext'=>'jpeg,jpg,png,gif'])->move( '../public/uploads/'); if($info){ // 打开图片 $image = \think\Image::open('../public/uploads/'. $info->getSaveName()); // 裁剪图片等比例缩放 $image->thumb(320, 500,\think\Image::THUMB_SCALING)->save('../public/uploads/'.$info->getSaveName()); //保存图片名称 $fileimg = str_replace("\\","/",$info->getSaveName()); }else{ return json(['status'=>0,'msg'=>$file->getError()]); } }catch (\Exception $e) { return json(['status'=>0,'url'=>'add','msg'=>"没有上传图片"]); } $data['img'] = $fileimg; // 模型添加数据 $indexmodel = new IndexModel(); $result =$indexmodel->save($data); if($result){ // 发送邮件 // sendEmail('xxx@qq.com', '微信活动参与',"姓名:".$data['username']."电话:".$data['tel']); return json(['status'=>1,'url'=>'index','msg'=>'参赛成功']); }else{ //return json(['url'=>'index','msg'=>'参赛失败','status'=>'400']); return json(['status'=>0,'msg'=>'参赛成功']); } } return view(); } // 投票方法 public function toupiao() { if(request()->isAjax()){ // 1、获取给谁投票id $id = input('post.id'); $oppenid =session::get('userid'); // $oppenid ='appid2'; // 2. 查询当前appid的票数是否已经达到2票 $getps = Db::name('user')->where('userid',$oppenid)->value('ps'); // 3、根据参数人数日期 判断 是否为今天 if($getps <= 1){ // 获取总票数 达到1000无法投票 $getcountres = Db::name('msg')->where('id',$id)->value('piaoshu'); // if($getcountres = 1000){ // return json(['status'=>0,'msg'=>'最高票数啦']); // } // 被投票人的票数+1 $tpres = Db::name('msg')->where('id',$id)->setInc('piaoshu',1); if($tpres){ // 发送邮件 //sendEmail('xxxxx@qq.com', '微信邮件',$data['ip']); // 当前微信号人的投票次数+1 Db::name('user')->where('userid',$oppenid)->update(['ps'=>$getps+1]); // 获取总投票数 $renshu = Db::name('msg')->where('id',$id)->value('piaoshu'); return json(['status'=>1,'msg'=>'投票成功啦','numres'=>$renshu]); }else{ return json(['status'=>0,'msg'=>'投票失败']); } }else{ return json(['status'=>0,'msg'=>'每天只能投2票哦']); } } } // 入口 public function getBaseInfo() { $appid = 'wxc74b8b385acb6e9'; $redirect_uri = urlencode('http://tp.hexinju.cn/index/index/getUserlst'); $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; header('location:'.$url); } // 获取oppid 并存入数据库 public function getWxCode() { $appid = "wxc74b8b385acb6e9"; $appsecrt = "8be8ba94c72533f95bd0ca5b8075b06"; $code = $_GET['code']; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecrt."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url,'get'); $openid = $res['openid']; //echo "你的oppenid是:".$openid; //var_dump($res); $data = ['userid'=>$openid]; $result = Db::name('user')->where('userid',$openid)->find(); session('userid',$openid); // dump(session::get('userid')); if($result){ //$this->success('已存在数据库中','index'); $this->redirect('/index/index/index'); }else{ Db::name('user')->insert($data); //$this->success('存入数据库成功','index'); $this->redirect('/index/index/index'); } } // 执行每天12点更新票数 public function clearTime() { Db::query("update hxj_user set ps = 0"); } // 用户测试 public function gettestinfo() { $appid = 'wxc74b8b385acb6e9'; $redirect_uri = urlencode('http://tp.hexinju.cn/index/index/getWxtest'); $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; header('location:'.$url); } public function getWxtest() { $appid = "wxc74b8b385acb6e9"; $appsecrt = "8be8ba94c72533f95bd0ca5b8075b06"; $code = $_GET['code']; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecrt."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url,'get'); $openid = $res['openid']; echo "用户测试中,oppenid是:".$openid; //var_dump($res); } // http_curl函数 public function http_curl($url,$type='get',$res='json',$arr='') { /* $url 请求的url $type 请求类型 $res 返回数据类型 $arr post请求参数 */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); } $output = curl_exec($ch); curl_close($ch); if ($res == 'json') { return json_decode($output, true); } } // 二维码 public function ewm() { return view(); } // 详细授权 public function getUserDetail() { $appid = 'wxc74b8b385acb6e9'; $redirect_uri = urlencode('http://tp.hexinju.cn/index/index/getUserlst'); $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; header('location:'.$url); } public function getUserInfo() { $appid = "wx6ef6c8370044da1"; $appsecrt = "b470f344e0258aa83f782d108c43f4d"; $code = $_GET['code']; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecrt."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url,'get'); $access_token = $res['access_token']; $openid = $res['openid']; // 拉取用户详细信息 $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $res = $this->http_curl($url); var_dump($res); } // 获取关注用户列表 public function getUserlst() { $appid = "wxc74b8b385acb6e9"; $appsecrt = "8be8ba94c72533f95bd0ca5b8075b06"; $code = $_GET['code']; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecrt."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url,'get'); $access_token = $res['access_token']; $openid = $res['openid']; $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecrt"; $access_msg = json_decode(file_get_contents($access_token)); // dump($access_msg); $token = $access_msg->access_token; // dump($token); $subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid"; // dump($subscribe_msg) $subscribe = json_decode(file_get_contents($subscribe_msg)); $gzxx = $subscribe->subscribe; $data['userid']=$openid; $data['create_time'] = time(); // dump($gzxx); if($gzxx === 1){ $result = Db::name('user')->where('userid',$openid)->find(); session('userid',$openid); // dump(session::get('userid')); if($result){ //$this->success('已存在数据库中','index'); $this->redirect('/index/index/index'); }else{ Db::name('user')->insert($data); //$this->success('存入数据库成功','index'); $this->redirect('/index/index/index'); } }else{ // echo "请先关注公众号"; $this->redirect('/index/index/ewm'); } } }