在线音乐网站网站开发项目 ,第二篇

第一篇看前几天发布那篇就可以了,接下来接着讲第二篇的内容,主要是降会员部分的功能.。

有什么不明白或者其他问题的可以加我扣扣8582-36016

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);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值