PHP+fastadmin实现公众号订阅号随手记功能

第一步、订阅号随手记笔记功能演示效果 

PHP+fastadmin实现公众号订阅号随手记功能_第1张图片PHP+fastadmin实现公众号订阅号随手记功能_第2张图片

 PHP+fastadmin实现公众号订阅号随手记功能_第3张图片PHP+fastadmin实现公众号订阅号随手记功能_第4张图片

 PHP+fastadmin实现公众号订阅号随手记功能_第5张图片

 第二步安装fastadmin框架和创建随手记表

1、安装fastadmin框架

PHP之fastadmin框架安装运行自动生成后台操作

 2、创建数据表fa_note

CREATE TABLE `fa_note` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ToUserName` varchar(50) DEFAULT NULL COMMENT '微信用户标识',
  `FromUserName` varchar(50) DEFAULT NULL COMMENT '用户id',
  `title` varchar(20) DEFAULT NULL COMMENT '标题',
  `Content` text COMMENT '内容',
  `CreateTime` varchar(30) DEFAULT NULL COMMENT '添加时间',
  `UpdateTime` varchar(30) DEFAULT NULL COMMENT '修改时间',
  `MsgType` varchar(30) DEFAULT NULL COMMENT '文本类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 COMMENT='笔记列表';

第三步、配置公众号和订阅号配置

fastadmin公众号服务器配置,安装微信管理点击参考下面

PHP之fastadmin公众号服务器配置 

PHP+fastadmin实现公众号订阅号随手记功能_第6张图片

安装好后打开权限显示微信管理

PHP+fastadmin实现公众号订阅号随手记功能_第7张图片

 第四步、公众号配置自动回复,修改index.php文件

PHP+fastadmin实现公众号订阅号随手记功能_第8张图片

 1、接受回复记录和处理返回 增加消息处理方法

PHP+fastadmin实现公众号订阅号随手记功能_第9张图片

   if ($message['MsgType'] == 'text'||$message['MsgType'] == 'image') {
                   $return= $this->note($message) ;
                   if($return){
                       return  $return;
                   }
                }

 message示例:

{"ToUserName":"gh_02c4fcb1e983","FromUserName":"o-iwh0oy9SJZ66Azl6F8eRMQlHec","CreateTime":"1690609936","MsgType":"text","Content":"第一个笔记","MsgId":"24203713717141674"}

 2、完整代码如下

app = Factory::officialAccount(Config::load());
    }

    /**
     *
     */
    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }

    /**
     * 微信API对接接口
     */
    public function api()
    {

        $this->app->server->push(function ($message) {
            $wechatService = new WechatService;

            $matches = null;
            $openid = $message['FromUserName'];
            $to_openid = $message['ToUserName'];

            $unknownMessage = WechatConfig::getValue('default.unknown.message');
            $unknownMessage = $unknownMessage ? $unknownMessage : "";
            switch ($message['MsgType']) {
                case 'event': //事件消息
                    $event = $message['Event'];
                    $eventkey = $message['EventKey'] ? $message['EventKey'] : $message['Event'];
                    //验证码消息
                    if (in_array($event, ['subscribe', 'SCAN']) && preg_match("/^captcha_([a-zA-Z0-9]+)_([0-9\.]+)/", $eventkey, $matches)) {
                        return WechatCaptcha::send($openid, $matches[1], $matches[2]);
                    }
                    switch ($event) {
                        case 'subscribe'://添加关注
                            $subscribeMessage = WechatConfig::getValue('default.subscribe.message');
                            $subscribeMessage = $subscribeMessage ?: "欢迎关注我们!";
                            return $subscribeMessage;
                        case 'unsubscribe'://取消关注
                            return '';
                        case 'LOCATION'://获取地理位置
                            return '';
                        case 'VIEW': //跳转链接,eventkey为链接
                            return '';
                        case 'SCAN': //扫码
                            return '';
                        default:
                            break;
                    }

                    $wechatResponse = WechatResponse::where(["eventkey" => $eventkey, 'status' => 'normal'])->find();
                    if ($wechatResponse) {
                        $responseContent = (array)json_decode($wechatResponse['content'], true);
                        $wechatContext = WechatContext::where(['openid' => $openid])->order('id', 'desc')->find();
                        $data = ['eventkey' => $eventkey, 'command' => '', 'refreshtime' => time(), 'openid' => $openid];
                        if ($wechatContext) {
                            $wechatContext->save($data);
                        } else {
                            $wechatContext = WechatContext::create($data, true);
                        }
                        $result = $wechatService->response($this, $openid, '', $responseContent, $wechatContext);
                        if ($result) {
                            return $result;
                        }
                    }
                    return $unknownMessage;
                case 'text': //文字消息
                case 'image': //图片消息
                case 'voice': //语音消息
                case 'video': //视频消息
                case 'location': //坐标消息
                case 'link': //链接消息
                default: //其它消息

                    if ($message['MsgType'] == 'text'||$message['MsgType'] == 'image') {
                        $return= $this->note($message) ;
                        if($return){
                            return  $return;
                        }
                    }

                    //自动回复处理
                    if ($message['MsgType'] == 'text') {
                        $autoreply = null;
                        $autoreplyList = WechatAutoreply::where('status', 'normal')->cache(true)->order('weigh DESC,id DESC')->select();
                        foreach ($autoreplyList as $index => $item) {
                            //完全匹配和正则匹配
                            if ($item['text'] == $message['Content'] || (in_array(mb_substr($item['text'], 0, 1), ['#', '~', '/']) && preg_match($item['text'], $message['Content'], $matches))) {
                                $autoreply = $item;
                                break;
                            }
                        }

                        if ($autoreply) {
                            $wechatResponse = WechatResponse::where(["eventkey" => $autoreply['eventkey'], 'status' => 'normal'])->find();
                            if ($wechatResponse) {
                                $responseContent = (array)json_decode($wechatResponse['content'], true);
                                $wechatContext = WechatContext::where(['openid' => $openid])->order('id', 'desc')->find();
                                $result = $wechatService->response($this, $openid, $message['Content'], $responseContent, $wechatContext, $matches);
                                if ($result) {
                                    return $result;
                                }
                            }
                        }
                    }
                    return $unknownMessage;
            }
            return ""; //SUCCESS
        });

        $response = $this->app->server->serve();
        // 将响应输出
        $response->send();
        return;
    }
    //小记功能
    private  function note($message){
        if ($message['MsgType'] == 'text') {
            $title="";
            $message['Content']=trim($message['Content']);
            if($message['Content']){

                if(in_array($message['Content'][0],array("!","@","#"))){
                    $firstpex=$message['Content'][0];
                    preg_match('/'.$firstpex.'(.*?)'.$firstpex.'/', $message['Content'], $matches);

                    if (isset($matches[1])) {
                        $title = $matches[1];
                        preg_match('/'.$firstpex.'.*?'.$firstpex.'(.*$)/', $message['Content'], $Content);

                        if (isset($Content[1])) {
                            $message['Content'] = $Content[1];
                        } else {
                            // 第二个#或者?或者!后面没有内容
                            $message['Content'] = mb_substr($message['Content'], 0, 9);
                        }
                    }

                }else{
                    $title=mb_substr($message['Content'], 0, 9);
                }
            }
        }
        if($message['MsgType'] == 'image'){
            $title="图".date("YmdHis");
            $message['Content']= $message['PicUrl'];
        }
        $data=[
            "title"=>$title,
            "ToUserName"=>$message['ToUserName'],
            "FromUserName"=>$message['FromUserName'],
            "CreateTime"=>$message['CreateTime'],
            "UpdateTime"=>$message['CreateTime'],
            "MsgType"=>$message['MsgType'],
            "Content"=>$message['Content'],
        ];
        $id= Db::table('fa_note')->insertGetId($data);
        if($id){
            return '已存笔记,点查看 链接';
        }
        return false;
    }
    /**
     * 登录回调
     */
    public function callback(){
    }

    /**
     * 支付回调
     */
    public function notify()
    {
        Log::record(file_get_contents('php://input'), "notify");
        $response = $this->app->handlePaidNotify(function ($message, $fail) {
            // 你的逻辑
            return true;
            // 或者错误消息
            $fail('Order not exists.');
        });

        $response->send();
        return;
    }

}

第五步创建随手详情页面和列表页面

1、创建控制器Wechat.php

PHP+fastadmin实现公众号订阅号随手记功能_第10张图片

request->get("user");
         $id = $this->request->get("id");
        if(!$id||!$user) {
            die();
        }
        $note= Db::table("fa_note")->where('ToUserName',$user)->where('id',$id)->find();
        if(!$note){
              die();
        }
       $this->view->assign("row", $note);
        return $this->view->fetch();
    }
     public function lst()
    {
   
         $user = $this->request->get("user");
        if(!$user) {
            die();
        }
        $note= Db::table("fa_note")->where('ToUserName',$user)->order("id desc")->select();
        if(!$note){
              die();
        }
        foreach ($note as &$value) {
            // code...
            $value['time']=date("Y-m-d H:i:s",$value['CreateTime']);
        }
         $this->view->assign("user", $user);
        $this->view->assign("list", json_encode($note));
        return $this->view->fetch();
    }

}

2、创建随手记详情和列表html

PHP+fastadmin实现公众号订阅号随手记功能_第11张图片

 详情index.html



    
    
    笔记
    


列表

{$row.title}

添加于{$row.CreateTime|date="Y-m-d H:i:s",###}
{if $row.MsgType=='text'}{$row.Content}{/if} {if $row.MsgType=='image'}{/if}
列表lst.html



    
    我的笔记
    
    


我的笔记

    你可能感兴趣的:(php,fastadmin,公众号,php,开发语言,html5,微信公众平台)