WechatService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\common\repositories\system\config\ConfigValueRepository;
  13. use app\common\repositories\wechat\WechatQrcodeRepository;
  14. use app\common\repositories\wechat\WechatReplyRepository;
  15. use app\common\repositories\wechat\WechatUserRepository;
  16. use crmeb\exceptions\WechatException;
  17. use EasyWeChat\Core\Exceptions\FaultException;
  18. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  19. use EasyWeChat\Core\Exceptions\RuntimeException;
  20. use EasyWeChat\Foundation\Application;
  21. use EasyWeChat\Message\Article;
  22. use EasyWeChat\Message\Image;
  23. use EasyWeChat\Message\Material;
  24. use EasyWeChat\Message\News;
  25. use EasyWeChat\Message\Text;
  26. use EasyWeChat\Message\Video;
  27. use EasyWeChat\Message\Voice;
  28. use EasyWeChat\Payment\Order;
  29. use EasyWeChat\Server\BadRequestException;
  30. use EasyWeChat\Support\Collection;
  31. use Exception;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use think\facade\Cache;
  34. use think\facade\Log;
  35. use think\facade\Route;
  36. use think\Response;
  37. /**
  38. * Class WechatService
  39. * @package crmeb\services
  40. * @author xaboy
  41. * @day 2020-04-20
  42. */
  43. class WechatService
  44. {
  45. /**
  46. * @var Application
  47. */
  48. protected $application;
  49. protected $config;
  50. /**
  51. * WechatService constructor.
  52. * @param $config
  53. */
  54. public function __construct(array $config)
  55. {
  56. $this->config = $config;
  57. $this->application = new Application($config);
  58. }
  59. /**
  60. * @return array
  61. * @author xaboy
  62. * @day 2020-04-24
  63. */
  64. public static function getConfig()
  65. {
  66. /** @var ConfigValueRepository $make */
  67. $make = app()->make(ConfigValueRepository::class);
  68. $wechat = $make->more([
  69. 'wechat_appid', 'wechat_appsecret', 'wechat_token', 'wechat_encodingaeskey', 'wechat_encode'
  70. ], 0);
  71. $payment = $make->more(['site_url', 'pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open'], 0);
  72. $config = [
  73. 'app_id' => trim($wechat['wechat_appid']),
  74. 'secret' => trim($wechat['wechat_appsecret']),
  75. 'token' => trim($wechat['wechat_token']),
  76. 'guzzle' => [
  77. 'timeout' => 10.0, // 超时时间(秒)
  78. 'verify' => false
  79. ],
  80. 'debug' => false,
  81. ];
  82. if ($wechat['wechat_encode'] > 0 && $wechat['wechat_encodingaeskey'])
  83. $config['aes_key'] = trim($wechat['wechat_encodingaeskey']);
  84. if (isset($payment['pay_weixin_open']) && $payment['pay_weixin_open'] == 1) {
  85. $config['payment'] = [
  86. 'merchant_id' => trim($payment['pay_weixin_mchid']),
  87. 'key' => trim($payment['pay_weixin_key']),
  88. 'cert_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_cert']),
  89. 'key_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_key']),
  90. 'notify_url' => $payment['site_url'] . Route::buildUrl('wechatNotify')->build(),
  91. 'pay_weixin_client_cert' => $payment['pay_weixin_client_cert'],
  92. 'pay_weixin_client_key' => $payment['pay_weixin_client_key'],
  93. ];
  94. }
  95. return $config;
  96. }
  97. /**
  98. * @return self
  99. * @author xaboy
  100. * @day 2020-04-24
  101. */
  102. public static function create()
  103. {
  104. return new self(self::getConfig());
  105. }
  106. /**
  107. * @return Application
  108. * @author xaboy
  109. * @day 2020-04-20
  110. */
  111. public function getApplication()
  112. {
  113. return $this->application;
  114. }
  115. /**
  116. * @param \think\Request $request
  117. * @return Response
  118. * @throws BadRequestException
  119. * @throws InvalidArgumentException
  120. * @author xaboy
  121. * @day 2020-04-26
  122. */
  123. public function serve(\think\Request $request)
  124. {
  125. $this->serverRequest($request);
  126. $this->wechatEventHook();
  127. return response($this->application->server->serve()->getContent());
  128. }
  129. protected function serverRequest(\think\Request $request)
  130. {
  131. $this->application->server->setRequest(new Request($request->get(), $request->post(), [], [], [], $request->server(), $request->getContent()));
  132. }
  133. /**
  134. * @throws InvalidArgumentException
  135. * @author xaboy
  136. * @day 2020-04-20
  137. */
  138. public function wechatEventHook()
  139. {
  140. $this->application->server->setMessageHandler(function ($message) {
  141. $openId = $message->FromUserName;
  142. $userInfo = $this->getUserInfo($openId);
  143. /** @var WechatUserRepository $wechatUserRepository */
  144. $wechatUserRepository = app()->make(WechatUserRepository::class);
  145. $users = $wechatUserRepository->syncUser($openId, $userInfo, true);
  146. $scanLogin = function () use ($message, $users) {
  147. $ticket = $message->EventKey;
  148. if (strpos($ticket, '_sys_scan_login.') === 0) {
  149. $key = str_replace('_sys_scan_login.', '', $ticket);
  150. Cache::set('_scan_login' . $key, $users[1]['uid']);
  151. }
  152. };
  153. $response = null;
  154. /** @var WechatReplyRepository $make * */
  155. $make = app()->make(WechatReplyRepository::class);
  156. event('WechatMessage', $message);
  157. switch ($message->MsgType) {
  158. case 'event':
  159. switch (strtolower($message->Event)) {
  160. case 'subscribe':
  161. $scanLogin();
  162. $response = $make->reply('subscribe');
  163. if (isset($message->EventKey) && $message->EventKey) {
  164. /** @var WechatQrcodeRepository $qr */
  165. $qr = app()->make(WechatQrcodeRepository::class);
  166. if ($qrInfo = $qr->ticketByQrcode($message->Ticket)) {
  167. $qrInfo->incTicket();
  168. if (strtolower($qrInfo['third_type']) == 'spread' && $users) {
  169. $spreadUid = $qrInfo['third_id'];
  170. if ($users[1]['uid'] == $spreadUid)
  171. return '自己不能推荐自己';
  172. else if ($users[1]['spread_uid'])
  173. return '已有推荐人!';
  174. try {
  175. $users[1]->setSpread($spreadUid);
  176. } catch (Exception $e) {
  177. return '绑定推荐人失败';
  178. }
  179. }
  180. }
  181. }
  182. event('WechatEventSubscribe', $message);
  183. break;
  184. case 'unsubscribe':
  185. $wechatUserRepository->unsubscribe($openId);
  186. event('WechatEventUnsubscribe', $message);
  187. break;
  188. case 'scan':
  189. $scanLogin();
  190. $response = $make->reply('subscribe');
  191. /** @var WechatQrcodeRepository $qr */
  192. $qr = app()->make(WechatQrcodeRepository::class);
  193. if ($message->EventKey && ($qrInfo = $qr->ticketByQrcode($message->Ticket))) {
  194. $qrInfo->incTicket();
  195. if (strtolower($qrInfo['third_type']) == 'spread' && $users) {
  196. $spreadUid = $qrInfo['third_id'];
  197. if ($users[1]['uid'] == $spreadUid)
  198. return '自己不能推荐自己';
  199. else if ($users[1]['spread_uid'])
  200. return '已有推荐人!';
  201. try {
  202. $users[1]->setSpread($spreadUid);
  203. } catch (Exception $e) {
  204. return '绑定推荐人失败';
  205. }
  206. }
  207. }
  208. event('WechatEventScan', $message);
  209. break;
  210. case 'location':
  211. event('wechatEventLocation', $message);
  212. break;
  213. case 'click':
  214. $response = $make->reply($message->EventKey);
  215. break;
  216. case 'view':
  217. event('wechatEventView', $message);
  218. break;
  219. }
  220. break;
  221. case 'text':
  222. $response = $make->reply($message->Content);
  223. event('wechatMessageText', $message);
  224. break;
  225. case 'image':
  226. event('wechatMessageImage', $message);
  227. break;
  228. case 'voice':
  229. event('wechatMessageVoice', $message);
  230. break;
  231. case 'video':
  232. event('wechatMessageVideo', $message);
  233. break;
  234. case 'location':
  235. event('wechatMessageLocation', $message);
  236. break;
  237. case 'link':
  238. event('wechatMessageLink', $message);
  239. break;
  240. // ... 其它消息
  241. default:
  242. event('WechatMessageOther', $message);
  243. break;
  244. }
  245. return $response ?? false;
  246. });
  247. }
  248. /**
  249. * @param $value
  250. * @return Collection
  251. * @author xaboy
  252. * @day 2020-04-20
  253. */
  254. public function qrcodeForever($value)
  255. {
  256. return $this->application->qrcode->forever($value);
  257. }
  258. /**
  259. * @param $value
  260. * @param int $expireSeconds
  261. * @return Collection
  262. * @author xaboy
  263. * @day 2020-04-20
  264. */
  265. public function qrcodeTemp($value, $expireSeconds = 2592000)
  266. {
  267. return $this->application->qrcode->temporary($value, $expireSeconds);
  268. }
  269. /**
  270. * @param string $openid
  271. * @param string $templateId
  272. * @param array $data
  273. * @param null $url
  274. * @param null $defaultColor
  275. * @return mixed
  276. * @author xaboy
  277. * @day 2020-04-20
  278. */
  279. public function sendTemplate(string $openid, string $templateId, array $data, $url = null, $defaultColor = null)
  280. {
  281. $notice = $this->application->notice->to($openid)->template($templateId)->andData($data);
  282. if ($url !== null) $notice->url($url);
  283. if ($defaultColor !== null) $notice->defaultColor($defaultColor);
  284. return $notice->send();
  285. }
  286. /**
  287. * @param $openid
  288. * @param $out_trade_no
  289. * @param $total_fee
  290. * @param $attach
  291. * @param $body
  292. * @param string $detail
  293. * @param string $trade_type
  294. * @param array $options
  295. * @return Order
  296. * @author xaboy
  297. * @day 2020-04-20
  298. */
  299. protected function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  300. {
  301. $total_fee = bcmul($total_fee, 100, 0);
  302. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
  303. if (!is_null($openid)) $order['openid'] = $openid;
  304. if ($order['detail'] == '') unset($order['detail']);
  305. $order['spbill_create_ip'] = \request()->ip();
  306. return new Order($order);
  307. }
  308. /**
  309. * @param $openid
  310. * @param $out_trade_no
  311. * @param $total_fee
  312. * @param $attach
  313. * @param $body
  314. * @param string $detail
  315. * @param string $trade_type
  316. * @param array $options
  317. * @return Collection
  318. * @author xaboy
  319. * @day 2020-04-20
  320. */
  321. public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  322. {
  323. $order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  324. $result = $this->application->payment->prepare($order);
  325. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  326. return $result;
  327. } else {
  328. if ($result->return_code == 'FAIL') {
  329. throw new WechatException('微信支付错误返回:' . $result->return_msg);
  330. } else if (isset($result->err_code)) {
  331. throw new WechatException('微信支付错误返回:' . $result->err_code_des);
  332. } else {
  333. throw new WechatException('没有获取微信支付的预支付ID,请重新发起支付!');
  334. }
  335. }
  336. }
  337. /**
  338. * @param $openid
  339. * @param $out_trade_no
  340. * @param $total_fee
  341. * @param $attach
  342. * @param $body
  343. * @param string $detail
  344. * @param string $trade_type
  345. * @param array $options
  346. * @return array|string
  347. * @author xaboy
  348. * @day 2020-04-20
  349. */
  350. public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  351. {
  352. $paymentPrepare = $this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  353. return $this->application->payment->configForJSSDKPayment($paymentPrepare->prepay_id);
  354. }
  355. /**
  356. * @param $orderNo
  357. * @param $refundNo
  358. * @param $totalFee
  359. * @param null $refundFee
  360. * @param null $opUserId
  361. * @param string $refundReason
  362. * @param string $type
  363. * @param string $refundAccount
  364. * @return Collection
  365. * @author xaboy
  366. * @day 2020-04-20
  367. */
  368. public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
  369. {
  370. if (empty($this->config['payment']['pay_weixin_client_cert']) || empty($this->config['payment']['pay_weixin_client_key'])) {
  371. throw new \Exception('请配置微信支付证书');
  372. }
  373. $totalFee = floatval($totalFee);
  374. $refundFee = floatval($refundFee);
  375. return $this->application->payment->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
  376. }
  377. /**
  378. * @param $orderNo
  379. * @param array $opt
  380. * @author xaboy
  381. * @day 2020-04-20
  382. */
  383. public function payOrderRefund($orderNo, array $opt)
  384. {
  385. if (!isset($opt['pay_price'])) throw new WechatException('缺少pay_price');
  386. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  387. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  388. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  389. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  390. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  391. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  392. /*仅针对老资金流商户使用
  393. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  394. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  395. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  396. try {
  397. $res = ($this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
  398. if ($res->return_code == 'FAIL') throw new WechatException('退款失败:' . $res->return_msg);
  399. if (isset($res->err_code)) throw new WechatException('退款失败:' . $res->err_code_des);
  400. } catch (Exception $e) {
  401. throw new WechatException($e->getMessage());
  402. }
  403. }
  404. /**
  405. * array (
  406. * 'appid' => '****',
  407. * 'attach' => 'user_recharge',
  408. * 'bank_type' => 'COMM_CREDIT',
  409. * 'cash_fee' => '1',
  410. * 'fee_type' => 'CNY',
  411. * 'is_subscribe' => 'Y',
  412. * 'mch_id' => ''*****'',
  413. * 'nonce_str' => '5ee9dac1bc302',
  414. * 'openid' => '*****',
  415. * 'out_trade_no' => ''*****'',
  416. * 'result_code' => 'SUCCESS',
  417. * 'return_code' => 'SUCCESS',
  418. * 'sign' => '51'*****'',
  419. * 'time_end' => '20200617165651',
  420. * 'total_fee' => '1',
  421. * 'trade_type' => 'JSAPI',
  422. * 'transaction_id' => ''*****'',
  423. * )
  424. *
  425. * @throws FaultException
  426. * @author xaboy
  427. * @day 2020-04-20
  428. */
  429. public function handleNotify()
  430. {
  431. $this->application->payment = new PaymentService($this->application->merchant);
  432. //TODO 微信支付
  433. return $this->application->payment->handleNotify(function ($notify, $successful) {
  434. Log::info('微信支付成功回调' . var_export($notify, 1));
  435. if (!$successful) return false;
  436. try {
  437. event('pay_success_' . $notify['attach'], ['order_sn' => $notify['out_trade_no'], 'data' => $notify]);
  438. } catch (\Exception $e) {
  439. Log::info('微信支付回调失败:' . $e->getMessage());
  440. return false;
  441. }
  442. return true;
  443. });
  444. }
  445. /**
  446. * @param string $url
  447. * @return array|string
  448. * @author xaboy
  449. * @day 2020-04-20
  450. */
  451. public function jsSdk($url)
  452. {
  453. $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'];
  454. $jsService = $this->application->js;
  455. $jsService->setUrl($url);
  456. try {
  457. return $jsService->config($apiList, false, false, false);
  458. } catch (Exception $e) {
  459. Log::info('微信参数获取失败' . $e->getMessage());
  460. return [];
  461. }
  462. }
  463. /**
  464. * 回复文本消息
  465. * @param string $content 文本内容
  466. * @return Text
  467. * @author xaboy
  468. * @day 2020-04-20
  469. */
  470. public static function textMessage($content)
  471. {
  472. return new Text(compact('content'));
  473. }
  474. /**
  475. * 回复图片消息
  476. * @param string $media_id 媒体资源 ID
  477. * @return Image
  478. * @author xaboy
  479. * @day 2020-04-20
  480. */
  481. public static function imageMessage($media_id)
  482. {
  483. return new Image(compact('media_id'));
  484. }
  485. /**
  486. * 回复视频消息
  487. * @param string $media_id 媒体资源 ID
  488. * @param string $title 标题
  489. * @param string $description 描述
  490. * @param null $thumb_media_id 封面资源 ID
  491. * @return Video
  492. * @author xaboy
  493. * @day 2020-04-20
  494. */
  495. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  496. {
  497. return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
  498. }
  499. /**
  500. * 回复声音消息
  501. * @param string $media_id 媒体资源 ID
  502. * @return Voice
  503. * @author xaboy
  504. * @day 2020-04-20
  505. */
  506. public static function voiceMessage($media_id)
  507. {
  508. return new Voice(compact('media_id'));
  509. }
  510. /**
  511. * 回复图文消息
  512. * @param string|array $title 标题
  513. * @param string $description 描述
  514. * @param string $url URL
  515. * @param string $image 图片链接
  516. * @return News|array<News>
  517. * @author xaboy
  518. * @day 2020-04-20
  519. */
  520. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  521. {
  522. if (is_array($title)) {
  523. if (isset($title[0]) && is_array($title[0])) {
  524. $newsList = [];
  525. foreach ($title as $news) {
  526. $newsList[] = self::newsMessage($news);
  527. }
  528. return $newsList;
  529. } else {
  530. $data = $title;
  531. }
  532. } else {
  533. $data = compact('title', 'description', 'url', 'image');
  534. }
  535. return new News($data);
  536. }
  537. /**
  538. * 回复文章消息
  539. * @param string|array $title 标题
  540. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  541. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  542. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  543. * @param string $author 作者
  544. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  545. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  546. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  547. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  548. * @return Article
  549. * @author xaboy
  550. * @day 2020-04-20
  551. */
  552. 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)
  553. {
  554. $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');
  555. return new Article($data);
  556. }
  557. /**
  558. * 回复素材消息
  559. * @param string $type [mpnews、 mpvideo、voice、image]
  560. * @param string $media_id 素材 ID
  561. * @return Material
  562. * @author xaboy
  563. * @day 2020-04-20
  564. */
  565. public static function materialMessage($type, $media_id)
  566. {
  567. return new Material($type, $media_id);
  568. }
  569. /**
  570. * @param $to
  571. * @param $message
  572. * @return bool
  573. * @throws InvalidArgumentException
  574. * @throws RuntimeException
  575. * @author xaboy
  576. * @day 2020-04-20
  577. */
  578. public function staffTo($to, $message)
  579. {
  580. $staff = $this->application->staff;
  581. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  582. $res = $staff->to($to)->send();
  583. return $res;
  584. }
  585. /**
  586. * @param $openid
  587. * @return array
  588. * @author xaboy
  589. * @day 2020-04-20
  590. */
  591. public function getUserInfo($openid)
  592. {
  593. $userService = $this->application->user;
  594. $userInfo = is_array($openid) ? $userService->batchGet($openid) : $userService->get($openid);
  595. return $userInfo->toArray();
  596. }
  597. /**
  598. * 模板消息接口
  599. * @return \EasyWeChat\Notice\Notice
  600. */
  601. public function noticeService()
  602. {
  603. return $this->application->notice;
  604. }
  605. /**
  606. * 微信二维码生成接口
  607. * @return \EasyWeChat\QRCode\QRCode
  608. */
  609. public function qrcodeService()
  610. {
  611. return $this->application->qrcode;
  612. }
  613. }