ThinkPHP 钩子函数使用

注册 -> 邮箱验证

通过钩子函数完成数据入库前后的操作

–Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/**
* 插入的前置钩子函数在注册数据入库前,进行密码加密
* @param array &$data 表单提交过来的数据
* @param array $option 表名和模型名以及一些查询条件(如果有)
*/
public function _before_insert(&$data,$option){
$data['salt'] = uniqid();
$data['password'] = md5(md5($data['password']) . $data['salt']);
}

/**
* 插入的后置钩子函数,在数据真正入库之后会被调用
* @param array $data 存入数据库的数据,包含主键id
* @param array $option 表名和模型名
*/
public function _after_insert($data,$option){
$email_key = md5($data['id'] . C('EMAIL_KEY'));
$content = "尊敬的用户您好,欢迎注册京西购物网站!<br><a href='http://local.shop.com/index.php/Member/Member/active/id/'" .$data['id']. "email_key" . $email_key. " target='_blank'>请点击激活您的账户</a>";
sendMail($data['email'], '京西购物商城', $content );
}
?>

–Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
// 会员邮箱激活
public function active(){
$id = I('id');
$email_key = I('email_key');
$memberModel = D('Member');
// 验证用户id与用户秘钥是否匹配
if (md5($id . C('EMAIL_KEY')) == $email_key) {
$userinfo = $memberModel->find($id);
if ($userinfo) {
$where = array('id'=>$id);
// setField 返回更新信息影响的函数
$status = $memberModel->where($where)->setField('isactive', 1);
if ($status) {
return $this->success('用户激活成功', U('Home/Index/index'));
}else{
return $this->error('用户激活失败', U('Home/Index/index'));
}
}else{
$this->error('用户不存在', U('Home/Index/index'));
}
}else{
return $this->error('非法操作,该用户不存在',U('Home/Index/index'));
}
}
?>

商品 -> 商品模块(简版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
<?php
/*
** +-------------------------------------------
** | Author: uiste [ JUST DO IT ]
** +-------------------------------------------
** | Connection: <blog.uiste.com>
** +-------------------------------------------
*/
namespace Admin\Model;
use Think\Model;

/**
* 商品模块
*/
class GoodsModel extends Model
{

//自动验证
protected $_validate = array(
array('goods_name', 'require', '商品名称不能为空'),
);

// 自动填充
protected $_auto = array(
array('goods_sn','_goods_sn',1,'callback'),
array('add_time','_add_time',1,'callback'),
);

// 商品货号填充检查
protected function _goods_sn($sn){
if (empty($sn)) {
return 'sn_' . uniqid();
}else{
return $sn;
}
}

// 商品添加时间检查
protected function _add_time($time){
if (empty($time)) {
return time();
}else{
return strtotime($time);
}
}

// 搜索分页功能
public function search(){
$where = '1 and is_delete = 0';

// 查询添加依据是否传递查询字段,没有传递默认为空,条件不成立
if ($goods_name = I('get.gn')) {
$where .= " and `goods_name` like '%$goods_name%'";
}

// 排序字段与排序方式通过判断前台是否有传递,有传递使用,没有就用默认值
$orderBy = !empty(I('get.ob')) ? I('get.ob') : 'goods_id';
$orderWay = !empty(I('get.ow')) ? I('get.ow') : 'desc';

$count = $this->where($where)->count();
$size = 5;
$page = new \Think\Page($count, $size);

$page->setConfig('prev','«');
$page->setConfig('next','»');
$show = $page->show();
$list = $this->where($where)
->alias('goods')
->join('left join jx_category cate on goods.cate_id = cate.cate_id')
->order("$orderBy $orderWay")
->limit($page->firstRow . ',' . $page->listRows)
->select();
return array(
'show' => $show,
'list' => $list,
);
}

// 插入前置钩子函数处理图片上传
protected function _before_insert(&$data, $option){
return $this->UploadFile($data, $option);
}

// 更新前置钩子函数处理更新信息
public function _before_update(&$data, $options){
// 如果文件上传成功,执行以下更新信息
// dump($options);exit();
// 如果没有文件$_FILES['goods_img']['error'] == 4
if ($_FILES['goods_img']['error']== '0' ) {
$status = $this->UploadFile($data, $option);
// dump($status);exit();
if ($status) {
$goods_id = $options['where']['goods_id'];
$goodsOldImg = $this->field('goods_img , goods_thumb')->find();
// dump($goodsOldImg);exit();
foreach ($goodsOldImg as $key => $value) {
@unlink( C('UPLOAD_ROOT_PATH') . $value );
}
}else{
return false;
}
}
}

// 图片上传处理函数
private function UploadFile(&$data, $option){
$configMaxSize = (int)C('MAX_UPLOAD_FILE_SIZE');
$phpiniMaxSize = (int)ini_get('upload_max_filesize');
$allowMaxSize = min($configMaxSize, $phpiniMaxSize);

$rootPath = C('UPLOAD_ROOT_PATH');
$exts = C('ALLOW_EXTS');

$config = array(
'maxSize' => $allowMaxSize * 1024 * 1024,
'rootPath' => $rootPath,
'savePath' => 'Goods/',
'exts' => $exts,
);

$upload = new \Think\Upload($config);
$info = $upload->upload();
if ($info) {
// 图片上传路径
$data['goods_img'] = $info['goods_img']['savepath'] . $info['goods_img']['savename'];
// 生成缩略图
$image = new \Think\Image();
$image->open( $rootPath . $data['goods_img'] );

$thumImg = $info['goods_img']['savepath'] . 'thumb_' . $info['goods_img']['savename'];
$image->thumb( C('THUMB.THUMB_W'), C('THUMB.THUMB_H'), C('THUMB.THUMB_S') )->save($rootPath . $thumImg );
$data['goods_thumb'] = $thumImg;
return true;
}else{
$uploadError = $upload->getError();
$this->error = $uploadError;
return false;
}
}
}
?>