Company.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\adminapi\controller\v1\company;
  3. use app\adminapi\controller\AuthController;
  4. use crmeb\services\{UtilService as Util};
  5. use app\models\section\SectionModel;
  6. use app\models\company\CompanyModel;
  7. use app\models\company\ExampleModel;
  8. use app\models\company\DesignerModel;
  9. use think\Request;
  10. class Company extends AuthController
  11. {
  12. /**
  13. * 显示资源列表
  14. *
  15. * @return \think\Response
  16. */
  17. public function index()
  18. {
  19. $where = Util::getMore([
  20. ['title', ''],
  21. ['sid', ''],
  22. ['page', 1],
  23. ['limit', 10]
  24. ], $this->request);
  25. $list = CompanyModel::systemPage($where);
  26. return $this->success($list);
  27. }
  28. /**
  29. * 保存新建的资源
  30. *
  31. * @param \think\Request $request
  32. * @return \think\Response
  33. */
  34. public function save(Request $request)
  35. {
  36. $data = Util::postMore([
  37. ['id', 0],
  38. ['sid', ''],
  39. ['title', ''],
  40. ['logo', ''],
  41. ['intro', ''],
  42. ['contact', ''],
  43. ['phone', ''],
  44. ['project', ''],
  45. ['slider_image', ''],
  46. ['sort', 0],
  47. ['status', 1],
  48. ['example', []],
  49. ['designer', []],
  50. ]);
  51. if (!$data['title']) return $this->fail('缺少参数');
  52. $data['slider_image'] = json_encode($data['slider_image']);
  53. $example = $data['example'];
  54. $designer = $data['designer'];
  55. unset($data['example'], $data['designer']);
  56. if ($data['id']) {
  57. $id = $data['id'];
  58. unset($data['id']);
  59. $res = CompanyModel::edit($data, $id, 'id');
  60. foreach($example as $key => $value){
  61. $example[$key]['cid'] = !empty($value['cid']) ? $value['cid'] : $id;
  62. }
  63. foreach($designer as $key => $value){
  64. $designer[$key]['cid'] = !empty($value['cid']) ? $value['cid'] : $id;
  65. }
  66. ExampleModel::saveExample($example, $id);
  67. DesignerModel::saveDesigner($designer, $id);
  68. return $this->success('修改成功!', ['id' => $id]);
  69. } else {
  70. $data['add_time'] = time();
  71. $res = CompanyModel::create($data);
  72. foreach($example as $key => $value){
  73. $example[$key]['cid'] = (int)($res['id']);
  74. }
  75. foreach($designer as $key => $value){
  76. $designer[$key]['cid'] = (int)($res['id']);
  77. }
  78. ExampleModel::saveExample($example, $res['id']);
  79. DesignerModel::saveDesigner($designer, $res['id']);
  80. return $this->success('添加成功!', ['id' => $res->id]);
  81. }
  82. }
  83. /**
  84. * 显示指定的资源
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function read($id)
  90. {
  91. $info = CompanyModel::getOne($id);
  92. return $this->success(compact('info'));
  93. }
  94. /**
  95. * 删除指定资源
  96. *
  97. * @param int $id
  98. * @return \think\Response
  99. */
  100. public function delete($id)
  101. {
  102. if (!$id) return $this->fail('数据不存在');
  103. $res = CompanyModel::get($id);
  104. if (!$res) return $this->fail('数据不存在!');
  105. if ($res['is_del']) return $this->fail('已删除!');
  106. $data['is_del'] = 1;
  107. if (!CompanyModel::edit($data, $id))
  108. return $this->fail(CompanyModel::getErrorInfo('删除失败,请稍候再试!'));
  109. else
  110. return $this->success('删除成功!');
  111. }
  112. /**
  113. * 修改状态
  114. * @param $id
  115. * @param $status
  116. * @return mixed
  117. */
  118. public function set_status($id, $status)
  119. {
  120. if ($status == '' || $id == 0) return $this->fail('参数错误');
  121. CompanyModel::where(['id' => $id])->update(['status' => $status]);
  122. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  123. }
  124. }