Constant.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\manage\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Session;
  6. use think\Config;
  7. use think\Cookie;
  8. use think\Request;
  9. use app\common\library\SysLogs;
  10. use app\common\library\UUIDs;
  11. /**
  12. *
  13. */
  14. class Constant extends Base {
  15. public function index() {
  16. // 查询出全部的机构,显示上级机构使用
  17. $rs = Db::table("constant")->select();
  18. $this->assign("constants", $rs);
  19. return $this->fetch('index');
  20. }
  21. /**
  22. * 列表数据
  23. */
  24. public function datas() {
  25. $parent_id = "";
  26. if (!empty($_GET["parent_id"])) {
  27. $parent_id = $_GET["parent_id"];
  28. }
  29. $rs = self::itsChild($parent_id);
  30. echo json_encode($rs);
  31. }
  32. public function itsChild($pid) {
  33. $rs = Db::table("constant")->where("parent_id", $pid)->order("ordernum", "1")->select();
  34. foreach ($rs as $key => $val) {
  35. $rs[$key]["text"] = $val["name"];
  36. $rs[$key]["children"] = self::itsChild($val["id"]);
  37. }
  38. return $rs;
  39. }
  40. /**
  41. * 更新或创建机构信息
  42. */
  43. public function save() {
  44. $request = Request::instance();
  45. $params = $request->param();
  46. $id = $params["id"];
  47. $data = array();
  48. $data["name"] = $params["name"];
  49. $data["parent_id"] = $params["parent_id"];
  50. $data["constant_key"] = $params["constant_key"];
  51. $data["constant_value"] = $params["constant_value"];
  52. $data["remark"] = $params["remark"];
  53. $data["sign"] = isset($params["sign"]) ? $params["sign"] : "0";
  54. $data["ordernum"] = isset($params["ordernum"]) ? $params["ordernum"] : "0";
  55. // ID有值,认为是更新
  56. if (empty($id)) {
  57. // 如果是新创建,且是父节点时,id = key
  58. // 无值,认为是添加
  59. if ($data["sign"] == "1") {
  60. // 父节点,ID = key
  61. $data["id"] = $data["constant_key"];
  62. } else {
  63. // 非父节点,ID自动生成
  64. $data["id"] = UUIDs::uuid16();
  65. }
  66. Db::table("constant")->insert($data);
  67. echo "insert_ok;" . $data["id"];
  68. SysLogs::log("constant", "C", json_encode($data));
  69. } else {
  70. // 更新
  71. Db::table("constant")->where("id", $id)->update($data);
  72. echo "update_ok";
  73. SysLogs::log("constant", "C", $id . " --> " . json_encode($data));
  74. }
  75. }
  76. /**
  77. * 编辑窗口
  78. * @return type
  79. */
  80. public function edit() {
  81. // 查询出全部的机构,显示上级机构使用
  82. $rs = self::itsCombChild("");
  83. $this->assign("insJson", json_encode($rs));
  84. if (isset($_GET["id"])) {
  85. $id = $_GET["id"];
  86. if ($id != null) {
  87. $constants = Db::table("constant")->where("id", $id)->select();
  88. if (count($constants) > 0) {
  89. $this->assign("constant", $constants[0]);
  90. }
  91. }
  92. }
  93. return $this->fetch('edit');
  94. }
  95. public function combs() {
  96. $rs = self::itsCombChild("");
  97. echo json_encode($rs);
  98. }
  99. public function itsCombChild($pid) {
  100. $retArr = Array();
  101. $whereArr = Array();
  102. $whereArr["parent_id"] = $pid;
  103. $whereArr["sign"] = "1";
  104. $rs = Db::table("constant")->where($whereArr)->order("ordernum", "1")->select();
  105. foreach ($rs as $key => $val) {
  106. $rs[$key]["text"] = $val["name"];
  107. $childrens = self::itsCombChild($val["id"]);
  108. $rs[$key]["children"] = $childrens;
  109. array_push($retArr, $rs[$key]);
  110. }
  111. return $retArr;
  112. }
  113. /**
  114. * 删除记录
  115. */
  116. public function delete() {
  117. $params = Request::instance()->param();
  118. if (isset($params["ids"])) {
  119. $ids = $params["ids"];
  120. if (!empty($ids)) {
  121. $idArr = explode(",", $ids);
  122. if (count($idArr) > 0) {
  123. Db::table("constant")->where("id", "in", $idArr)->delete();
  124. SysLogs::log("constant", "D", "ids --> " . json_encode($idArr));
  125. }
  126. echo "delete_ok";
  127. return;
  128. }
  129. }
  130. echo "fail";
  131. }
  132. }