WechatService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/23
  6. */
  7. namespace crmeb\services;
  8. use app\admin\model\wechat\WechatMessage;
  9. use app\admin\model\wechat\WechatReply;
  10. use app\models\order\Level;
  11. use app\models\store\StoreOrder as StoreOrderWapModel;
  12. use app\models\user\User;
  13. use app\models\user\UserRecharge;
  14. use app\models\user\WechatUser;
  15. use app\Request;
  16. use crmeb\repositories\MessageRepositories;
  17. use crmeb\repositories\PaymentRepositories;
  18. use EasyWeChat\Foundation\Application;
  19. use EasyWeChat\Message\Article;
  20. use EasyWeChat\Message\Image;
  21. use EasyWeChat\Message\Material;
  22. use EasyWeChat\Message\News;
  23. use EasyWeChat\Message\Text;
  24. use EasyWeChat\Message\Video;
  25. use EasyWeChat\Message\Voice;
  26. use EasyWeChat\Payment\Order;
  27. use EasyWeChat\Server\Guard;
  28. use think\facade\Env;
  29. use think\facade\Log;
  30. use think\Response;
  31. use crmeb\utils\Hook;
  32. class WechatService
  33. {
  34. private static $instance = null;
  35. public static function options()
  36. {
  37. $wechat = SystemConfigService::more(['wechat_appid', 'wechat_appsecret', 'wechat_token', 'wechat_encodingaeskey', 'wechat_encode']);
  38. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
  39. $config = [
  40. 'app_id' => isset($wechat['wechat_appid']) ? trim($wechat['wechat_appid']) : '',
  41. 'secret' => isset($wechat['wechat_appsecret']) ? trim($wechat['wechat_appsecret']) : '',
  42. 'token' => isset($wechat['wechat_token']) ? trim($wechat['wechat_token']) : '',
  43. 'guzzle' => [
  44. 'timeout' => 10.0, // 超时时间(秒)
  45. 'verify' => false
  46. ],
  47. ];
  48. if (isset($wechat['wechat_encode']) && (int)$wechat['wechat_encode'] > 0 && isset($wechat['wechat_encodingaeskey']) && !empty($wechat['wechat_encodingaeskey']))
  49. $config['aes_key'] = $wechat['wechat_encodingaeskey'];
  50. $prefix = getcwd();
  51. if (strpos($prefix, 'public') === false) {
  52. $prefix .= "/public";
  53. }
  54. if (isset($payment['pay_weixin_open']) && $payment['pay_weixin_open'] == 1) {
  55. $config['payment'] = [
  56. 'merchant_id' => trim($payment['pay_weixin_mchid']),
  57. 'key' => trim($payment['pay_weixin_key']),
  58. // 'cert_path' => realpath('.' . $payment['pay_weixin_client_cert']),
  59. // 'key_path' => realpath('.' . $payment['pay_weixin_client_key']),
  60. 'cert_path' => $prefix . $payment['pay_weixin_client_cert'],
  61. 'key_path' => $prefix . $payment['pay_weixin_client_key'],
  62. 'notify_url' => sys_config('site_url') . '/api/wechat/notify'
  63. ];
  64. }
  65. // Log::write($prefix, 'error');
  66. return $config;
  67. }
  68. public static function application($cache = false)
  69. {
  70. (self::$instance === null || $cache === true) && (self::$instance = new Application(self::options()));
  71. return self::$instance;
  72. }
  73. public static function serve(): Response
  74. {
  75. $wechat = self::application(true);
  76. $server = $wechat->server;
  77. self::hook($server);
  78. $response = $server->serve();
  79. return response($response->getContent());
  80. }
  81. /**
  82. * 监听行为
  83. * @param Guard $server
  84. */
  85. private static function hook($server)
  86. {
  87. $server->setMessageHandler(function ($message) {
  88. event('WechatMessageBefore', [$message]);
  89. switch ($message->MsgType) {
  90. case 'event':
  91. switch (strtolower($message->Event)) {
  92. case 'subscribe':
  93. $response = WechatReply::reply('subscribe');
  94. if (isset($message->EventKey)) {
  95. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  96. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  97. if (strtolower($qrInfo['third_type']) == 'spread') {
  98. try {
  99. $spreadUid = $qrInfo['third_id'];
  100. $uid = WechatUser::openidToUid($message->FromUserName, 'openid');
  101. if ($spreadUid == $uid) return '自己不能推荐自己';
  102. $userInfo = User::getUserInfo($uid);
  103. if ($userInfo['spread_uid']) return '已有推荐人!';
  104. if (!User::setSpreadUid($userInfo['uid'], $spreadUid)) {
  105. $response = '绑定推荐人失败!';
  106. }
  107. } catch (\Exception $e) {
  108. $response = $e->getMessage();
  109. }
  110. }
  111. }
  112. }
  113. break;
  114. case 'unsubscribe':
  115. event('WechatEventUnsubscribeBefore', [$message]);
  116. break;
  117. case 'scan':
  118. $response = WechatReply::reply('subscribe');
  119. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  120. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  121. if (strtolower($qrInfo['third_type']) == 'spread') {
  122. try {
  123. $spreadUid = $qrInfo['third_id'];
  124. $uid = WechatUser::openidToUid($message->FromUserName, 'openid');
  125. if ($spreadUid == $uid) return '自己不能推荐自己';
  126. $userInfo = User::getUserInfo($uid);
  127. if ($userInfo['spread_uid']) return '已有推荐人!';
  128. if (User::setSpreadUid($userInfo['uid'], $spreadUid)) {
  129. $response = '绑定推荐人失败!';
  130. }
  131. } catch (\Exception $e) {
  132. $response = $e->getMessage();
  133. }
  134. }
  135. }
  136. break;
  137. case 'location':
  138. $response = MessageRepositories::wechatEventLocation($message);
  139. break;
  140. case 'click':
  141. $response = WechatReply::reply($message->EventKey);
  142. break;
  143. case 'view':
  144. $response = MessageRepositories::wechatEventView($message);
  145. break;
  146. }
  147. break;
  148. case 'text':
  149. //$response = WechatReply::reply($message->Content);
  150. $word = $message->Content;
  151. if ($word == "客服") {
  152. // @file_put_contents('message.txt',json_encode($message));
  153. // $arr['ToUserName'] = $openid;
  154. // $arr['FromUserName'] = 'jiangmiaojun123';
  155. // $arr['CreateTime'] = time();
  156. // $arr['MsgType'] = 'transfer_customer_service';
  157. // $xml = arraytoxml($arr);
  158. // echo $xml;
  159. $xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[transfer_customer_service]]></MsgType></xml>";
  160. echo sprintf($xml, $message->FromUserName, $message->ToUserName, $message->CreateTime);
  161. die;
  162. } else {
  163. $response = WechatReply::reply($message->Content);
  164. }
  165. break;
  166. case 'image':
  167. $response = MessageRepositories::wechatMessageImage($message);
  168. break;
  169. case 'voice':
  170. $response = MessageRepositories::wechatMessageVoice($message);
  171. break;
  172. case 'video':
  173. $response = MessageRepositories::wechatMessageVideo($message);
  174. break;
  175. case 'location':
  176. $response = MessageRepositories::wechatMessageLocation($message);
  177. break;
  178. case 'link':
  179. $response = MessageRepositories::wechatMessageLink($message);
  180. break;
  181. // ... 其它消息
  182. default:
  183. $response = MessageRepositories::wechatMessageOther($message);
  184. break;
  185. }
  186. return $response ?? false;
  187. });
  188. }
  189. /**
  190. * 多客服消息转发
  191. * @param string $account
  192. * @return \EasyWeChat\Message\Transfer
  193. */
  194. public static function transfer($account = '')
  195. {
  196. $transfer = new \EasyWeChat\Message\Transfer();
  197. return empty($account) ? $transfer : $transfer->to($account);
  198. }
  199. /**
  200. * 上传永久素材接口
  201. * @return \EasyWeChat\Material\Material
  202. */
  203. public static function materialService()
  204. {
  205. return self::application()->material;
  206. }
  207. /**
  208. * 上传临时素材接口
  209. * @return \EasyWeChat\Material\Temporary
  210. */
  211. public static function materialTemporaryService()
  212. {
  213. return self::application()->material_temporary;
  214. }
  215. /**
  216. * 用户接口
  217. * @return \EasyWeChat\User\User
  218. */
  219. public static function userService()
  220. {
  221. return self::application()->user;
  222. }
  223. /**
  224. * 客服消息接口
  225. * @param null $to
  226. * @param null $message
  227. */
  228. public static function staffService()
  229. {
  230. return self::application()->staff;
  231. }
  232. /**
  233. * 微信公众号菜单接口
  234. * @return \EasyWeChat\Menu\Menu
  235. */
  236. public static function menuService()
  237. {
  238. return self::application()->menu;
  239. }
  240. /**
  241. * 微信二维码生成接口
  242. * @return \EasyWeChat\QRCode\QRCode
  243. */
  244. public static function qrcodeService()
  245. {
  246. return self::application()->qrcode;
  247. }
  248. /**
  249. * 微信永久二维码生成接口 小于10万个
  250. * @return \EasyWeChat\QRCode\QRCode
  251. */
  252. public static function qrcodeForeverService($sceneValue)
  253. {
  254. return self::application()->qrcode->forever($sceneValue);
  255. }
  256. /**
  257. * 微信临时二维码生成接口 30天有效期
  258. * @return \EasyWeChat\QRCode\QRCode
  259. */
  260. public static function qrcodeTempService($sceneValue, $expireSeconds = 2592000)
  261. {
  262. return self::application()->qrcode->temporary($sceneValue, $expireSeconds);
  263. }
  264. /**
  265. * 短链接生成接口
  266. * @return \EasyWeChat\Url\Url
  267. */
  268. public static function urlService()
  269. {
  270. return self::application()->url;
  271. }
  272. /**
  273. * 用户授权
  274. * @return \Overtrue\Socialite\Providers\WeChatProvider
  275. */
  276. public static function oauthService()
  277. {
  278. return self::application()->oauth;
  279. }
  280. /**
  281. * 模板消息接口
  282. * @return \EasyWeChat\Notice\Notice
  283. */
  284. public static function noticeService()
  285. {
  286. return self::application()->notice;
  287. }
  288. public static function sendTemplate($openid, $templateId, array $data, $url = null, $defaultColor = null)
  289. {
  290. $notice = self::noticeService()->to($openid)->template($templateId)->andData($data);
  291. if ($url !== null) $notice->url($url);
  292. if ($defaultColor !== null) $notice->defaultColor($defaultColor);
  293. return $notice->send();
  294. }
  295. /**
  296. * 支付
  297. * @return \EasyWeChat\Payment\Payment
  298. */
  299. public static function paymentService()
  300. {
  301. return self::application()->payment;
  302. }
  303. public static function downloadBill($day, $type = 'ALL')
  304. {
  305. // $payment = self::paymentService();
  306. // $merchant = $payment->getMerchant();
  307. // $params = [
  308. // 'appid' => $merchant->app_id,
  309. // 'bill_date'=>$day,
  310. // 'bill_type'=>strtoupper($type),
  311. // 'mch_id'=> $merchant->merchant_id,
  312. // 'nonce_str' => uniqid()
  313. // ];
  314. // $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $merchant->key, 'md5');
  315. // $xml = XML::build($params);
  316. // dump(self::paymentService()->downloadBill($day)->getContents());
  317. // dump($payment->getHttp()->request('https://api.mch.weixin.qq.com/pay/downloadbill','POST',[
  318. // 'body' => $xml,
  319. // 'stream'=>true
  320. // ])->getBody()->getContents());
  321. }
  322. public static function userTagService()
  323. {
  324. return self::application()->user_tag;
  325. }
  326. public static function userGroupService()
  327. {
  328. return self::application()->user_group;
  329. }
  330. /**
  331. * 生成支付订单对象
  332. * @param $openid
  333. * @param $out_trade_no
  334. * @param $total_fee
  335. * @param $attach
  336. * @param $body
  337. * @param string $detail
  338. * @param string $trade_type
  339. * @param array $options
  340. * @return Order
  341. */
  342. protected static function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  343. {
  344. $total_fee = bcmul($total_fee, 100, 0);
  345. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
  346. if (!is_null($openid)) $order['openid'] = $openid;
  347. if ($order['detail'] == '') unset($order['detail']);
  348. return new Order($order);
  349. }
  350. /**
  351. * 获得下单ID
  352. * @param $openid
  353. * @param $out_trade_no
  354. * @param $total_fee
  355. * @param $attach
  356. * @param $body
  357. * @param string $detail
  358. * @param string $trade_type
  359. * @param array $options
  360. * @return mixed
  361. */
  362. public static function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  363. {
  364. $order = self::paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  365. $result = self::paymentService()->prepare($order);
  366. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  367. try {
  368. PaymentRepositories::wechatPaymentPrepare($order, $result->prepay_id);
  369. } catch (\Exception $e) {
  370. }
  371. return $result;
  372. } else {
  373. if ($result->return_code == 'FAIL') {
  374. exception('微信支付错误返回:' . $result->return_msg);
  375. } else if (isset($result->err_code)) {
  376. exception('微信支付错误返回:' . $result->err_code_des);
  377. } else {
  378. exception('没有获取微信支付的预支付ID,请重新发起支付!');
  379. }
  380. exit;
  381. }
  382. }
  383. /**
  384. * 获得jsSdk支付参数
  385. * @param $openid
  386. * @param $out_trade_no
  387. * @param $total_fee
  388. * @param $attach
  389. * @param $body
  390. * @param string $detail
  391. * @param string $trade_type
  392. * @param array $options
  393. * @return array|string
  394. */
  395. public static function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  396. {
  397. $paymentPrepare = self::paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  398. return self::paymentService()->configForJSSDKPayment($paymentPrepare->prepay_id);
  399. }
  400. /**
  401. * 使用商户订单号退款
  402. * @param $orderNo
  403. * @param $refundNo
  404. * @param $totalFee
  405. * @param null $refundFee
  406. * @param null $opUserId
  407. * @param string $refundReason
  408. * @param string $type
  409. * @param string $refundAccount
  410. */
  411. public static function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
  412. {
  413. $totalFee = floatval($totalFee);
  414. $refundFee = floatval($refundFee);
  415. return self::paymentService()->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
  416. }
  417. public static function payOrderRefund($orderNo, array $opt)
  418. {
  419. if (!isset($opt['pay_price'])) exception('缺少pay_price');
  420. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  421. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  422. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  423. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  424. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  425. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  426. /*仅针对老资金流商户使用
  427. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  428. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  429. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  430. try {
  431. $res = (self::refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
  432. if ($res->return_code == 'FAIL') exception('退款失败:' . $res->return_msg);
  433. if (isset($res->err_code)) exception('退款失败:' . $res->err_code_des);
  434. } catch (\Exception $e) {
  435. exception($e->getMessage());
  436. }
  437. return true;
  438. }
  439. /**
  440. * 微信支付成功回调接口
  441. */
  442. public static function handleNotify()
  443. {
  444. self::paymentService()->handleNotify(function ($notify, $successful) {
  445. if ($successful && isset($notify->out_trade_no)) {
  446. if (isset($notify->attach) && $notify->attach) {
  447. if (($count = strpos($notify->out_trade_no, '_')) !== false) {
  448. $notify->out_trade_no = substr($notify->out_trade_no, $count + 1);
  449. }
  450. if (strtolower($notify->attach) == 'product') {//TODO 商品订单支付成功后
  451. try {
  452. if (StoreOrderWapModel::be(['order_id' => $notify->out_trade_no, 'paid' => 1])) return true;
  453. return StoreOrderWapModel::paySuccess($notify->out_trade_no);
  454. } catch (\Exception $e) {
  455. return false;
  456. }
  457. } else if (strtolower($notify->attach) == 'user_level') {//TODO 用户开通成功后
  458. try {
  459. if (UserRecharge::be(['order_id' => $notify->out_trade_no, 'paid' => 1])) return true;
  460. return Level::paySuccess($notify->out_trade_no);
  461. } catch (\Exception $e) {
  462. return false;
  463. }
  464. }
  465. (new Hook(PaymentRepositories::class, 'wechat'))->listen($notify->attach, $notify->out_trade_no);
  466. }
  467. WechatMessage::setOnceMessage($notify, $notify->openid, 'payment_success', $notify->out_trade_no);
  468. return false;
  469. }
  470. });
  471. }
  472. /**
  473. * jsSdk
  474. * @return \EasyWeChat\Js\Js
  475. */
  476. public static function jsService()
  477. {
  478. return self::application()->js;
  479. }
  480. public static function jsSdk($url = '')
  481. {
  482. $apiList = ['editAddress', 'openAddress', 'updateTimelineShareData', 'updateAppMessageShareData', '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'];
  483. $jsService = self::jsService();
  484. if ($url) $jsService->setUrl($url);
  485. try {
  486. return $jsService->config($apiList);
  487. } catch (\Exception $e) {
  488. return '{}';
  489. }
  490. }
  491. /**
  492. * 回复文本消息
  493. * @param string $content 文本内容
  494. * @return Text
  495. */
  496. public static function textMessage($content)
  497. {
  498. return new Text(compact('content'));
  499. }
  500. /**
  501. * 回复图片消息
  502. * @param string $media_id 媒体资源 ID
  503. * @return Image
  504. */
  505. public static function imageMessage($media_id)
  506. {
  507. return new Image(compact('media_id'));
  508. }
  509. /**
  510. * 回复视频消息
  511. * @param string $media_id 媒体资源 ID
  512. * @param string $title 标题
  513. * @param string $description 描述
  514. * @param null $thumb_media_id 封面资源 ID
  515. * @return Video
  516. */
  517. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  518. {
  519. return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
  520. }
  521. /**
  522. * 回复声音消息
  523. * @param string $media_id 媒体资源 ID
  524. * @return Voice
  525. */
  526. public static function voiceMessage($media_id)
  527. {
  528. return new Voice(compact('media_id'));
  529. }
  530. /**
  531. * 回复图文消息
  532. * @param string|array $title 标题
  533. * @param string $description 描述
  534. * @param string $url URL
  535. * @param string $image 图片链接
  536. */
  537. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  538. {
  539. if (is_array($title)) {
  540. if (isset($title[0]) && is_array($title[0])) {
  541. $newsList = [];
  542. foreach ($title as $news) {
  543. $newsList[] = self::newsMessage($news);
  544. }
  545. return $newsList;
  546. } else {
  547. $data = $title;
  548. }
  549. } else {
  550. $data = compact('title', 'description', 'url', 'image');
  551. }
  552. return new News($data);
  553. }
  554. /**
  555. * 回复文章消息
  556. * @param string|array $title 标题
  557. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  558. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  559. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  560. * @param string $author 作者
  561. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  562. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  563. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  564. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  565. * @return Article
  566. */
  567. 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)
  568. {
  569. $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');
  570. return new Article($data);
  571. }
  572. /**
  573. * 回复素材消息
  574. * @param string $type [mpnews、 mpvideo、voice、image]
  575. * @param string $media_id 素材 ID
  576. * @return Material
  577. */
  578. public static function materialMessage($type, $media_id)
  579. {
  580. return new Material($type, $media_id);
  581. }
  582. /**
  583. * 作为客服消息发送
  584. * @param $to
  585. * @param $message
  586. * @return bool
  587. */
  588. public static function staffTo($to, $message)
  589. {
  590. $staff = self::staffService();
  591. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  592. $res = $staff->to($to)->send();
  593. return $res;
  594. }
  595. /**
  596. * 获得用户信息
  597. * @param array|string $openid
  598. * @return \EasyWeChat\Support\Collection
  599. */
  600. public static function getUserInfo($openid)
  601. {
  602. $userService = self::userService();
  603. $userInfo = is_array($openid) ? $userService->batchGet($openid) : $userService->get($openid);
  604. return $userInfo;
  605. }
  606. }