ConfigCityManagementController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\ConfigCityManagement;
  4. use App\Transformers\CityTransformer;
  5. use Illuminate\Http\Request;
  6. use App\Http\ApiHelper;
  7. use Illuminate\Support\Facades\Validator;
  8. /**
  9. * Created by PhpStorm.
  10. * User: qinyaer
  11. * Date: 2019/4/23
  12. * Time: 下午3:56
  13. */
  14. class ConfigCityManagementController extends Controller
  15. {
  16. /**
  17. * @api {get} /city/lists 城市管理列表
  18. * @apiVersion 0.1
  19. * @apiName ConfigCityManagement lists
  20. * @apiGroup ConfigCityManagement
  21. * @apiPermission none
  22. * @apiSuccessExample 成功响应:
  23. {
  24. "data": [],
  25. "extra": {
  26. "filters": [
  27. "筛选字段1",
  28. "筛选字段2"
  29. ],
  30. "columns": [
  31. "列表显示数据字段1",
  32. "列表显示数据字段2"
  33. ]
  34. },
  35. "meta": {
  36. "pagination": {}
  37. }
  38. }
  39. */
  40. public function lists()
  41. {
  42. $cityList = ConfigCityManagement::orderBy('id', 'desc')
  43. ->paginate();
  44. if (count($cityList)>0){
  45. foreach ($cityList as $k=>$v){
  46. $cityList[$k]->express_type = $v->getExpressTypeAttribute();
  47. if ($v->status == 0){
  48. $cityList[$k]->status = '启用';
  49. }else{
  50. $cityList[$k]->status = '禁用';
  51. }
  52. }
  53. }
  54. return $this->response->paginator($cityList, new CityTransformer());
  55. }
  56. /**
  57. * @api {post} /city/edit 编辑城市
  58. * @apiVersion 0.1
  59. * @apiName ConfigCityManagement edit
  60. * @apiGroup ConfigCityManagement
  61. * @apiPermission none
  62. * @apiSuccessExample 成功响应:
  63. {
  64. "data": [],
  65. "extra": {
  66. "filters": [
  67. "筛选字段1",
  68. "筛选字段2"
  69. ],
  70. "columns": [
  71. "列表显示数据字段1",
  72. "列表显示数据字段2"
  73. ]
  74. },
  75. "meta": {
  76. "pagination": {}
  77. }
  78. }
  79. */
  80. public function edit(Request $request)
  81. {
  82. $data = $request->all();
  83. $data['id'] = $request->input('id');
  84. $cityManagement = ConfigCityManagement::where('id',$data['id'])->first();
  85. if (!$cityManagement) {
  86. return $this->response->array(ApiHelper::error('该城市不存在!', 402));
  87. }
  88. $rules = [
  89. 'province_name' => 'required',
  90. 'province_id' => 'required',
  91. 'city_id' => 'required',
  92. 'city_name' => 'required',
  93. ];
  94. $validator = Validator::make($data, $rules);
  95. if ($validator->fails()) {
  96. return $this->response->array(ApiHelper::error('请求参数格式不正确!', 412));
  97. }
  98. $city_management_update = $cityManagement->update($data);
  99. if (!$city_management_update){
  100. return $this->response->array(ApiHelper::error('修改失败,请重试!', 412));
  101. }
  102. return $this->response->array(ApiHelper::success());
  103. }
  104. /**
  105. * @api {post} /city/delete 删除城市
  106. * @apiVersion 0.1
  107. * @apiName ConfigCityManagement delete
  108. * @apiGroup ConfigCityManagement
  109. * @apiPermission none
  110. * @apiSuccessExample 成功响应:
  111. {
  112. "meta": {
  113. "message": "Success.",
  114. "status_code": 200
  115. },
  116. "data": {
  117. "id": "2"
  118. }
  119. }
  120. */
  121. public function delete(Request $request)
  122. {
  123. $id = $request->input('id');
  124. $cityManagement = ConfigCityManagement::find($id);
  125. if (!$cityManagement) {
  126. return $this->response->array(ApiHelper::error('该城市不存在!', 402));
  127. }
  128. $city_delete = $cityManagement->delete();
  129. if (!$city_delete) {
  130. return $this->response->array(ApiHelper::error('删除城市失败!', 500));
  131. }
  132. return $this->response->array(ApiHelper::success('Success.', 200, array('id' => $id)));
  133. }
  134. }