链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
7.0 KiB

5 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\ImsCjdcMarket;
  4. use App\Models\LanzuArea;
  5. use App\Models\LanzuMmInfo;
  6. use App\Models\v3\Market;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Controllers\AdminController;
  12. use App\Models\LanzuMpInfo;
  13. use Illuminate\Support\Facades\Log;
  14. class ImsCjdcMarketController extends AdminController
  15. {
  16. /**
  17. * Make a grid builder.
  18. *
  19. * @return Grid
  20. */
  21. protected function grid()
  22. {
  23. return Grid::make(new Market(), function (Grid $grid) {
  24. $adu = Admin::user();
  25. if ($adu->isRole('lanzu_mp')){
  26. $mp = LanzuMpInfo::where('admin_user_id',$adu->id)->first();
  27. $grid->model()->where('mp_id',$mp->id);
  28. $grid->disableCreateButton();
  29. $grid->disableActions();
  30. }
  31. $grid->id->sortable();
  32. $grid->logo->image('', 50, 50);
  33. $grid->name;
  34. $grid->column('mp_id', '服务商')->display(function ($mid) {
  35. if ($mid == 0) return '懒族自营';
  36. $mp = LanzuMpInfo::find($mid);
  37. if (!$mp) {
  38. return '<span style="color: #ff0000">数据错误</span>';
  39. }
  40. return $mp->name;
  41. });
  42. $grid->column('status', '状态')->filter(
  43. Grid\Column\Filter\In::make([
  44. 0 => '禁用',
  45. 1 => '正常',
  46. ])
  47. )->display(function ($status) {
  48. if ($status == 0) {
  49. return '禁用';
  50. } else {
  51. return '正常';
  52. }
  53. });
  54. $grid->address;
  55. $grid->column('status')->switch();
  56. $grid->created_at->display(function ($time){
  57. return date('Y-m-d H:i:s',$time);
  58. });
  59. $grid->filter(function (Grid\Filter $filter) {
  60. $filter->equal('id');
  61. });
  62. $grid->disableFilterButton();
  63. $grid->quickSearch(['name'])->placeholder('输入市场名称');
  64. $grid->actions(function (Grid\Displayers\Actions $actions) {
  65. if (!\Admin::user()->isAdministrator() && $actions->row->mp_id == 0) {
  66. $actions->disableDelete();
  67. $actions->disableEdit();
  68. }
  69. });
  70. });
  71. }
  72. /**
  73. * Make a show builder.
  74. *
  75. * @param mixed $id
  76. *
  77. * @return Show
  78. */
  79. protected function detail($id)
  80. {
  81. return Show::make($id, new Market(), function (Show $show) {
  82. if (!\Admin::user()->isAdministrator() && $show->model()->mp_id == 0) {
  83. $show->panel()
  84. ->tools(function ($tools) {
  85. $tools->disableEdit();
  86. $tools->disableList();
  87. $tools->disableDelete();
  88. // 显示快捷编辑按钮
  89. $tools->showQuickEdit();
  90. });
  91. }
  92. $show->name;
  93. $show->logo->image();
  94. $show->introduce;
  95. $show->imgs->image();
  96. $show->addtime('创建时间');
  97. $show->sort;
  98. $show->status()->using([0=>'禁用',1=>'正常']);
  99. $show->remark;
  100. $show->address;
  101. $show->created_at->as(function ($time){
  102. if ($time){
  103. return date('Y-m-d H:i',$time);
  104. }else{
  105. return '-';
  106. }
  107. });
  108. $show->updated_at->as(function ($time){
  109. if ($time){
  110. return date('Y-m-d H:i',$time);
  111. }else{
  112. return '-';
  113. }
  114. });
  115. });
  116. }
  117. /**
  118. * Make a form builder.
  119. *
  120. * @return Form
  121. */
  122. protected function form()
  123. {
  124. return Form::make(new Market(), function (Form $form) {
  125. $form->display('id');
  126. $form->text('name')->required();
  127. $form->select('mp_id', '服务商')->options('/api/getMpInfo');
  128. $form->mobile('tel','电话')->required();
  129. $form->select('province_id','省')
  130. ->options('/api/area')->required()
  131. ->load('city_id','/api/area');
  132. $form->select('city_id','市')->required()->load('region_id','/api/area');
  133. $form->select('region_id','区')->required();
  134. $form->text('address')->required();
  135. if ($form->isEditing()){
  136. $form->text('coordinates')
  137. ->value(implode(',',[$form->model()->lng,$form->model()->lat]))
  138. ->required()
  139. ->placeholder('输入 经纬度,如: 108.281552,22.83731')
  140. ->help("通过网址 <a href='https://lbs.amap.com/console/show/picker' target='_blank'>https://lbs.amap.com/console/show/picker</a> 获取经纬度");
  141. if (!($form->mp_id)){
  142. $form->mp_id = '0';
  143. }
  144. }else{
  145. $form->text('coordinates')
  146. ->required()
  147. ->placeholder('输入 经纬度,如: 108.281552,22.83731')
  148. ->help("通过网址 <a href='https://lbs.amap.com/console/show/picker' target='_blank'>https://lbs.amap.com/console/show/picker</a> 获取经纬度");
  149. }
  150. $form->number('sort', '排序');
  151. $form->switch('status', '状态');
  152. $form->image('logo')->required();
  153. $form->multipleImage('imgs', '市场图片');
  154. $form->textarea('remark')->required();
  155. $form->hidden('lng');
  156. $form->hidden('lat');
  157. $form->saving(function (Form $form){
  158. if($form->coordinates){
  159. $coordinates = explode(',',$form->coordinates);
  160. $form->lng = $coordinates[0];
  161. $form->lat = $coordinates[1];
  162. $form->deleteInput('coordinates');
  163. }
  164. });
  165. $form->deleting(function (Form $form){
  166. //删除市场前,校验该市场下是否存在市场经理
  167. $mids = array_column($form->model()->toArray(), 'id');
  168. $count = LanzuMmInfo::whereIn('market_id',$mids)->count();
  169. if ($count){
  170. return $form->error('该市场下关联有市场经理, 无法删除');
  171. }
  172. });
  173. });
  174. }
  175. /**
  176. * 服务商信息
  177. * @return \Illuminate\Http\JsonResponse
  178. */
  179. protected function getMpInfo()
  180. {
  181. $mps = LanzuMpInfo::all();
  182. $ret = [['id'=>'0','text'=>'懒族自营']];
  183. foreach ($mps as $key => $value) {
  184. $item = [];
  185. $item['id'] = $value->id;
  186. $item['text'] = $value->name;
  187. $ret[] = $item;
  188. }
  189. return response()->json($ret);
  190. }
  191. }