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)路徑生成的二維碼