WechatService.php 22 KB

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