Dynamic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 动态接口
  7. * 本文件已作废,topichub 和 topicdongtai 完全替代了动态功能
  8. */
  9. class Dynamic extends Api
  10. {
  11. protected $noNeedLogin = [''];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 发布动态
  15. */
  16. public function addDynamic() {
  17. $content = $this->request->request('content'); // 动态内容
  18. $image = $this->request->request('image'); // 图片/视频/语音说明
  19. $image_thumb = $this->request->request('image_thumb'); // 视频的图片缩略图
  20. $res_type = $this->request->request('res_type',1); // 1=图片,2=视频,3=音频
  21. if (!$content) {
  22. $this->error(__('请填写内容!'));
  23. }
  24. if (!$image) {
  25. $this->error(__('请上传图片/视频/语音内容'));
  26. }
  27. $dynamicModel = new \app\common\model\Dynamic();
  28. Db::startTrans();
  29. try{
  30. // 添加动态
  31. $data = [];
  32. $data["user_id"] = $this->auth->id;
  33. $data["content"] = $content;
  34. $data["image"] = $image;
  35. $data["image_thumb"] = $image_thumb;
  36. $data["res_type"] = $res_type;
  37. $data["createtime"] = time();
  38. $res = $dynamicModel->insertGetId($data);
  39. if($res) {
  40. Db::commit();
  41. $this->success("动态发布成功,审核成功后即可显示在动态列表哦!");
  42. } else {
  43. $this->success("网络错误,请稍后重试!");
  44. }
  45. }catch (ValidateException $e) {
  46. Db::rollback();
  47. $this->error($e->getMessage());
  48. } catch (PDOException $e) {
  49. Db::rollback();
  50. $this->error($e->getMessage());
  51. } catch (Exception $e) {
  52. Db::rollback();
  53. $this->error($e->getMessage());
  54. }
  55. }
  56. /**
  57. * 删除动态
  58. */
  59. public function delDynamic() {
  60. $id = $this->request->request('id',0,"intval"); // 动态id
  61. if (!$id) {
  62. $this->error(__('参数有误!'));
  63. }
  64. $dynamicModel = new \app\common\model\Dynamic();
  65. $dyInfo = $dynamicModel->where(["id"=>$id,"user_id"=>$this->auth->id])->find();
  66. if(!$dyInfo) {
  67. $this->error(__('信息不存在!'));
  68. }
  69. if($dyInfo->delete()) {
  70. $this->success("删除成功!");
  71. } else {
  72. $this->error("删除失败!");
  73. }
  74. }
  75. /**
  76. * 不感兴趣
  77. */
  78. public function noLike() {
  79. $id = $this->request->request('id',0,"intval"); // 动态id
  80. if(!$id) $this->error("参数缺失!");
  81. $user_id = $this->auth->id;
  82. $res = \app\common\model\DynamicNolike::insert(["user_id"=>$user_id,"dynamic_id"=>$id]);
  83. if($res) {
  84. $this->success("添加成功!");
  85. } else {
  86. $this->error("网络错误,请稍后重试!");
  87. }
  88. }
  89. /**
  90. * 获取黑名单用户列表
  91. */
  92. public function getBlacklist() {
  93. $user_id = $this->auth->id;
  94. // 获取黑名单用户
  95. $blacklistids = \app\common\model\UserBlacklist::where(["user_id"=>$user_id])->column("black_user_id");
  96. $this->success("获取成功!",$blacklistids);
  97. }
  98. /**
  99. * 获取动态列表
  100. */
  101. public function getDynamicList() {
  102. $type = $this->request->request('type',1); // 获取类型:1=推荐2=关注
  103. $page = $this->request->request('page',1); // 分页
  104. $pageNum = $this->request->request('pageNum',10); // 分页
  105. // 分页搜索构建
  106. $pageStart = ($page-1)*$pageNum;
  107. $this_user_id = $this->auth->id;
  108. // 获取所有当前用户关注列表
  109. $fansfollowModel = new \app\common\model\UserFansFollow();
  110. $ids = $fansfollowModel->where(["fans_id"=>$this_user_id])->column("user_id");
  111. $ids || $ids = [0];
  112. // 获取不感兴趣内容
  113. $nolikeids = \app\common\model\DynamicNolike::where(["user_id"=>$this_user_id])->column("dynamic_id");
  114. // 获取黑名单用户
  115. $blacklistids = \app\common\model\UserBlacklist::where(["user_id"=>$this_user_id])->column("black_user_id");
  116. $dynamicModel = new \app\common\model\Dynamic();
  117. $where = [];
  118. $type == 1 && $where["a.is_recommend"] = 1;
  119. $type == 2 && $where["a.user_id"] = ["in",$ids];
  120. $nolikeids && $where["a.id"] = ["not in",$nolikeids];
  121. $type != 2 && $blacklistids && $where["a.user_id"] = ["not in",$blacklistids];
  122. $where["a.status"] = 1;
  123. $dynamicList = $dynamicModel->alias("a")
  124. ->field("a.id,a.user_id,u.avatar,u.nickname,u.level,u.gender,a.content,a.res_type,a.image,a.image_thumb,a.commit,a.likes")
  125. ->join("hx_user u","u.id = a.user_id")
  126. ->where($where)
  127. ->limit($pageStart,$pageNum)
  128. ->order("a.createtime","desc")
  129. ->select();
  130. if(!$dynamicList) {
  131. $this->success("数据为空!",[]);
  132. }
  133. $dynamiclikesModel = new \app\common\model\DynamicLikes();
  134. // 获取我的点赞列表
  135. $likesList = $dynamiclikesModel->where(["user_id"=>$this_user_id])->select();
  136. $likesArr = [];
  137. if($likesList) {
  138. foreach($likesList as $k => $v) {
  139. $likesArr[$v["dynamic_id"]] = $v["user_id"];
  140. }
  141. }
  142. foreach($dynamicList as $k => $v) {
  143. // 判断当前用户是否点赞
  144. $dynamicList[$k]["is_likes"] = 0;
  145. if ($likesArr && isset($likesArr[$v["id"]])) {
  146. if ($likesArr[$v["id"]] == $this_user_id) $dynamicList[$k]["is_likes"] = 1;
  147. }
  148. if(in_array($v["user_id"],$ids)) {
  149. $dynamicList[$k]["is_follow"] = 1;
  150. } else {
  151. $dynamicList[$k]["is_follow"] = 0;
  152. }
  153. $v["image"] && $dynamicList[$k]["image"] = explode(",",$v["image"]);
  154. }
  155. $this->success("获取成功!",$dynamicList);
  156. }
  157. /**
  158. * 添加评论
  159. */
  160. public function addCommit() {
  161. $dynamic_id = $this->request->request('dynamic_id'); // 动态id
  162. $content = $this->request->request('content'); // 评论内容
  163. if (!$dynamic_id && !$content) {
  164. $this->error(__('Invalid parameters'));
  165. }
  166. $dynamiccommitModel = new \app\common\model\DynamicCommit();
  167. Db::startTrans();
  168. try{
  169. // 添加动态评论
  170. $data = [];
  171. $data["user_id"] = $this->auth->id;
  172. $data["dynamic_id"] = $dynamic_id;
  173. $data["content"] = $content;
  174. $data["createtime"] = time();
  175. $res1 = $dynamiccommitModel->insertGetId($data);
  176. // 更新动态评论数
  177. $res2 = \app\common\model\Dynamic::where(["id"=>$dynamic_id])->setInc("commit");
  178. if($res1 && $res2) {
  179. Db::commit();
  180. $this->success("评论成功!");
  181. } else {
  182. $this->success("网络错误,请稍后重试!");
  183. }
  184. }catch (ValidateException $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. } catch (PDOException $e) {
  188. Db::rollback();
  189. $this->error($e->getMessage());
  190. } catch (Exception $e) {
  191. Db::rollback();
  192. $this->error($e->getMessage());
  193. }
  194. }
  195. /**
  196. * 获取评论列表
  197. */
  198. public function getCommitList() {
  199. $dynamic_id = $this->request->request('dynamic_id'); // 动态ID
  200. $page = $this->request->request('page',1); // 分页
  201. $pageNum = $this->request->request('pageNum',10); // 分页
  202. // 分页搜索构建
  203. $pageStart = ($page-1)*$pageNum;
  204. if (!$dynamic_id) {
  205. $this->error(__('Invalid parameters'));
  206. }
  207. $dynamiccommitModel = new \app\common\model\DynamicCommit();
  208. $where = [];
  209. $where["dynamic_id"] = $dynamic_id;
  210. $dynamiccomitList = $dynamiccommitModel->alias("a")
  211. ->field("a.id,a.user_id,u.avatar,u.nickname,a.content,a.createtime")
  212. ->join("hx_user u","u.id = a.user_id")
  213. ->where($where)
  214. ->limit($pageStart,$pageNum)
  215. ->order("a.createtime","desc")
  216. ->select();
  217. if(!$dynamiccomitList) {
  218. $this->success("数据为空!",[]);
  219. }
  220. foreach($dynamiccomitList as $k => $v) {
  221. // 评论时间转换
  222. $dynamiccomitList[$k]["createtime"] = $this->get_last_time($v["createtime"]);
  223. }
  224. $this->success("获取成功!",$dynamiccomitList);
  225. }
  226. /**
  227. * 评论时间转换
  228. * @param null $time
  229. * @return false|string
  230. */
  231. private function get_last_time($time = NULL) {
  232. $text = '';
  233. $time = $time === NULL || $time > time() ? time() : intval($time);
  234. $t = time() - $time; //时间差 (秒)
  235. $y = date('Y', $time)-date('Y', time());//是否跨年
  236. switch($t){
  237. case $t == 0:
  238. $text = '刚刚';
  239. break;
  240. case $t < 60:
  241. $text = $t . '秒前'; // 一分钟内
  242. break;
  243. case $t < 60 * 60:
  244. $text = floor($t / 60) . '分钟前'; //一小时内
  245. break;
  246. case $t < 60 * 60 * 24:
  247. $text = floor($t / (60 * 60)) . '小时前'; // 一天内
  248. break;
  249. case $t < 60 * 60 * 24 * 3:
  250. $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
  251. break;
  252. case $t < 60 * 60 * 24 * 30:
  253. $text = date('m月d日 H:i', $time); //一个月内
  254. break;
  255. case $t < 60 * 60 * 24 * 365&&$y==0:
  256. $text = date('m月d日', $time); //一年内
  257. break;
  258. default:
  259. $text = date('Y年m月d日', $time); //一年以前
  260. break;
  261. }
  262. return $text;
  263. }
  264. /**
  265. * 添加点赞
  266. */
  267. public function addLikes() {
  268. $dynamic_id = $this->request->request('dynamic_id'); // 动态id
  269. if (!$dynamic_id) {
  270. $this->error(__('Invalid parameters'));
  271. }
  272. $dynamiclikesModel = new \app\common\model\DynamicLikes();
  273. $where = [];
  274. $where["user_id"] = $this->auth->id;
  275. $where["dynamic_id"] = $dynamic_id;
  276. if($dynamiclikesModel->where($where)->find()) $this->error("您已经点赞过了!");
  277. Db::startTrans();
  278. try{
  279. // 添加动态评论
  280. $data = [];
  281. $data["user_id"] = $this->auth->id;
  282. $data["dynamic_id"] = $dynamic_id;
  283. $data["createtime"] = time();
  284. $res1 = $dynamiclikesModel->insertGetId($data);
  285. // 更新动态评论数
  286. $res2 = \app\common\model\Dynamic::where(["id"=>$dynamic_id])->setInc("likes");
  287. if($res1 && $res2) {
  288. // +exp
  289. \app\common\model\TaskLog::tofinish($this->auth->id,"VpXtablCsZ",1);
  290. Db::commit();
  291. $this->success("点赞成功!");
  292. } else {
  293. $this->success("网络错误,请稍后重试!");
  294. }
  295. }catch (ValidateException $e) {
  296. Db::rollback();
  297. $this->error($e->getMessage());
  298. } catch (PDOException $e) {
  299. Db::rollback();
  300. $this->error($e->getMessage());
  301. } catch (Exception $e) {
  302. Db::rollback();
  303. $this->error($e->getMessage());
  304. }
  305. }
  306. /**
  307. * 获取我的动态列表 - 个人中心
  308. */
  309. public function getMyDynamicList() {
  310. $user_id = $this->request->request('user_id',0); // 用户ID(不传或传0或空时为查询当前用户技能信息)
  311. $page = $this->request->request('page',1); // 分页
  312. $pageNum = $this->request->request('pageNum',10); // 分页
  313. // 分页搜索构建
  314. $pageStart = ($page-1)*$pageNum;
  315. $this_user_id = $this->auth->id;
  316. $dynamicModel = new \app\common\model\Dynamic();
  317. $dynamiclikesModel = new \app\common\model\DynamicLikes();
  318. $where = [];
  319. $where["user_id"] = $user_id>0?$user_id:$this_user_id;
  320. if($this_user_id != $user_id){
  321. $where["status"] = 1;
  322. }
  323. $dynamicList = $dynamicModel->where($where)->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  324. if(!$dynamicList) {
  325. $this->success("数据为空!",[]);
  326. }
  327. // 获取我的点赞列表
  328. $likesList = $dynamiclikesModel->where(["user_id"=>$this_user_id])->select();
  329. $likesArr = [];
  330. if($likesList) {
  331. foreach($likesList as $k => $v) {
  332. $likesArr[$v["dynamic_id"]] = $v["user_id"];
  333. }
  334. }
  335. $data = [];
  336. foreach($dynamicList as $k => $v) {
  337. // 判断当前用户是否点赞
  338. $dynamicList[$k]["is_likes"] = 0;
  339. if ($user_id > 0 && $likesArr && isset($likesArr[$v["id"]])) {
  340. if ($likesArr[$v["id"]] == $this_user_id) $dynamicList[$k]["is_likes"] = 1;
  341. }
  342. $time = $this->get_last_time_ext($v["createtime"]);
  343. $v["image"] && $dynamicList[$k]["image"] = explode(",",$v["image"]);
  344. $data[$time][] = $v;
  345. }
  346. $timedata = [];
  347. foreach($data as $k => $v) {
  348. $timedata[] = [
  349. "time" => $k,
  350. "data" => $v
  351. ];
  352. }
  353. $this->success("获取成功!",$timedata);
  354. }
  355. /**
  356. * 评论时间转换
  357. * @param null $time
  358. * @return false|string
  359. */
  360. private function get_last_time_ext($time = NULL) {
  361. $time = $time === NULL || $time > time() ? time() : intval($time);
  362. $t = time() - $time; //时间差 (秒)
  363. $y = date('Y', $time)-date('Y', time());//是否跨年
  364. switch($t){
  365. case $t < 60 * 60 * 24:
  366. $text = '今天'; // 一天内
  367. break;
  368. case $t < 60 * 60 * 24 * 3:
  369. $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
  370. break;
  371. case $t < 60 * 60 * 24 * 30:
  372. $text = date('m月d日', $time); //一个月内
  373. break;
  374. case $t < 60 * 60 * 24 * 365&&$y==0:
  375. $text = date('m月d日', $time); //一年内
  376. break;
  377. default:
  378. $text = date('Y年m月d日', $time); //一年以前
  379. break;
  380. }
  381. return $text;
  382. }
  383. }