ProductController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Controllers\Manage\Book;
  3. use App\Modules\Book\Services\ProductService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. class ProductController extends Controller
  7. {
  8. /**
  9. * @apiDefine Product 产品/充值
  10. */
  11. /**
  12. * @apiVersion 1.0.0
  13. * @apiDescription 充值列表
  14. * @api {get} product/chargelists 充值列表
  15. * @apiGroup Product
  16. * @apiName getChargeLists
  17. * @apiSuccess {int} code 状态码
  18. * @apiSuccess {String} msg 信息
  19. * @apiSuccess {object} data 结果集
  20. * @apiSuccessExample {json} Success-Response:
  21. * HTTP/1.1 200 OK
  22. * {
  23. * code: 0,
  24. * msg: "",
  25. * data: [
  26. * {
  27. * product_id: 1,
  28. * price: "30.00",
  29. * given: 0,
  30. * is_default: 0,
  31. * type: "TICKET_RECHARGE"
  32. * },
  33. * {
  34. * product_id: 4,
  35. * price: "500.00",
  36. * given: 20000,
  37. * is_default: 0,
  38. * type: "TICKET_RECHARGE"
  39. * },
  40. * {
  41. * product_id: 5,
  42. * price: "365.00",
  43. * given: 0,
  44. * is_default: 1,
  45. * type: "TICKET_RECHARGE"
  46. * }
  47. * ]
  48. * }
  49. */
  50. public function getChargeLists(){
  51. $res = ProductService::getChargeProduct();
  52. return response()->collection(new ProductTransformer,$res);
  53. }
  54. /**
  55. * @apiVersion 1.0.0
  56. * @apiDescription 添加产品
  57. * @api {post} product 添加产品
  58. * @apiGroup Product
  59. * @apiName createProduct
  60. * @apiParam {float} price 价格
  61. * @apiParam {String} type 类型
  62. * @apiParam {int} given 奖励
  63. * @apiParam {int} is_enabled 是否启用
  64. * @apiParam {int} [sequence] 排序
  65. * @apiSuccess {int} code 状态码
  66. * @apiSuccess {String} msg 信息
  67. * @apiSuccess {object} data 结果集
  68. * @apiSuccessExample {json} Success-Response:
  69. * HTTP/1.1 200 OK
  70. * {
  71. * code: 0,
  72. * msg: "",
  73. * data:
  74. * {
  75. * product_id: 1,
  76. * price: "30.00",
  77. * given: 0,
  78. * is_default: 0,
  79. * type: "TICKET_RECHARGE"
  80. * }
  81. *
  82. * }
  83. */
  84. public function createProduct(Request $request){
  85. $param = $request->except('_url');
  86. if(checkParam($param,['price','type','given','is_enabled'])){
  87. return response()->error('PARAM_ERROR');
  88. }
  89. $param['is_default'] = 0;
  90. $res = ProductService::addProduct($param);
  91. return response()->item(new ProductTransformer,$res);
  92. }
  93. /**
  94. * @apiVersion 1.0.0
  95. * @apiDescription 编辑产品
  96. * @api {post} product/update 编辑产品
  97. * @apiGroup Product
  98. * @apiName updateProduct
  99. * @apiParam {int} is_default(1|0) 是否默认(只能有一个)
  100. * @apiParam {int} is_enabled(1|0) 是否启用
  101. * @apiSuccess {int} code 状态码
  102. * @apiSuccess {String} msg 信息
  103. * @apiSuccess {object} data 结果集
  104. * @apiSuccessExample {json} Success-Response:
  105. * HTTP/1.1 200 OK
  106. * {
  107. * code: 0,
  108. * msg: "",
  109. * data: [
  110. * {
  111. * product_id: 1,
  112. * price: "30.00",
  113. * given: 0,
  114. * is_default: 0,
  115. * type: "TICKET_RECHARGE"
  116. * },
  117. * {
  118. * product_id: 4,
  119. * price: "500.00",
  120. * given: 20000,
  121. * is_default: 0,
  122. * type: "TICKET_RECHARGE"
  123. * },
  124. * {
  125. * product_id: 5,
  126. * price: "365.00",
  127. * given: 0,
  128. * is_default: 1,
  129. * type: "TICKET_RECHARGE"
  130. * }
  131. * ]
  132. * }
  133. */
  134. public function updateProduct(Request $request){
  135. $id = $request->input('product_id');
  136. if(empty($id)) return response()->error('PARAM_ERROR');
  137. $default = $request->input('is_default');
  138. $is_enabled = $request->input('is_enabled');
  139. if($default == '' && $is_enabled == ''){
  140. return response()->error('PARAM_ERROR');
  141. }
  142. ProductService::updateProductDefault($id, $default);
  143. ProductService::updateProductEnabled($id, $is_enabled);
  144. return response()->success();
  145. }
  146. /**
  147. * @apiVersion 1.0.0
  148. * @apiDescription 根据id获取产品
  149. * @api {get} product/{id} 根据id获取产品
  150. * @apiGroup Product
  151. * @apiName getProductInfoById
  152. * @apiSuccess {int} code 状态码
  153. * @apiSuccess {String} msg 信息
  154. * @apiSuccess {object} data 结果集
  155. * @apiSuccess {Int} product_id product_id
  156. * @apiSuccess {float} price 价格
  157. * @apiSuccess {String} type 类型
  158. * @apiSuccess {int} given 奖励
  159. * @apiSuccessExample {json} Success-Response:
  160. * HTTP/1.1 200 OK
  161. * {
  162. * code: 0,
  163. * msg: "",
  164. * data:
  165. * {
  166. * product_id: 1,
  167. * price: "30.00",
  168. * given: 0,
  169. * is_default: 0,
  170. * type: "TICKET_RECHARGE"
  171. * }
  172. *
  173. * }
  174. */
  175. public function getProductInfoById(Request $request,$id){
  176. $res = ProductService::getProductSingle($id);
  177. return response()->item(new ProductTransformer,$res);
  178. }
  179. }