123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841 |
- <?php
- namespace ln\services;
- use app\common\repositories\store\product\ProductAssistSetRepository;
- use app\common\repositories\store\product\ProductGroupBuyingRepository;
- use app\common\repositories\store\product\ProductGroupRepository;
- use app\common\repositories\store\product\ProductPresellRepository;
- use app\common\repositories\store\product\ProductRepository;
- use app\common\repositories\system\config\ConfigValueRepository;
- use app\common\repositories\system\merchant\MerchantRepository;
- use app\common\repositories\wechat\WechatQrcodeRepository;
- use app\common\repositories\wechat\WechatReplyRepository;
- use app\common\repositories\wechat\WechatUserRepository;
- use ln\exceptions\WechatException;
- use EasyWeChat\Core\Exceptions\FaultException;
- use EasyWeChat\Core\Exceptions\InvalidArgumentException;
- use EasyWeChat\Core\Exceptions\RuntimeException;
- use EasyWeChat\Foundation\Application;
- use EasyWeChat\Message\Article;
- use EasyWeChat\Message\Image;
- use EasyWeChat\Message\Material;
- use EasyWeChat\Message\News;
- use EasyWeChat\Message\Text;
- use EasyWeChat\Message\Video;
- use EasyWeChat\Message\Voice;
- use EasyWeChat\Payment\Order;
- use EasyWeChat\Server\BadRequestException;
- use EasyWeChat\Support\Collection;
- use Exception;
- use Symfony\Component\HttpFoundation\Request;
- use think\exception\ValidateException;
- use think\facade\Cache;
- use think\facade\Log;
- use think\facade\Route;
- use think\Response;
- /**
- * Class WechatService
- * @package ln\services
- * @author xaboy
- * @day 2020-04-20
- */
- class WechatService
- {
- /**
- * @var Application
- */
- protected $application;
- protected $config;
- /**
- * WechatService constructor.
- * @param $config
- */
- public function __construct(array $config)
- {
- $this->config = $config;
- $this->application = new Application($config);
- $this->application->register(new \ln\services\easywechat\certficates\ServiceProvider());
- $this->application->register(new \ln\services\easywechat\merchant\ServiceProvider);
- $this->application->register(new \ln\services\easywechat\combinePay\ServiceProvider);
- }
- /**
- * @return array
- * @author xaboy
- * @day 2020-04-24
- */
- public static function getConfig($isApp)
- {
- /** @var ConfigValueRepository $make */
- $make = app()->make(ConfigValueRepository::class);
- $wechat = $make->more([
- 'wechat_appid', 'wechat_appsecret', 'wechat_token', 'wechat_encodingaeskey', 'wechat_encode', 'wecaht_app_appid', 'wechat_app_appsecret'
- ], 0);
- if ($isApp ?? request()->isApp()) {
- $wechat['wechat_appid'] = trim($wechat['wecaht_app_appid']);
- $wechat['wechat_appsecret'] = trim($wechat['wechat_app_appsecret']);
- }
- $payment = $make->more(['site_url', 'pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open',
- 'wechat_service_merid', 'wechat_service_key', 'wechat_service_v3key', 'wechat_service_client_cert', 'wechat_service_client_key', 'wechat_service_serial_no'], 0);
- $config = [
- 'app_id' => trim($wechat['wechat_appid']),
- 'secret' => trim($wechat['wechat_appsecret']),
- 'token' => trim($wechat['wechat_token']),
- 'guzzle' => [
- 'timeout' => 10.0, // 超时时间(秒)
- 'verify' => false
- ],
- 'debug' => false,
- ];
- if ($wechat['wechat_encode'] > 0 && $wechat['wechat_encodingaeskey'])
- $config['aes_key'] = trim($wechat['wechat_encodingaeskey']);
- if (isset($payment['pay_weixin_open']) && $payment['pay_weixin_open'] == 1) {
- $config['payment'] = [
- 'merchant_id' => trim($payment['pay_weixin_mchid']),
- 'key' => trim($payment['pay_weixin_key']),
- 'cert_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_cert']),
- 'key_path' => (app()->getRootPath() . 'public' . $payment['pay_weixin_client_key']),
- 'notify_url' => $payment['site_url'] . Route::buildUrl('wechatNotify')->build(),
- 'pay_weixin_client_cert' => $payment['pay_weixin_client_cert'],
- 'pay_weixin_client_key' => $payment['pay_weixin_client_key'],
- ];
- }
- $config['service_payment'] = [
- 'merchant_id' => trim($payment['wechat_service_merid']),
- 'type' => 'wechat',
- 'key' => trim($payment['wechat_service_key']),
- 'cert_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_cert']),
- 'key_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_key']),
- 'pay_weixin_client_cert' => $payment['wechat_service_client_cert'],
- 'pay_weixin_client_key' => $payment['wechat_service_client_key'],
- 'serial_no' => trim($payment['wechat_service_serial_no']),
- 'apiv3_key' => trim($payment['wechat_service_v3key']),
- ];
- return $config;
- }
- /**
- * @return self
- * @author xaboy
- * @day 2020-04-24
- */
- public static function create($isApp = null)
- {
- return new self(self::getConfig($isApp));
- }
- /**
- * @return Application
- * @author xaboy
- * @day 2020-04-20
- */
- public function getApplication()
- {
- return $this->application;
- }
- /**
- * @param \think\Request $request
- * @return Response
- * @throws BadRequestException
- * @throws InvalidArgumentException
- * @author xaboy
- * @day 2020-04-26
- */
- public function serve(\think\Request $request)
- {
- $this->serverRequest($request);
- $this->wechatEventHook();
- return response($this->application->server->serve()->getContent());
- }
- protected function serverRequest(\think\Request $request)
- {
- $this->application->server->setRequest(new Request($request->get(), $request->post(), [], [], [], $request->server(), $request->getContent()));
- }
- /**
- * @throws InvalidArgumentException
- * @author xaboy
- * @day 2020-04-20
- */
- public function wechatEventHook()
- {
- $this->application->server->setMessageHandler(function ($message) {
- $openId = $message->FromUserName;
- $message->EventKey = str_replace('qrscene_','',$message->EventKey);
- $userInfo = $this->getUserInfo($openId);
- /** @var WechatUserRepository $wechatUserRepository */
- $wechatUserRepository = app()->make(WechatUserRepository::class);
- $users = $wechatUserRepository->syncUser($openId, $userInfo, true);
- $scanLogin = function () use ($message, $users) {
- $ticket = $message->EventKey;
- if (strpos($ticket, '_sys_scan_login.') === 0) {
- $key = str_replace('_sys_scan_login.', '', $ticket);
- Cache::set('_scan_login' . $key, $users[1]['uid']);
- }
- };
- $response = null;
- /** @var WechatReplyRepository $make * */
- $make = app()->make(WechatReplyRepository::class);
- event('WechatMessage', $message);
- switch ($message->MsgType) {
- case 'event':
- switch (strtolower($message->Event)) {
- case 'subscribe':
- $scanLogin();
- $response = $this->qrKeyByMessage($message->EventKey) ?: $make->reply('subscribe');
- if (isset($message->EventKey) && $message->EventKey) {
- /** @var WechatQrcodeRepository $qr */
- $qr = app()->make(WechatQrcodeRepository::class);
- if ($qrInfo = $qr->ticketByQrcode($message->Ticket)) {
- $qrInfo->incTicket();
- if (strtolower($qrInfo['third_type']) == 'spread' && $users) {
- $spreadUid = $qrInfo['third_id'];
- if ($users[1]['uid'] == $spreadUid)
- return '自己不能推荐自己';
- else if ($users[1]['spread_uid'])
- return '已有推荐人!';
- try {
- $users[1]->setSpread($spreadUid);
- } catch (Exception $e) {
- return '绑定推荐人失败';
- }
- }
- }
- }
- event('WechatEventSubscribe', $message);
- break;
- case 'unsubscribe':
- $wechatUserRepository->unsubscribe($openId);
- event('WechatEventUnsubscribe', $message);
- break;
- case 'scan':
- $scanLogin();
- $response = $this->qrKeyByMessage($message->EventKey) ?: $make->reply('subscribe');
- /** @var WechatQrcodeRepository $qr */
- $qr = app()->make(WechatQrcodeRepository::class);
- if ($message->EventKey && ($qrInfo = $qr->ticketByQrcode($message->Ticket))) {
- $qrInfo->incTicket();
- if (strtolower($qrInfo['third_type']) == 'spread' && $users) {
- $spreadUid = $qrInfo['third_id'];
- if ($users[1]['uid'] == $spreadUid)
- return '自己不能推荐自己';
- else if ($users[1]['spread_uid'])
- return '已有推荐人!';
- try {
- $users[1]->setSpread($spreadUid);
- } catch (Exception $e) {
- return '绑定推荐人失败';
- }
- }
- }
- event('WechatEventScan', $message);
- break;
- case 'location':
- event('wechatEventLocation', $message);
- break;
- case 'click':
- $response = $make->reply($message->EventKey);
- break;
- case 'view':
- event('wechatEventView', $message);
- break;
- }
- break;
- case 'text':
- $response = $make->reply($message->Content);
- event('wechatMessageText', $message);
- break;
- case 'image':
- event('wechatMessageImage', $message);
- break;
- case 'voice':
- event('wechatMessageVoice', $message);
- break;
- case 'video':
- event('wechatMessageVideo', $message);
- break;
- case 'location':
- event('wechatMessageLocation', $message);
- break;
- case 'link':
- event('wechatMessageLink', $message);
- break;
- // ... 其它消息
- default:
- event('WechatMessageOther', $message);
- break;
- }
- return $response ?? false;
- });
- }
- /**
- * @param $value
- * @return Collection
- * @author xaboy
- * @day 2020-04-20
- */
- public function qrcodeForever($value)
- {
- return $this->application->qrcode->forever($value);
- }
- /**
- * @param $value
- * @param int $expireSeconds
- * @return Collection
- * @author xaboy
- * @day 2020-04-20
- */
- public function qrcodeTemp($value, $expireSeconds = 2592000)
- {
- return $this->application->qrcode->temporary($value, $expireSeconds);
- }
- /**
- * @param string $openid
- * @param string $templateId
- * @param array $data
- * @param null $url
- * @param null $defaultColor
- * @return mixed
- * @author xaboy
- * @day 2020-04-20
- */
- public function sendTemplate(string $openid, string $templateId, array $data, $url = null, $defaultColor = null)
- {
- $notice = $this->application->notice->to($openid)->template($templateId)->andData($data);
- if ($url !== null) $notice->url($url);
- if ($defaultColor !== null) $notice->defaultColor($defaultColor);
- return $notice->send();
- }
- /**
- * @param $openid
- * @param $out_trade_no
- * @param $total_fee
- * @param $attach
- * @param $body
- * @param string $detail
- * @param string $trade_type
- * @param array $options
- * @return Order
- * @author xaboy
- * @day 2020-04-20
- */
- protected function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
- {
- $total_fee = bcmul($total_fee, 100, 0);
- $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
- if (!is_null($openid)) $order['openid'] = $openid;
- if ($order['detail'] == '') unset($order['detail']);
- $order['spbill_create_ip'] = \request()->ip();
- return new Order($order);
- }
- /**
- * @param $openid
- * @param $out_trade_no
- * @param $total_fee
- * @param $attach
- * @param $body
- * @param string $detail
- * @param string $trade_type
- * @param array $options
- * @return Collection
- * @author xaboy
- * @day 2020-04-20
- */
- public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
- {
- $order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
- $result = $this->application->payment->prepare($order);
- if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
- return $result;
- } else {
- if ($result->return_code == 'FAIL') {
- throw new WechatException('微信支付错误返回:' . $result->return_msg);
- } else if (isset($result->err_code)) {
- throw new WechatException('微信支付错误返回:' . $result->err_code_des);
- } else {
- throw new WechatException('没有获取微信支付的预支付ID,请重新发起支付!');
- }
- }
- }
- /**
- * @param $openid
- * @param $out_trade_no
- * @param $total_fee
- * @param $attach
- * @param $body
- * @param string $detail
- * @param string $trade_type
- * @param array $options
- * @return array|string
- * @author xaboy
- * @day 2020-04-20
- */
- public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
- {
- $paymentPrepare = $this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
- return $trade_type === 'APP'
- ? $this->application->payment->configForAppPayment($paymentPrepare->prepay_id)
- : $this->application->payment->configForJSSDKPayment($paymentPrepare->prepay_id);
- }
- /**
- * @param $orderNo
- * @param $refundNo
- * @param $totalFee
- * @param null $refundFee
- * @param null $opUserId
- * @param string $refundReason
- * @param string $type
- * @param string $refundAccount
- * @return Collection
- * @author xaboy
- * @day 2020-04-20
- */
- public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
- {
- if (empty($this->config['payment']['pay_weixin_client_cert']) || empty($this->config['payment']['pay_weixin_client_key'])) {
- throw new \Exception('请配置微信支付证书');
- }
- $totalFee = floatval($totalFee);
- $refundFee = floatval($refundFee);
- return $this->application->payment->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
- }
- /**
- * @param $orderNo
- * @param array $opt
- * @author xaboy
- * @day 2020-04-20
- */
- public function payOrderRefund($orderNo, array $opt)
- {
- if (!isset($opt['pay_price'])) throw new WechatException('缺少pay_price');
- $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
- $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
- $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
- $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
- $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
- $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
- /*仅针对老资金流商户使用
- REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
- REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
- $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
- try {
- $res = ($this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
- if ($res->return_code == 'FAIL') throw new WechatException('退款失败:' . $res->return_msg);
- if (isset($res->err_code)) throw new WechatException('退款失败:' . $res->err_code_des);
- } catch (Exception $e) {
- throw new WechatException($e->getMessage());
- }
- }
- /**
- * array (
- * 'appid' => '****',
- * 'attach' => 'user_recharge',
- * 'bank_type' => 'COMM_CREDIT',
- * 'cash_fee' => '1',
- * 'fee_type' => 'CNY',
- * 'is_subscribe' => 'Y',
- * 'mch_id' => ''*****'',
- * 'nonce_str' => '5ee9dac1bc302',
- * 'openid' => '*****',
- * 'out_trade_no' => ''*****'',
- * 'result_code' => 'SUCCESS',
- * 'return_code' => 'SUCCESS',
- * 'sign' => '51'*****'',
- * 'time_end' => '20200617165651',
- * 'total_fee' => '1',
- * 'trade_type' => 'JSAPI',
- * 'transaction_id' => ''*****'',
- * )
- *
- * @throws FaultException
- * @author xaboy
- * @day 2020-04-20
- */
- public function handleNotify()
- {
- $this->application->payment = new PaymentService($this->application->merchant);
- //TODO 微信支付
- return $this->application->payment->handleNotify(function ($notify, $successful) {
- Log::info('微信支付成功回调' . var_export($notify, 1));
- if (!$successful) return false;
- try {
- event('pay_success_' . $notify['attach'], ['order_sn' => $notify['out_trade_no'], 'data' => $notify, 'is_combine' => 0]);
- } catch (\Exception $e) {
- Log::info('微信支付回调失败:' . $e->getMessage());
- return false;
- }
- return true;
- });
- }
- public function handleCombinePayNotify($type)
- {
- $this->application->combinePay->handleNotify(function ($notify, $successful) use ($type) {
- Log::info('微信支付成功回调' . var_export($notify, 1));
- if (!$successful) return false;
- try {
- event('pay_success_' . $type, ['order_sn' => $notify['combine_out_trade_no'], 'data' => $notify, 'is_combine' => 1]);
- } catch (\Exception $e) {
- Log::info('微信支付回调失败:' . $e->getMessage());
- return false;
- }
- return true;
- });
- }
- /**
- * @param string $url
- * @return array|string
- * @author xaboy
- * @day 2020-04-20
- */
- public function jsSdk($url)
- {
- $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'];
- $jsService = $this->application->js;
- $jsService->setUrl($url);
- try {
- return $jsService->config($apiList, false, false, false);
- } catch (Exception $e) {
- Log::info('微信参数获取失败' . $e->getMessage());
- return [];
- }
- }
- /**
- * 回复文本消息
- * @param string $content 文本内容
- * @return Text
- * @author xaboy
- * @day 2020-04-20
- */
- public static function textMessage($content)
- {
- return new Text(compact('content'));
- }
- /**
- * 回复图片消息
- * @param string $media_id 媒体资源 ID
- * @return Image
- * @author xaboy
- * @day 2020-04-20
- */
- public static function imageMessage($media_id)
- {
- return new Image(compact('media_id'));
- }
- /**
- * 回复视频消息
- * @param string $media_id 媒体资源 ID
- * @param string $title 标题
- * @param string $description 描述
- * @param null $thumb_media_id 封面资源 ID
- * @return Video
- * @author xaboy
- * @day 2020-04-20
- */
- public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
- {
- return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
- }
- /**
- * 回复声音消息
- * @param string $media_id 媒体资源 ID
- * @return Voice
- * @author xaboy
- * @day 2020-04-20
- */
- public static function voiceMessage($media_id)
- {
- return new Voice(compact('media_id'));
- }
- /**
- * 回复图文消息
- * @param string|array $title 标题
- * @param string $description 描述
- * @param string $url URL
- * @param string $image 图片链接
- * @return News|array<News>
- * @author xaboy
- * @day 2020-04-20
- */
- public static function newsMessage($title, $description = '...', $url = '', $image = '')
- {
- if (is_array($title)) {
- if (isset($title[0]) && is_array($title[0])) {
- $newsList = [];
- foreach ($title as $news) {
- $newsList[] = self::newsMessage($news);
- }
- return $newsList;
- } else {
- $data = $title;
- }
- } else {
- $data = compact('title', 'description', 'url', 'image');
- }
- return new News($data);
- }
- /**
- * 回复文章消息
- * @param string|array $title 标题
- * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
- * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
- * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
- * @param string $author 作者
- * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
- * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
- * @param int $need_open_comment 是否打开评论,0不打开,1打开
- * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
- * @return Article
- * @author xaboy
- * @day 2020-04-20
- */
- 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)
- {
- $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');
- return new Article($data);
- }
- /**
- * 回复素材消息
- * @param string $type [mpnews、 mpvideo、voice、image]
- * @param string $media_id 素材 ID
- * @return Material
- * @author xaboy
- * @day 2020-04-20
- */
- public static function materialMessage($type, $media_id)
- {
- return new Material($type, $media_id);
- }
- /**
- * @param $to
- * @param $message
- * @return bool
- * @throws InvalidArgumentException
- * @throws RuntimeException
- * @author xaboy
- * @day 2020-04-20
- */
- public function staffTo($to, $message)
- {
- $staff = $this->application->staff;
- $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
- $res = $staff->to($to)->send();
- return $res;
- }
- /**
- * @param $openid
- * @return array
- * @author xaboy
- * @day 2020-04-20
- */
- public function getUserInfo($openid)
- {
- $userService = $this->application->user;
- $userInfo = is_array($openid) ? $userService->batchGet($openid) : $userService->get($openid);
- return $userInfo->toArray();
- }
- /**
- * 模板消息接口
- * @return \EasyWeChat\Notice\Notice
- */
- public function noticeService()
- {
- return $this->application->notice;
- }
- /**
- * 微信二维码生成接口
- * @return \EasyWeChat\QRCode\QRCode
- */
- public function qrcodeService()
- {
- return $this->application->qrcode;
- }
- public function merchantPay($data)
- {
- $ret = [
- 'partner_trade_no' => $data['sn'], //随机字符串作为订单号,跟红包和支付一个概念。
- 'openid' => $data['openid'], //收款人的openid
- 'check_name' => 'NO_CHECK', //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
- //'re_user_name'=>'张三', //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
- 'amount' => $data['price'] * 100, //单位为分
- 'desc' => '企业付款',
- 'spbill_create_ip' => request()->ip(), //发起交易的IP地址
- ];
- $result = $this->application->merchant_pay->send($ret);
- if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
- return $result;
- } else {
- if ($result->return_code == 'FAIL') {
- throw new WechatException('微信支付错误返回:' . $result->return_msg);
- } else if (isset($result->err_code)) {
- throw new WechatException('微信支付错误返回:' . $result->err_code_des);
- } else {
- throw new WechatException('微信支付错误返回:' . $result->return_msg);
- }
- }
- }
- /**
- * TODO 分账商户
- * @return mixed
- * @author Qinii
- * @day 6/24/21
- */
- public function applyments()
- {
- return $this->application->sub_merchant;
- }
- /**
- * TODO 上传图片
- * @param array $filed
- * @return mixed
- * @author Qinii
- * @day 6/21/21
- */
- public function uploadImages(array $filed)
- {
- foreach ($filed as $file) {
- $item = $this->application->sub_merchant->upload($file['path'], $file['name']);
- $data[] = [
- 'dir' => $file['dir'],
- 'media_id' => $item['media_id'],
- ];
- }
- return $data;
- }
- public function qrKeyByMessage($key)
- {
- if (strpos($key, '_scan_url_') === 0) {
- $key = str_replace('_scan_url_', '', $key);
- $data = explode('_', $key);
- $siteUrl = rtrim(systemConfig('site_url'), '/');
- $make = app()->make(ProductRepository::class);
- if ($data[0] === 'home') {
- $share = systemConfig(['share_title', 'share_info', 'share_pic']);
- $share['url'] = $siteUrl . '?spid=' . $data[1];
- } else if ($data[0] === 'mer') {
- $ret = app()->make(MerchantRepository::class)->get($data[1]);
- if (!$ret) return;
- $share = [
- 'share_title' => $ret['mer_name'],
- 'share_info' => $ret['mer_info'],
- 'share_pic' => $ret['mer_avatar'],
- 'url' => $siteUrl . '/pages/store/home/index?id=' . $data[1],
- ];
- } else if ($data[0] === 'p0') {
- $ret = $make->get($data[1]);
- if (!$ret) return;
- $share = [
- 'share_title' => $ret['store_name'],
- 'share_info' => $ret['store_info'],
- 'share_pic' => $ret['image'],
- 'url' => $siteUrl . '/pages/goods_details/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else if ($data[0] === 'p1') {
- $ret = $make->get($data[1]);
- if (!$ret) return;
- $share = [
- 'share_title' => $ret['store_name'],
- 'share_info' => $ret['store_info'],
- 'share_pic' => $ret['image'],
- 'url' => $siteUrl . '/pages/activity/goods_seckill_details/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else if ($data[0] === 'p2') {
- $ret = app()->make(ProductPresellRepository::class)->search(['product_presell_id' => $data[1]])->find();
- $res = $make->get($ret['product_id']);
- if (!$ret) return;
- $share = [
- 'share_title' => $ret['store_name'],
- 'share_info' => $ret['store_info'],
- 'share_pic' => $res['image'],
- 'url' => $siteUrl . '/pages/activity/presell_details/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else if ($data[0] === 'p3') {
- $ret = app()->make(ProductAssistSetRepository::class)->getSearch(['product_assist_set_id' => $data[1]])->find();
- $res = $make->get($ret['product_id']);
- if (!$ret) return;
- $share = [
- 'share_title' => $res['store_name'],
- 'share_info' => $res['store_info'],
- 'share_pic' => $res['image'],
- 'url' => $siteUrl . '/pages/activity/assist_detail/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else if ($data[0] === 'p4') {
- $ret = app()->make(ProductGroupRepository::class)->get($data[1]);
- $res = $make->get($ret['product_id']);
- if (!$ret) return;
- $share = [
- 'share_title' => $res['store_name'],
- 'share_info' => $res['store_info'],
- 'share_pic' => $res['image'],
- 'url' => $siteUrl . '/pages/activity/combination_details/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else if ($data[0] === 'p40') {
- $res = app()->make(ProductGroupBuyingRepository::class)->getSearch(['group_buying_id' => $data[1]])->find();
- $ret = $make->get($res->productGroup['product_id']);
- if (!$ret) return;
- $share = [
- 'share_title' => $ret['store_name'],
- 'share_info' => $ret['store_info'],
- 'share_pic' => $ret['image'],
- 'url' => $siteUrl . '/pages/activity/combination_status/index?id=' . $data[1] . '&spid=' . ($data[2] ?? 0),
- ];
- } else {
- return;
- }
- return self::newsMessage($share['share_title'], $share['share_info'], $share['url'], $share['share_pic']);
- }
- }
- /**
- * @return easywechat\combinePay\Client
- */
- public function combinePay()
- {
- return $this->application->combinePay;
- }
- }
|