Customer.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 客户管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Customer extends Backend {
  11. /**
  12. * StockCustomer模型对象
  13. * @var \app\admin\model\StockCustomer
  14. */
  15. protected $model = null;
  16. protected $noNeedLogin = ['madeEncode', 'getwdTree'];
  17. public function _initialize() {
  18. parent::_initialize();
  19. $this->model = model('\app\admin\model\stock\Customer');
  20. $customerList = collection($this->model->order('weigh')->select())->toArray();
  21. Tree::instance()->init($customerList);
  22. $customer = [];
  23. $this->customerList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  24. $primary = array(array('name' => '==请选择==', 'id' => '0'));
  25. $result = array_merge_recursive($primary, $this->customerList);
  26. foreach ($result as $k => $v) {
  27. $customer[$v['id']] = $v['name'];
  28. }
  29. $this->view->assign('customer', $customer);
  30. $this->view->assign("enabledmarkList", $this->model->getEnabledmarkList());
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index() {
  41. if ($this->request->isAjax()) {
  42. $list = $this->customerList;
  43. $total = count($this->customerList);
  44. $result = array("total" => $total, "rows" => $list);
  45. return json($result);
  46. }
  47. return $this->view->fetch();
  48. }
  49. /**
  50. * 添加
  51. */
  52. public function add() {
  53. if ($this->request->isPost()) {
  54. $params = $this->request->post("row/a");
  55. if ($params) {
  56. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  57. $params[$this->dataLimitField] = $this->auth->id;
  58. }
  59. try {
  60. //是否采用模型验证
  61. if ($this->modelValidate) {
  62. $name = basename(str_replace('\\', '/', get_class($this->model)));
  63. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  64. $this->model->validate($validate);
  65. }
  66. $result = $this->model->allowField(true)->save($params);
  67. if ($result !== false) {
  68. $this->success();
  69. } else {
  70. $this->error($this->model->getError());
  71. }
  72. } catch (\think\exception\PDOException $e) {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. $result = $this->model->where('pid', 0)->order('encode desc')->limit(1)->select();
  79. if ($result) {
  80. $suffix = (int) ($result[0]['encode']);
  81. $suffix ++;
  82. } else {
  83. $suffix = 0;
  84. }
  85. $encode = sprintf("%02d", $suffix);
  86. $this->view->assign('encode', $encode);
  87. return $this->view->fetch();
  88. }
  89. /**
  90. * 根据父级部门生成部门代码
  91. */
  92. public function madeEncode() {
  93. if ($this->request->isPost()) {
  94. $id = $this->request->post('id');
  95. if ($id || $id == 0) {
  96. try {
  97. $result = $this->model->where('pid', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
  98. $Encode = '';
  99. if (count($result) > 0) {
  100. $EncodeArr = explode('.', $result[0]['Encode']);
  101. $EncodeArr[count($EncodeArr) - 1] = sprintf("%03d", $EncodeArr[count($EncodeArr) - 1] + 1);
  102. $Encode = implode('.', $EncodeArr);
  103. } else {
  104. $result = $this->model->where('id', $id)->field('Encode')->order('Encode desc')->limit(1)->select();
  105. if (count($result) > 0) {
  106. $Encode = $result[0]['Encode'] . '.001';
  107. } else {
  108. $Encode = '001';
  109. }
  110. }
  111. return json($Encode);
  112. } catch (\think\exception\PDOException $e) {
  113. $this->error($e->getMessage());
  114. }
  115. }
  116. $this->error(__('Parameter %s can not be empty', ''));
  117. }
  118. $this->error('只接受POST请求', '');
  119. }
  120. /**
  121. * 获取部门树
  122. */
  123. public function getwdTree() {
  124. if ($this->auth->isSuperAdmin()) {
  125. $categoryList = collection($this->model->select())->toArray();
  126. } else {
  127. $categoryList = collection($this->model->where('id', $this->auth->customerid)->select())->toArray();
  128. $categoryList[0]['pid'] = 0;
  129. }
  130. Tree::instance()->init($categoryList);
  131. $result = Tree::instance()->getwdTreeArray(0, 'name');
  132. return json($result);
  133. }
  134. }