Policy.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace app\admin\controller;
  8. use app\admin\common\AdminController;
  9. use think\facade\Db;
  10. /**
  11. * Description of Policy
  12. *
  13. * @author sgq
  14. */
  15. class Policy extends AdminController {
  16. protected $companyId;
  17. public function __construct(\think\App $app) {
  18. parent::__construct($app);
  19. $this->companyId = $this->user["companyId"];
  20. }
  21. public function index() {
  22. $company = \app\common\api\CompanyApi::getOne($this->companyId);
  23. $tmps = Db::table("new_policy_tmp")->where("companyId", "=", $this->companyId)->select()->toArray();
  24. $policyIds = array_column($tmps, "policyId");
  25. $policys = Db::table("new_policy")->order("level asc")->select()->toArray();
  26. foreach ($policys as $key => $policy) {
  27. if (in_array($policy["id"], $policyIds)) {
  28. $policys[$key]["checked"] = true;
  29. }
  30. }
  31. return view("", ["company" => $company, "policys" => $policys]);
  32. }
  33. public function checked() {
  34. if ($this->request->isPost()) {
  35. $policyId = $this->request->param("id");
  36. $uniqueId = sprintf("%s_%s", $policyId, $this->companyId);
  37. $checked = $this->request->param("checked") ?: 1;
  38. $lockFileName = sprintf("storage/policy_logs/%s.lock", $uniqueId);
  39. if (!file_exists("storage/policy_logs")) {
  40. mkdir("storage/policy_logs");
  41. }
  42. $lockFile = fopen($lockFileName, "a");
  43. if (flock($lockFile, LOCK_EX | LOCK_NB)) {//文件锁(独占)
  44. try {
  45. $record = Db::table("new_policy_tmp")->where("id", "=", $uniqueId)->findOrEmpty();
  46. if ($checked == 1) {
  47. //需要本单位审核
  48. if ($record) {
  49. $log = sprintf("[%s][account:%s][ERR]Already checked!", date("Y-m-d H:i:s"), $this->user["account"]);
  50. fwrite($lockFile, $log . "\n");
  51. return json(["msg" => "已经添加审核,不需要重复添加"]);
  52. } else {
  53. $data["id"] = $uniqueId;
  54. $data["companyId"] = $this->companyId;
  55. $data["policyId"] = $policyId;
  56. $data["createUser"] = $this->user["uid"];
  57. $data["createTime"] = date("Y-m-d H:i:s");
  58. if (Db::table("new_policy_tmp")->insert($data)) {
  59. $log = sprintf("[%s][account:%s][INFO]Checked!", date("Y-m-d H:i:s"), $this->user["account"]);
  60. fwrite($lockFile, $log . "\n");
  61. return json(["msg" => "添加审核成功", "code" => 200]);
  62. } else {
  63. $log = sprintf("[%s][account:%s][ERR]Check failure!", date("Y-m-d H:i:s"), $this->user["account"]);
  64. fwrite($lockFile, $log . "\n");
  65. return json(["msg" => "添加审核失败"]);
  66. }
  67. }
  68. } else {
  69. //取消本单位审核
  70. if ($record) {
  71. if (Db::table("new_policy_tmp")->delete($uniqueId)) {
  72. $log = sprintf("[%s][account:%s][INFO]Deleted!", date("Y-m-d H:i:s"), $this->user["account"]);
  73. fwrite($lockFile, $log . "\n");
  74. return json(["msg" => "取消审核成功", "code" => 200]);
  75. } else {
  76. $log = sprintf("[%s][account:%s][ERR]Delete failure!", date("Y-m-d H:i:s"), $this->user["account"]);
  77. fwrite($lockFile, $log . "\n");
  78. return json(["msg" => "取消审核失败"]);
  79. }
  80. } else {
  81. $log = sprintf("[%s][account:%s][ERR]Already deleted!", date("Y-m-d H:i:s"), $this->user["account"]);
  82. fwrite($lockFile, $log . "\n");
  83. return json(["msg" => "已经取消审核,不需要重复取消"]);
  84. }
  85. }
  86. } catch (\Exception $e) {
  87. $log = sprintf("[%s][account:%s][ERR]%s", date("Y-m-d H:i:s"), $this->user["account"], $e->getMessage());
  88. fwrite($lockFile, $log . "\n");
  89. return json(["msg" => $e->getMessage()]);
  90. } finally {
  91. flock($lockFile, LOCK_UN);
  92. }
  93. } else {
  94. return json(["msg" => "已经有同单位的人员在操作同一条记录,请稍候片刻"]);
  95. }
  96. }
  97. }
  98. }