1.表结构设计
可以用无限极分类,但是用无限级分类没有必要:
- 查询麻烦,只能查看自己的上级.
- 查上上级,上上上级需要循环或递归.
- 事实上不用无限级,3级就足够.
所以表结构可以设计为:
2.生成迁移文件
php artisan make:migration add_p1_p2_p3_to_users --table=users
3.修改迁移文件
//up
$table->integer('p1')->unsigned()->default(0);
$table->integer('p2')->unsigned()->default(0);
$table->integer('p3')->unsigned()->default(0);
//down
$table->dropColumn('p1');
$table->dropColumn('p2');
$table->dropColumn('p3');
4. 执行迁移文件
php artisan migrate
5. 关注方法进行修改如下
public function guanzhu($message)
{
/**
*获取用户信息
*将需要信息放到数据库
*返回回复消息
*/
$user = new User();//实例化model
$openid = $message->FromUserName;//获取关注用户的openid
$fans = $user -> where('openid',$openid) -> first();
$codeId= false;
if(isset($message->EventKey)){ //当微信二维码带参数时
$codeId = substr($message->EventKey,8);
}
//判断之前是否关注
if($fans && $fans->state == 0){ //关注过但是又取消关注了
$fans->subtime = time();//关注users表的时间
$fans->state = 1;
$userService = $this->app->user;//得到用户实例
$Tuser = $userService->get($openid);//获取用户信息
$fans->name = $Tuser->nickname;
$fans->save();
}elseif(!$fans){ //没有关注过
$user->openid = $openid;//对应users表的openid
$user->subtime = time();//关注users表的时间
$userService = $this->app->user;//得到用户实例
$Tuser = $userService->get($openid);//获取用户信息
$user->name = $Tuser->nickname;
if($codeId){ //当扫描的二维码不带参数时
//层级关系
$fans = $user->find($codeId);
$user->p1 = $codeId;
$user->p2 = $fans->p1;
$user->p3 = $fans->p2;
}
$user->save();
//利用uid生成永久性场景二维码,作为用户唯一标示
$user->code = $this->erCode($user->uid);
$user->save();
}
$text = new Text();
$text->content = '您好!欢迎关注我!';
return $text;
}