微信树洞(php+EasyWechat)--记录你的心里话

2017-06-18  本文已影响0人  csuhan

突然想记录一些简短的文字,可以是心里话,也可以是一些感慨,而且要方便使用,大概就是微信了吧

通过微信的消息输入窗口,输入简短的话,就像丢进了树洞一样。
开始!

实现思路

1 技术架构:原生php+EasyWechat框架+mysql。
2 微信公众号:中南小箱子。
3 测试:ngrok本地调试工具。

小插曲,如实保证mysql存入中文不乱码?

require装载配置文件

//config.php
return [arg1,arg2...];
//index.php
$config = require('config.php');
//不可使用require 'config.php';
//前者是函数,后者是结构
var_dump($config);

ngrok

强势安利一下ngrok

ngrok

也就是说,ngrok就是实现内网穿透的,因此用在本地调试微信开发再合适不过了。

架构

小程序,因此就怎样简单怎样来。


程序架构

贴上代码

1. index.php

<?php
/**
 * Created by PhpStorm.
 * User: han
 * Date: 2017/6/17
 * Time: 19:52
 */
include "vendor/autoload.php";
include "./application/config.php"; //加载配置
include "./application/helper.php"; //加载帮助文件
//加载控制器
include "./application/MessageHandler.php";
include "./application/Model.php";
include "./application/WechatController.php";

2. WechatController.php

<?php
/**
 * Created by PhpStorm.
 * User: han
 * Date: 2017/6/17
 * Time: 20:08
 * 控制器
 */
//引入类库
use EasyWeChat\Foundation\Application;
use Applicaiton\MessageHandler;

//配置文件
$config = require('config.php');
//实例对象
$app = new Application($config);
$server = $app->server;
//消息处理
$server->setMessageHandler(function ($message){
    $handler = new MessageHandler($message);
    switch($message->MsgType){
        case "event":
            return $handler->eventHandler();
            break;
        case 'text':
            return $handler->textHandler();
            break;
        default:
            break;
    }
});
//响应
$response = $server->serve();
//返回消息
$response->send();

3. MessageHandler.php

<?php
/**
 * Created by PhpStorm.
 * User: han
 * Date: 2017/6/17
 * Time: 20:20
 */
//消息处理类
namespace Applicaiton;
use EasyWeChat\Message\Text;
use Applicaiton\Model;
class MessageHandler
{
    /*
     * 消息对象
     */
    private $message;
    public function __construct($message)
    {
        $this->message = $message;
    }
    /*
     * 事件响应函数
     */
    public function eventHandler()
    {
        return new Text(['content' => "指令已收到!"]);
    }
    /*
     * 文本消息响应
     */
    public function textHandler()
    {
        //所有日记
        if($this->message->Content == "我的日记")
        {
            $str = "";
            $notes = Model::get_all_notes($this->message['FromUserName']);
            foreach ($notes as $key => $values)
            {
                $strTemp  = $values['times'];
                $str.=$strTemp."\n";
                $str.=$values['content']."\n\n";
            }
            return $str;
        }
        //添加数据
        if(Model::add_note([$this->message['FromUserName'],$this->message['Content']]) )
            return new Text(['content' => "收到文本消息:\n".$this->message['Content']]);
        else
            return "插入失败";
    }
}

4. Model.php

<?php
/**
 * Created by PhpStorm.
 * User: han
 * Date: 2017/6/17
 * Time: 20:53
 */
/*
 * 数据模型
 */
namespace Applicaiton;

class Model
{
    private static $conn;
    private static $table;
    private static function _connect()
    {
        $config = require('config.php');
        self::$conn = new \Mysqli($config['db']['host'],$config['db']['user'],$config['db']['pwd'],$config['db']['dbname']);
        self::$conn->query("set names utf8");
        //设置数据表
        self::$table = $config['db']['table'];
    }
    //插入数据
    /**
     * @param $note_arr
     * @return mixed
     */
    public static function add_note($note_arr)
    {
        static::_connect();
        //拼接sql
        $sql = "insert into ".self::$table." (openid,content) values ('$note_arr[0]','$note_arr[1]')";

        return self::$conn->query($sql);
    }
    /*
     * 返回所有留言
     */
    public static function get_all_notes($openid)
    {
        static::_connect();
        $sql = "select * from ".self::$table." where openid = '$openid'";
       return  self::$conn->query($sql);
    }
}

效果演示

目前仅仅实现了添加日记和读取日记。回复我的日记即收到所有日记。

效果 数据库

总结

两个小时的练习,使我熟悉了EasyWechat框架,同时对有关问题进行探讨,之后会进一步完善。
TODO--有空再写。

上一篇 下一篇

猜你喜欢

热点阅读