审查视图

application/index/controller/User.php 7.0 KB
Karson authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Random;
use fast\third\Application;
use fast\ucenter\client\Client;
use think\Config;
use think\Cookie;
use think\Loader;

/**
 * 会员中心
 */
class User extends Frontend
{

    // 使用布局
    protected $layout = 'bootstrap';
21
    protected $noNeedLogin = ['*'];
Karson authored
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    public function _initialize()
    {
        parent::_initialize();
        //导入UC常量
        Loader::import('uc', APP_PATH);
    }

    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 注册会员
     */
    public function register()
    {
        $url = $this->request->get('url', '/');
        if ($this->user->check())
Karson authored
42
            $this->error(__('You\'ve logged in, do not login again'), $url);
Karson authored
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        if ($this->request->isPost())
        {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $repassword = $password;
            $email = $this->request->post('email');
            $captcha = $this->request->post('captcha');
            if (!captcha_check($captcha))
            {
                $this->error(__('Captcha is incorrect'));
            }
            if ($this->user->register($username, $password, $email))
            {
                $synchtml = '';
                ////////////////同步到Ucenter////////////////
                if (defined('UC_STATUS') && UC_STATUS)
                {
                    $uc = new Client();
                    $synchtml = $uc->uc_user_synregister($this->user->id, $password);
                }
                $referer = Cookie::get('referer_url');
64
                $this->success(__('Sign up successful') . $synchtml, $url);
Karson authored
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            }
            else
            {
                $this->error($this->user->getError());
            }
        }
        return $this->view->fetch();
    }

    /**
     * 会员登录
     */
    public function login()
    {
        $url = $this->request->get('url', '/');
        if ($this->user->check())
Karson authored
81
            $this->error(__('You\'ve logged in, do not login again'), $url);
Karson authored
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        if ($this->request->isPost())
        {
            $account = $this->request->post('account');
            $password = $this->request->post('password');
//            $captcha = $this->request->post('captcha');
//            if (!captcha_check($captcha))
//            {
//                $this->error(__('Captcha is incorrect'));
//            }
            if ($this->user->login($account, $password))
            {
                $synchtml = '';
                ////////////////同步到Ucenter////////////////
                if (defined('UC_STATUS') && UC_STATUS)
                {
                    $uc = new Client();
                    $synchtml = $uc->uc_user_synlogin($this->user->id);
                }
100
                $this->success(__('Logged in successful') . $synchtml, $url);
Karson authored
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            }
            else
            {
                $this->error($this->user->getError());
            }
        }
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    function logout()
    {
        //注销本站
        $this->user->logout();
        $synchtml = '';
        ////////////////同步到Ucenter////////////////
        if (defined('UC_STATUS') && UC_STATUS)
        {
            $uc = new Client();
            $synchtml = $uc->uc_user_synlogout();
        }
        $this->success(__('Logout successful') . $synchtml, '/');
    }

    /**
     * 第三方登录跳转和回调处理
     */
    public function third()
    {
        $action = $this->request->param('action');
        $platform = $this->request->param('platform');
        $config = Config::get('third');
        if (!isset($config[$platform]))
        {
            $this->error(__('Invalid parameters'));
            return;
        }
        $thirdapp = new Application();
        if ($action == 'redirect')
        {
            // 跳转到登录授权页面
            $this->redirect($thirdapp->{$platform}->getAuthorizeUrl());
        }
        else if ($action == 'callback')
        {
            // 授权成功后的回调
            $result = $thirdapp->{$platform}->getUserInfo();
            if ($result)
            {
                $loginret = $this->user->connect($platform, $result);
                if ($loginret)
                {
                    $synchtml = '';
                    ////////////////同步到Ucenter////////////////
                    if (defined('UC_STATUS') && UC_STATUS)
                    {
                        $uc = new Client();
                        $synchtml = $uc->uc_user_synlogin($this->user->id);
                    }
                    $this->success(__('Logged in successful') . $synchtml, '/');
                    return;
                }
            }
            $this->error(__('Operation failed'), 'user/login');
        }
        else
        {
            $this->error(__('Invalid parameters'));
        }

        return;
    }

    /**
     * 修改密码
     */
    public function changepwd()
    {
        if ($this->request->isPost())
        {
            $oldpassword = $this->request->post("oldpassword");
            $newpassword = $this->request->post("newpassword");
            //判断旧密码是否正确
            if ($this->user->password == $this->user->getEncryptPassword($oldpassword, $this->user->salt))
            {
                ////////////////同步到Ucenter////////////////
                if (defined('UC_STATUS') && UC_STATUS)
                {
                    $uc = new Client();
                    $ret = $uc->uc_user_edit($this->user->id, $this->user->username, $newpassword, $this->user->email, $this->user->mobile);
                    // 如果小于0则说明发生错误
                    if ($ret < 0)
                    {
                        $this->error(__('Change password failure'));
                    }
                }

                $salt = Random::alnum();
                $newpassword = $this->user->getEncryptPassword($newpassword, $salt);
                $this->user->save(['password' => $newpassword, 'salt' => $salt]);
                $this->user->logout();
                $synchtml = '';
                ////////////////同步到Ucenter////////////////
                if (defined('UC_STATUS') && UC_STATUS)
                {
                    $uc = new Client();
                    $synchtml = $uc->uc_user_synlogout();
                }
                $this->success(__('Change password successful') . $synchtml, "user/login");
            }
            else
            {
                //旧密码不正确
                $this->error(__('Password is incorrect'));
            }
        }
        return $this->view->fetch();
    }

}