Operation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\api\controller\Base;
  16. use think\Db;
  17. use think\Model;
  18. /**
  19. * @title 操作管理(关注,评论,点赞,收藏。。。)
  20. * @controller Operation
  21. * @group member
  22. */
  23. class Operation extends Base
  24. {
  25. function initialize()
  26. {
  27. $this->check_login();
  28. }
  29. /**
  30. * @title 关注/取消关注
  31. * @desc 关注/取消关注
  32. * @author QGF
  33. * @url /api/Operation/attention
  34. * @method GET
  35. * @tag 关注/取消关注
  36. * @header name:Authorization require:1 desc:Token
  37. * @param name:user_id type:int require:1 default:-- desc:被关注/取消关注的用户ID/媒体ID(详情返回的user_id字段)
  38. * @param name:operation_type type:int require:1 default:-- desc:操作类型(0:取消关注,1:关注)
  39. */
  40. public function attention(){
  41. $uid = $this->uid;
  42. $user_id = input('user_id');
  43. $type = input('operation_type');
  44. if(empty($user_id) || !isset($type)){
  45. $this->error('参数错误');
  46. }
  47. if($uid == $user_id){
  48. $this->error('不能操作自己');
  49. }
  50. $attention = Db::name('store_attention')->where('user_id',$uid)->where('from_user_id',$user_id)->find();
  51. $msg = '关注成功';
  52. if($type == 0){ //取消关注
  53. if(empty($attention['status'])){
  54. $this->error('没关注无需取消');
  55. }
  56. Db::name('store_attention')->where('user_id',$uid)->where('from_user_id',$user_id)->update(array('status'=>0));
  57. $msg = '取消成功';
  58. }else{
  59. if(!empty($attention['status'])){
  60. $this->error('已关注无需继续关注');
  61. }
  62. if($attention['id']){
  63. Db::name('store_attention')->where('user_id',$uid)->where('from_user_id',$user_id)->update(array('status'=>1));
  64. }else{
  65. $data = array(
  66. 'user_id' => $uid,
  67. 'from_user_id' => $user_id,
  68. 'type' => user_type($user_id),
  69. 'status' => 1
  70. );
  71. Db::name('store_attention')->insert($data);
  72. }
  73. }
  74. $this->success($msg);
  75. }
  76. /**
  77. * @title 提交评论
  78. * @desc 提交评论
  79. * @author QGF
  80. * @url /api/Operation/submit_comment
  81. * @method GET
  82. * @tag 提交评论
  83. * @header name:Authorization require:1 desc:Token
  84. * @param name:type type:int require:1 default:-- desc:类型(1:新闻,2:咨询)
  85. * @param name:id type:int require:1 default:-- desc:新闻ID或咨询ID
  86. * @param name:content type:string require:1 default:-- desc:评论内容
  87. */
  88. public function submit_comment(){
  89. $uid = $this->uid;
  90. $type = input('type');
  91. $id = input('id');
  92. $content = input('content');
  93. if(empty($type) || empty($id) || empty($content)){
  94. $this->error('参数错误');
  95. }
  96. $table_nme = $type==1?'store_goods':'store_consult';
  97. $type_name = $type==1?'新闻':'咨询';
  98. $info = Db::name($table_nme)->field('user_id,title')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  99. if(empty($info)){
  100. $this->error($type_name.'信息有误');
  101. }
  102. if($uid == $info['user_id']){
  103. $this->error('不能操作自己');
  104. }
  105. $comment_info = Db::name('store_comment')->field('id')->where('user_id',$uid)->where('forum_id',$id)->where('type',1)->where('forum_type',$type)->find();
  106. if(!empty($comment_info)){
  107. $this->error('已评论过该'.$type_name);
  108. }
  109. $comment_data = array(
  110. 'user_id' => $uid,
  111. 'from_user_id' => $info['user_id'],
  112. 'forum_id' => $id,
  113. 'content' => $content,
  114. 'forum_type' => $type,
  115. 'type' => 1
  116. );
  117. Db::name('store_comment')->insert($comment_data);
  118. $comment_id = Db::name('store_comment')->getLastInsID();
  119. Db::name('store_comment')->where('id',$comment_id)->update(array('root_comment_id'=>$comment_id));
  120. $user_type = user_type($info['user_id']);
  121. if($user_type == 1){ //用户发表的咨询有评论的会给发表者发送信息通知
  122. $user_name = Db::name('store_member')->where('id',$uid)->value('name');
  123. $news_data = array(
  124. 'user_id' => $info['user_id'], //获得这个消息的会员
  125. 'content' => $user_name.'评论了你发表的['.$info['title'].']咨询',
  126. 'comment_user_id' => $uid,
  127. 'type' => 2,
  128. );
  129. Db::name('store_news')->insert($news_data);
  130. }
  131. $this->success('评论成功');
  132. }
  133. /**
  134. * @title 回复评论
  135. * @desc 回复评论
  136. * @author QGF
  137. * @url /api/Operation/reply_comment
  138. * @method GET
  139. * @tag 回复评论
  140. * @header name:Authorization require:1 desc:Token
  141. * @param name:comment_id type:int require:1 default:-- desc:要回复的评论ID
  142. * @param name:content type:string require:1 default:-- desc:评论内容
  143. */
  144. public function reply_comment(){
  145. $uid = $this->uid;
  146. $comment_id = input('comment_id');//要回复的评论ID
  147. $content = input('content');//回复评论的内容
  148. if(empty($content) || empty($comment_id)){
  149. $this->error('参数错误');
  150. }
  151. $comment = Db::name('store_comment')->field('forum_id,user_id,root_comment_id,forum_type')->where('id',$comment_id)->find();
  152. if(empty($comment)){
  153. $this->error('要回复的评论信息有误');
  154. }
  155. if($comment['user_id'] == $uid){
  156. $this->error('不能回复自己的评论');
  157. }
  158. //判断要是否回复过次评论了
  159. $reply_comment = Db::name('store_comment')->field('id')->where('user_id',$uid)->where('type',2)->where('comment_id',$comment_id)->find();
  160. if(!empty($reply_comment)){
  161. $this->error('已回复过此评论');
  162. }
  163. $root_comment_id = $comment['root_comment_id']?$comment['root_comment_id']:$comment_id;
  164. $comment_data = array(
  165. 'forum_id' => $comment['forum_id'],
  166. 'user_id' => $uid,
  167. 'from_user_id' => $comment['user_id'],
  168. 'content' => $content,
  169. 'comment_id' => $comment_id,
  170. 'forum_type' => $comment['forum_type'],
  171. 'root_comment_id' => $root_comment_id,
  172. 'type' => 2
  173. );
  174. Db::name('store_comment')->insert($comment_data);
  175. //回复的谁给谁消息通知
  176. $user_name = Db::name('store_member')->where('id',$uid)->value('name');
  177. $news_data = array(
  178. 'user_id' => $comment['user_id'], //获得这个消息的会员
  179. 'content' => $user_name.'回复的你的评论',
  180. 'comment_user_id' => $uid,
  181. 'type' => 2,
  182. );
  183. Db::name('store_news')->insert($news_data);
  184. $this->success('回复评论成功');
  185. }
  186. /**
  187. * @title 点赞/取消点赞
  188. * @desc 点赞/取消点赞
  189. * @author QGF
  190. * @url /api/Operation/like
  191. * @method GET
  192. * @tag 点赞/取消点赞
  193. * @header name:Authorization require:1 desc:Token
  194. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  195. * @param name:operation_type type:int require:1 default:-- desc:操作类型(0:取消点赞,1:点赞)
  196. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  197. */
  198. public function like(){
  199. $uid = $this->uid;
  200. $id = input('id');
  201. $operation_type = input('operation_type');
  202. $type = input('type');
  203. if(empty($id) || !isset($operation_type) || empty($type)){
  204. $this->error('参数错误');
  205. }
  206. $table_nme = $type==1?'store_goods':'store_consult';
  207. $type_name = $type==1?'新闻':'咨询';
  208. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  209. if(empty($info)){
  210. $this->error($type_name.'信息有误');
  211. }
  212. if($uid == $info['user_id']){
  213. $this->error('不能操作自己');
  214. }
  215. $like = Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->where('user_id',$uid)->find();
  216. $msg = '点赞成功';
  217. if($operation_type == 0){ //取消关注
  218. if(empty($like['status'])){
  219. $this->error('没点赞无需取消');
  220. }
  221. Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->update(array('status'=>0));
  222. $msg = '取消成功';
  223. }else{
  224. if(!empty($like['status'])){
  225. $this->error('已点赞无需继续点赞');
  226. }
  227. if($like['id']){
  228. Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>1));
  229. }else{
  230. $data = array(
  231. 'user_id' => $uid,
  232. 'from_user_id' => $info['user_id'],
  233. 'forum_id' => $id,
  234. 'type' => $type,
  235. 'status' => 1
  236. );
  237. Db::name('store_like')->insert($data);
  238. }
  239. }
  240. $this->success($msg);
  241. }
  242. /**
  243. * @title 收藏/取消收藏
  244. * @desc 收藏/取消收藏
  245. * @author QGF
  246. * @url /api/Operation/collect
  247. * @method GET
  248. * @tag 收藏/取消收藏
  249. * @header name:Authorization require:1 desc:Token
  250. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  251. * @param name:operation_type type:int require:1 default:-- desc:操作类型(0:取消收藏,1:收藏)
  252. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  253. */
  254. public function collect(){
  255. $uid = $this->uid;
  256. $id = input('id');
  257. $operation_type = input('operation_type');
  258. $type = input('type');
  259. if(empty($id) || !isset($operation_type) || empty($type)){
  260. $this->error('参数错误');
  261. }
  262. $table_nme = $type==1?'store_goods':'store_consult';
  263. $type_name = $type==1?'新闻':'咨询';
  264. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  265. if(empty($info)){
  266. $this->error($type_name.'信息有误');
  267. }
  268. if($uid == $info['user_id']){
  269. $this->error('不能操作自己');
  270. }
  271. $collect = Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->find();
  272. $msg = '收藏成功';
  273. if($operation_type == 0){ //取消收藏
  274. if(empty($collect['status'])){
  275. $this->error('没收藏无需取消');
  276. }
  277. Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>0));
  278. $msg = '取消成功';
  279. }else{
  280. if(!empty($collect['status'])){
  281. $this->error('已收藏无需继续点赞');
  282. }
  283. if($collect['id']){
  284. Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>1));
  285. }else{
  286. $data = array(
  287. 'user_id' => $uid,
  288. 'from_user_id' => $info['user_id'],
  289. 'forum_id' => $id,
  290. 'type' => $type,
  291. 'status' => 1
  292. );
  293. Db::name('store_collect')->insert($data);
  294. }
  295. }
  296. $this->success($msg);
  297. }
  298. /**
  299. * @title 转发
  300. * @desc 转发(转发成功调用此接口)
  301. * @author QGF
  302. * @url /api/Operation/transpond
  303. * @method GET
  304. * @tag 转发
  305. * @header name:Authorization require:1 desc:Token
  306. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  307. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  308. */
  309. public function transpond(){
  310. $uid = $this->uid;
  311. $id = input('id');
  312. $type = input('type');
  313. if(empty($id) || empty($type)){
  314. $this->error('参数错误');
  315. }
  316. $table_nme = $type==1?'store_goods':'store_consult';
  317. $type_name = $type==1?'新闻':'咨询';
  318. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  319. if(empty($info)){
  320. $this->error($type_name.'信息有误');
  321. }
  322. if($uid == $info['user_id']){
  323. $this->error('不能操作自己');
  324. }
  325. $data = array(
  326. 'user_id' => $uid,
  327. 'forum_id' => $id,
  328. 'type' => $type
  329. );
  330. Db::name('store_transpond')->insert($data);
  331. $this->success('操作成功');
  332. }
  333. }