WechatService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/23
  6. */
  7. namespace service;
  8. use app\admin\model\wechat\WechatMessage;
  9. use behavior\wechat\MessageBehavior;
  10. use behavior\wechat\PaymentBehavior;
  11. use EasyWeChat\Foundation\Application;
  12. use EasyWeChat\Message\Article;
  13. use EasyWeChat\Message\Image;
  14. use EasyWeChat\Message\Material;
  15. use EasyWeChat\Message\News;
  16. use EasyWeChat\Message\Text;
  17. use EasyWeChat\Message\Video;
  18. use EasyWeChat\Message\Voice;
  19. use EasyWeChat\Payment\Order;
  20. use EasyWeChat\Server\Guard;
  21. use EasyWeChat\Support\XML;
  22. use think\Url;
  23. class WechatService
  24. {
  25. private static $instance = null;
  26. public static function options()
  27. {
  28. $wechat = SystemConfigService::more(['wechat_appid','wechat_appsecret','wechat_token','wechat_encodingaeskey','wechat_encode']);
  29. $payment = SystemConfigService::more(['pay_weixin_mchid','pay_weixin_client_cert','pay_weixin_client_key','pay_weixin_key','pay_weixin_open']);
  30. $config = [
  31. 'app_id'=>isset($wechat['wechat_appid']) ? $wechat['wechat_appid']:'',
  32. 'secret'=>isset($wechat['wechat_appsecret']) ? $wechat['wechat_appsecret']:'',
  33. 'token'=>isset($wechat['wechat_token']) ? $wechat['wechat_token']:'',
  34. 'guzzle' => [
  35. 'timeout' => 10.0, // 超时时间(秒)
  36. ],
  37. ];
  38. if((int)$wechat['wechat_encode']>0 && isset($wechat['wechat_encodingaeskey']) && !empty($wechat['wechat_encodingaeskey']))
  39. $config['aes_key'] = $wechat['wechat_encodingaeskey'];
  40. if(isset($payment['pay_weixin_open']) && $payment['pay_weixin_open'] == 1){
  41. $config['payment'] = [
  42. 'merchant_id'=>$payment['pay_weixin_mchid'],
  43. 'key'=>$payment['pay_weixin_key'],
  44. 'cert_path'=>realpath('.'.$payment['pay_weixin_client_cert']),
  45. 'key_path'=>realpath('.'.$payment['pay_weixin_client_key']),
  46. 'notify_url'=>SystemConfigService::get('site_url').Url::build('wap/Wechat/notify')
  47. ];
  48. }
  49. return $config;
  50. }
  51. public static function application($cache = false)
  52. {
  53. (self::$instance === null || $cache === true) && (self::$instance = new Application(self::options()));
  54. return self::$instance;
  55. }
  56. public static function serve()
  57. {
  58. $wechat = self::application(true);
  59. $server = $wechat->server;
  60. self::hook($server);
  61. $response = $server->serve();
  62. exit($response->getContent());
  63. }
  64. /**
  65. * 监听行为
  66. * @param Guard $server
  67. */
  68. private static function hook($server)
  69. {
  70. $server->setMessageHandler(function($message){
  71. $behavior = MessageBehavior::class;
  72. HookService::beforeListen('wechat_message',$message,null,true,$behavior);
  73. switch ($message->MsgType){
  74. case 'event':
  75. switch (strtolower($message->Event)){
  76. case 'subscribe':
  77. if(isset($message->EventKey)){
  78. $response = HookService::resultListen('wechat_event_scan_subscribe',$message,$message->EventKey,true,$behavior);
  79. }else{
  80. $response = HookService::resultListen('wechat_event_subscribe',$message,null,true,$behavior);
  81. }
  82. break;
  83. case 'unsubscribe':
  84. $response = HookService::resultListen('wechat_event_unsubscribe',$message,null,true,$behavior);
  85. break;
  86. case 'scan':
  87. $response = HookService::resultListen('wechat_event_scan',$message,$message->EventKey,true,$behavior);
  88. break;
  89. case 'location':
  90. $response = HookService::resultListen('wechat_event_location',$message,null,true,$behavior);
  91. break;
  92. case 'click':
  93. $response = HookService::resultListen('wechat_event_click',$message,null,true,$behavior);
  94. break;
  95. case 'view':
  96. $response = HookService::resultListen('wechat_event_view',$message,null,true,$behavior);
  97. break;
  98. }
  99. break;
  100. case 'text':
  101. $response = HookService::resultListen('wechat_message_text',$message,null,true,$behavior);
  102. break;
  103. case 'image':
  104. $response = HookService::resultListen('wechat_message_image',$message,null,true,$behavior);
  105. break;
  106. case 'voice':
  107. $response = HookService::resultListen('wechat_message_voice',$message,null,true,$behavior);
  108. break;
  109. case 'video':
  110. $response = HookService::resultListen('wechat_message_video',$message,null,true,$behavior);
  111. break;
  112. case 'location':
  113. $response = HookService::resultListen('wechat_message_location',$message,null,true,$behavior);
  114. break;
  115. case 'link':
  116. $response = HookService::resultListen('wechat_message_link',$message,null,true,$behavior);
  117. break;
  118. // ... 其它消息
  119. default:
  120. $response = HookService::resultListen('wechat_message_other',$message,null,true,$behavior);
  121. break;
  122. }
  123. return $response;
  124. });
  125. }
  126. /**
  127. * 多客服消息转发
  128. * @param string $account
  129. * @return \EasyWeChat\Message\Transfer
  130. */
  131. public static function transfer($account = '')
  132. {
  133. $transfer = new \EasyWeChat\Message\Transfer();
  134. return empty($account) ? $transfer : $transfer->to($account);
  135. }
  136. /**
  137. * 上传永久素材接口
  138. * @return \EasyWeChat\Material\Material
  139. */
  140. public static function materialService()
  141. {
  142. return self::application()->material;
  143. }
  144. /**
  145. * 上传临时素材接口
  146. * @return \EasyWeChat\Material\Temporary
  147. */
  148. public static function materialTemporaryService()
  149. {
  150. return self::application()->material_temporary;
  151. }
  152. /**
  153. * 用户接口
  154. * @return \EasyWeChat\User\User
  155. */
  156. public static function userService()
  157. {
  158. return self::application()->user;
  159. }
  160. /**
  161. * 客服消息接口
  162. * @param null $to
  163. * @param null $message
  164. */
  165. public static function staffService()
  166. {
  167. return self::application()->staff;
  168. }
  169. /**
  170. * 微信公众号菜单接口
  171. * @return \EasyWeChat\Menu\Menu
  172. */
  173. public static function menuService()
  174. {
  175. return self::application()->menu;
  176. }
  177. /**
  178. * 微信二维码生成接口
  179. * @return \EasyWeChat\QRCode\QRCode
  180. */
  181. public static function qrcodeService()
  182. {
  183. return self::application()->qrcode;
  184. }
  185. /**
  186. * 短链接生成接口
  187. * @return \EasyWeChat\Url\Url
  188. */
  189. public static function urlService()
  190. {
  191. return self::application()->url;
  192. }
  193. /**
  194. * 用户授权
  195. * @return \Overtrue\Socialite\Providers\WeChatProvider
  196. */
  197. public static function oauthService()
  198. {
  199. return self::application()->oauth;
  200. }
  201. /**
  202. * 模板消息接口
  203. * @return \EasyWeChat\Notice\Notice
  204. */
  205. public static function noticeService()
  206. {
  207. return self::application()->notice;
  208. }
  209. public static function sendTemplate($openid,$templateId,array $data,$url = null,$defaultColor = null)
  210. {
  211. $notice = self::noticeService()->to($openid)->template($templateId)->andData($data);
  212. if($url !== null) $notice->url($url);
  213. if($defaultColor !== null) $notice->defaultColor($defaultColor);
  214. return $notice->send();
  215. }
  216. /**
  217. * 支付
  218. * @return \EasyWeChat\Payment\Payment
  219. */
  220. public static function paymentService()
  221. {
  222. return self::application()->payment;
  223. }
  224. public static function downloadBill($day,$type = 'ALL')
  225. {
  226. // $payment = self::paymentService();
  227. // $merchant = $payment->getMerchant();
  228. // $params = [
  229. // 'appid' => $merchant->app_id,
  230. // 'bill_date'=>$day,
  231. // 'bill_type'=>strtoupper($type),
  232. // 'mch_id'=> $merchant->merchant_id,
  233. // 'nonce_str' => uniqid()
  234. // ];
  235. // $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $merchant->key, 'md5');
  236. // $xml = XML::build($params);
  237. // dump(self::paymentService()->downloadBill($day)->getContents());
  238. // dump($payment->getHttp()->request('https://api.mch.weixin.qq.com/pay/downloadbill','POST',[
  239. // 'body' => $xml,
  240. // 'stream'=>true
  241. // ])->getBody()->getContents());
  242. }
  243. public static function userTagService()
  244. {
  245. return self::application()->user_tag;
  246. }
  247. public static function userGroupService()
  248. {
  249. return self::application()->user_group;
  250. }
  251. /**
  252. * 生成支付订单对象
  253. * @param $openid
  254. * @param $out_trade_no
  255. * @param $total_fee
  256. * @param $attach
  257. * @param $body
  258. * @param string $detail
  259. * @param string $trade_type
  260. * @param array $options
  261. * @return Order
  262. */
  263. protected static function paymentOrder($openid,$out_trade_no,$total_fee,$attach,$body,$detail='',$trade_type='JSAPI',$options = [])
  264. {
  265. $total_fee = bcmul($total_fee,100,0);
  266. $order = array_merge(compact('openid','out_trade_no','total_fee','attach','body','detail','trade_type'),$options);
  267. if($order['detail'] == '') unset($order['detail']);
  268. return new Order($order);
  269. }
  270. /**
  271. * 获得下单ID
  272. * @param $openid
  273. * @param $out_trade_no
  274. * @param $total_fee
  275. * @param $attach
  276. * @param $body
  277. * @param string $detail
  278. * @param string $trade_type
  279. * @param array $options
  280. * @return mixed
  281. */
  282. public static function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail='', $trade_type='JSAPI', $options = [])
  283. {
  284. $order = self::paymentOrder($openid,$out_trade_no,$total_fee,$attach,$body,$detail,$trade_type,$options);
  285. $result = self::paymentService()->prepare($order);
  286. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
  287. try{
  288. HookService::listen('wechat_payment_prepare',$order,$result->prepay_id,false,PaymentBehavior::class);
  289. }catch (\Exception $e){}
  290. return $result->prepay_id;
  291. }else{
  292. if($result->return_code == 'FAIL'){
  293. exception('微信支付错误返回:'.$result->return_msg);
  294. }else if(isset($result->err_code)){
  295. exception('微信支付错误返回:'.$result->err_code_des);
  296. }else{
  297. exception('没有获取微信支付的预支付ID,请重新发起支付!');
  298. }
  299. exit;
  300. }
  301. }
  302. /**
  303. * 获得jsSdk支付参数
  304. * @param $openid
  305. * @param $out_trade_no
  306. * @param $total_fee
  307. * @param $attach
  308. * @param $body
  309. * @param string $detail
  310. * @param string $trade_type
  311. * @param array $options
  312. * @return array|string
  313. */
  314. public static function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail='', $trade_type='JSAPI', $options = [])
  315. {
  316. return self::paymentService()->configForJSSDKPayment(self::paymentPrepare($openid,$out_trade_no,$total_fee,$attach,$body,$detail,$trade_type,$options));
  317. }
  318. /**
  319. * 使用商户订单号退款
  320. * @param $orderNo
  321. * @param $refundNo
  322. * @param $totalFee
  323. * @param null $refundFee
  324. * @param null $opUserId
  325. * @param string $refundReason
  326. * @param string $type
  327. * @param string $refundAccount
  328. */
  329. public static function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '' , $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
  330. {
  331. $totalFee = floatval($totalFee);
  332. $refundFee = floatval($refundFee);
  333. return self::paymentService()->refund($orderNo,$refundNo,$totalFee,$refundFee,$opUserId,$type,$refundAccount,$refundReason);
  334. }
  335. public static function payOrderRefund($orderNo, array $opt)
  336. {
  337. if(!isset($opt['pay_price'])) exception('缺少pay_price');
  338. $totalFee = floatval(bcmul($opt['pay_price'],100,0));
  339. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'],100,0)) : null;
  340. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  341. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  342. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  343. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  344. /*仅针对老资金流商户使用
  345. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  346. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  347. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  348. try{
  349. $res = (self::refund($orderNo,$refundNo,$totalFee,$refundFee,$opUserId,$refundReason,$type,$refundAccount));
  350. if($res->return_code == 'FAIL') exception('退款失败:'.$res->return_msg);
  351. if(isset($res->err_code)) exception('退款失败:'.$res->err_code_des);
  352. }catch (\Exception $e){
  353. exception($e->getMessage());
  354. }
  355. return true;
  356. }
  357. /**
  358. * 微信支付成功回调接口
  359. */
  360. public static function handleNotify()
  361. {
  362. self::paymentService()->handleNotify(function($notify, $successful){
  363. if($successful && isset($notify->out_trade_no)){
  364. WechatMessage::setOnceMessage($notify,$notify->openid,'payment_success',$notify->out_trade_no);
  365. return HookService::listen('wechat_pay_success',$notify,null,true,PaymentBehavior::class);
  366. }
  367. });
  368. }
  369. /**
  370. * jsSdk
  371. * @return \EasyWeChat\Js\Js
  372. */
  373. public static function jsService()
  374. {
  375. return self::application()->js;
  376. }
  377. public static function jsSdk($url = '')
  378. {
  379. $apiList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard'];
  380. $jsService = self::jsService();
  381. if($url) $jsService->setUrl($url);
  382. try{
  383. return $jsService->config($apiList);
  384. }catch (\Exception $e){
  385. return '{}';
  386. }
  387. }
  388. /**
  389. * 回复文本消息
  390. * @param string $content 文本内容
  391. * @return Text
  392. */
  393. public static function textMessage($content)
  394. {
  395. return new Text(compact('content'));
  396. }
  397. /**
  398. * 回复图片消息
  399. * @param string $media_id 媒体资源 ID
  400. * @return Image
  401. */
  402. public static function imageMessage($media_id)
  403. {
  404. return new Image(compact('media_id'));
  405. }
  406. /**
  407. * 回复视频消息
  408. * @param string $media_id 媒体资源 ID
  409. * @param string $title 标题
  410. * @param string $description 描述
  411. * @param null $thumb_media_id 封面资源 ID
  412. * @return Video
  413. */
  414. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  415. {
  416. return new Video(compact('media_id','title','description','thumb_media_id'));
  417. }
  418. /**
  419. * 回复声音消息
  420. * @param string $media_id 媒体资源 ID
  421. * @return Voice
  422. */
  423. public static function voiceMessage($media_id)
  424. {
  425. return new Voice(compact('media_id'));
  426. }
  427. /**
  428. * 回复图文消息
  429. * @param string|array $title 标题
  430. * @param string $description 描述
  431. * @param string $url URL
  432. * @param string $image 图片链接
  433. */
  434. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  435. {
  436. if(is_array($title)){
  437. if(isset($title[0]) && is_array($title[0])){
  438. $newsList = [];
  439. foreach ($title as $news){
  440. $newsList[] = self::newsMessage($news);
  441. }
  442. return $newsList;
  443. }else{
  444. $data = $title;
  445. }
  446. }else{
  447. $data = compact('title','description','url','image');
  448. }
  449. return new News($data);
  450. }
  451. /**
  452. * 回复文章消息
  453. * @param string|array $title 标题
  454. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  455. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  456. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  457. * @param string $author 作者
  458. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  459. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  460. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  461. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  462. * @return Article
  463. */
  464. public static function articleMessage($title, $thumb_media_id, $source_url, $content = '', $author = '', $digest = '', $show_cover_pic = 0, $need_open_comment = 0, $only_fans_can_comment = 1)
  465. {
  466. $data = is_array($title) ? $title : compact('title','thumb_media_id','source_url','content','author','digest','show_cover_pic','need_open_comment','only_fans_can_comment');
  467. return new Article($data);
  468. }
  469. /**
  470. * 回复素材消息
  471. * @param string $type [mpnews、 mpvideo、voice、image]
  472. * @param string $media_id 素材 ID
  473. * @return Material
  474. */
  475. public static function materialMessage($type, $media_id)
  476. {
  477. return new Material($type,$media_id);
  478. }
  479. /**
  480. * 作为客服消息发送
  481. * @param $to
  482. * @param $message
  483. * @return bool
  484. */
  485. public static function staffTo($to, $message)
  486. {
  487. $staff = self::staffService();
  488. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  489. $res = $staff->to($to)->send();
  490. HookService::afterListen('wechat_staff_to',compact('to','message'),$res);
  491. return $res;
  492. }
  493. /**
  494. * 获得用户信息
  495. * @param array|string $openid
  496. * @return \EasyWeChat\Support\Collection
  497. */
  498. public static function getUserInfo($openid)
  499. {
  500. $userService = self::userService();
  501. $userInfo = is_array($openid) ? $userService->batchGet($openid) : $userService->get($openid);
  502. return $userInfo;
  503. }
  504. }