WechatService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. @file_put_contents("quanju.txt", json_encode($order)."-微信支付传值\r\n", 8);
  272. $result = self::payment(false, $cid)->order->unify(
  273. $order
  274. );
  275. return $result;
  276. }
  277. /**
  278. * 使用商户订单号退款
  279. * @param $orderNo
  280. * @param $opt
  281. */
  282. public static function payOrderRefund($cid, $orderNo, array $opt)
  283. {
  284. if (!isset($opt['pay_price'])) exception('缺少pay_price');
  285. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  286. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  287. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  288. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  289. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  290. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  291. /*仅针对老资金流商户使用
  292. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  293. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  294. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  295. try {
  296. $res = self::payment('false', $cid)->byOutTradeNumber($orderNo, $refundNo, $totalFee, $refundFee, ['refund_desc' => $refundReason]);
  297. if ($res->return_code == 'FAIL') exception('退款失败:' . $res->return_msg);
  298. if (isset($res->err_code)) exception('退款失败:' . $res->err_code_des);
  299. } catch (\Exception $e) {
  300. exception($e->getMessage());
  301. }
  302. return true;
  303. }
  304. /**
  305. * 微信支付成功回调接口
  306. */
  307. public static function handleNotify($cid)
  308. {
  309. $response = self::payment(true, $cid)->handlePaidNotify(function ($notify, $successful) use ($cid) {
  310. if ($successful && isset($notify['out_trade_no'])) {
  311. if (isset($notify['attach']) && $notify['attach']) {
  312. if (($count = strpos($notify['out_trade_no'], '_')) !== false) {
  313. $notify['out_trade_no'] = substr($notify['out_trade_no'], $count + 1);
  314. }
  315. $params = [$cid, $notify['out_trade_no']];
  316. Hook::exec("\\liuniu\\repositories\\PaymentRepositories", "wechat" . ucfirst($notify['attach']), $params);
  317. }
  318. $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'], 'message' => json_encode($notify)];
  319. @file_put_contents("quanju.txt", json_encode($data)."-微信支付成功回调接口\r\n", 8);
  320. $wechatContext = WechatContext::create($data, true);
  321. return true;
  322. }
  323. });
  324. $response->send();
  325. }
  326. /**
  327. * jsSdk
  328. * @return \EasyWeChat\Js\Js
  329. */
  330. public static function jsService($cid = 0)
  331. {
  332. return self::payment(false, $cid)->jssdk;
  333. }
  334. public static function WeixinJSBridge($cid, $prepayId)
  335. {
  336. $json = self::jsService($cid)->bridgeConfig($prepayId);
  337. return $json;
  338. }
  339. public static function jspay($cid, $prepayId)
  340. {
  341. $json = self::jsService($cid)->sdkConfig($prepayId);
  342. return $json;
  343. }
  344. public static function jsSdk($url = '', $cid = 0)
  345. {
  346. $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'];
  347. $jsService = self::jsService($cid);
  348. if ($url) $jsService->setUrl($url);
  349. try {
  350. return $jsService->buildConfig($apiList, false);
  351. } catch (\Exception $e) {
  352. // var_dump($e->getMessage());
  353. return '{}';
  354. }
  355. }
  356. /**
  357. * 回复文本消息
  358. * @param string $content 文本内容
  359. * @return Text
  360. */
  361. public static function textMessage($content)
  362. {
  363. return new Text($content);
  364. }
  365. /**
  366. * 回复图片消息
  367. * @param string $media_id 媒体资源 ID
  368. * @return Image
  369. */
  370. public static function imageMessage($media_id)
  371. {
  372. return new Image('media_id');
  373. }
  374. /**
  375. * 回复视频消息
  376. * @param string $media_id 媒体资源 ID
  377. * @param string $title 标题
  378. * @param string $description 描述
  379. * @param null $thumb_media_id 封面资源 ID
  380. * @return Video
  381. */
  382. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  383. {
  384. return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
  385. }
  386. /**
  387. * 回复声音消息
  388. * @param string $media_id 媒体资源 ID
  389. * @return Voice
  390. */
  391. public static function voiceMessage($media_id)
  392. {
  393. return new Voice(compact('media_id'));
  394. }
  395. /**
  396. * 回复图文消息
  397. * @param string|array $title 标题
  398. * @param string $description 描述
  399. * @param string $url URL
  400. * @param string $image 图片链接
  401. */
  402. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  403. {
  404. if (is_array($title)) {
  405. if (isset($title[0]) && is_array($title[0])) {
  406. $newsList = [];
  407. foreach ($title as $news) {
  408. $newsList[] = self::newsMessage($news);
  409. }
  410. return $newsList;
  411. } else {
  412. $data = $title;
  413. }
  414. } else {
  415. $data = compact('title', 'description', 'url', 'image');
  416. }
  417. return new News($data);
  418. }
  419. /**
  420. * 回复文章消息
  421. * @param string|array $title 标题
  422. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  423. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  424. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  425. * @param string $author 作者
  426. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  427. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  428. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  429. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  430. * @return Article
  431. */
  432. 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)
  433. {
  434. $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');
  435. return new Article($data);
  436. }
  437. /**
  438. * 作为客服消息发送
  439. * @param $to
  440. * @param $message
  441. * @return bool
  442. */
  443. public static function staffTo($to, $message)
  444. {
  445. $staff = self::staffService();
  446. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  447. $res = $staff->to($to)->send();
  448. return $res;
  449. }
  450. /**
  451. * 获得用户信息
  452. * @param array|string $openid
  453. * @return \EasyWeChat\Support\Collection
  454. */
  455. public static function getUserInfo($cid, $openid)
  456. {
  457. $userService = self::userService($cid);
  458. $userInfo = is_array($openid) ? $userService->select($openid) : $userService->get($openid);
  459. return $userInfo;
  460. }
  461. /**
  462. * 生成支付签约订单对象
  463. * @param $openid
  464. * @param $out_trade_no
  465. * @param $total_fee
  466. * @param $attach
  467. * @param $body
  468. * @param string $detail
  469. * @param string $trade_type
  470. * @param array $options
  471. * @return Order
  472. */
  473. public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body,$contract_code, $plan_id,$spbill_create_ip,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
  474. {
  475. $total_fee = bcmul($total_fee, 100, 0);
  476. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid','contract_code','plan_id','spbill_create_ip','contract_display_account'), $options);
  477. if ($order['detail'] == '') unset($order['detail']);
  478. $order['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  479. $result = self::payment(false, $cid)->order->unify(
  480. $order,true
  481. );
  482. return $result;
  483. }
  484. /**
  485. * 签约申请扣款
  486. * @param $openid
  487. * @param $out_trade_no
  488. * @param $total_fee
  489. * @param $attach
  490. * @param $body
  491. * @param string $detail
  492. * @param string $trade_type
  493. * @param array $options
  494. * @return Order
  495. */
  496. public static function papPayApply($out_trade_no, $total_fee, $attach, $detail = '', $trade_type = 'PAP', $options = [], $cid = 0,$mch_id='',$contract_id='')
  497. {
  498. $total_fee = bcmul($total_fee, 100, 0);
  499. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'detail', 'trade_type','mch_id','contract_id'), $options);
  500. if ($order['detail'] == '') unset($order['detail']);
  501. $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  502. $result = self::payment(false, $cid)->contract->apply(
  503. $order
  504. );
  505. return $result;
  506. }
  507. }