微信使用整理2

<?php
namespace App\Http\Controllers;


引入所需的类 资源



class WechatController extends Controller
{

    public $appId;
    public $secret;
    public $token;
    public $encodingAESKey;

    public function getAppID()
    {
        return $this->appId=config('wxconfig.appId');
    }
    public function getSecret()
    {
        return $this->secret=config('wxconfig.secret');
    }
     public function getToken()
    {
        return $this->token=config('wxconfig.TOKEN');
    }
     public function getEncodingAESKey()
    {
        return $this->encodingAESKey=config('wxconfig.encodeingAESKey');
    }

    /*
     * 微信入口
     */
    public function wechatserver()
    {

        $server = new Server($this->getAppID(), $this->getToken(), $this->getEncodingAESKey());

        // 只监听指定类型事件
        $server->on('event', 'subscribe', function ($event) {

            $eventKey = $event['EventKey'];
            $ticket = $event['Ticket'];
            $openid = $event['FromUserName'];

        });

        // 已经关注公众号  扫码
        $server->on('event', 'SCAN', function ($event) {


            $meeting_id = $event['EventKey'];
            $ticket = $event['Ticket'];
            $openid = $event['FromUserName'];

           
        });

        // 只监听指定类型事件
        $server->on('event', 'unsubscribe', function ($event) {

         
        });

        // 监听所有类型
        $server->on('message', 'text', function ($message) {

           return Message::make('text')->content(config('msgconfig.AUTO_PUSH_MSG'));
        });
        // // 监听所有类型
        // $server->on('message', function ($message) {
       
        // });

        // 监听指定类型
        $server->on('message', 'image', function ($message) {
            // $message = file_get_contents('php://input');
            // MailController::imageMessage($message);

            return Message::make('text')->content(config('msgconfig.AUTO_PUSH_MSG'));

        });

        $result = $server->serve();

        echo $result;
    }

    public function pushTempMessage($meeting_id, $openid)
    {
       //自己的逻辑
        return $this->pushNoMeeting();
    }

    public function pushWecolme()
    {
    	return Message::make('text')->content(config('msgconfig.PUSH_WELCOME_MSG'));
        
    }
    public function pushNoMeeting()
    {
    	return Message::make('text')->content(config('msgconfig.PUSH_No_Meeting'));
    }

    public function addWechatUser($openid)
    {
        // Log::info('START -- FUNCTION----addWechatUser');
        $userService = new Userwechat($this->getAppID(), $this->getSecret());
        $userwedhat = $userService->get($openid);
        if ($userwedhat) {
            $headimgurl = $this->saveMedia($userwedhat['headimgurl']);
            //自己的逻辑
            //比如更改用户状态等信息
        }

        return 1;
    }

    public function saveMedia($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);

        curl_close($ch);
        $media = array_merge(array('mediaBody' => $package), $httpinfo);

        preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];
        $filename = time() . rand(100, 999) . ".{$fileExt}";
        $dirname = "images/photo/";
        if (!file_exists($dirname)) {
            mkdir($dirname, 0777, true);
        }
        file_put_contents($dirname . $filename, $media['mediaBody']);
        return $dirname . $filename;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值