Storezuobiao.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 门店服务范围
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Storezuobiao extends Backend
  11. {
  12. /**
  13. * Storezuobiao模型对象
  14. * @var \app\admin\model\Storezuobiao
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Storezuobiao;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //当前是否为关联查询
  33. $this->relationSearch = true;
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. $store_id = input('store_id',0);
  37. $this->assignconfig('store_id',$store_id);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->with(['company','store'])
  46. ->where($where)
  47. ->where('storezuobiao.store_id',$store_id)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('company')->visible(['name']);
  52. $row->getRelation('store')->visible(['name']);
  53. }
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加
  61. *
  62. * @return string
  63. * @throws \think\Exception
  64. */
  65. public function add()
  66. {
  67. $store_id = input('store_id',0);
  68. $company_id = Db::name('store')->where('id',$store_id)->value('company_id');
  69. if (false === $this->request->isPost()) {
  70. $this->assign('store_id',$store_id);
  71. $this->assign('company_id',$company_id);
  72. return $this->view->fetch();
  73. }
  74. $params = $this->request->post('row/a');
  75. if (empty($params)) {
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. $params = $this->preExcludeFields($params);
  79. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  80. $params[$this->dataLimitField] = $this->auth->id;
  81. }
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. //是否采用模型验证
  86. if ($this->modelValidate) {
  87. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  88. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  89. $this->model->validateFailException()->validate($validate);
  90. }
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException|PDOException|Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result === false) {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. $this->success();
  101. }
  102. /**
  103. * 设置服务范围地图
  104. */
  105. public function zuobiao(){
  106. $id = input('id');
  107. $this->assign('id',$id);
  108. $zuobiao = Db::name('store_zuobiao')->where('id',$id)->value('zuobiao');
  109. $zuobiao = json_decode($zuobiao,true);
  110. $this->assign('zuobiao',$zuobiao);
  111. $center = isset($zuobiao[0]) ? $zuobiao[0] : '';
  112. $this->assign('center',$center);
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 保存坐标
  117. */
  118. public function zuobiaosubmit(){
  119. $id = input('id');
  120. $zuobiao = input('zuobiao');
  121. $zuobiao = json_decode($zuobiao,true);
  122. $new_zuobiao = [];
  123. foreach($zuobiao as $key => $val){
  124. $new_zuobiao[$key] = [$val['lng'],$val['lat']];
  125. }
  126. $new_zuobiao = json_encode($new_zuobiao);
  127. $rs = Db::name('store_zuobiao')->where('id',$id)->update(['zuobiao'=>$new_zuobiao]);
  128. $this->success('success');
  129. }
  130. }