微信公众号发红包活动

    xiaoxiao2021-11-13  52

    先看看效果图:

    上图这个效果,可能会有疑问,等等下边会解释为何这样的版面。

    可以到自己的零钱里看看,是不是真的领到红包了,答案是肯定的。

    为何领取红包的版面不是网上或者官方宣传的那种呢?其实是因为:

    官方说的很详细了,首先要关注公众号,并且关注的时间超过50个小时,并且还的允许接收“接收消息”,

    官方宣传的样式是防伪消息,两者的区别是这样的,真是一比较就看出差距了:

    活动说明:

    打开活动介绍界面,下边有个按钮触发抢红包事件,其实是页面跳转,在红包页面自动获取红包,

    获取成功后,返回和公众号的聊天窗口就能查到红包消息了,点击消息就可以拆红包了。

    首页index.php

    获取红包页面:getRedPack.php

    类库:

    include("WxPay.php"); require_once "jssdk.php"; require_once "interface/common.php";

    先上首页index.php:

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title></title> <script src="js/jquery-1.7.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function pageRedrict(){ $("#divWaiting").show(); document.location.replace("getRedPack.php"); } </script> </head> <body style="margin: 0;padding: 0;" οnclick="closeWin()"> <div style="width: 100%;"><img src="img/tc_bg.png" style="width: 100%;" /></div> <div οnclick="pageRedrict()" style="background-color: #c70202;color: #ffffff;margin-bottom: 50px; width: 100px;margin:0 auto;text-align: center;height: 35px;margin-top:20px;line-height: 35px;">抢红包</div> <div style="height: 50px;"> </div> <div style="position: fixed;top:200px;left: 0;width: 100%;display: none;" id="divWaiting"> <div style="background: #3e3e3e;width: 200px;height: 60px;text-align: center;margin: 0 auto;color: #ffffff;line-height: 60px;border-radius: 5px;">正在获取红包...</div> </div> </body> </html>

    getRedPack.php

    思路:

    先通过用户允许获取Code,然后通过Code获取用户OpenID,再按照规定的格式拼接字符串XML,

    然后再发送给服务器,接收服务器返回结果,因服务器返回XML格式的字符串,接收要转换为数组,

    方便针对成功和失败不同的处理方式。

    <?php define('APPID', "22222"); define('MCHID', "33333"); define('PARTNERKEY', "eeeeeeeee"); include("WxPay.php"); require_once "jssdk.php"; require_once "interface/common.php"; $jssdk = new JSSDK("33333333", "444444444444444"); $signPackage = $jssdk->GetSignPackage(); $openid; ?> <?php if (isset($_GET['code'])){ $access_token=$jssdk->get_access_token($_GET['code']); try{ $openid=$access_token->openid; // echo $openid; $token=$access_token->access_token; $userinfo=$jssdk->getUserInfo($token,$openid); $url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; getwxPay($openid); } catch(exception $exp){ //header("Location:http://www.sqjushi.com/baishi/index.php"); echo "<script>alert('请重新打开本页面');</script>"; } }else{ //echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; $url=$jssdk->getCode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); } function getbillno(){ $arr=date('Ymd',time()); for($i=0;$i<10;$i++){ $arr.=rand(0,9); } return MCHID."".$arr; } function getrandNum(){ return rand(0,99); } function getwxPay($openid){ $common=new common(); $money=100+getrandNum();//单位是分,这样处理的目的是把金额控制在1-2元之间, $sender="巨石网络"; $obj2=array(); $obj2['mch_billno']="".getbillno();//订单号 $obj2['mch_id']="".MCHID; $obj2['wxappid']="".APPID; $obj2['send_name']="宿迁市巨石网络"; $obj2['re_openid']="".$openid;//发给谁 $obj2['total_amount']=$money; //金额,单位是分 $obj2['total_num']=1;//数量,普通红包一次只能给一个用户发送一个红包 $obj2['wishing']="巨石网络,技术强悍!"; $obj2['client_ip']="".$common->GetIP(); $obj2['act_name']="巨石网络红包现行";//这里有坑,好像只能是8个字,或者最多8个字,超过8个字,蛋疼的提示。 $obj2['remark']="不要自私,记得分享给朋友哦"; //var_dump($obj2); $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; $wxpayHelper=new WxPay(); $result=$wxpayHelper->pay($url, $obj2); $temp=array(); $temp=$common->xmlToArray($result); // echo "<script>alert('".$temp['return_msg']."');</script>"; if($temp['return_code']=="SUCCESS"&&$temp['return_msg']=="发放成功"){ echo "<script>alert('红包已经成功发送,请返回查收!');</script>"; } else{ echo "<script>alert('".$temp['return_msg']."');</script>"; echo "<script>document.location.replace('index.php');</script>"; } //echo "<script>alert('".$temp->return_msg."');</script>"; // var_dump($temp); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title></title> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> //通过config接口注入权限验证配置 //, user-scalable=no wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '333333333', // 必填,公众号的唯一标识 timestamp: '<?php echo $signPackage['timestamp'];?>', // 必填,生成签名的时间戳 nonceStr: '<?php echo $signPackage['nonceStr'];?>', // 必填,生成签名的随机串 signature: '<?php echo $signPackage['signature'];?>',// 必填,签名 jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'hideOptionMenu', 'onMenuShareAppMessage' ] // 必填,需要使用的JS接口列表 }); //通过ready接口处理成功验证 wx.ready(function(){ // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后 function closeWin(){ wx.closeWindow(); } }); </script> </head> <body > </body> </html> WxPay.php

    大伙研究的成果,加班熬夜都不容易,核心代码有偿提供,

    可以加QQ【705722325】索取,10块8块也是爱。

    微信支付,好了告诉我就行了,没有标准价格,纯粹自由支付

    jssdk.php

    同上。

    common.php

    <?php class common { public function GetIP() {//获取客户端IP if (!empty($_SERVER["HTTP_CLIENT_IP"])) { $cip = $_SERVER["HTTP_CLIENT_IP"]; } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif (!empty($_SERVER["REMOTE_ADDR"])) { $cip = $_SERVER["REMOTE_ADDR"]; } else { $cip = "无法获取!"; } return $cip; } public function xmlToArray($xml) {//xml转为数组 //禁止引用外部xml实体 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring), true); return $val; } }

    有几个坑需要注意:

    坑一、根据官方API你会打开这个网站:http://www.itrus.cn/html/fuwuyuzhichi/436.html

    因为按照要求,服务器需要支持https协议,你会发现天威诚信的邮箱服务器已经关机了,邮件根本无法发送。

    参照其他步骤做了一遍,也不知道是不是起效果了,暂且认为是有效了吧。

    坑二、控制用户领取红包数量

    微信支付|商户平台,登陆后,

    点击修改,用户领取上限,改为10个,真正领取6个就不行了,提示超限了。

    转载请注明原文地址: https://ju.6miu.com/read-678252.html

    最新回复(0)