WelfareController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Http\Controllers\Manage\Welfare;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Manage\Welfare\Transformer\WelfarePromotionBookTransform;
  5. use App\Modules\Welfare\Models\WelfareBook;
  6. use App\Modules\Welfare\Services\WelfareBookService;
  7. use Cookie;
  8. use DB;
  9. use Illuminate\Http\Request;
  10. use OSS\Core\OssException;
  11. use OSS\OssClient;
  12. use Storage;
  13. class WelfareController extends Controller
  14. {
  15. /**
  16. * @apiDefine Welfare 福利
  17. */
  18. /**
  19. * @apiVersion 1.0.0
  20. * @api {POST} Welfare/addWelfarePromotionBook 添加活动推广书籍
  21. * @apiGroup Welfare
  22. * @apiName addWelfarePromotionBook
  23. * @apiParam {String} bid 书本id
  24. * @apiParam {String} book_name 书本名称
  25. * @apiParam {String} charge_rate 付费率
  26. * @apiParam {String} is_important 是否是重要渠道(1:是,0:否)
  27. * @apiParam {String} channer_user_id 渠道id
  28. * @apiParam {String} force_subscribe_rate 强关率
  29. * @apiParam {String} data_img 数据图片url
  30. * @apiSuccessExample {json} Success-Response:
  31. *
  32. * {
  33. * "code": 0,
  34. * "msg": "",
  35. * "data":{ }
  36. * }
  37. */
  38. public function add_welfare_promotion_book(Request $request)
  39. {
  40. // $data_img = $request->hasFile('data_img') ? $request->file('data_img') : null;
  41. $data_img = $request->has('data_img') ? $request->input('data_img') : '';
  42. $is_enable = 1;
  43. $bid = $request->has('bid') ? $request->input('bid') : '';
  44. $book_name = $request->has('book_name') ? $request->input('book_name') : '';
  45. $charge_rate = $request->has('charge_rate') ? $request->input('charge_rate') : '';
  46. $is_important = $request->has('is_important') ? $request->input('is_important') : '';
  47. $channer_user_id = $request->has('channer_user_id') ? $request->input('channer_user_id') : 0;
  48. $force_subscribe_rate = $request->has('force_subscribe_rate') ? $request->input('force_subscribe_rate') : '';
  49. if (!$bid || !$book_name || !$charge_rate || !is_numeric($is_important) || !$force_subscribe_rate) {
  50. return response()->error('PARAM_EMPTY');
  51. }
  52. if ($data_img) {
  53. // $file_path = $data_img->path();
  54. // $extension = $data_img->extension();
  55. // $file_name = date('YmdHis') . $bid . '.' . $extension;
  56. // $result = $this->ossObject()->uploadFile(env('OSS_BUCKET', 'zhuishuyun'), 'distribution/welfare/' . $file_name, $file_path);
  57. // $imageUrl = str_ireplace('zhuishuyun.oss-cn-hangzhou.aliyuncs.com', 'cdn-novel.iycdm.com', $result['oss-request-url']);
  58. // $data_img = str_ireplace('http://', 'https://', $imageUrl);
  59. $welfareBook = WelfareBook::createWelfareBook(compact('bid', 'book_name', 'charge_rate', 'data_img', 'is_enable', 'is_important', 'channer_user_id', 'force_subscribe_rate'));
  60. if ($welfareBook) {
  61. return response()->success();
  62. } else {
  63. return response()->error("HANDLE_FAILED");
  64. }
  65. } else {
  66. return response()->error('PARAM_EMPTY');
  67. }
  68. }
  69. private function ossObject()
  70. {
  71. $accessKeyId = env('OSS_ACCESS_ID');
  72. $accessKeySecret = env('OSS_ACCESS_KEY');
  73. $endpoint = env('OSS_END_POINT');
  74. $ossClient = null;
  75. try {
  76. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  77. } catch (OssException $e) {
  78. return null;
  79. }
  80. return $ossClient;
  81. }
  82. /**
  83. * @apiVersion 1.0.0
  84. * @api {get} Welfare/getWelfarePromotionBook 添加活动推广书籍
  85. * @apiGroup Welfare
  86. * @apiName getWelfarePromotionBook
  87. * @apiSuccess {String} bid 书本id
  88. * @apiSuccess {String} book_name 书本名称
  89. * @apiSuccess {String} charge_rate 付费率
  90. * @apiSuccess {String} is_important 是否是重要渠道(1:是,0:否)
  91. * @apiSuccess {String} channer_user_id 渠道id
  92. * @apiSuccess {String} updated_at 更新时间
  93. * @apiSuccess {String} created_at 创建时间
  94. * @apiSuccess {String} force_subscribe_rate 强关率
  95. * @apiSuccess {String} data_img 数据图片url
  96. * @apiSuccessExample {json} Success-Response:
  97. *
  98. * {
  99. * "code": 0,
  100. * "msg": "",
  101. * "data":
  102. * [
  103. * {
  104. * " " bid": "11111",
  105. * "book_name": "图书1111",
  106. * "charge_rate":"75%"
  107. * "is_important": "1",
  108. * "channer_user_id": 1111,
  109. * "force_subscribe_rate": "9%",
  110. * "created_at": "2018-6-16 12:12;12"
  111. * "updated_at": "2018-6-16 12:12;12"
  112. * "data_img": "http://www.baidu.com",
  113. * },
  114. * {
  115. * "bid": "11111",
  116. * "book_name": "图书1111",
  117. * "charge_rate":"75%"
  118. * "is_important": "1",
  119. * "channer_user_id": 1111,
  120. * "created_at": "2018-6-16 12:12;12"
  121. * "updated_at": "2018-6-16 12:12;12"
  122. * "force_subscribe_rate": "9%",
  123. * "data_img": "http://www.baidu.com",
  124. * }.
  125. * ]
  126. * }
  127. */
  128. public function get_welfare_promotion_books(Request $request)
  129. {
  130. $is_important_channel = $request->has('is_important') ? $request->input('is_important') : 0;
  131. $id = $request->has('id') ? $request->input('id') : 0;
  132. $params = ['is_important' => $is_important_channel, 'id' => $id];
  133. $data = WelfareBookService::getWelfarePromotionBooks($params, false);
  134. return response()->pagination(new WelfarePromotionBookTransform(), $data);
  135. }
  136. /**
  137. * @api {GET} Welfare/exportWelfarePromotionBook 导出活动推广书籍
  138. * @apiSuccess {String} bid 书本id
  139. * @apiSuccess {String} book_name 书本名称
  140. * @apiSuccess {String} charge_rate 付费率
  141. * @apiSuccess {String} is_important 是否是重要渠道(1:是,0:否)
  142. * @apiSuccess {String} channer_user_id 渠道id
  143. * @apiSuccess {String} updated_at 更新时间
  144. * @apiSuccess {String} created_at 创建时间
  145. * @apiSuccess {String} force_subscribe_rate 强关率
  146. * @apiSuccess {String} data_img 数据图片url
  147. * @apiSuccessExample {json} Success-Response:
  148. * {
  149. * "code": 0,
  150. * "msg": "",
  151. * "data":
  152. * [
  153. * {
  154. * " " bid": "11111",
  155. * "book_name": "图书1111",
  156. * "charge_rate":"75%"
  157. * "is_important": "1",
  158. * "channer_user_id": 1111,
  159. * "force_subscribe_rate": "9%",
  160. * "created_at": "2018-6-16 12:12;12"
  161. * "updated_at": "2018-6-16 12:12;12"
  162. * "data_img": "http://www.baidu.com",
  163. * },
  164. * {
  165. * "bid": "11111",
  166. * "book_name": "图书1111",
  167. * "charge_rate":"75%"
  168. * "is_important": "1",
  169. * "channer_user_id": 1111,
  170. * "created_at": "2018-6-16 12:12;12"
  171. * "updated_at": "2018-6-16 12:12;12"
  172. * "force_subscribe_rate": "9%",
  173. * "data_img": "http://www.baidu.com",
  174. * }.
  175. * ]
  176. * }
  177. * @return mixed
  178. */
  179. function export_welfare_promotion_books(Request $request)
  180. {
  181. $is_important_channel = $request->has('is_important') ? $request->input('is_important') : 0;
  182. $params = ['is_important' => $is_important_channel];
  183. $data = WelfareBookService::getWelfarePromotionBooks($params, true);
  184. header("Content-type:application/vnd.ms-excel");
  185. header("Content-Disposition:attachment;filename=" . "推广书籍信息" . date("YmdHis") . ".csv");
  186. echo iconv("UTF-8", "GBK", "\"id\",\"图书id\",\"图书名称\",\"付费率\",\"强关率\",\"创建时间\",\"最近修改时间\",\"是否为重要渠道\",\"数据图片url\"\r\n");
  187. if ($data) {
  188. foreach ($data as $item) {
  189. echo("\"" . iconv("UTF-8", "GBK", ($item->id ? $item->id : "未填")) . "\",");
  190. echo("\"" . iconv("UTF-8", "GBK", ($item->bid ? $item->bid : "未填")) . "\",");
  191. echo("\"" . iconv("UTF-8", "GBK", ($item->book_name ? $item->book_name : "未填")) . "\",");
  192. echo("\"" . iconv("UTF-8", "GBK", ($item->charge_rate ? $item->charge_rate : "未填")) . "\",");
  193. echo("\"" . iconv("UTF-8", "GBK", ($item->force_subscribe_rate ? $item->force_subscribe_rate : "未填")) . "\",");
  194. echo("\"" . iconv("UTF-8", "GBK", ($item->created_at->toDateTimeString())) . "\",");
  195. echo("\"" . iconv("UTF-8", "GBK", ($item->updated_at->toDateTimeString())) . "\",");
  196. echo("\"" . iconv("UTF-8", "GBK", ($item->is_important ? $item->is_important : "未填")) . "\",");
  197. echo("\"" . iconv("UTF-8", "GBK", ($item->data_img ? $item->data_img : "未填")) . "\"\r\n");
  198. }
  199. }
  200. exit();
  201. }
  202. /**
  203. * @apiVersion 1.0.0
  204. * @api {POST} Welfare/updateWelfarePromotionBook 添加活动推广书籍
  205. * @apiGroup Welfare
  206. * @apiName updateWelfarePromotionBook
  207. * @apiParam {String} bid 书本id [必传]
  208. * @apiParam {Number} channer_user_id 渠道id [必传]
  209. *
  210. * @apiParam {Number} is_enable 是否可用(1: 可用,0:不可用) [可不传]
  211. * @apiParam {String} book_name 书本名称 [可不传]
  212. * @apiParam {String} charge_rate 付费率 [可不传]
  213. * @apiParam {String} is_important 是否是重要渠道(1:是,0:否) [可不传]
  214. * @apiParam {String} force_subscribe_rate 强关率 [可不传]
  215. * @apiParam {String} data_img 数据图片url [可不传]
  216. * @apiSuccessExample {json} Success-Response:
  217. *
  218. * {
  219. * "code": 0,
  220. * "msg": "",
  221. * "data":{ }
  222. * }
  223. */
  224. function updateWelfarePromotionBook(Request $request)
  225. {
  226. $id = $request->has('id') ? $request->input('id') : '';
  227. $data_img = $request->has('data_img') ? $request->input('data_img') : '';
  228. $is_enable = $request->has('is_enable') ? $request->input('is_enable') : '';
  229. $bid = $request->has('bid') ? $request->input('bid') : '';
  230. $book_name = $request->has('book_name') ? $request->input('book_name') : '';
  231. $charge_rate = $request->has('charge_rate') ? $request->input('charge_rate') : '';
  232. $is_important = $request->has('is_important') ? $request->input('is_important') : '';
  233. $force_subscribe_rate = $request->has('force_subscribe_rate') ? $request->input('force_subscribe_rate') : '';
  234. if (!$id && !$data_img && !$book_name && !$charge_rate && !is_numeric($is_important) && !$force_subscribe_rate) {
  235. return response()->error('PARAM_EMPTY');
  236. }
  237. $params = ['data_img' => $data_img, 'is_enable' => $is_enable, 'book_name' => $book_name,
  238. 'charge_rate' => $charge_rate, 'is_important' => $is_important, 'force_subscribe_rate' => $force_subscribe_rate];
  239. $result = WelfareBookService::updateWelfarePromotionBook($id, $params);
  240. if ($result) {
  241. return response()->success();
  242. } else {
  243. return response()->error("HANDLE_FAILED");
  244. }
  245. }
  246. }