| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace app\admin\controller\stock;
- use app\common\controller\Backend;
- use fast\Tree;
- /**
- * 客户管理
- *
- * @icon fa fa-circle-o
- */
- class Customer extends Backend {
- /**
- * StockCustomer模型对象
- * @var \app\admin\model\StockCustomer
- */
- protected $model = null;
- protected $noNeedLogin = ['madeEncode', 'getwdTree'];
- public function _initialize() {
- parent::_initialize();
- $this->model = model('\app\admin\model\stock\Customer');
- $customerList = collection($this->model->order('weigh')->select())->toArray();
- Tree::instance()->init($customerList);
- $customer = [];
- $this->customerList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
- $primary = array(array('name' => '==请选择==', 'id' => '0'));
- $result = array_merge_recursive($primary, $this->customerList);
- foreach ($result as $k => $v) {
- $customer[$v['id']] = $v['name'];
- }
- $this->view->assign('customer', $customer);
- $this->view->assign("enabledmarkList", $this->model->getEnabledmarkList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index() {
- if ($this->request->isAjax()) {
- $list = $this->customerList;
- $total = count($this->customerList);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add() {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false) {
- $this->success();
- } else {
- $this->error($this->model->getError());
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $result = $this->model->where('pid', 0)->order('encode desc')->limit(1)->select();
- if ($result) {
- $suffix = (int) ($result[0]['encode']);
- $suffix ++;
- } else {
- $suffix = 0;
- }
- $encode = sprintf("%02d", $suffix);
- $this->view->assign('encode', $encode);
- return $this->view->fetch();
- }
- /**
- * 根据父级部门生成部门代码
- */
- public function madeEncode() {
- if ($this->request->isPost()) {
- $id = $this->request->post('id');
- if ($id || $id == 0) {
- try {
- $result = $this->model->where('pid', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
- $Encode = '';
- if (count($result) > 0) {
- $EncodeArr = explode('.', $result[0]['Encode']);
- $EncodeArr[count($EncodeArr) - 1] = sprintf("%03d", $EncodeArr[count($EncodeArr) - 1] + 1);
- $Encode = implode('.', $EncodeArr);
- } else {
- $result = $this->model->where('id', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
- if (count($result) > 0) {
- $Encode = $result[0]['Encode'] . '.001';
- } else {
- $Encode = '001';
- }
- }
- return json($Encode);
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->error('只接受POST请求', '');
- }
- /**
- * 获取部门树
- */
- public function getwdTree() {
- if ($this->auth->isSuperAdmin()) {
- $categoryList = collection($this->model->select())->toArray();
- } else {
- $categoryList = collection($this->model->where('id', $this->auth->customerid)->select())->toArray();
- $categoryList[0]['pid'] = 0;
- }
- Tree::instance()->init($categoryList);
- $result = Tree::instance()->getwdTreeArray(0, 'name');
- return json($result);
- }
- }
|