|
发表于 2013-9-15 14:43:45
|
显示全部楼层
把我写了一上午的代码贴上来。不想写了。
PHP复制代码 <?php
if (! defined ( 'BASEPATH' ))
exit ( 'No direct script access allowed' );
class Welcome extends CI_Controller {
public function __construct () {
parent ::__construct ();
$this->load->library ( 'session' );
$this->load->database ();
$this->load->helper ( 'url' );
}
public function index () {
$this->home ();
}
public function edit () {
}
public function home ($page = 0) {
$this->check_login ();
$this->load->library ( 'pagination' );
$config ['base_url'] = site_url ( 'home' );
$config ['per_page'] = 20;
$users = $this->db->get_where ( 'user', array (
'role' => ROLE_GUEST
), $config ['per_page'], $page );
$config ['total_rows'] = $users->num_rows;
$config ['cur_page'] = $page;
$this->pagination->initialize ( $config );
$data ['users'] = $users->result_array ();
$data ['page'] = $this->pagination->create_links ();
$this->load->view ( 'home', $data );
}
public function admin () {
$this->check_login();
if ($this->session->userdata['role'] != ROLE_ADMIN ) {
$this->session->set_flashdata('msg', '您没有管理权限');
redirect ('login');
}
}
public function install () {
$this->db->query ( "
CREATE TABLE IF NOT EXISTS `crm_user` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NULL,
`userpass` VARCHAR(50) NULL,
`created_at` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '创建时间',
`last_login_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '最后登录时间',
`role` TINYINT(10) NOT NULL DEFAULT '3' COMMENT '角色 1 管理员 2 操作员 3 客户',
`info` TEXT NULL COMMENT '其他信息 json 格式',
PRIMARY KEY (`id`),
UNIQUE INDEX `username` (`username`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;" );
$this->db->query ( "
CREATE TABLE IF NOT EXISTS `crm_log` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`user_id` INT(10) NOT NULL COMMENT '操作员',
`guest_id` INT(10) NOT NULL COMMENT '被操作的客户',
`action` INT(10) NOT NULL COMMENT '1 新增 2 删除 3 更改',
`created_at` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '操作时间',
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;" );
$q = $this->db->get ( 'user', 1, 0 );
if ($q->num_rows ()) {
echo 'Your app has already been installed. Please click <a href="'.site_url ('login').'">here</a> to log in.';
die ();
}
$admin = array (
'username' => 'admin',
'userpass' => $this->generate_pass ( '123456' ),
'role' => ROLE_ADMIN ,
'created_at' => date ( 'Y-m-d H:i:s' )
);
$this->db->insert ( 'user', $admin );
$this->session->set_userdata ( $admin );
redirect ( 'home' );
}
public function handle_login () {
$this->db->where ( array (
'username' => $this->input->post ( 'name' ),
'userpass' => $this->generate_pass ( $this->input->post ( 'pass' ) )
) );
$user = $this->db->get ( 'user' );
if ($user = $user->row ()) {
$this->session->set_userdata ( $user );
redirect ( 'home' );
} else {
$this->session->set_flashdata ( 'msg', '用户或密码错误' );
redirect ( 'login' );
}
}
public function login () {
$this->load->view ( 'login' );
}
public function logout () {
$this->session->unset_userdata('role');
$this->session->set_flashdata('msg', '您已注销登录');
redirect ( 'login' );
}
private function check_login () {
if (! $this->session->userdata ( 'role' )) {
redirect ( 'login' );
}
}
private function generate_pass ($str) {
return md5 ( $str . 'CRM_SALT_rgergsadfaerge' );
}
} 复制代码 |
|