weixin.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiepeng
  5. * Date: 2017/8/17
  6. */
  7. class weixin
  8. {
  9. /**线上***/
  10. const SCOPE_USERINFO = 'snsapi_base'; //授权方法
  11. /**测试***/
  12. const TOKEN_BASE_KEY = "weixin_access_token";
  13. const JS_TICKET_KEY = "weixin_js_ticket";
  14. const LimitTime = 7100;
  15. public $setFlag = false;
  16. public $msgtype = 'text'; //('text','image','location')
  17. public $msg = array();
  18. /****微信支付支付向相关参数****/
  19. public function __construct()
  20. {
  21. }
  22. public function index()
  23. {
  24. $timestamp = $_GET['timestamp'];
  25. $nonce = $_GET['nonce'];
  26. $token = 'weixin_xian_rd_2018';
  27. $signature = $_GET['signature'];
  28. $echostr = $_GET['echostr'];
  29. $array = array($timestamp, $nonce, $token);
  30. sort($array);
  31. $tmpstr = sha1(implode('', $array));
  32. if ($tmpstr == $signature && $echostr) {
  33. echo $echostr;
  34. }
  35. }
  36. public function getAccessToken()
  37. {
  38. $rs = Baseconfig::getInfoByKey(self::TOKEN_BASE_KEY);
  39. if (empty($rs) || (time() - $rs["lastupdate"]) > self::LimitTime) {
  40. $accessToken = self::refreshToken();
  41. //var_dump($accessToken);
  42. } else {
  43. $accessToken = $rs["value"];
  44. }
  45. if(empty($accessToken))
  46. {
  47. echo("获取access_token失败") ;
  48. exit;
  49. }
  50. return $accessToken;
  51. }
  52. public function refreshToken()
  53. {
  54. $conf = $this->_conf();
  55. $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $conf["appid"] . "&secret=" . $conf["appkey"];
  56. $res = file_get_contents($token_access_url); //
  57. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量s
  58. $access_token = $result['access_token'];
  59. if(empty($access_token))
  60. {
  61. echo("获取刷新access_token失败") ;
  62. exit;
  63. }
  64. $rs = Baseconfig::getInfoByKey(self::TOKEN_BASE_KEY);
  65. if ($rs) {
  66. $res = Baseconfig::update($rs["id"], array("value" => $access_token, "lastupdate" => time()));
  67. } else {
  68. $res = Baseconfig::add(self::TOKEN_BASE_KEY, $access_token, "微信开发access_token");
  69. }
  70. if ($res) {
  71. return $access_token;
  72. }
  73. }
  74. public function getUserInfo($openId)
  75. {
  76. $token = $this->getAccessToken();
  77. $token_access_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . "&openid=" . $openId . "&lang=zh_CN";
  78. $res = file_get_contents($token_access_url); //
  79. return json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  80. }
  81. public function getInfoByCode()
  82. {
  83. $code = isset($_REQUEST["code"])?$_REQUEST["code"]:"";
  84. if(empty($code))
  85. {
  86. return 0;
  87. }
  88. $conf = $this->_conf();
  89. $token_access_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $conf["appid"] . "&secret=" . $conf["appkey"] . "&code=" . $code . "&grant_type=authorization_code";
  90. $res = file_get_contents($token_access_url); //
  91. return json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量,若不加TURE,则是一个对象
  92. }
  93. public function createMenu()
  94. {
  95. global $HTTP_PATH;
  96. $access_token = $this->getAccessToken();
  97. $data = '{
  98. "button": [
  99. {
  100. "name": "使用贴士",
  101. "sub_button": [
  102. {
  103. "type": "view",
  104. "name": "锅炉常识",
  105. "url": "'.$HTTP_PATH.'weixin/weixin_industry_info.php"
  106. },
  107. {
  108. "type": "view",
  109. "name": "使用指南",
  110. "url": "'.$HTTP_PATH.'weixin/weixin_product_describe.php"
  111. }
  112. ]
  113. },
  114. {
  115. "name": "我的",
  116. "sub_button": [
  117. {
  118. "type": "view",
  119. "name": "一键报修",
  120. "url": "'.$HTTP_PATH.'weixin/weixin_repair.php"
  121. },
  122. {
  123. "type": "view",
  124. "name": "个人信息",
  125. "url": "'.$HTTP_PATH.'weixin/weixin_personal_detail.php"
  126. }
  127. ]
  128. }
  129. ]
  130. }';
  131. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
  132. return $this->curlPost($data, $url);
  133. }
  134. public function curlPost($data, $url)
  135. {
  136. //var_dump($data);
  137. $ch = curl_init();
  138. curl_setopt($ch, CURLOPT_URL, $url);
  139. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  140. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  141. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  142. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  143. // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  144. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  145. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  146. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147. $tmpInfo = curl_exec($ch);
  148. if (curl_errno($ch)) {
  149. return curl_error($ch) ;
  150. }
  151. curl_close($ch);
  152. return $tmpInfo;
  153. }
  154. public function getMsg()
  155. {
  156. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  157. if (!empty($postStr)) {
  158. $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  159. $this->msgtype = strtolower($this->msg['MsgType']);
  160. }
  161. }
  162. public function makeText($text='')
  163. {
  164. $CreateTime = time();
  165. $FuncFlag = $this->setFlag ? 1 : 0;
  166. $textTpl = "<xml>
  167. <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
  168. <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
  169. <CreateTime>{$CreateTime}</CreateTime>
  170. <MsgType><![CDATA[text]]></MsgType>
  171. <Content><![CDATA[%s]]></Content>
  172. <FuncFlag>%s</FuncFlag>
  173. </xml>";
  174. return sprintf($textTpl,$text,$FuncFlag);
  175. }
  176. public function makeTextPic($arr)
  177. {
  178. $CreateTime = time();
  179. $len = count($arr);
  180. $textTpl = "<xml><ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>{$len}</ArticleCount><Articles>";
  181. $itemTpl = "<item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item>";
  182. foreach ($arr as $item)
  183. {
  184. $textTpl .= sprintf($itemTpl,$item["title"],$item["description"],$item["picUrl"],$item["url"]);
  185. }
  186. $textTpl .= "</Articles></xml>";
  187. return $textTpl;
  188. }
  189. public function reply($data)
  190. {
  191. echo $data;
  192. }
  193. //客服消息
  194. public function messageToUserName($content,$fromUsername)//content 就是回复的消息,$fromUsername就是openid
  195. {
  196. //这里要获取token
  197. $access_token = $this->getAccessToken();
  198. $data = '{
  199. "touser":"'.$fromUsername.'",
  200. "msgtype":"text",
  201. "text":
  202. {
  203. "content":"'.$content.'"
  204. }
  205. }';
  206. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
  207. $result = $this->curlPost($data,$url);
  208. $final = json_decode($result);
  209. return $final;
  210. }
  211. public function getSignPackage() {
  212. $jsapiTicket = $this->getJsTicket();
  213. $conf = $this->_conf();
  214. // 注意 URL 一定要动态获取,不能 hardcode.
  215. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  216. $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  217. $nonceStr = $this->createNonceStr();
  218. $timestamp = time();
  219. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  220. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  221. $signature = sha1($string);
  222. $signPackage = array(
  223. "appId" => $conf["appid"],
  224. "nonceStr" => $nonceStr,
  225. "timestamp" => $timestamp,
  226. "url" => $url,
  227. "signature" => $signature,
  228. "rawString" => $string
  229. );
  230. return $signPackage;
  231. }
  232. //url示例 “weixinHtml/new_repaire.html”
  233. public function getSignPackageByURL($url = '') {
  234. $jsapiTicket = $this->getJsTicket();
  235. $conf = $this->_conf();
  236. // 注意 URL 一定要动态获取,不能 hardcode.
  237. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  238. $url = "$protocol$_SERVER[HTTP_HOST]/".trim($url);
  239. $nonceStr = $this->createNonceStr();
  240. $timestamp = time();
  241. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  242. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  243. $signature = sha1($string);
  244. $signPackage = array(
  245. "appId" => $conf["appid"],
  246. "nonceStr" => $nonceStr,
  247. "timestamp" => $timestamp,
  248. "url" => $url,
  249. "signature" => $signature,
  250. "rawString" => $string
  251. );
  252. return $signPackage;
  253. }
  254. private function createNonceStr($length = 16) {
  255. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  256. $str = "";
  257. for ($i = 0; $i < $length; $i++) {
  258. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  259. }
  260. return $str;
  261. }
  262. public function downLoadFile($serverId,$foldername){
  263. $access_token = $this->getAccessToken();
  264. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$serverId;
  265. if (!file_exists($foldername)) {
  266. mkdir($foldername, 0777, true);
  267. }
  268. $targetName = date('YmdHis').rand(1000,9999).'.jpg';
  269. $targetPathName = $foldername.$targetName;
  270. //获取微信“获取临时素材”接口返回来的内容(即刚上传的图片)
  271. $a = file_get_contents($url);
  272. //以读写方式打开一个文件,若没有,则自动创建
  273. $resource = fopen($targetPathName , 'w+');
  274. //将图片内容写入上述新建的文件
  275. fwrite($resource, $a);
  276. //关闭资源
  277. fclose($resource);
  278. return $targetName;
  279. }
  280. public function downLoadFiles($serverId,$foldername,$access_token){
  281. //用于下载师傅端上传的图片到后台
  282. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$serverId;
  283. if (!file_exists($foldername)) {
  284. mkdir($foldername, 0777, true);
  285. }
  286. $targetName = date('YmdHis').rand(1000,9999).'.jpg';
  287. $targetPathName = $foldername.$targetName;
  288. //获取微信“获取临时素材”接口返回来的内容(即刚上传的图片)
  289. $a = file_get_contents($url);
  290. //以读写方式打开一个文件,若没有,则自动创建
  291. $resource = fopen($targetPathName , 'w+');
  292. //将图片内容写入上述新建的文件
  293. fwrite($resource, $a);
  294. //关闭资源
  295. fclose($resource);
  296. return $targetName;
  297. }
  298. public function sendImageToUser($openId,$mediaId)
  299. {
  300. $acccess_token = $this->getAccessToken();
  301. $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$acccess_token}";
  302. $sendData=' {
  303. "touser":"'.$openId.'",
  304. "msgtype":"image",
  305. "image":
  306. {
  307. "media_id":"'.$mediaId.'"
  308. }
  309. }';
  310. return $this->curlPost($sendData,$url);
  311. }
  312. public function sendMsgToUser($openId,$content)
  313. {
  314. $acccess_token = $this->getAccessToken();
  315. $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$acccess_token}";
  316. $sendData='{
  317. "touser": "'.$openId.'",
  318. "msgtype": "text",
  319. "text": {
  320. "content": "'.$content.'"
  321. }
  322. }';
  323. return $this->curlPost($sendData,$url);
  324. }
  325. public function getJsTicket(){ // 只允许本类调用,继承的都不可以调用,公开调用就更不可以了
  326. $rs = Baseconfig::getInfoByKey(self::JS_TICKET_KEY);
  327. if(empty($rs)||(time()-$rs["lastupdate"])>self::LimitTime)
  328. {
  329. $jsticket = self::refreshJsTicket();
  330. }
  331. else
  332. {
  333. $jsticket = $rs["value"];
  334. }
  335. return $jsticket ;
  336. }
  337. public function refreshJsTicket()
  338. {
  339. $access_token = $this->getAccessToken();
  340. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi"; // 两小时有效
  341. $rurl = file_get_contents($url);
  342. $rurl = json_decode($rurl,true);
  343. if($rurl['errcode'] != 0){
  344. return false;
  345. }else{
  346. $jsticket = $rurl['ticket'];
  347. $rs = Baseconfig::getInfoByKey(self::JS_TICKET_KEY);
  348. if($rs)
  349. {
  350. $res = Baseconfig::update($rs["id"],array("value"=>$jsticket,"lastupdate"=>time()));
  351. }
  352. else
  353. {
  354. $res = Baseconfig::add(self::JS_TICKET_KEY,$jsticket,"微信开发js_ticket");
  355. }
  356. if($res)
  357. {
  358. return $jsticket;
  359. }
  360. }
  361. }
  362. /**
  363. *
  364. * 获取支付结果通知数据
  365. * return array
  366. */
  367. public function getNotifyData(){
  368. //获取通知的数据
  369. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  370. $data = array();
  371. if( empty($xml) ){
  372. return false;
  373. }
  374. $data = self::xml_to_data( $xml );
  375. if( !empty($data['return_code']) ){
  376. if( $data['return_code'] == 'FAIL' ){
  377. return false;
  378. }
  379. }
  380. return $data;
  381. }
  382. /**
  383. * 将xml转为array
  384. * @param string $xml
  385. * return array
  386. */
  387. public function xml_to_data($xml){
  388. if(!$xml){
  389. return false;
  390. }
  391. //将XML转为array
  392. //禁止引用外部xml实体
  393. libxml_disable_entity_loader(true);
  394. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  395. return $data;
  396. }
  397. /**
  398. * 用户隐秘授权
  399. * @param $redirectUrl
  400. * @return string
  401. */
  402. function login_url($redirectUrl)
  403. {
  404. $conf = $this->_conf();
  405. $params = array(
  406. 'appid' => $conf["appid"],
  407. 'redirect_uri' => $redirectUrl,
  408. 'response_type' => 'code',
  409. 'scope' => self::SCOPE_USERINFO,
  410. 'state' => time(),
  411. );
  412. return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($params) . '#wechat_redirect';
  413. }
  414. public static function savePicToServer($url,$fileName) {
  415. // 要存在你服务器哪个位置?
  416. $ch = curl_init();
  417. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  418. $fp = fopen($fileName,'wb');
  419. curl_setopt($ch,CURLOPT_URL,$url);
  420. curl_setopt($ch,CURLOPT_FILE,$fp);
  421. curl_setopt($ch,CURLOPT_HEADER,0);
  422. curl_exec($ch);
  423. curl_close($ch);
  424. fclose($fp);
  425. }
  426. public function _conf()
  427. {
  428. $arr = Baseconfig::getInfoByArr(array(Baseconfig::CFG_WEIXIN_APPID,Baseconfig::CFG_WEIXIN_APPKEY));
  429. $info = array();
  430. foreach ($arr as $v)
  431. {
  432. if ($v['key'] == Baseconfig::CFG_WEIXIN_APPID)
  433. {
  434. $info['appid'] = $v['value'];
  435. }
  436. if ($v['key'] == Baseconfig::CFG_WEIXIN_APPKEY)
  437. {
  438. $info['appkey'] = $v['value'];
  439. }
  440. }
  441. return $info;
  442. }
  443. public function sendMsgToUserHaveUrl($openid,$text)
  444. {
  445. $ToUserName = Baseconfig::getInfoByKey(Baseconfig::CFG_WEIXIN_APPID)['value'];
  446. $CreateTime = time();
  447. $textTpl = "<xml>
  448. <ToUserName><![CDATA[$ToUserName}]]></ToUserName>
  449. <FromUserName><![CDATA[{$openid}]]></FromUserName>
  450. <CreateTime>{$CreateTime}</CreateTime>
  451. <MsgType><![CDATA[text]]></MsgType>
  452. <Content><![CDATA[%s]]></Content>
  453. </xml>";
  454. return sprintf($textTpl,$text);
  455. }
  456. public function get_template(){
  457. $acccess_token = $this->getAccessToken();
  458. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$acccess_token;
  459. $sendData='';
  460. $res = $this->curlPost($sendData,$url);
  461. return json_decode($res, true);
  462. }
  463. //推送用户下单时的订单,客服推送
  464. public function send_user_by_template($dataUrl,$openid,$dataInfo,$template_id) { //发送$kf_template_id模板消息
  465. $acccess_token = $this->getAccessToken();
  466. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  467. $data =' {
  468. "touser":"'.$openid.'",
  469. "template_id":"'.$template_id.'",
  470. "url":"'.$dataUrl.'",
  471. "data":{
  472. "first": {
  473. "value":"您收到一个新的预约订单,请及时处理",
  474. "color":"#173177"
  475. },
  476. "keyword1":{
  477. "value": "'.$dataInfo['type'].'"
  478. },
  479. "keyword2": {
  480. "value":"'.$dataInfo['register_person'].'"
  481. },
  482. "keyword3": {
  483. "value":"'.$dataInfo['link_phone'].'"
  484. },
  485. "keyword4": {
  486. "value":"'.$dataInfo['register_phone'].'"
  487. },
  488. "keyword5": {
  489. "value":"'.$dataInfo['address'].'"
  490. },
  491. "remark":{
  492. "value":"点击查看订单详情",
  493. "color":"#173177"
  494. }
  495. }
  496. }';
  497. $result = $this->curlPost($data , $url);
  498. // $result = $this->send_post( $url, $data);
  499. return $result;
  500. }
  501. //发放的优惠劵推送
  502. public function send_coupon_by_template($dataUrl,$openid,$dataInfo,$template_id) { //发送模板消息
  503. $acccess_token = $this->getAccessToken();
  504. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  505. $data =' {
  506. "touser":"'.$openid.'",
  507. "template_id":"'.$template_id.'",
  508. "url":"'.$dataUrl.'",
  509. "data":{
  510. "first": {
  511. "value":"您收到了由“小元服务”为您发放的优惠券",
  512. "color":"#173177"
  513. },
  514. "keyword1":{
  515. "value": "'.$dataInfo['type'].'"
  516. },
  517. "keyword2": {
  518. "value":"'.$dataInfo['time'].'"
  519. },
  520. "remark":{
  521. "value":"点击查看优惠券",
  522. "color":"#173177"
  523. }
  524. }
  525. }';
  526. $result = $this->curlPost($data , $url);
  527. return $result;
  528. }
  529. //推送客户端派单消息的模板函数
  530. //user sxx
  531. public function send_coupon_by_templates($dataUrl,$openid,$dataInfo,$template_id) {
  532. $acccess_token = $this->getAccessToken();
  533. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  534. $data =' {
  535. "touser":"'.$openid.'",
  536. "template_id":"'.$template_id.'",
  537. "url":"'.$dataUrl.'",
  538. "data":{
  539. "first": {
  540. "value":"您已成功预约",
  541. "color":"#173177"
  542. },
  543. "keyword1":{
  544. "value": "'.$dataInfo['type'].'"
  545. },
  546. "keyword2": {
  547. "value":"'.$dataInfo['time'].'"
  548. },
  549. "remark":{
  550. "value":"点击查看详情",
  551. "color":"#173177"
  552. }
  553. }
  554. }';
  555. $result = $this->curlPost($data , $url);
  556. return $result;
  557. }
  558. //推送客户端已接单消息的模板函数
  559. //user sxx
  560. public function send_coupon_by_templatess($dataUrl,$openid,$dataInfo,$template_id) {
  561. $acccess_token = $this->getAccessToken();
  562. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  563. $data =' {
  564. "touser":"'.$openid.'",
  565. "template_id":"'.$template_id.'",
  566. "url":"'.$dataUrl.'",
  567. "data":{
  568. "first": {
  569. "value":"维修师傅已接单,请保持电话畅通",
  570. "color":"#173177"
  571. },
  572. "keyword1":{
  573. "value": "'.$dataInfo['name'].'"
  574. },
  575. "keyword2": {
  576. "value":"'.$dataInfo['linkphone'].'"
  577. },
  578. "keyword3": {
  579. "value":"'.$dataInfo['type'].'"
  580. },
  581. "keyword4": {
  582. "value":"'.$dataInfo['time'].'"
  583. },
  584. "remark":{
  585. "value":"点击查看详情",
  586. "color":"#173177"
  587. }
  588. }
  589. }';
  590. $result = $this->curlPost($data , $url);
  591. return $result;
  592. }
  593. //推送客户端待支付消息的模板函数
  594. //user sxx
  595. public function send_coupon_by_pay($dataUrl,$openid,$dataInfo,$template_id) {
  596. $acccess_token = $this->getAccessToken();
  597. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  598. $data =' {
  599. "touser":"'.$openid.'",
  600. "template_id":"'.$template_id.'",
  601. "url":"'.$dataUrl.'",
  602. "data":{
  603. "first": {
  604. "value":"您预约的订单已经维修完成,请您尽快支付",
  605. "color":"#173177"
  606. },
  607. "keyword1":{
  608. "value": "'.$dataInfo['time'].'"
  609. },
  610. "keyword2": {
  611. "value":"服务费'.$dataInfo['money1'].',配件售价'.$dataInfo['money2'].',共计'.$dataInfo['money3'].'"
  612. },
  613. "keyword3": {
  614. "value":"'.$dataInfo['name'].'"
  615. },
  616. "remark":{
  617. "value":"点击查看详情",
  618. "color":"#173177"
  619. }
  620. }
  621. }';
  622. $result = $this->curlPost($data , $url);
  623. return $result;
  624. }
  625. //推送客户端支付成功消息的模板函数(报修故障过保/其他类型)
  626. //user sxx
  627. public function send_coupon_by_finsh1($dataUrl,$openid,$dataInfo,$template_id) {
  628. $acccess_token = $this->getAccessToken();
  629. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  630. $data =' {
  631. "touser":"'.$openid.'",
  632. "template_id":"'.$template_id.'",
  633. "url":"'.$dataUrl.'",
  634. "data":{
  635. "first": {
  636. "value":"您好,您本次服务已完成。",
  637. "color":"#173177"
  638. },
  639. "keyword1":{
  640. "value": "'.$dataInfo['type'].'"
  641. },
  642. "keyword2": {
  643. "value": "'.$dataInfo['money3'].'"
  644. },
  645. "remark":{
  646. "value":"查看订单详情",
  647. "color":"#173177"
  648. }
  649. }
  650. }';
  651. $result = $this->curlPost($data , $url);
  652. return $result;
  653. }
  654. //推送客户端支付成功消息的模板函数(报修故障(保质期内))
  655. //user sxx
  656. public function send_coupon_by_finsh2($dataUrl,$openid,$dataInfo,$template_id) {
  657. $acccess_token = $this->getAccessToken();
  658. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  659. $data =' {
  660. "touser":"'.$openid.'",
  661. "template_id":"'.$template_id.'",
  662. "url":"'.$dataUrl.'",
  663. "data":{
  664. "first": {
  665. "value":"您好,您本次服务已完成。",
  666. "color":"#173177"
  667. },
  668. "keyword1":{
  669. "value": "'.$dataInfo['type'].'"
  670. },
  671. "keyword2": {
  672. "value":"0元(保质期内不收取任何费用)"
  673. },
  674. "remark":{
  675. "value":"查看订单详情",
  676. "color":"#173177"
  677. }
  678. }
  679. }';
  680. $result = $this->curlPost($data , $url);
  681. return $result;
  682. }
  683. //推送客户端支付成功消息的模板函数(报修故障过保/其他类型(现金支付))
  684. //user sxx
  685. public function send_coupon_by_finsh3($dataUrl,$openid,$dataInfo,$template_id) {
  686. $acccess_token = $this->getAccessToken();
  687. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  688. $data =' {
  689. "touser":"'.$openid.'",
  690. "template_id":"'.$template_id.'",
  691. "url":"'.$dataUrl.'",
  692. "data":{
  693. "first": {
  694. "value":"您好,您本次服务已完成。",
  695. "color":"#173177"
  696. },
  697. "keyword1":{
  698. "value": "'.$dataInfo['type'].'"
  699. },
  700. "keyword2": {
  701. "value":"'.$dataInfo['money3'].'(现金支付)"
  702. },
  703. "remark":{
  704. "value":"查看订单详情",
  705. "color":"#173177"
  706. }
  707. }
  708. }';
  709. $result = $this->curlPost($data , $url);
  710. return $result;
  711. }
  712. //推送给密语客服的申请重派提醒
  713. //user sxx
  714. public function send_custom_by_template($dataUrl,$openid,$dataInfo,$template_id) {
  715. $acccess_token = $this->getAccessToken();
  716. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  717. $data =' {
  718. "touser":"'.$openid.'",
  719. "template_id":"'.$template_id.'",
  720. "url":"'.$dataUrl.'",
  721. "data":{
  722. "first": {
  723. "value":"'.$dataInfo['name'].'申请重派,请及时处理",
  724. "color":"#173177"
  725. },
  726. "keyword1":{
  727. "value": "'.$dataInfo['type'].'"
  728. },
  729. "keyword2": {
  730. "value":"'.$dataInfo['time'].'"
  731. },
  732. "remark":{
  733. "value":"点击查看详情",
  734. "color":"#173177"
  735. }
  736. }
  737. }';
  738. $result = $this->curlPost($data , $url);
  739. return $result;
  740. }
  741. // //推送客户端在保支付成功消息的模板函数
  742. // //user sxx
  743. // public function send_coupon_by_finshs($dataUrl,$openid,$dataInfo,$template_id) {
  744. // $acccess_token = $this->getAccessToken();
  745. //
  746. // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  747. //
  748. // $data =' {
  749. // "touser":"'.$openid.'",
  750. // "template_id":"'.$template_id.'",
  751. // "url":"'.$dataUrl.'",
  752. // "data":{
  753. // "first": {
  754. // "value":"您预约的'.$dataInfo['type'].'",
  755. // "color":"#173177"
  756. // },
  757. // "keyword1":{
  758. // "value": "'.$dataInfo['type'].'"
  759. // },
  760. // "keyword2": {
  761. // "value":"'.$dataInfo['time'].'"
  762. // },
  763. //
  764. // "remark":{
  765. // "value":"点击去评价",
  766. // "color":"#173177"
  767. // }
  768. // }
  769. // }';
  770. //
  771. // $result = $this->curlPost($data , $url);
  772. // return $result;
  773. // }
  774. // //推送师傅端待接单消息的模板函数
  775. // //user sxx
  776. // public function send_coupon_by_teacher($dataUrl,$openid,$dataInfo,$template_id) {
  777. // $acccess_token = $this->getAccessToken();
  778. //
  779. // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  780. // $data =' {
  781. // "touser":"'.$openid.'",
  782. // "template_id":"'.$template_id.'",
  783. // "url":"'.$dataUrl.'",
  784. // "data":{
  785. // "first": {
  786. // "value":"您有新的订单,请尽快接单",
  787. // "color":"#173177"
  788. // },
  789. // "keyword1":{
  790. // "value": "'.$dataInfo['time'].'"
  791. // },
  792. // "keyword2": {
  793. // "value":"'.$dataInfo['type'].'"
  794. // },
  795. //
  796. // "remark":{
  797. // "value":"点击查看详情",
  798. // "color":"#173177"
  799. // }
  800. // }
  801. // }';
  802. //
  803. // $result = $this->curlPost($data , $url);
  804. // return $result;
  805. // }
  806. //
  807. // //推送师傅端已支付消息的模板函数
  808. // //user sxx
  809. // public function send_coupon_by_pays($dataUrl,$openid,$dataInfo,$template_id) {
  810. // $acccess_token = $this->getAccessToken();
  811. //
  812. // $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$acccess_token}";
  813. // $data =' {
  814. // "touser":"'.$openid.'",
  815. // "template_id":"'.$template_id.'",
  816. // "url":"'.$dataUrl.'",
  817. // "data":{
  818. // "first": {
  819. // "value":"用户已经付款",
  820. // "color":"#173177"
  821. // },
  822. // "keyword1":{
  823. // "value": "'.$dataInfo['name'].'"
  824. // },
  825. // "keyword2": {
  826. // "value":"'.$dataInfo['style'].'"
  827. // },
  828. // "keyword3": {
  829. // "value":"'.$dataInfo['money'].'"
  830. // },
  831. // "keyword4": {
  832. // "value":"'.$dataInfo['address'].'"
  833. // },
  834. // "keyword5": {
  835. // "value":"'.$dataInfo['time'].'"
  836. // },
  837. //
  838. // "remark":{
  839. // "value":"点击查看详情",
  840. // "color":"#173177"
  841. // }
  842. // }
  843. // }';
  844. //
  845. // $result = $this->curlPost($data , $url);
  846. // return $result;
  847. // }
  848. public function send_post( $url, $post_data )
  849. {
  850. $options = array(
  851. 'http' => array(
  852. 'method' => 'POST',
  853. 'header' => 'Content-type:application/json;charset=utf-8',
  854. //header 需要设置为 JSON
  855. 'content' => $post_data,
  856. 'timeout' => 60
  857. //超时时间
  858. )
  859. );
  860. $context = stream_context_create($options);
  861. $result = file_get_contents($url, false, $context);
  862. return $result;
  863. }
  864. }