Agentshop.php 9.0 KB
<?php

namespace app\admin\controller\agent;

use app\common\controller\Backend;
use think\Db;
use app\api\controller\v1\WxXcxQrcode;

/**
 * 企业门店管理
 *
 * @icon fa fa-circle-o
 */
class Agentshop extends Backend
{
    protected $noNeedRight = ['index'];
    /**
     * Agentshop模型对象
     * @var \app\admin\model\agent\Agentshop
     */
    protected $model = null;
    protected $dataLimit = 'auth';
    protected $dataLimitField = 'admin_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\agent\Agentshop;
        $this->view->assign("agentStuList", $this->model->getAgentStuList());
        $this->view->assign("payStuList", $this->model->getPayStuList());
        //选择的企业id
        $agentListIds = $this->request->param('ids', 0);
        $ginfo = Db::name('agent_list')
            ->where(['id' => $agentListIds])
            ->find();
        $this->view->assign("ginfo", $ginfo);
        //判断是否是超级管理员
        if (in_array($this->auth->id, [1, 2, 26, 32, 37])) {
            $isSup = 1;
        } else {
            $isSup = 0;
        }
        $this->view->assign("isSup", $isSup);
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $ids = $this->request->param('ids');
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparamsAdm();

            $list = $this->model
                ->with(['agentlist'])
                ->where(['agent_list_id' => $ids])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'agent_list_id', 'shop_name', 'head_image', 'createtime', 'updatetime', 'shop_fr_name', 'shop_fr_mobile', 'shop_fzr_name', 'shop_fzr_mobile', 'address', 'latitude', 'longitude', 'add_day', 'card_image', 'cert_images', 'cert_desc', 'agent_content', 'agent_stu', 'credit']);
                $row->visible(['agentlist']);
                $row->getRelation('agentlist')->visible(['agent_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['admin_id'] = agent_list_admin_id($params['agent_list_id']);
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;

                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);


                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if ($params['agent_stu'] == "1") {
                        //审核通过;生成小程序二维码
                        $qrcode_api = new WxXcxQrcode();
                        $path = '/pages/home/company/storeDetail?id=' . $this->model->id;
                        $qrcode = $qrcode_api->qrcode($this->model->id, $path, true);
                        if (!empty($qrcode)) {
                            Db::name("agent_shop")->where("id", $this->model->id)->update(["qrcode" => $qrcode, 'updatetime' => time()]);
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        /*if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }*/
        if (!in_array($this->auth->id, [1, 2, 26, 32, 37])) {
            if (is_array($adminIds)) {
                if (!in_array($row[$this->dataLimitField], $adminIds)) {
                    $this->error(__('You have no permission'));
                }
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['admin_id'] = agent_list_admin_id($params['agent_list_id']);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    if ($params['agent_stu'] == "1") {
                        //审核通过;生成小程序二维码
                        $qrcode_api = new WxXcxQrcode();
                        $path = '/pages/home/company/storeDetail?id=' . $row->id;
                        $params['qrcode'] = $qrcode_api->qrcode($row->id, $path, true);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', [1, 2, 26, 32, 37]);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}