Operation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if($type == 0){ //取消关注
  52. if(empty($attention['status'])){
  53. $this->error('没关注无需取消');
  54. }
  55. Db::name('store_attention')->where('user_id',$uid)->where('from_user_id',$user_id)->update(array('status'=>0));
  56. }else{
  57. if(!empty($attention['status'])){
  58. $this->error('已关注无需继续关注');
  59. }
  60. if($attention['id']){
  61. Db::name('store_attention')->where('user_id',$uid)->where('from_user_id',$user_id)->update(array('status'=>1));
  62. }else{
  63. $data = array(
  64. 'user_id' => $uid,
  65. 'from_user_id' => $user_id,
  66. 'type' => user_type($user_id),
  67. 'status' => 1
  68. );
  69. Db::name('store_attention')->insert($data);
  70. }
  71. }
  72. $this->success('操作成功');
  73. }
  74. /**
  75. * @title 提交评论
  76. * @desc 提交评论
  77. * @author QGF
  78. * @url /api/Operation/submit_comment
  79. * @method GET
  80. * @tag 提交评论
  81. * @header name:Authorization require:1 desc:Token
  82. * @param name:type type:int require:1 default:-- desc:类型(1:新闻,2:咨询)
  83. * @param name:id type:int require:1 default:-- desc:新闻ID或咨询ID
  84. * @param name:content type:string require:1 default:-- desc:评论内容
  85. */
  86. public function submit_comment(){
  87. $uid = $this->uid;
  88. $type = input('type');
  89. $id = input('id');
  90. $content = input('content');
  91. if(empty($type) || empty($id) || empty($content)){
  92. $this->error('参数错误');
  93. }
  94. $table_nme = $type==1?'store_goods':'store_consult';
  95. $type_name = $type==1?'新闻':'咨询';
  96. $info = Db::name($table_nme)->field('user_id,title')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  97. if(empty($info)){
  98. $this->error($type_name.'信息有误');
  99. }
  100. if($uid == $info['user_id']){
  101. $this->error('不能操作自己');
  102. }
  103. $comment_info = Db::name('store_comment')->field('id')->where('user_id',$uid)->where('forum_id',$id)->where('type',1)->where('forum_type',$type)->find();
  104. if(!empty($comment_info)){
  105. $this->error('已评论过该'.$type_name);
  106. }
  107. $comment_data = array(
  108. 'user_id' => $uid,
  109. 'from_user_id' => $info['user_id'],
  110. 'forum_id' => $id,
  111. 'content' => $content,
  112. 'forum_type' => $type,
  113. 'type' => 1
  114. );
  115. Db::name('store_comment')->insert($comment_data);
  116. $comment_id = Db::name('store_comment')->getLastInsID();
  117. Db::name('store_comment')->where('id',$comment_id)->update(array('root_comment_id'=>$comment_id));
  118. $user_type = user_type($info['user_id']);
  119. if($user_type == 1){ //用户发表的咨询有评论的会给发表者发送信息通知
  120. $user_name = Db::name('store_member')->where('id',$uid)->value('name');
  121. $news_data = array(
  122. 'user_id' => $info['user_id'], //获得这个消息的会员
  123. 'content' => $user_name.'评论了你发表的'.$info['title'].'咨询',
  124. 'comment_user_id' => $uid,
  125. 'type' => 2,
  126. );
  127. Db::name('store_news')->insert($news_data);
  128. }
  129. $this->success('评论成功');
  130. }
  131. /**
  132. * @title 回复评论
  133. * @desc 回复评论
  134. * @author QGF
  135. * @url /api/Operation/reply_comment
  136. * @method GET
  137. * @tag 回复评论
  138. * @header name:Authorization require:1 desc:Token
  139. * @param name:comment_id type:int require:1 default:-- desc:要回复的评论ID
  140. * @param name:content type:string require:1 default:-- desc:评论内容
  141. */
  142. public function reply_comment(){
  143. $uid = $this->uid;
  144. $comment_id = input('comment_id');//要回复的评论ID
  145. $content = input('content');//回复评论的内容
  146. if(empty($content) || empty($comment_id)){
  147. $this->error('参数错误');
  148. }
  149. $comment = Db::name('store_comment')->field('forum_id,user_id,root_comment_id')->where('id',$comment_id)->find();
  150. if(empty($comment)){
  151. $this->error('要回复的评论信息有误');
  152. }
  153. if($comment['user_id'] == $uid){
  154. $this->error('不能回复自己的评论');
  155. }
  156. //判断要是否回复过次评论了
  157. $reply_comment = Db::name('store_comment')->field('id')->where('user_id',$uid)->where('type',2)->where('comment_id',$comment_id)->find();
  158. if(!empty($reply_comment)){
  159. $this->error('已回复过此评论');
  160. }
  161. $root_comment_id = $comment['root_comment_id']?$comment['root_comment_id']:$comment_id;
  162. $comment_data = array(
  163. 'forum_id' => $comment['forum_id'],
  164. 'user_id' => $uid,
  165. 'from_user_id' => $comment['user_id'],
  166. 'content' => $content,
  167. 'comment_id' => $comment_id,
  168. 'root_comment_id' => $root_comment_id,
  169. 'type' => 2
  170. );
  171. Db::name('store_comment')->insert($comment_data);
  172. //回复的谁给谁消息通知
  173. $user_name = Db::name('store_member')->where('id',$uid)->value('name');
  174. $news_data = array(
  175. 'user_id' => $comment['user_id'], //获得这个消息的会员
  176. 'content' => $user_name.'回复的你的评论',
  177. 'comment_user_id' => $uid,
  178. 'type' => 2,
  179. );
  180. Db::name('store_news')->insert($news_data);
  181. $this->success('回复评论成功');
  182. }
  183. /**
  184. * @title 点赞/取消点赞
  185. * @desc 点赞/取消点赞
  186. * @author QGF
  187. * @url /api/Operation/like
  188. * @method GET
  189. * @tag 点赞/取消点赞
  190. * @header name:Authorization require:1 desc:Token
  191. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  192. * @param name:operation_type type:int require:1 default:-- desc:操作类型(0:取消点赞,1:点赞)
  193. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  194. */
  195. public function like(){
  196. $uid = $this->uid;
  197. $id = input('id');
  198. $operation_type = input('operation_type');
  199. $type = input('type');
  200. if(empty($id) || !isset($operation_type) || empty($type)){
  201. $this->error('参数错误');
  202. }
  203. $table_nme = $type==1?'store_goods':'store_consult';
  204. $type_name = $type==1?'新闻':'咨询';
  205. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  206. if(empty($info)){
  207. $this->error($type_name.'信息有误');
  208. }
  209. if($uid == $info['user_id']){
  210. $this->error('不能操作自己');
  211. }
  212. $like = Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->where('user_id',$uid)->find();
  213. if($operation_type == 0){ //取消关注
  214. if(empty($like['status'])){
  215. $this->error('没点赞无需取消');
  216. }
  217. Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->update(array('status'=>0));
  218. }else{
  219. if(!empty($like['status'])){
  220. $this->error('已点赞无需继续点赞');
  221. }
  222. if($like['id']){
  223. Db::name('store_like')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>1));
  224. }else{
  225. $data = array(
  226. 'user_id' => $uid,
  227. 'from_user_id' => $info['user_id'],
  228. 'forum_id' => $id,
  229. 'type' => $type,
  230. 'status' => 1
  231. );
  232. Db::name('store_like')->insert($data);
  233. }
  234. }
  235. $this->success('操作成功');
  236. }
  237. /**
  238. * @title 收藏/取消收藏
  239. * @desc 收藏/取消收藏
  240. * @author QGF
  241. * @url /api/Operation/collect
  242. * @method GET
  243. * @tag 收藏/取消收藏
  244. * @header name:Authorization require:1 desc:Token
  245. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  246. * @param name:operation_type type:int require:1 default:-- desc:操作类型(0:取消收藏,1:收藏)
  247. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  248. */
  249. public function collect(){
  250. $uid = $this->uid;
  251. $id = input('id');
  252. $operation_type = input('operation_type');
  253. $type = input('type');
  254. if(empty($id) || !isset($operation_type) || empty($type)){
  255. $this->error('参数错误');
  256. }
  257. $table_nme = $type==1?'store_goods':'store_consult';
  258. $type_name = $type==1?'新闻':'咨询';
  259. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  260. if(empty($info)){
  261. $this->error($type_name.'信息有误');
  262. }
  263. if($uid == $info['user_id']){
  264. $this->error('不能操作自己');
  265. }
  266. $collect = Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->find();
  267. if($operation_type == 0){ //取消收藏
  268. if(empty($collect['status'])){
  269. $this->error('没收藏无需取消');
  270. }
  271. Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>0));
  272. }else{
  273. if(!empty($collect['status'])){
  274. $this->error('已收藏无需继续点赞');
  275. }
  276. if($collect['id']){
  277. Db::name('store_collect')->where('user_id',$uid)->where('forum_id',$id)->where('type',$type)->update(array('status'=>1));
  278. }else{
  279. $data = array(
  280. 'user_id' => $uid,
  281. 'from_user_id' => $info['user_id'],
  282. 'forum_id' => $id,
  283. 'type' => $type,
  284. 'status' => 1
  285. );
  286. Db::name('store_collect')->insert($data);
  287. }
  288. }
  289. $this->success('操作成功');
  290. }
  291. /**
  292. * @title 转发
  293. * @desc 转发(转发成功调用此接口)
  294. * @author QGF
  295. * @url /api/Operation/transpond
  296. * @method GET
  297. * @tag 转发
  298. * @header name:Authorization require:1 desc:Token
  299. * @param name:id type:int require:1 default:-- desc:新闻或咨询ID
  300. * @param name:type type:int require:1 default:-- desc:信息类型(1:新闻,2:咨询)
  301. */
  302. public function transpond(){
  303. $uid = $this->uid;
  304. $id = input('id');
  305. $type = input('type');
  306. if(empty($id) || empty($type)){
  307. $this->error('参数错误');
  308. }
  309. $table_nme = $type==1?'store_goods':'store_consult';
  310. $type_name = $type==1?'新闻':'咨询';
  311. $info = Db::name($table_nme)->field('user_id')->where('id',$id)->where('status',1)->where('is_deleted',0)->find();
  312. if(empty($info)){
  313. $this->error($type_name.'信息有误');
  314. }
  315. if($uid == $info['user_id']){
  316. $this->error('不能操作自己');
  317. }
  318. $data = array(
  319. 'user_id' => $uid,
  320. 'forum_id' => $id,
  321. 'type' => $type
  322. );
  323. Db::name('store_transpond')->insert($data);
  324. $this->success('操作成功');
  325. }
  326. }