WechatService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace liuniu;
  3. use app\admin\model\Company;
  4. use app\common\model\WechatContext;
  5. use EasyWeChat\Factory;
  6. use EasyWeChat\Kernel\Messages\Article;
  7. use EasyWeChat\Kernel\Messages\Image;
  8. use EasyWeChat\Kernel\Messages\News;
  9. use EasyWeChat\Kernel\Messages\Text;
  10. use EasyWeChat\Kernel\Messages\Video;
  11. use think\Hook;
  12. use think\Request;
  13. use think\Response;
  14. class WechatService
  15. {
  16. private static $instance = null;
  17. private static $app = null;
  18. public static function options($cid)
  19. {
  20. $info = Company::where('id',$cid)->find();
  21. $config = [
  22. 'app_id' => isset($info['wechat_appid']) ? trim($info['wechat_appid']) : '',
  23. 'secret' => isset($info['wechat_appsecret']) ? trim($info['wechat_appsecret']) : '',
  24. 'token' => isset($info['wechat_token']) ? trim($info['wechat_token']) : '',
  25. 'guzzle' => [
  26. 'timeout' => 10.0, // 超时时间(秒)
  27. 'verify' => false
  28. ],
  29. ];
  30. if (isset($info['wechat_encode']) && (int)$info['wechat_encode'] > 0 && isset($info['wechat_encodingaeskey']) && !empty($info['wechat_encodingaeskey']))
  31. $config['aes_key'] = $info['wechat_encodingaeskey'];
  32. if (isset($info['pay_weixin_open']) && $info['pay_weixin_open'] == 1) {
  33. $config1 = [
  34. 'mch_id' => trim($info['pay_weixin_mchid']),
  35. 'key' => trim($info['pay_weixin_key']),
  36. 'cert_path' => realpath('.' . $info['pay_weixin_client_certfile']),
  37. 'key_path' => realpath('.' . $info['pay_weixin_client_keyfile']),
  38. 'notify_url' => Request::instance()->domain(). "/api/wechat/notify/".$cid
  39. ];
  40. $config = array_merge($config,$config1);
  41. }
  42. return $config;
  43. }
  44. public static function application($cache = false,$cid=0)
  45. {
  46. (self::$instance[$cid] === null || $cache === true) && (self::$instance[$cid] = Factory::officialAccount(self::options($cid)));
  47. return self::$instance[$cid];
  48. }
  49. /**
  50. * 支付接口
  51. * @param false $cache
  52. * @param int $cid
  53. * @return \EasyWeChat\Payment\Application|mixed
  54. */
  55. public static function payment($cache = false,$cid=0)
  56. {
  57. (self::$app[$cid] === null || $cache === true) && (self::$app[$cid] = Factory::payment(self::options($cid)));
  58. return self::$app[$cid];
  59. }
  60. public static function serve($cid=0): Response
  61. {
  62. $wechat = self::application(true,$cid);
  63. $server = $wechat->server;
  64. self::hook($server);
  65. $response = $server->serve();
  66. return Response($response->getContent());
  67. }
  68. /**
  69. * 监听行为
  70. * @param Guard $server
  71. */
  72. private static function hook($server)
  73. {
  74. $server->push(function ($message) {
  75. switch ($message['MsgType']) {
  76. case 'event':
  77. switch (strtolower($message['Event'])) {
  78. case 'subscribe':
  79. $response = WechatReply::reply('subscribe');
  80. if (isset($message->EventKey)) {
  81. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  82. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  83. if (strtolower($qrInfo['third_type']) == 'spread') {
  84. }
  85. }
  86. }
  87. break;
  88. case 'unsubscribe':
  89. event('WechatEventUnsubscribeBefore', [$message]);
  90. break;
  91. case 'scan':
  92. $response = WechatReply::reply('subscribe');
  93. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  94. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  95. if (strtolower($qrInfo['third_type']) == 'spread') {
  96. }
  97. }
  98. break;
  99. case 'location':
  100. $response = MessageRepositories::wechatEventLocation($message);
  101. break;
  102. case 'click':
  103. $response = WechatReply::reply($message->EventKey);
  104. break;
  105. case 'view':
  106. $response = MessageRepositories::wechatEventView($message);
  107. break;
  108. }
  109. break;
  110. case 'text':
  111. @file_put_contents("tt.txt",'1');
  112. $response = self::textMessage('绑定推荐人失败');
  113. @file_put_contents("tt.txt",'2',8);
  114. @file_put_contents("tt.txt",json_encode($response),8);
  115. break;
  116. $response = WechatReply::reply($message->Content);
  117. break;
  118. case 'image':
  119. $response = MessageRepositories::wechatMessageImage($message);
  120. break;
  121. case 'voice':
  122. $response = MessageRepositories::wechatMessageVoice($message);
  123. break;
  124. case 'video':
  125. $response = MessageRepositories::wechatMessageVideo($message);
  126. break;
  127. case 'location':
  128. $response = MessageRepositories::wechatMessageLocation($message);
  129. break;
  130. case 'link':
  131. $response = MessageRepositories::wechatMessageLink($message);
  132. break;
  133. // ... 其它消息
  134. default:
  135. $response = MessageRepositories::wechatMessageOther($message);
  136. break;
  137. }
  138. return $response ?? false;
  139. });
  140. }
  141. /**
  142. * 多客服消息转发
  143. * @param string $account
  144. * @return \EasyWeChat\Message\Transfer
  145. */
  146. public static function transfer($account = '')
  147. {
  148. $transfer = new \EasyWeChat\Message\Transfer();
  149. return empty($account) ? $transfer : $transfer->to($account);
  150. }
  151. /**
  152. * 上传永久素材接口
  153. * @return \EasyWeChat\Material\Material
  154. */
  155. public static function materialService($cid=0)
  156. {
  157. return self::application(false,$cid)->material;
  158. }
  159. /**
  160. * 上传临时素材接口
  161. * @return \EasyWeChat\Material\Temporary
  162. */
  163. public static function materialTemporaryService($cid=0)
  164. {
  165. return self::application(false,$cid)->media;
  166. }
  167. /**
  168. * 用户接口
  169. * @return \EasyWeChat\User\User
  170. */
  171. public static function userService($cid=0)
  172. {
  173. return self::application(false,$cid)->user;
  174. }
  175. /**
  176. * 客服消息接口
  177. * @param null $to
  178. * @param null $message
  179. */
  180. public static function staffService($cid=0)
  181. {
  182. return self::application(false,$cid)->staff;
  183. }
  184. /**
  185. * 微信公众号菜单接口
  186. * @return \EasyWeChat\Menu\Menu
  187. */
  188. public static function menuService($cid=0)
  189. {
  190. return self::application(false,$cid)->menu;
  191. }
  192. /**
  193. * 微信二维码生成接口
  194. * @return \EasyWeChat\QRCode\QRCode
  195. */
  196. public static function qrcodeService($cid=0)
  197. {
  198. return self::application(false,$cid)->qrcode;
  199. }
  200. /**
  201. * 微信永久二维码生成接口 小于10万个
  202. * @return \EasyWeChat\QRCode\QRCode
  203. */
  204. public static function qrcodeForeverService($sceneValue,$cid=0)
  205. {
  206. return self::application(false,$cid)->qrcode->forever($sceneValue);
  207. }
  208. /**
  209. * 微信临时二维码生成接口 30天有效期
  210. * @return \EasyWeChat\QRCode\QRCode
  211. */
  212. public static function qrcodeTempService($sceneValue, $expireSeconds = 2592000,$cid=0)
  213. {
  214. return self::application(false,$cid)->qrcode->temporary($sceneValue, $expireSeconds);
  215. }
  216. /**
  217. * 短链接生成接口
  218. * @return \EasyWeChat\Url\Url
  219. */
  220. public static function urlService($cid=0)
  221. {
  222. return self::application(false,$cid)->url;
  223. }
  224. /**
  225. * 用户授权
  226. * @return \Overtrue\Socialite\Providers\WeChatProvider
  227. */
  228. public static function oauthService($cid=0)
  229. {
  230. return self::application(false,$cid)->oauth;
  231. }
  232. /**
  233. * 模板消息接口
  234. * @return \EasyWeChat\Notice\Notice
  235. */
  236. public static function noticeService($cid=0)
  237. {
  238. return self::application(false,$cid)->template_message;
  239. }
  240. public static function sendTemplate($openid, $templateId, array $data, $url = null,$cid=0)
  241. {
  242. $notice = self::noticeService($cid);
  243. return $notice->send([
  244. 'touser' => $openid,
  245. 'template_id' => $templateId,
  246. 'url' => $url,
  247. 'data' => $data,
  248. ]);
  249. }
  250. public static function userTagService($cid=0)
  251. {
  252. return self::application(false,$cid)->user_tag;
  253. }
  254. /**
  255. * 生成支付订单对象
  256. * @param $openid
  257. * @param $out_trade_no
  258. * @param $total_fee
  259. * @param $attach
  260. * @param $body
  261. * @param string $detail
  262. * @param string $trade_type
  263. * @param array $options
  264. * @return Order
  265. */
  266. public static function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [],$cid=0)
  267. {
  268. $total_fee = bcmul($total_fee, 100, 0);
  269. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type','openid'), $options);
  270. if ($order['detail'] == '') unset($order['detail']);
  271. $result = self::payment(false,$cid)->order->unify(
  272. $order
  273. );
  274. return $result;
  275. }
  276. /**
  277. * 使用商户订单号退款
  278. * @param $orderNo
  279. * @param $opt
  280. */
  281. public static function payOrderRefund($cid,$orderNo, array $opt)
  282. {
  283. if (!isset($opt['pay_price'])) exception('缺少pay_price');
  284. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  285. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  286. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  287. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  288. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  289. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  290. /*仅针对老资金流商户使用
  291. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  292. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  293. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  294. try {
  295. $res = self::payment('false',$cid)->byOutTradeNumber($orderNo,$refundNo,$totalFee,$refundFee,['refund_desc'=>$refundReason]);
  296. if ($res->return_code == 'FAIL') exception('退款失败:' . $res->return_msg);
  297. if (isset($res->err_code)) exception('退款失败:' . $res->err_code_des);
  298. } catch (\Exception $e) {
  299. exception($e->getMessage());
  300. }
  301. return true;
  302. }
  303. /**
  304. * 微信支付成功回调接口
  305. */
  306. public static function handleNotify($cid)
  307. {
  308. $response = self::payment(true,$cid)->handlePaidNotify(function ($notify, $successful) use($cid){
  309. if ($successful && isset($notify['out_trade_no'])) {
  310. if (isset($notify['attach']) && $notify['attach']) {
  311. if (($count = strpos($notify['out_trade_no'], '_')) !== false) {
  312. $notify['out_trade_no'] = substr($notify['out_trade_no'], $count + 1);
  313. }
  314. $params = [$cid,$notify['out_trade_no']];
  315. Hook::exec("\\liuniu\\repositories\\PaymentRepositories","wechat".ucfirst($notify['attach']),$params);
  316. }
  317. $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'],'message'=>json_encode($notify)];
  318. $wechatContext = WechatContext::create($data, true);
  319. return true;
  320. }
  321. });
  322. $response->send();
  323. }
  324. /**
  325. * jsSdk
  326. * @return \EasyWeChat\Js\Js
  327. */
  328. public static function jsService($cid=0)
  329. {
  330. return self::payment(false,$cid)->jssdk;
  331. }
  332. public static function WeixinJSBridge($cid,$prepayId)
  333. {
  334. $json = self::jsService($cid)->bridgeConfig($prepayId);
  335. return $json;
  336. }
  337. public static function jspay($cid,$prepayId)
  338. {
  339. $json = self::jsService($cid)->sdkConfig($prepayId);
  340. return $json;
  341. }
  342. public static function jsSdk($url = '',$cid=0)
  343. {
  344. $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'];
  345. $jsService = self::jsService($cid);
  346. if ($url) $jsService->setUrl($url);
  347. try {
  348. return $jsService->buildConfig($apiList,false);
  349. } catch (\Exception $e) {
  350. return '{}';
  351. }
  352. }
  353. /**
  354. * 回复文本消息
  355. * @param string $content 文本内容
  356. * @return Text
  357. */
  358. public static function textMessage($content)
  359. {
  360. return new Text($content);
  361. }
  362. /**
  363. * 回复图片消息
  364. * @param string $media_id 媒体资源 ID
  365. * @return Image
  366. */
  367. public static function imageMessage($media_id)
  368. {
  369. return new Image('media_id');
  370. }
  371. /**
  372. * 回复视频消息
  373. * @param string $media_id 媒体资源 ID
  374. * @param string $title 标题
  375. * @param string $description 描述
  376. * @param null $thumb_media_id 封面资源 ID
  377. * @return Video
  378. */
  379. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  380. {
  381. return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
  382. }
  383. /**
  384. * 回复声音消息
  385. * @param string $media_id 媒体资源 ID
  386. * @return Voice
  387. */
  388. public static function voiceMessage($media_id)
  389. {
  390. return new Voice(compact('media_id'));
  391. }
  392. /**
  393. * 回复图文消息
  394. * @param string|array $title 标题
  395. * @param string $description 描述
  396. * @param string $url URL
  397. * @param string $image 图片链接
  398. */
  399. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  400. {
  401. if (is_array($title)) {
  402. if (isset($title[0]) && is_array($title[0])) {
  403. $newsList = [];
  404. foreach ($title as $news) {
  405. $newsList[] = self::newsMessage($news);
  406. }
  407. return $newsList;
  408. } else {
  409. $data = $title;
  410. }
  411. } else {
  412. $data = compact('title', 'description', 'url', 'image');
  413. }
  414. return new News($data);
  415. }
  416. /**
  417. * 回复文章消息
  418. * @param string|array $title 标题
  419. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  420. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  421. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  422. * @param string $author 作者
  423. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  424. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  425. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  426. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  427. * @return Article
  428. */
  429. 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)
  430. {
  431. $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');
  432. return new Article($data);
  433. }
  434. /**
  435. * 作为客服消息发送
  436. * @param $to
  437. * @param $message
  438. * @return bool
  439. */
  440. public static function staffTo($to, $message)
  441. {
  442. $staff = self::staffService();
  443. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  444. $res = $staff->to($to)->send();
  445. return $res;
  446. }
  447. /**
  448. * 获得用户信息
  449. * @param array|string $openid
  450. * @return \EasyWeChat\Support\Collection
  451. */
  452. public static function getUserInfo($cid,$openid)
  453. {
  454. $userService = self::userService($cid);
  455. $userInfo = is_array($openid) ? $userService->select($openid) : $userService->get($openid);
  456. return $userInfo;
  457. }
  458. }