首页 > 代码库 > 微信消息模板

微信消息模板

  1 <?php
  2 class refund_success {
  3     public $appid = ‘‘; //wxcf1349c1fd949597
  4     public $appsecret = ‘‘; //0d5e3d5ee7955e524088291d4fbe7546__construct  ($_POST[‘mid‘])
  5     public $mid = ‘‘;
  6     function __construct($mid) {
  7         //echo $mid;die;
  8         $config = M(cashier_payconfig)->getwxuserConf($mid);
  9         // var_dump($config[‘appid‘]);die;
 10         //file_put_contents(‘./upload/log/tet.txt‘, $config);
 11         $this->appid = $config[appid];
 12         $this->appsecret = $config[appSecret];
 13         //file_put_contents(‘./upload/log/appsecret.txt‘, $this->appsecret);
 14         //file_put_contents(‘./upload/log/mmmmmid.txt‘, $mid);
 15         $this->mid = $mid;
 16     }
 17     public function bonus_success($openid, $wxmodel_id, $order_id, $price) {
 18         if ($_SESSION[mid] == ‘‘) {
 19             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid;
 20         } else {
 21             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid . &openid= . $openid;
 22         }
 23         $template = array(touser => $openid, template_id => $wxmodel_id, //‘eEKd8R0JVbod1yx2xPWFMrRoXGgOI-KuOqyBvk5CeAg‘,
 24         url => $urlback, topcolor => #FF0000, data => array(first => [value => urlencode(您好,您的分红返现已成功到账,请注意查收), color => #173177], order => [value => urlencode($order_id) ], money => [value => urlencode($price . ) ], remark => [value => urlencode(分红返现已成功到账至您的余额账户,乐呗收银祝您生活愉快!) ],),);
 25         $url = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= . $this->getAccessToken();
 26         // file_put_contents(‘./upload/log/889.txt‘, $url);
 27         $ret = $this->post($url, urldecode(json_encode($template)));
 28         return $ret;
 29     }
 30     public function refund($openid, $nickname, $order_id, $price, $wxmodel_id) {
 31         if ($_SESSION[mid] == ‘‘) {
 32             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid;
 33         } else {
 34             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid . &openid= . $openid;
 35         }
 36         $template = array(touser => $openid, template_id => $wxmodel_id, //fPOz4DE8P-NsaohwYolCSpuFbXRNJluGa29QU2_K_II
 37         url => $urlback, topcolor => #FF0000, data => array(first => [value => urlencode(您好, . $nickname . ,乐呗收银通知您,您的退款申请已成功办理), color => #173177], keyword1 => [value => urlencode($order_id) ], keyword2 => [value => urlencode($price . ) ], remark => [value => urlencode(您的退款已成功办理,退款金额已打至原支付账户,请注意查收!) ],),);
 38         $url = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= . $this->getAccessToken();
 39         // file_put_contents(‘./upload/log/27.txt‘,$url);
 40         $ret = $this->post($url, urldecode(json_encode($template)));
 41         // file_put_contents(‘./upload/log/28.txt‘,$ret);
 42         // if($ret){
 43         //    return true;
 44         // }else{
 45         //   return false;
 46         // }
 47         return $ret;
 48     }
 49     public function wxBalance($openid, $price, $order_id, $wxmodel_id) {
 50         if ($_SESSION[mid] == ‘‘) {
 51             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid;
 52         } else {
 53             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid . &openid= . $openid;
 54         }
 55         $template = array(touser => $openid, template_id => $wxmodel_id, //‘DihsJV0kGP4BkdGPjS9pxC6wMZBD4BjKTuPKfDV2N-Y‘,
 56         url => $urlback, topcolor => #FF0000, data => array(first => [value => urlencode(您好,您的余额支付已成功!), color => #173177], keyword1 => [value => urlencode($order_id) ], keyword2 => [value => urlencode($price . ) ], remark => [value => urlencode(余额支付成功,乐呗收银祝您生活愉快!) ],),);
 57         $url = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= . $this->getAccessToken();
 58         //echo $url;die;
 59         //file_put_contents(‘./upload/log/wxBalance.txt‘, $url);
 60         $ret = $this->post($url, urldecode(json_encode($template)));
 61         // file_put_contents(‘./upload/log/881.txt‘, $ret);
 62         return $ret;
 63     }
 64     function sendMsg($openid, $price, $nickname, $wxmodel_id) {
 65         if ($_SESSION[mid] == ‘‘) {
 66             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid;
 67         } else {
 68             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid . &openid= . $openid;
 69         }
 70         //echo $this->appid;die;
 71         $template = array(touser => $openid, template_id => $wxmodel_id, url => $urlback, topcolor => #FF0000, data => array(first => [value => urlencode(您好,欢迎来到乐呗收银,您的提现申请已提交), color => #173177], keyword1 => [value => urlencode($nickname) ], keyword2 => [value => urlencode(date(Y-m-d H:i:s, time())) ], keyword3 => [value => urlencode($price . ) ], keyword4 => [value => urlencode(微信提现) ], remark => [value => urlencode(您的申请已提交,请耐心等待商家审核,祝您生活愉快!) ],),);
 72         $url = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= . $this->getAccessToken();
 73         // file_put_contents(‘./upload/log/99999999999999.txt‘, $url);
 74         $ret = $this->post($url, urldecode(json_encode($template)));
 75         // file_put_contents(‘./upload/log/888888881.txt‘, $ret);
 76         // echo $ret;die;
 77         return $ret;
 78     }
 79     function secondMsg($openid, $price, $wxmodel_id) //secondMsg
 80     {
 81         if ($_SESSION[mid] == ‘‘) {
 82             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid;
 83         } else {
 84             $urlback = https://lepay.51ao.com/merchants.php?m=Index&c=index&a=bonus&mid= . $this->mid . &openid= . $openid;
 85         }
 86         $template = array(touser => $openid, template_id => $wxmodel_id, //‘TQmLhxpxKybOnEcUDBqgbWX6tbOUWIX8uJoVfRfSU18‘,
 87         url => $urlback, topcolor => #FF0000, data => array(first => [value => urlencode(您好,乐呗收银提醒您,您的秒提申请办理失败), color => #173177], money => [value => urlencode($price . ) ], time => [value => urlencode(date(Y-m-d H:i:s, time())) ], remark => [value => urlencode(您的秒提现办理失败,请及时联系商家核实,谢谢合作!) ],),);
 88         $url = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= . $this->getAccessToken();
 89         // file_put_contents(‘./upload/log/33.txt‘,$url);
 90         $ret = $this->post($url, urldecode(json_encode($template)));
 91         // var_dump($ret);die;
 92         // file_put_contents(‘./upload/log/22222.txt‘, $ret);
 93         return $ret;
 94     }
 95     public function post($url, $data = http://www.mamicode.com/‘‘, $isHttps = TRUE) {
 96         // 创建curl对象
 97         $ch = curl_init();
 98         // 配置这个对象
 99         curl_setopt($ch, CURLOPT_URL, $url); // 请求的URL地址
100         if ($isHttps) {
101             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
102             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
103             
104         }
105         curl_setopt($ch, CURLOPT_POST, 1); // 是否是POST请求
106         curl_setopt($ch, CURLOPT_HEADER, 0); // 去掉HTTP协议头
107         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 返回接口的结果,而不是输出
108         curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 提交的数据
109         // 发出请求
110         $return = curl_exec($ch);
111         // 返回数据
112         return $return;
113         // 关闭对象
114         curl_close($ch);
115     }
116     public function getAccessToken1() {
117         $access_token = getCache(Cache_access_token_ . $this->_mid);
118         if ($access_token && is_array($access_token) && ($access_token[expires_in] > time())) {
119             return $access_token[access_token];
120         }
121         $url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= . $this->appid . &secret= . $this->appsecret;
122         $Tokenarr = $this->https_request($url);
123         if (isset($Tokenarr[access_token])) {
124             $Tokenarr[expires_in] = time() + $Tokenarr[expires_in];
125             setCache(Cache_access_token_ . $this->_mid, $Tokenarr);
126             return $Tokenarr[access_token];
127         }
128         return false;
129     }
130     protected function https_request($url, $data = http://www.mamicode.com/null, $noprocess = false) {
131         $curl = curl_init();
132         curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
133         curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
134         $header = array("Accept-Charset: utf-8");
135         curl_setopt($curl, CURLOPT_URL, $url);
136         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
137         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
138         //curl_setopt($curl, CURLOPT_SSLVERSION, 3);
139         curl_setopt($curl, CURLOPT_HTTPHEADER, $header); /* * *$header 必须是一个数组** */
140         curl_setopt($curl, CURLOPT_HEADER, FALSE);
141         curl_setopt($curl, CURLINFO_HEADER_OUT, true);
142         if (!empty($data)) {
143             curl_setopt($curl, CURLOPT_POST, 1);
144             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
145         }
146         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
147         $output = curl_exec($curl);
148         if ($noprocess) return $output;
149         $errorno = curl_errno($curl);
150         if ($errorno) {
151             return array(curl => false, errorno => $errorno);
152         } else {
153             $res = json_decode($output, 1);
154             if ($res[errcode]) {
155                 return array(errcode => $res[errcode], errmsg => $res[errmsg]);
156             } else {
157                 return $res;
158             }
159         }
160         curl_close($curl);
161     }
162     public function getAccessToken() {
163         //获取文件创建时间的函数filemtime()
164         //判断的生成的access_token值是否过期
165         //echo $this->mid.‘35‘;die;
166         $filename = ./upload/token/accesstoken . $this->mid;
167         // echo $filename;die;
168         if (!file_exists($filename) || (file_exists($filename) && (time() - filemtime($filename)) > 4000)) {
169             //1.url地址
170             $url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= . $this->appid . &secret= . $this->appsecret;
171             //2.判断是否为post请求
172             //3.发送请求
173             $content = $this->post($url);
174             //4.处理返回值
175             //返回数据格式为json,php不可以直接操作json格式,需要json_decode转化一下
176             $content = json_decode($content);
177             //file_put_contents(‘./upload/log/33333333333333.txt‘, $content);
178             $access_token = $content->access_token;
179             //把access_token保存到文件
180             file_put_contents($filename, $access_token);
181         }
182         //如果没有过期,那么就去读取缓存文件里的access_token
183         else {
184             $access_token = file_get_contents($filename);
185             //file_put_contents(‘./upload/log/77777777777777.txt‘, $access_token);
186             
187         }
188         //把access_token返回
189         return $access_token;
190     }
191 }
1 bpBase::loadOrg(refund_success);
2             $wxBalances = new refund_success(1);
3             $wxBalances->wxBalance($orderinfo[openid], $orderinfo[goods_price], $orderinfo[order_id], $wxmodel[model_id]);

 

微信消息模板