Dictionary.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. /**
  7. * 常用字典接口
  8. */
  9. class Dictionary extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 首页
  15. *
  16. */
  17. public function index()
  18. {
  19. $this->success('请求成功');
  20. }
  21. /**
  22. * 获取其他常用字典
  23. * @ApiMethod GET
  24. *
  25. */
  26. public function getTab(){
  27. if (Request::instance()->isGet() == false){
  28. $this->error('非法请求');
  29. }
  30. $list = db('erp_常用字典')->order('编号 asc')->column('trim(名称) as name');
  31. // 分割字符串并合并相同项
  32. $resultArray = [];
  33. foreach ($list as $item) {
  34. $parts = explode('_', $item);
  35. // 使用第一个部分作为一级键,第二个部分作为二级键
  36. $firstKey = $parts[0];
  37. $secondKey = $parts[1];
  38. $thirdValue = $parts[2];
  39. $resultArray[$firstKey][$secondKey][] = $thirdValue;
  40. }
  41. // 对废品分类下的数组进行升序排序
  42. if (isset($resultArray['data']['废品分类'])) {
  43. foreach ($resultArray['data']['废品分类'] as &$category) {
  44. // 判断是关联数组(单凹等)还是索引数组(分切等)
  45. if (is_array($category) && !empty($category) && is_array(current($category))) {
  46. foreach ($category as &$subCategory) {
  47. ksort($subCategory);
  48. }
  49. } else {
  50. ksort($category);
  51. }
  52. }
  53. }
  54. $this->success('请求成功',$resultArray);
  55. }
  56. /**
  57. * 常用字典维护界面列表
  58. * @ApiParams GET
  59. * @params string class
  60. * @params string search
  61. */
  62. public function getEditList(){
  63. if (Request::instance()->isGet() == false){
  64. $this->error('非法请求');
  65. }
  66. $params = Request::instance()->param();
  67. if (empty($params['class'])){
  68. $this->error('参数错误');
  69. }
  70. $where = [];
  71. $where['分类'] = $params['class'];
  72. if (!empty($params['search'])){
  73. $where['名称'] = array('like','%'.$params['search'].'%');
  74. }
  75. $list = db('erp_常用字典')->where($where)->order('次序 desc,UniqId asc')->select();
  76. $this->success('请求成功',$list);
  77. }
  78. /**
  79. * 常用字典维护修改
  80. * @ApiMethod POST
  81. * @params string UniqId
  82. */
  83. public function edit(){
  84. if (Request::instance()->isPost() == false){
  85. $this->error('非法请求');
  86. }
  87. $params = Request::instance()->post();
  88. if (!isset($params) || !isset($params[0]['UniqID'])){
  89. $this->error('参数不能为空');
  90. }
  91. $i = 0;
  92. foreach ($params as $key=>$value){
  93. $data = [];
  94. if (!empty($value['编号'])){
  95. $data['编号'] = $value['编号'];
  96. }
  97. if (!empty($value['名称'])){
  98. $data['名称'] = $value['名称'];
  99. }
  100. if (!empty($value['状态'])){
  101. $data['禁用状态'] = 1;
  102. }else{
  103. $data['禁用状态'] = 0;
  104. }
  105. if (!empty($value['次序'])){
  106. $data['次序'] = $value['次序'];
  107. }
  108. $sql = db('erp_常用字典')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  109. $res = Db::query($sql);
  110. if ($res !== false){
  111. $i++;
  112. }
  113. }
  114. if ($i !== 0){
  115. $this->success('更新成功');
  116. }else{
  117. $this->error('更新失败');
  118. }
  119. }
  120. }