DailyController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Resources\Admin\DailyCollection;
  4. use App\Models\Daily\DailyPushModel;
  5. use App\Models\User\Openid;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. class DailyController extends Controller
  9. {
  10. /**
  11. * 列表
  12. * @param Request $request
  13. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. $articles = DailyPushModel::when($request->filled('date'), function ($query) use ($request) {
  18. return $query->where('date', $request->date);
  19. })->orderBy('date', 'desc')->paginate(10);
  20. return new DailyCollection($articles);
  21. }
  22. /**
  23. * 创建文章
  24. * @param Request $request
  25. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  26. */
  27. public function store(Request $request)
  28. {
  29. $this->validate($request, [
  30. 'push_content' => 'required',
  31. 'article_url' => 'required',
  32. 'reply_content' => 'required',
  33. 'date' => 'required|date',
  34. ]);
  35. if (DailyPushModel::where('date', $request->date)->first()) {
  36. return response([
  37. 'code' => 409,
  38. 'message' => '日期已存在'
  39. ]);
  40. }
  41. $article = DailyPushModel::create($request->all());
  42. return response([
  43. 'code' => 200,
  44. 'data' => $article
  45. ]);
  46. }
  47. /**
  48. * 修改文章
  49. * @param Request $request
  50. * @param $id
  51. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  52. */
  53. public function update(Request $request, $id)
  54. {
  55. $this->validate($request, [
  56. 'push_content' => '',
  57. 'article_url' => '',
  58. 'reply_content' => '',
  59. 'date' => 'date',
  60. ]);
  61. $article = DailyPushModel::findOrFail($id);
  62. if (DailyPushModel::where('date', $request->date)->where('id', '<>', $id)->first()) {
  63. return response([
  64. 'code' => 409,
  65. 'message' => '日期已存在'
  66. ]);
  67. }
  68. $article->update($request->all());
  69. return response([
  70. 'code' => 200,
  71. 'data' => $article
  72. ]);
  73. }
  74. /**
  75. * 删除
  76. * @param Request $request
  77. * @param $id
  78. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  79. */
  80. public function destroy(Request $request, $id)
  81. {
  82. $article = DailyPushModel::findOrFail($id);
  83. $article->delete();
  84. return response([
  85. 'code' => 200,
  86. 'message' => 'OK'
  87. ]);
  88. }
  89. /**
  90. * 发送预览
  91. * @param Request $request
  92. * @param $id
  93. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  94. */
  95. public function preview(Request $request, $id)
  96. {
  97. $this->validate($request, [
  98. 'uid' => 'required|integer',
  99. ]);
  100. $uid = $request->uid;
  101. $publicId = 'gh_b598cb7474d8';
  102. $openid = Openid::where('public_id', $publicId)->where('uid', $uid)->value('openid');
  103. $article = DailyPushModel::findOrFail($id);
  104. $content = sprintf(
  105. $article->content,
  106. "<a href=\"weixin://bizmsgmenu?msgmenucontent=每日&msgmenuid=234234\" >这里</a>"
  107. );
  108. $content .= "\n若不想收到每日精选,回复【JXTD】可进行退订~";
  109. $result = \WeChat::KfAccount($openid, 'text', [
  110. 'content' => $content
  111. ]);
  112. if ($result['code'] == 0) {
  113. return response([
  114. 'code' => 200,
  115. 'message' => $result['msg']
  116. ]);
  117. } else {
  118. return response([
  119. 'code' => 500,
  120. 'message' => $result['msg']
  121. ]);
  122. }
  123. }
  124. }