開發(fā)普通應(yīng)用

3章 開始開發(fā)

3.1 關(guān)注微信號(hào)

  • 將關(guān)注的回復(fù)功能移到自己的方法中(后面邏輯也是在此方法中判斷)
#index方法如下修改
// 當(dāng) $message->MsgType 為 event 時(shí)為事件
            if ($message->MsgType == 'event' && $message->Event== 'subscribe') {
                return $this->guanzhu($message);//調(diào)用guanzhu方法,讓index不至于累積成堆
            }
#guanzhu方法
public function guanzhu($message)
    {
        $text = new Text();
        $text->content = '您好!歡迎關(guān)注我!';
        return $text;
    }
  • 將用戶信息存入數(shù)據(jù)庫(kù)(新建個(gè)weixin數(shù)據(jù)庫(kù))

配置.env文件
建立數(shù)據(jù)庫(kù)表users的遷移文件(遷移文件有默認(rèn)的,修改一下)

Schema::create('users', function (Blueprint $table) {
            $table->increments('uid');
            $table->string('openid',32);
            $table->string('name');
            $table->boolean('state')->default(1);
            $table->integer('subtime');    
        });

執(zhí)行遷移文件 php artisan magrate;
刪除原有的user的model,重新生成model

//控制臺(tái)
php artisan make:model User
//user.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    protected $primaryKey = 'uid';//指定自增id
    public $timestamps = false;//指定不添加默認(rèn)兩種時(shí)間
}

寫入數(shù)據(jù)庫(kù)

use App\User;
.
.
.
/**
    *
    *用于關(guān)注事件
    *@param obj $message 關(guān)注時(shí)提交的信息
    *@return string $text 給用戶返回的消息
    *
    */
 public function guanzhu($message)
    {
        /**
        *獲取用戶信息
        *將需要信息放到數(shù)據(jù)庫(kù)
        *返回回復(fù)消息
        */
        $user = new User();//實(shí)例化model
        $openid = $message->FromUserName;//獲取關(guān)注用戶的openid
        $fans = $user -> where('openid',$openid) -> first();
        //判斷之前是否關(guān)注
        if($fans){
            $fans->subtime = time();//關(guān)注users表的時(shí)間
            $fans->state = 1;
            $userService = $this->app->user;//得到用戶實(shí)例
            $Tuser = $userService->get($openid);//獲取用戶信息
            $fans->name = $Tuser->nickname;
            $fans->save();
        }else{
            $user->openid = $openid;//對(duì)應(yīng)users表的openid
            $user->subtime = time();//關(guān)注users表的時(shí)間
            $userService = $this->app->user;//得到用戶實(shí)例
            $Tuser = $userService->get($openid);//獲取用戶信息
            $user->name = $Tuser->nickname;
            $user->save();
        }       
        $text = new Text();
        $text->content = '您好!歡迎關(guān)注我!';
        return $text;
    }

注意,這塊會(huì)報(bào)錯(cuò)誤,是因?yàn)?,目前php版本在https請(qǐng)求時(shí),默認(rèn)需要開啟證書,錯(cuò)誤日志如下:


查看wechat常見錯(cuò)誤:https://easywechat.org/zh-cn/docs/troubleshooting.html 第一條就是解決方法

注意:curl.cainfo好像在5.6以上版本才有

重啟apache
測(cè)試成功!數(shù)據(jù)庫(kù)中添加了一條數(shù)據(jù)

4 取消關(guān)注

#index方法中
···
$server->setMessageHandler(function($message){
      elseif($message->MsgType == 'event' && $message->Event== 'unsubscribe'){
                $this->unguanzhu($message);//調(diào)用取消關(guān)注方法
            }
}
···
#unguanzhu方法
/**
*
*用于取消關(guān)注
*@param $message 取消關(guān)注事件傳遞的信息
*
*/
public function unguanzhu($message)
    {
        $openid = $message -> FromUserName;
        $user = new User();
        $fans = $user->where('openid',$openid)->first();
        if($fans){
            $fans -> state = 0;//修改狀態(tài)為0
            $fans -> save();
        }       
    }

5 為關(guān)注用戶生成場(chǎng)景二維碼

執(zhí)行artisan命令給users表添加一個(gè)字段

//命令 php artisan make:migration add_code_to_users --table=users
修改遷移文件
//up
 $table->string('code',100);
//down
$table->dropColumn('code');
#guanzhu方法中在else的save下面添加如下兩條demo
//利用uid生成永久性場(chǎng)景二維碼,作為用戶唯一標(biāo)示
$user->code = $this->erCode($user->uid);
$user->save();
在控制器中創(chuàng)建如下兩個(gè)方法
/**
    *
    *利用uid生成永久性場(chǎng)景二維碼,作為用戶唯一標(biāo)示
    *@param obj $uid 用戶的uid
    *@return string $path 二維碼路徑
    */
    public function erCode($uid)
    {
        $qrcode = $this->app->qrcode;
        $result = $qrcode->forever($uid);// 或者 $qrcode->forever("foo");
        $ticket = $result->ticket; // 或者 $result['ticket']
        $url = $qrcode->url($ticket);//通過(guò)url輸出內(nèi)容
        $content = file_get_contents($url); // 得到二進(jìn)制圖片內(nèi)容
        $path= $this->mkd() . '/qr_'.$uid.'.jpg';//二維碼的路徑
        file_put_contents(public_path().$path, $content); // 寫入文件
        return $path;
    }
    /**
    *
    *根據(jù)年月日創(chuàng)建目錄
    *@return string $today /Y/m格式
    *
    */
    protected function mkd() {
        $today = date('/Y/m');
        if( !is_dir( public_path() . $today ) ) {
            mkdir( public_path() . $today , 0777 , true);
        }
        return $today;
    }

刪除數(shù)據(jù)表的所有數(shù)據(jù) truncate users;
微信取消關(guān)注,再關(guān)注,看一下數(shù)據(jù)表code字段有沒(méi)有路徑,在public下會(huì)有對(duì)應(yīng)路徑生成的二維碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 22年12月更新:個(gè)人網(wǎng)站關(guān)停,如果仍舊對(duì)舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,401評(píng)論 22 257
  • 那夜下起了鵝毛大雪,周遭寂無(wú)人聲。彎曲蔓延的小道也變得寂寥幽深起來(lái)了。 “吱嘎、吱嘎、吱嘎”,十幾厘米厚的雪被雙黑...
    葉齋閱讀 331評(píng)論 0 2
  • 我愿意 愿意在周一的清晨做一份你醒來(lái)就能吃的早餐。 我愿意 愿意在周二的上午跟你你討論著你向往的美好。 我愿意 愿...
    圖愛(ài)閱讀 248評(píng)論 0 0
  • 本月讀書 6 本。 《尷尬時(shí)代》,解讀野史的閑書。★★★★☆ 《不朽者》,好小說(shuō),立刻加深對(duì)都德的好感,周克希譯文...
    loveisbug閱讀 157評(píng)論 0 0

友情鏈接更多精彩內(nèi)容