DiaryController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace App\Http\Controllers\Diary;
  3. use App\Http\Controllers\Core\Auth;
  4. use App\Http\Resources\Diary\DiaryCollection;
  5. use App\Http\Resources\Diary\DiaryResource;
  6. use App\Models\Diary\DiaryModel;
  7. use App\Models\Diary\ThumbModel;
  8. use App\Models\User\UserModel;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use Tymon\JWTAuth\Exceptions\JWTException;
  12. class DiaryController extends Controller
  13. {
  14. private $admins = [10001, 10002, 10007, 11111, 62726, 91031, 94302, 98047, 103143, 762663, 795723, 10771042];
  15. /**
  16. * 获取用户信息
  17. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  18. * @throws JWTException
  19. */
  20. public function me()
  21. {
  22. $uid = Auth::auth();
  23. $user = UserModel::select('nickname', 'headimgurl', 'sex')->findOrFail($uid);
  24. $user->is_admin = in_array($uid, $this->admins);
  25. return response([
  26. 'code' => 200,
  27. 'message' => 'OK',
  28. 'data' => $user
  29. ]);
  30. }
  31. /**
  32. * 日记列表
  33. * @param Request $request
  34. * @return DiaryCollection
  35. */
  36. public function index(Request $request)
  37. {
  38. try {
  39. $uid = Auth::auth();
  40. } catch (JWTException $e) {
  41. $uid = 0;
  42. }
  43. $diaries = DiaryModel::where(function ($query) use ($uid) {
  44. // 显示私有或我的
  45. return $query->where('private', 0)
  46. ->orWhere('uid', $uid);
  47. })->when($request->get('topping'), function ($query) {
  48. // 置顶的
  49. return $query->where('topped_at', '>', time() - 24 * 3600);
  50. })->when($request->get('selecting'), function ($query) {
  51. // 精选的
  52. return $query->where('selected_at', '>', time() - 24 * 3600);
  53. })->whereNull('violationed_at')
  54. ->OrderBy('created_at', 'desc')
  55. ->paginate($request->get('per_page', 20));
  56. foreach ($diaries as $diary) {
  57. $diary->user;
  58. $diary->self_thumb = ThumbModel::where('uid', $uid)->where('diary_id', $diary->id)->count() ? true : false;
  59. $diary->self = ($uid == $diary->uid) ? true : false;
  60. $diary->created_time_ago = $diary->created_at->diffForHumans();
  61. }
  62. return new DiaryCollection($diaries);
  63. }
  64. /**
  65. * 日记信息
  66. * @param Request $request
  67. * @param $id
  68. * @return DiaryResource
  69. */
  70. public function show(Request $request, $id)
  71. {
  72. try {
  73. $uid = Auth::auth();
  74. } catch (JWTException $e) {
  75. $uid = 0;
  76. }
  77. $diary = DiaryModel::findOrFail($id);
  78. $diary->user;
  79. $diary->self_thumb = ThumbModel::where('uid', $uid)->where('diary_id', $diary->id)->count() ? true : false;
  80. $diary->self = ($uid == $diary->uid) ? true : false;
  81. $diary->created_time_ago = $diary->created_at->diffForHumans();
  82. return new DiaryResource($diary);
  83. }
  84. /**
  85. * 创建日记
  86. * @param Request $request
  87. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  88. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  89. */
  90. public function store(Request $request)
  91. {
  92. $this->validate($request, [
  93. 'images' => '',
  94. 'content' => 'required',
  95. 'private' => 'integer',
  96. ]);
  97. $uid = Auth::auth();
  98. if (
  99. $last_at = DiaryModel::where('uid', $uid)->where('created_at', '>', time() - 60)->orderBy(
  100. 'created_at',
  101. 'desc'
  102. )->value('created_at')
  103. ) {
  104. return response([
  105. 'code' => 429001,
  106. 'message' => "操作发送频繁,请一分钟后再发"
  107. ]);
  108. }
  109. if (strstr($request->images, ",")) {
  110. list(, $image) = explode(',', $request->images);
  111. } else {
  112. $image = $request->images;
  113. }
  114. $filename = storage_path() . '/app/' . uniqid() . '.png';
  115. \File::put($filename, base64_decode($image));
  116. $response = \Curl::to("http://www.koudaidaxue.com/Tools/Upload/upFile")
  117. ->withData([
  118. 'file' => "@{$filename}"
  119. ])
  120. ->withFile('file', $filename)
  121. ->asJsonResponse()
  122. ->post();
  123. \File::delete($filename);
  124. if ($response->errcode == 0) {
  125. $image = $response->errmsg->cdn_site . $response->errmsg->file;
  126. } else {
  127. $image = $request->input('images', '');
  128. }
  129. $data = ($request->only('content', 'mood', 'private', 'uid'));
  130. $data = array_merge($data, ['uid' => $uid, 'images' => $image]);
  131. DiaryModel::create($data);
  132. return response([
  133. 'code' => 200,
  134. 'message' => 'OK'
  135. ]);
  136. }
  137. /**
  138. * 删除日记
  139. * @param Request $request
  140. * @param $id
  141. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  142. * @throws JWTException
  143. */
  144. public function destroy(Request $request, $id)
  145. {
  146. $diary = DiaryModel::find($id);
  147. $uid = Auth::auth();
  148. if ($uid == $diary->uid) {
  149. $diary->delete();
  150. return response([
  151. 'code' => 200,
  152. 'message' => 'OK'
  153. ]);
  154. }
  155. // 管理员可删除
  156. if (in_array($uid, $this->admins)) {
  157. $diary->violationed_at = time();
  158. $diary->save();
  159. return response([
  160. 'code' => 200,
  161. 'message' => 'OK'
  162. ]);
  163. }
  164. return response([
  165. 'code' => 403001,
  166. 'message' => '非作者不可删除'
  167. ]);
  168. }
  169. /**
  170. * 精选
  171. * @param Request $request
  172. * @param $id
  173. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  174. * @throws JWTException
  175. */
  176. public function setSelecting(Request $request, $id)
  177. {
  178. $uid = Auth::auth();
  179. $diary = DiaryModel::findOrFail($id);
  180. if (!in_array($uid, $this->admins)) {
  181. return response([
  182. 'code' => 403500,
  183. 'message' => '无权限操作'
  184. ]);
  185. }
  186. if ($diary->selected_at > 0) {
  187. $diary->selected_at = 0;
  188. } else {
  189. $diary->selected_at = time();
  190. }
  191. $diary->save();
  192. return response([
  193. 'code' => 200,
  194. 'message' => 'OK'
  195. ]);
  196. }
  197. /**
  198. * 置顶
  199. * @param Request $request
  200. * @param $id
  201. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  202. * @throws JWTException
  203. */
  204. public function setTopping(Request $request, $id)
  205. {
  206. $uid = Auth::auth();
  207. $diary = DiaryModel::findOrFail($id);
  208. if (!in_array($uid, $this->admins)) {
  209. return response([
  210. 'code' => 403500,
  211. 'message' => '无权限操作'
  212. ]);
  213. }
  214. if ($diary->topped_at > 0) {
  215. $diary->topped_at = 0;
  216. } else {
  217. $diary->topped_at = time();
  218. }
  219. $diary->save();
  220. return response([
  221. 'code' => 200,
  222. 'message' => 'OK'
  223. ]);
  224. }
  225. /**
  226. * 点赞or取消赞
  227. *
  228. * @param Request $request
  229. * @param $diary_id 日记ID
  230. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  231. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  232. */
  233. public function thumbing(Request $request, $id)
  234. {
  235. $uid = Auth::auth();
  236. \DB::beginTransaction();
  237. try {
  238. $diary = DiaryModel::findOrFail($id);
  239. $thumb = ThumbModel::where(['uid' => $uid, 'diary_id' => $diary->id])->first();
  240. if ($thumb) {
  241. $thumb->delete();
  242. $diary->decrement('thumb');
  243. } else {
  244. $diary->increment('thumb');
  245. ThumbModel::create([
  246. 'uid' => $uid,
  247. 'diary_id' => $diary->id
  248. ]);
  249. }
  250. \DB::commit();
  251. } catch (\Exception $e) {
  252. \DB::rollBack();
  253. }
  254. return response([
  255. 'code' => 200,
  256. 'message' => 'OK'
  257. ]);
  258. }
  259. }