第一篇看前几天发布那篇就可以了,接下来接着讲第二篇的内容,主要是降会员部分的功能.。
class IndexController extends BaseController
{
protected $user;
protected $loginUser = null;
/**
* 用户页面
* @return \think\Response
*/
public function index()
{
$title = '会员广场 - ' . config('web_site_title');
return $this->fetch('', ['meta_title' => $title]);
}
/**
* 当前用户个人页面
* @param int $uid
* @return \think\Response
*/
public function read($uid = 0)
{
//获取用户信息
$this->initUser($uid);
$this->seoMeta($this->user);
return $this->fetch('');
}
/**
* 显示当前用户音乐
* @param int $uid
* @return \think\Response
*/
public function music($uid = 0)
{
$this->initUser($uid);
$this->seoMeta($this->user, 'read');
return $this->fetch('');
}
/**
* 用户专辑页面
* @param int $uid
* @return \think\response
*/
public function album($uid = 0)
{
$this->initUser($uid);
$this->seoMeta($this->user, 'read');
return $this->fetch('');
}
/**
* 用户粉丝页面
* @param int $uid
* @return \think\Response
*/
public function fans($uid = 0)
{
$this->initUser($uid);
$this->seoMeta($this->user, 'read');
return $this->fetch('');
}
/**
* 用户访客页面
* @param int $uid
* @return \think\Response
*/
public function visitor($uid = 0)
{
$this->initUser($uid);
$this->seoMeta($this->user, 'read');
return $this->fetch('');
}
/**
* 初始化用户
* @return void
*/
protected function initUser($uid = 0)
{
if (!$uid = intval($uid)) {
$this->error('你查看的用户不存在!');
}
define('UID', is_login());
$this->user = get_user_info($uid);
if (UID > 0 && $uid != UID) {
$this->loginUser = get_user_info(UID);
$this->setVisitor();
} else {
$this->loginUser = $this->user;
}
$this->assign('login_user', $this->loginUser);
$this->assign('user', $this->user);
}
}
记录访问 缓存
protected function setVisitor()
{
$visitor = [
'uid' => $this->loginUser['uid'],
'avatar' => $this->loginUser['avatar'],
'sex' => $this->loginUser['sex'],
'nickname' => $this->loginUser['nickname'],
'url' => $this->loginUser['url'],
'visit_time' => time(),
];
$visitors = file_cache('visitor_' . $this->user['uid']);
$visitors[$this->loginUser['uid']] = $visitor;
if (is_array($visitors) && (count($visitors) >= 100)) {
//删除最后一个
array_pop($visitors);
}
//缓存7天 7天无任何访问者 清除
file_cache('visitor_' . $this->user['uid'], $visitors);
}
Auth 用户认证控制器
class AuthController extends BaseController
{
/**
* 显示用户登录页面
* @return \think\Response
*/
public function login()
{
if (is_login()) {
$this->redirect('user/Account/index');
}
$title = '用户登录 - ' . config('web_site_title');
return $this->fetch('', ['meta_title' => $title]);
}
/**
* 显示注册用户
* @return \think\Response
*/
public function signup()
{
if (is_login()) {
$this->redirect('user/Account/index');
}
if (!config('user_allow_register')) {
$this->error(lang('signup_off'));
}
$title = '用户注册 - ' . config('web_site_title');
return $this->fetch('', ['meta_title' => $title]);
}
/**
* 显示当前登录用户
* @return \think\Response
*/
public function active()
{
$uid = is_login();
$api = new UserApi();
//检测自动登录
if (!$uid && $autoUid = cookie('JYUUID')) {
if ($uid = jy_decrypt($autoUid)) {
$uid = $api->autoLogin($uid);
}
}
if (intval($uid)) {
$info = $api->info($uid);
$return['uid'] = $uid;
$return['url'] = url('user/Index/read', ['uid' => $uid]);
$return['avatar'] = $info['avatar'];
$return['nickname'] = $info['nickname'];
$return['location'] = $info['location'];
unset($info);
return $this->retSucc([
'msg' => '已登录',
'uid' => $uid,
'result' => $return,
]);
}
return $this->retErr(40001);
}
/**
* 用户登录
* @return \think\response
*/
public function loginUser()
{
if (is_login()) {
return $this->retErr(40038);
}
$options = [
'action' => 'login_user',
'limit_time' => 3600,
'limit_num' => 10,
'lock_time' => 3600,
];
if (isOffSpite($options)) {
return $this->retErr(40405);
}
$data = $this->request->param();
//验证码
if (config('verify_off') && !captcha_check($data['verify'])) {
return $this->retErr(40035);
}
$validate = Loader::validate('UcenterMember');
if (!$validate->scene('login')->check($data)) {
return $this->retErr(40030, $validate->getError());
}
$api = new UserApi();
if ($uid = $api->login($data['username'], $data['password'])) {
if (isset($data['autologin']) && $data['autologin'] == 'on') {
$api->remember($uid);
}
return $this->retSucc(['msg' => lang('login_success'), 'url' => url('user/Account/index')]);
}
$error = $api->getError();
return $this->retErr(40502, $error);
}
注册一个新用户
public function createUser(Request $request)
{
if (!config('user_allow_register')) {
abort(404, lang('signup_off'));
}
$options = [
'action' => 'create_user',
'limit_time' => 3600,
'limit_num' => 10,
'lock_time' => 3600,
];
if (isOffSpite($options)) {
return $this->retErr(40405);
}
if (is_login()) {
return $this->retErr(40038);
}
//api 内置验证
$post = $request->post();
$result = $this->validate($post, 'UcenterMember.register');
if (true !== $result) {
return $this->retErr(40030, $result);
}
$api = new UserApi();
//激活邮件
if (config('send_activate_mail')) {
//发送激活邮件
$post['status'] = 2;
$uid = $api->register($post);
if (!$uid) {
$error = $api->getError();
return $this->retErr(40501, $error);
}
if ($this->sendActiveEmail($uid, $post)) {
return $this->retSucc(lang('active_email_success'));
} else {
return $this->retErr(40503);
}
} else {
$uid = $api->register($post, true);
if ($uid) {
return $this->retSucc(['msg' => lang('signup_success'), 'url' => url('user/Account/index')]);
}
$error = $api->getError();
return $this->retErr(40501, $error);
}
}