BubbleController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace Home\Controller;
  3. /**
  4. * 冒泡
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-03-02T22:48:35+0800
  9. */
  10. class BubbleController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2017-03-02T22:48:35+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. }
  26. /**
  27. * [Index 冒泡]
  28. * @author Devil
  29. * @blog http://gong.gg/
  30. * @version 0.0.1
  31. * @datetime 2017-03-02T22:48:35+0800
  32. */
  33. public function Index()
  34. {
  35. // 条件
  36. $where = array();
  37. if(I('type') == 'own')
  38. {
  39. $where['u.id'] = $this->user['id'];
  40. }
  41. // 模型
  42. $m = M('Mood');
  43. // 分页
  44. $number = 10;
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->alias('m')->join('__USER__ AS u ON m.user_id=u.id')->where($where)->count(),
  48. 'where' => $_GET,
  49. 'url' => U('Home/Bubble/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 查询字段
  53. $field = array('m.id', 'm.user_id', 'm.content', 'm.visible', 'm.add_time', 'u.nickname');
  54. // 数据处理
  55. $data = $this->MoodDataHandle($m->alias('m')->join('__USER__ AS u ON m.user_id=u.id')->field($field)->where($where)->limit($page->GetPageStarNumber(), $number)->order('m.id desc')->select());
  56. $this->assign('data', $data);
  57. // 分页
  58. $this->assign('page_html', $page->GetPageHtml());
  59. // 基础数据
  60. $this->assign('common_user_visible_list', L('common_user_visible_list'));
  61. $this->assign('bubble_nav_list', L('bubble_nav_list'));
  62. $this->display('Index');
  63. }
  64. /**
  65. * [MoodDataHandle 说说数据处理]
  66. * @author Devil
  67. * @blog http://gong.gg/
  68. * @version 0.0.1
  69. * @datetime 2017-04-08T20:14:19+0800
  70. * @param [array] $data [需要处理的数据]
  71. * @return [array] [处理好的说说数据]
  72. */
  73. private function MoodDataHandle($data)
  74. {
  75. if(!empty($data) && is_array($data))
  76. {
  77. $mp = M('MoodPraise');
  78. $mc = M('MoodComments');
  79. foreach($data as $k=>&$v)
  80. {
  81. // 昵称
  82. if(empty($v['nickname']))
  83. {
  84. $v['nickname'] = L('common_bubble_mood_nickname');
  85. }
  86. // 发表时间
  87. $v['add_time'] = date('m-d H:i', $v['add_time']);
  88. // 点赞
  89. $v['praise_count'] = $mp->where(array('mood_id'=>$v['id']))->count();
  90. // 用户是否已点赞过
  91. $v['is_praise'] = ($mp->where(array('mood_id'=>$v['id'], 'user_id'=>$this->user['id']))->getField('id') > 0) ? 'ok' : 'no';
  92. // 评论总数
  93. $v['comments_count'] = $mc->where(array('mood_id'=>$v['id']))->count();
  94. // 评论列表
  95. $v['comments'] = $this->GetMoodComments($v['id']);
  96. }
  97. }
  98. return $data;
  99. }
  100. /**
  101. * [UserIsStateCheck 用户状态校验]
  102. * @author Devil
  103. * @blog http://gong.gg/
  104. * @version 0.0.1
  105. * @datetime 2017-04-12T15:29:45+0800
  106. */
  107. private function UserIsStateCheck()
  108. {
  109. $state = M('User')->where(array('id'=>$this->user['id']))->getField('state');
  110. if($state > 0)
  111. {
  112. $this->ajaxReturn(L('common_user_state_list')[$state]['tips'], -10);
  113. }
  114. }
  115. /**
  116. * [GetMoodComments 获取说说评论]
  117. * @author Devil
  118. * @blog http://gong.gg/
  119. * @version 0.0.1
  120. * @datetime 2017-04-10T14:38:00+0800
  121. * @param [int] $mood_id [说说id]
  122. * @return [array] [评论列表]
  123. */
  124. private function GetMoodComments($mood_id)
  125. {
  126. // 参数
  127. if(empty($mood_id))
  128. {
  129. return array();
  130. }
  131. // 评论列表
  132. $m = M('MoodComments');
  133. $field = array('mc.id', 'mc.user_id', 'mc.content', 'mc.reply_id', 'mc.add_time', 'u.nickname');
  134. $where = array('m.id'=>$mood_id, 'mc.reply_id'=>0);
  135. $data = $m->alias('mc')->join('__MOOD__ AS m ON mc.mood_id=m.id')->join('__USER__ AS u ON mc.user_id=u.id')->field($field)->where($where)->order('mc.id asc')->select();
  136. // 回复列表
  137. if(!empty($data))
  138. {
  139. $u = M('User');
  140. foreach($data as &$v)
  141. {
  142. // 评论时间
  143. $v['add_time'] = date('m-d H:i', $v['add_time']);
  144. // 评论内容
  145. $v['content'] = str_replace("\n", "<br />", $v['content']);
  146. $item_where = array('m.id'=>$mood_id, 'mc.parent_id'=>$v['id'], 'reply_id'=>array('gt', 0));
  147. $item = $m->alias('mc')->join('__MOOD__ AS m ON mc.mood_id=m.id')->join('__USER__ AS u ON mc.user_id=u.id')->field($field)->where($item_where)->order('mc.id asc')->select();
  148. if(!empty($item))
  149. {
  150. foreach($item as &$vs)
  151. {
  152. // 评论时间
  153. $vs['add_time'] = date('m-d H:i', $vs['add_time']);
  154. // 评论内容
  155. $vs['content'] = str_replace("\n", "<br />", $vs['content']);
  156. // 被回复的用户
  157. if($vs['reply_id'] > 0)
  158. {
  159. $uid = $m->where(array('id'=>$vs['reply_id']))->getField('user_id');
  160. if(!empty($uid))
  161. {
  162. $user = $u->field(array('id AS reply_user_id', 'nickname AS reply_nickname'))->find($uid);
  163. if(empty($user['reply_nickname']))
  164. {
  165. $user['reply_nickname'] = L('common_bubble_mood_nickname');
  166. }
  167. $vs = array_merge($vs, $user);
  168. }
  169. }
  170. }
  171. $v['item'] = $item;
  172. }
  173. }
  174. }
  175. return $data;
  176. }
  177. /**
  178. * [MoodSave 说说保存]
  179. * @author Devil
  180. * @blog http://gong.gg/
  181. * @version 0.0.1
  182. * @datetime 2017-04-08T13:21:34+0800
  183. */
  184. public function MoodSave()
  185. {
  186. // 是否ajax请求
  187. if(!IS_AJAX)
  188. {
  189. $this->error(L('common_unauthorized_access'));
  190. }
  191. // 用户状态校验
  192. $this->UserIsStateCheck();
  193. // 说说模型
  194. $m = D('Mood');
  195. // 编辑
  196. if($m->create($_POST, 1) !== false)
  197. {
  198. $m->user_id = $this->user['id'];
  199. $m->content = I('content');
  200. $m->add_time = time();
  201. // 开启事务
  202. $m->startTrans();
  203. // 更新用户签名
  204. if(I('is_sign') == 1)
  205. {
  206. $data = array(
  207. 'signature' => $m->content,
  208. 'upd_time' => time(),
  209. );
  210. $user_state = (M('User')->where(array('id'=>$this->user['id']))->save($data) !== false);
  211. } else {
  212. $user_state = true;
  213. }
  214. // 添加历史签名
  215. $mood_state = $m->add();
  216. // 状态
  217. if($user_state && $mood_state > 0)
  218. {
  219. // 提交事务
  220. $m->commit();
  221. // 更新用户session数据
  222. if(I('is_sign') == 1)
  223. {
  224. $this->UserLoginRecord($this->user['id']);
  225. }
  226. $this->ajaxReturn(L('common_operation_publish_success'));
  227. } else {
  228. // 回滚事务
  229. $m->rollback();
  230. $this->ajaxReturn(L('common_operation_publish_error'), -100);
  231. }
  232. } else {
  233. $this->ajaxReturn($m->getError(), -1);
  234. }
  235. }
  236. /**
  237. * [MoodDelete 说说删除]
  238. * @author Devil
  239. * @blog http://gong.gg/
  240. * @version 0.0.1
  241. * @datetime 2016-12-25T22:36:12+0800
  242. */
  243. public function MoodDelete()
  244. {
  245. // 是否ajax请求
  246. if(!IS_AJAX)
  247. {
  248. $this->error(L('common_unauthorized_access'));
  249. }
  250. // 参数
  251. $m = M('Mood');
  252. $id = I('id');
  253. // 只能删除自己的说说
  254. $user_id = $m->where(array('id'=>$id))->getField('user_id');
  255. if($user_id != $this->user['id'])
  256. {
  257. $this->ajaxReturn(L('bubble_mood_delete_error'), -2);
  258. }
  259. // 开启事务
  260. $m->startTrans();
  261. // 数据删除[说说,点赞,评论]
  262. $mood_state = $m->where(array('id'=>$id, 'user_id'=>$this->user['id']))->delete();
  263. $praise_state = M('MoodPraise')->where(array('mood_id'=>$id))->delete();
  264. $comments_state = M('MoodComments')->where(array('mood_id'=>$id))->delete();
  265. if($mood_state !== false && $praise_state !== false && $comments_state !== false)
  266. {
  267. // 提交事务
  268. $m->commit();
  269. $this->ajaxReturn(L('common_operation_delete_success'));
  270. } else {
  271. // 回滚事务
  272. $m->rollback();
  273. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  274. }
  275. }
  276. /**
  277. * [MoodCommentsDelete 说说评论删除]
  278. * @author Devil
  279. * @blog http://gong.gg/
  280. * @version 0.0.1
  281. * @datetime 2016-12-25T22:36:12+0800
  282. */
  283. public function MoodCommentsDelete()
  284. {
  285. // 是否ajax请求
  286. if(!IS_AJAX)
  287. {
  288. $this->error(L('common_unauthorized_access'));
  289. }
  290. // 模型
  291. $m = M('MoodComments');
  292. // 只能删除自己的评论
  293. $user_id = $m->where(array('id'=>I('id')))->getField('user_id');
  294. if($user_id != $this->user['id'])
  295. {
  296. $this->ajaxReturn(L('bubble_comments_delete_error'), -2);
  297. }
  298. // 开启事务
  299. $m->startTrans();
  300. // 数据删除
  301. $state = $m->where(array('id'=>I('id'), 'user_id'=>$this->user['id']))->delete();
  302. $item_state = $m->where(array('parent_id'=>I('id')))->delete();
  303. $reply_state = $m->where(array('reply_id'=>I('id')))->delete();
  304. if($state !== false && $item_state !== false && $reply_state !== false)
  305. {
  306. // 提交事务
  307. $m->commit();
  308. $this->ajaxReturn(L('common_operation_delete_success'));
  309. } else {
  310. // 回滚事务
  311. $m->rollback();
  312. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  313. }
  314. }
  315. /**
  316. * [MoodPraise 说说点赞]
  317. * @author Devil
  318. * @blog http://gong.gg/
  319. * @version 0.0.1
  320. * @datetime 2017-04-09T12:24:24+0800
  321. */
  322. public function MoodPraise()
  323. {
  324. // 是否ajax请求
  325. if(!IS_AJAX)
  326. {
  327. $this->error(L('common_unauthorized_access'));
  328. }
  329. // 用户状态校验
  330. $this->UserIsStateCheck();
  331. // 参数
  332. if(empty($_POST['id']))
  333. {
  334. $this->ajaxReturn(L('common_param_error'), -1);
  335. }
  336. $id = I('id');
  337. // 不能点赞自己的说说
  338. $mood = M('Mood')->field(array('id', 'user_id'))->find($id);
  339. if(empty($mood))
  340. {
  341. $this->ajaxReturn(L('bubble_mood_no_exist_error'), -2);
  342. } else {
  343. if($mood['user_id'] == $this->user['id'])
  344. {
  345. $this->ajaxReturn(L('bubble_mood_praise_error'), -3);
  346. }
  347. }
  348. // 查询数据
  349. $m = M('MoodPraise');
  350. $where = array('user_id'=>$this->user['id'], 'mood_id'=>$id);
  351. $temp = $m->where($where)->getField('id');
  352. // 数据存在删除, 则添加
  353. if(empty($temp))
  354. {
  355. $data = array(
  356. 'mood_id' => $id,
  357. 'user_id' => $this->user['id'],
  358. 'add_time' => time(),
  359. );
  360. $state = ($m->add($data) > 0);
  361. } else {
  362. $state = ($m->where($where)->delete() !== false);
  363. }
  364. // 状态
  365. if($state)
  366. {
  367. $this->ajaxReturn(L('common_operation_success'));
  368. } else {
  369. $this->ajaxReturn(L('common_operation_error'), -100);
  370. }
  371. }
  372. /**
  373. * [MoodComments 说说评论]
  374. * @author Devil
  375. * @blog http://gong.gg/
  376. * @version 0.0.1
  377. * @datetime 2017-04-09T18:52:32+0800
  378. */
  379. public function MoodComments()
  380. {
  381. // 是否ajax请求
  382. if(!IS_AJAX)
  383. {
  384. $this->error(L('common_unauthorized_access'));
  385. }
  386. // 用户状态校验
  387. $this->UserIsStateCheck();
  388. // 说说评论模型
  389. $m = D('MoodComments');
  390. // 编辑
  391. if($m->create($_POST, 1) !== false)
  392. {
  393. // 不能回复自己的评论
  394. if($m->reply_id > 0)
  395. {
  396. $user_id = $m->where(array('id'=>$m->reply_id))->getField('user_id');
  397. if($user_id == $this->user['id'])
  398. {
  399. $this->ajaxReturn(L('bubble_comments_reply_error'), -2);
  400. }
  401. }
  402. // 额外数据处理
  403. $m->add_time = time();
  404. $m->content = I('content');
  405. $m->user_id = $this->user['id'];
  406. // 写入数据库
  407. if($m->add())
  408. {
  409. $this->ajaxReturn(L('common_operation_comments_success'));
  410. } else {
  411. $this->ajaxReturn(L('common_operation_comments_error'), -100);
  412. }
  413. } else {
  414. $this->ajaxReturn($m->getError(), -1);
  415. }
  416. }
  417. }
  418. ?>