Dynamic.php 15 KB

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