Official.php 5.4 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * Trait类 微信公众号
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Factories\Wechat
 * @package   App\Factories\Wechat
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年5月6日10:44:56
 * @copyright 2019-2020 Richer (http://www.umu888.com)
 * @license   http://www.xxxxxx.com License
 * @link      http://www.xxxxxx.com
 */
namespace App\Factories\Wechat;

use App\Models\User\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

/**
 * Trait Official
 *
 * @category  App\Factories\Wechat
 * @package   App\Factories\Wechat
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年5月6日10:44:56
 * @copyright 2019-2020 Richer (http://www.umu888.com)
 * @license   http://www.xxxxxx.com License
 * @link      http://www.xxxxxx.com
 */
trait Official
{

    /**
     * 通过code获取用户session信息
     *
     * @return mixed
     */
    public function loginOfficialAccount()
    {
        return $this->official->oauth->user();
    }

    /**
     * @param null $config
     * @return $this
     */
    public function setOfficialAccountConfigure($config = null)
    {
        if (!$config) {
            $config = $this->getOfficialAccountConfigure();
        }

        $this->official = Factory::officialAccount($config);

        return $this;
    }

    /**
     * @return array|bool
     */
    public function getOfficialAccountConfigure()
    {
        return config('wechat.official_account.default');
    }

    /**
     * 消息发送
     *
     * 根据消息类型分别处理
     *
     * @return mixed
     */
    public function push()
    {
        //
        $this->official->server->push(function ($message) {
            $method = Str::camel('handle_'. $message['MsgType']);
            if (method_exists($this, $method)) {
                return call_user_func_array([$this, $method], [$message]);
            }
        });

        return $this->official->server->serve();
    }

    /**
     * 事件类消息处理
     *
     * @param $message
     * @return mixed
     */
    protected function handleEvent($message)
    {
        Log::info('---------------------微信返回的消息---------------------');
        Log::info($message);

        //按事件类型处理
        $event = Str::camel('event_'. $message['Event']);

        if (method_exists($this, $event)) {
            return call_user_func_array([$this, $event], [$message]);
        }
    }

    /**
     * 事件类消息处理
     *
     * 关注公众号
     *
     * @param $message
     * @return string
     */
    protected function eventSubscribe($message)
    {
        // 设置了场景
        if (Arr::get($message, 'EventKey')) {
            $message['EventKey'] = str_replace('qrscene_', '', Arr::get($message, 'EventKey'));
            $user_id = str_replace('INVITE', '', $message['EventKey']);
            $user = User::findOrFail($user_id);
            if ($user) {
                $register_url = url('register') . '?inviter_id=' . $user_id;
                return '欢迎关注"游米游"公众号,定制师【' . $user->nickname . '】邀请您成为游米游平台定制师,忙里偷闲赚取佣金,请前往注册:' . $register_url;
            }
        }

        return '欢迎关注"游米游"公众号';
    }

    /**
     * 事件类消息处理
     *
     * 扫描带参数二维码
     *
     * @param $message
     * @return string
     */
    protected function eventSCAN($message)
    {
        if (Arr::get($message, 'EventKey')) {
            $message['EventKey'] = str_replace('qrscene_', '', Arr::get($message, 'EventKey'));
            $user_id = str_replace('INVITE', '', $message['EventKey']);
            $user = User::findOrFail($user_id);
            if ($user) {
                $register_url = url('register') . '?inviter_id=' . $user_id;
                return '欢迎来到"游米游"公众号,定制师【' . $user->nickname . '】邀请您成为游米游平台定制师,忙里偷闲赚取佣金,请前往注册:' . $register_url;
            }
        }

        return '欢迎来到"游米游"公众号';
    }

    /**
     * 获取菜单
     *
     * @return void
     */
    public function menu()
    {
        $list = $this->official->menu->list();

        return $list;
    }

    public function pushTempMessage($templateId, $data)
    {
//        $userInfo   = $request->userInfo;
//        $first      = '恭喜,您有新的粉丝挷定成功';
//        $remark     = '请您及时查看!';
//
//        $result = $this->official->template_message->send([
//           'touser'        => $openid,
//           'template_id'   => '5OfQTYHbeXsqT2f8nkGeagyocjtBysNrGDaiZ_lStdw',
//           'url'           => config('app.url'),
//           'miniprogram'   => [
//               'appid' => config('wechat.mini_program.default.app_id'),
//               'path'  => 'pages/details/standardDetail/standardDetail?id='.$id,
//           ],
//           'data' => [
//               'first'     => $first,
//               'keyword1'  => $userInfo['nickName'],
//               'keyword2'  =>  '无',
//               'remark'    => $remark,
//           ],
//       ]);
    }
}