Commit 190dcfab758c0ec209979514234b7aa495bc0696

Authored by zhang
1 parent 0f42fbcc

'控制器优化'

trunk/Message/Api/Controller/Message/ListController.class.php
... ... @@ -27,7 +27,7 @@ class ListController extends AbstractController
27 27  
28 28 $page = isset($input['page']) ? intval($input['page']) : 1;
29 29  
30   - $message_list = $m_message->messageList($input,$page);
  30 + $message_list = $m_message->message_list($input,$page);
31 31  
32 32 $this->_result = $message_list;
33 33 }
... ...
trunk/Message/Apicp/Controller/Message/DetailController.class.php
... ... @@ -15,7 +15,7 @@ class DetailController extends \Apicp\Controller\AbstractController
15 15 * User: <362431947@qq.com>
16 16 * Date: 2018-10-11 Time: 11:51
17 17 */
18   - public function detail_post()
  18 + public function Index_post()
19 19 {
20 20 $input = I('post.');
21 21  
... ...
trunk/Message/Apicp/Controller/Message/ListController.class.php
... ... @@ -25,6 +25,6 @@ class ListController extends \Apicp\Controller\AbstractController
25 25  
26 26 $m_message = new MessageModel();
27 27  
28   - $this->_result = $m_message->messageList($input,$page);
  28 + $this->_result = $m_message->message_list($input,$page);
29 29 }
30 30 }
31 31 \ No newline at end of file
... ...
trunk/Message/Common/Model/MessageModel.class.php
... ... @@ -37,6 +37,14 @@ class MessageModel extends AbstractModel
37 37 */
38 38 public function message_list($conds = [],$page = 0)
39 39 {
  40 + $conds = $this->check_fields($conds);
  41 +
  42 + $title = isset($conds['title']) ? trim($conds['title']) : '';
  43 + unset($conds['title']);
  44 +
  45 + $message = isset($conds['message']) ? trim($conds['message']) : '';
  46 + unset($conds['message']);
  47 +
40 48 if ($page < 1 || !$page) {
41 49 $page = self::PAGING_DEFAULT_PAGE;
42 50 }
... ... @@ -44,13 +52,25 @@ class MessageModel extends AbstractModel
44 52 $params = [];
45 53 $wheres = [];
46 54  
47   - $conds['a.status'] = $this->get_st_create();
48   -
49   -
50 55 if (!$this->_parse_where($wheres, $params, $conds)) {
51 56 return false;
52 57 }
53 58  
  59 +
  60 + $wheres[] = 'a.`status` < ?';
  61 + $params[] = $this->get_st_delete();
  62 +
  63 + if ($title) {
  64 + $wheres[] = 'a.`title` LIKE ?';
  65 + $params[] = "%$title%";
  66 + }
  67 +
  68 + if ($message) {
  69 + $wheres[] = "a.`message` LIKE ?";
  70 + $params[] = "%$message%";
  71 + }
  72 +
  73 +
54 74 $page_size = self::PAGING_DEFAULT_LIMIT;
55 75  
56 76 $page_start = ($page - 1) * $page_size;
... ... @@ -63,48 +83,34 @@ class MessageModel extends AbstractModel
63 83 $table = $m_person->get_tname();
64 84  
65 85  
66   - if (in_array('title',$conds)) {
67   - $where = " title LIKE '?' ";
68   - }
69   - else{
70   - $fields = array_keys($conds);
71   -
72   - if ($fields != 'status') {
73   - $field = $fields[0];
74   - }
75   -
76   - $where = " $field = ? ";
77   -
78   - }
79   -
80 86 $sql = "SELECT a.message_id,a.title,a.message,IFNULL(b.name,'') AS `from`,IFNULL(c.name,'') AS `to`,a.checked
81 87 FROM __TABLE__ a
82 88 LEFT JOIN {$table} b ON a.uid = b.person_id
83 89 LEFT JOIN {$table} c ON a.receiver_uid = c.person_id
84   - WHERE $where";
  90 + WHERE " . implode(' AND ', $wheres);
85 91  
86 92 $list = $this->_m->fetch_array($sql, $params,$option);
87 93  
88   - $messageList['total'] = count($list);
89   - $messageList['page'] = $page;
90   - $messageList['limit'] = $page_size;
91   - $messageList['list'] = $list;
92 94  
93   - return $messageList;
  95 + $message_list['total'] = count($list);
  96 + $message_list['page'] = $page;
  97 + $message_list['limit'] = $page_size;
  98 + $message_list['list'] = $list;
  99 +
  100 + return $message_list;
94 101 }
95 102  
96 103 /**
97 104 * 后台留言删除
98 105 * User: <362431947@qq.com>
99 106 * @param int $message_id
100   - * @return bool
101   - * Date: 2018-10-10 Time: 22:24
  107 + * @throws \Think\Exception
  108 + * Date: 2018-10-14 Time: 22:07
102 109 */
103 110 public function del($message_id = 0)
104 111 {
105 112 $this->_m->startTrans();
106 113  
107   -
108 114 $attachment_id = $this->getAttachmentIdByMsgId($message_id);
109 115  
110 116 // 删除相关附件图片
... ... @@ -114,8 +120,6 @@ class MessageModel extends AbstractModel
114 120 $attachment_del = $m_attachment->del($attachment_id);
115 121 }
116 122  
117   -
118   -
119 123 // 删除留言
120 124 $message_del = $this->_m->delete($message_id);
121 125  
... ... @@ -132,11 +136,10 @@ class MessageModel extends AbstractModel
132 136  
133 137 if ($message_del && $comment_del && $attachment_del) {
134 138 $this->_m->commit();
135   - return true;
136 139 }
137 140 else{
138 141 $this->_m->rollback();
139   - return false;
  142 + E('_ERROR_DEL_MSG_FAIL');
140 143 }
141 144 }
142 145  
... ... @@ -266,8 +269,9 @@ class MessageModel extends AbstractModel
266 269 * 留言审核
267 270 * User: <362431947@qq.com>
268 271 * @param int $message_id
269   - * @return bool
270   - * Date: 2018-10-11 Time: 21:32
  272 + * @param int $checked
  273 + * @throws \Think\Exception
  274 + * Date: 2018-10-14 Time: 22:05
271 275 */
272 276 public function check($message_id = 0,$checked = 1)
273 277 {
... ... @@ -284,11 +288,50 @@ class MessageModel extends AbstractModel
284 288 'checked' =>$checked,
285 289 ];
286 290  
287   - return $this->update($message_id,$data,true);
  291 + $result = $this->update($message_id,$data,true);
  292 +
  293 + if (!$result) {
  294 + E('_ERROR_CHECK_MSG_FAIL');
  295 + }
288 296 }
289 297 else{
290   - return false;
  298 + E('_ERROR_MSG_NOT_EXISTS');
  299 + }
  300 +
  301 + }
  302 +
  303 + /**
  304 + * 验证字段有效性
  305 + * User: <362431947@qq.com>
  306 + * @param array $conds
  307 + * @return array
  308 + * Date: 2018-10-14 Time: 19:14
  309 + */
  310 + private function check_fields($conds = [])
  311 + {
  312 + $tmp = [];
  313 +
  314 + // 姓名、性别(选择)、手机号、邮箱、留言标题以及留言内容
  315 + $check_fields = ['name','sex','mobile','email','title','message'];
  316 + $check_person_fields = ['name','sex','mobile','email'];
  317 +
  318 + if (!$conds) {
  319 + return $conds;
  320 + }
  321 +
  322 + foreach ($conds as $key => $value) {
  323 + if (!$value || !in_array($key, $check_fields)) {
  324 + unset($conds[$key]);
  325 + continue;
  326 + }
  327 +
  328 + if (in_array($key, $check_person_fields)) {
  329 + $k = 'b.' . $key;
  330 + $conds[$k] = $conds[$key];
  331 + unset($conds[$key]);
  332 + }
291 333 }
292 334  
  335 + return $conds;
293 336 }
294 337 }
... ...