HeadlineController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/20
  6. * Time: 16:17
  7. */
  8. namespace App\Http\Controllers\Manage\SendOrder;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Controllers\Manage\SendOrder\Transformers\HeadlinelTransformer;
  11. use App\Modules\Promotion\Models\Headline;
  12. use App\Modules\Promotion\Services\PromotionService;
  13. use DB;
  14. use Illuminate\Http\Request;
  15. class HeadlineController extends Controller
  16. {
  17. /**
  18. * 标题
  19. */
  20. /**
  21. * @api {post} SendOrder/uploadHeadlins 导入标题
  22. * @apiParam {File} [headlines] 标题文件
  23. * @apiSuccessExample {json} Success-Response:
  24. *
  25. * {
  26. * "code": 0,
  27. * "msg": "",
  28. * "data":[]
  29. * }
  30. */
  31. function uploadHeadlins(Request $request)
  32. {
  33. if (!$request->hasFile('file')) {
  34. return response()->error('PARAM_EMPTY');
  35. }
  36. $file = $request->file('file');
  37. $currentIndex = 0;
  38. if (null != $file) {
  39. $file_name = date('YmdHis') . '.csv';
  40. $path = $request->file->storeAs('file', $file_name);
  41. $path = storage_path('app/' . $path);
  42. $file = fopen($path, "r");
  43. $serial_number = Headline::getMaxSerialNumber();
  44. while ($data = fgetcsv($file)) {
  45. if ($currentIndex > 0) {
  46. \Log::info($data);
  47. $title = mb_convert_encoding(trim($data[0]), "UTF-8", "GBK");
  48. $category = mb_convert_encoding(trim($data[1]), "UTF-8", "GBK");
  49. $remark = mb_convert_encoding(trim($data[2]), "UTF-8", "GBK");
  50. $type = mb_convert_encoding(trim($data[3]), "UTF-8", "GBK");
  51. $quality = mb_convert_encoding(trim($data[4]), "UTF-8", "GBK");
  52. $serial_number = $serial_number + 1;
  53. if (!$title || !$remark || !$category || !$serial_number || !$quality || !$type) {
  54. return response()->error('PARAM_EMPTY');
  55. }
  56. Headline::addHeadline(compact('title', 'category', 'remark', 'type', 'quality', 'serial_number'));
  57. }
  58. $currentIndex++;
  59. }
  60. return response()->success();
  61. }
  62. }
  63. /**
  64. * @api {post} SendOrder/deleteHeadlines 删除标题
  65. * @apiParam {Number} [id] 标题id(可不传,多个之间用逗号隔开)
  66. * @apiSuccessExample {json} Success-Response:
  67. *
  68. * {
  69. * "code": 0,
  70. * "msg": "",
  71. * "data":[]
  72. * }
  73. */
  74. function deleteHeadlines(Request $request)
  75. {
  76. $id = $request->has('id') ? $request->input('id') : '';
  77. if (empty($id)) {
  78. return response()->error("PARAM_EMPTY");
  79. }
  80. $ids = explode(',', $id);
  81. $result = Headline::delHeadlines($ids);
  82. if ($result) {
  83. return response()->success();
  84. }
  85. }
  86. /**
  87. * @api {GET} sendOrder/getHeadlines 获取标题
  88. * @apiParam {Number} [id] 标题id(可不传)
  89. * @apiParam {String} [remark] 标题的备注(可不传)
  90. * @apiParam {String} [quality] 质量(可不传)
  91. * @apiParam {Number} [type] 类型(1: 推文标题, 2:客服专用)(可不传)
  92. *
  93. * @apiSuccess {Number} id 标题id.
  94. * @apiSuccess {String} title 标题内容id
  95. * @apiSuccess {String} category 类型(男频、女频)
  96. * @apiSuccess {String} remark 备注
  97. * @apiSuccess {Number} serial_number 序号
  98. * @apiSuccess {String} updated_at 更新时间
  99. * @apiSuccess {String} created_at 创建时间
  100. * @apiSuccessExample {json} Success-Response:
  101. *
  102. * {
  103. * "code": 0,
  104. * "msg": "",
  105. * "data": [
  106. * {
  107. * "id": 5,
  108. * "title": "标题111",
  109. * "category": "男频",
  110. * "remark": "1",
  111. * "serial_number": 11111,
  112. * "updated_at": "2018-08-22 14:14:20",
  113. * "created_at": "2018-08-22 14:14:20"
  114. * },
  115. * {
  116. * "id": 6,
  117. * "title": "标题2222",
  118. * "category": "女频",
  119. * "remark": "2",
  120. * "serial_number": 111111,
  121. * "updated_at": "2018-08-22 14:14:20",
  122. * "created_at": "2018-08-22 14:14:20"
  123. * }
  124. * ],
  125. * "meta": {
  126. * "total": 1,
  127. * "per_page": 15,
  128. * "current_page": 1,
  129. * "last_page": 1,
  130. * "next_page_url": "",
  131. * "prev_page_url": ""
  132. * }
  133. * }
  134. */
  135. function getHeadlines(Request $request)
  136. {
  137. $id = $request->has('id') ? $request->input('id') : '';
  138. $remark = $request->has('remark') ? $request->input('remark') : '';
  139. $title = $request->has('title') ? $request->input('title') : '';
  140. $type = $request->has('type') ? $request->input('type') : '';
  141. $category = $request->has('category') ? $request->input('category') : '';
  142. $quality = $request->has('quality') ? $request->input('quality') : '';
  143. $param = [];
  144. if ($id) {
  145. $param['id'] = $id;
  146. }
  147. if ($remark) {
  148. $param['remark'] = $remark;
  149. }
  150. if ($title){
  151. $param['title'] = $title;
  152. }
  153. if ($type) {
  154. $param['type'] = $type;
  155. }
  156. if ($category) {
  157. $param['category'] = $category;
  158. }
  159. if ($quality) {
  160. $param['quality'] = $quality;
  161. }
  162. $result = PromotionService::getHeadlineByParams($param);
  163. return response()->pagination(new HeadlinelTransformer(), $result);
  164. }
  165. }