Users.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\system;
  10. use app\admin\service\Service;
  11. use app\admin\model\system\Users as UsersModel;
  12. use app\common\models\system\Manage as ManageModel;
  13. class Users extends Service {
  14. /**
  15. * 获取列表数据
  16. * @param $data
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getList($data){
  23. $count = UsersModel::withJoin("manage")->count();
  24. $result = UsersModel::withJoin("manage")->page($data["page"]??1,$data["limit"]??10)->order("id","desc")->select()->toArray();
  25. return [ "count"=>$count, "data"=>$result ];
  26. }
  27. /**
  28. * 详情
  29. * @param $id
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public static function detail($id){
  36. $row = UsersModel::where("id",$id)->find();
  37. return [
  38. "cat"=>ManageModel::where(["status"=>0])->select()->toArray(),
  39. "data"=>$row??[]
  40. ];
  41. }
  42. /**
  43. * 保存数据
  44. * @param $data
  45. * @return bool
  46. * @throws \Exception
  47. */
  48. public static function save($data){
  49. if(UsersModel::where("id",$data["id"])->count()){
  50. if(!empty($data["password"]) || !empty($data["confirm_password"])){
  51. if($data["password"] != $data["confirm_password"]){
  52. throw new \Exception("您输入的两次密码不致。",0);
  53. }
  54. $data["password"] = md5($data["password"]);
  55. }else{
  56. unset($data["password"],$data["confirm_password"]);
  57. }
  58. if(UsersModel::where("username",$data["username"])->where("id","<>",$data["id"])->count()){
  59. throw new \Exception("该用户名已存在,请更换用户名。",0);
  60. }
  61. UsersModel::where("id",$data["id"])->save($data);
  62. }else{
  63. if(empty($data["password"])){
  64. throw new \Exception("请填写密码",0);
  65. }else if(empty($data["confirm_password"])){
  66. throw new \Exception("请填写确认密码",0);
  67. }else if($data["password"] != $data["confirm_password"]){
  68. throw new \Exception("您输入的两次密码不致。",0);
  69. }
  70. if(UsersModel::where("username",$data["username"])->count()){
  71. throw new \Exception("该用户名已存在,请更换用户名。",0);
  72. }
  73. $data["password"] = md5($data["password"]);
  74. $data["time"] = time();
  75. UsersModel::create($data);
  76. }
  77. return true;
  78. }
  79. /**
  80. * 删除
  81. * @param $id
  82. * @return bool
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public static function delete($id){
  88. $row = UsersModel::where('id',$id)->find();
  89. if(empty($row)){
  90. throw new \Exception("您要查找的数据不存在!",0);
  91. }
  92. if($row["lock"] == 1){
  93. throw new \Exception("该用户为系统用户,不允许删除。",0);
  94. }
  95. return UsersModel::where("id",$id)->delete();
  96. }
  97. /**
  98. * 更新字段值
  99. * @return bool
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public static function setFields(){
  105. $data = self::getFields();
  106. $result = UsersModel::where('id',$data["id"])->find();
  107. if(empty($result)){
  108. throw new \Exception("您要查找的数据不存在!",0);
  109. }
  110. if($result["lock"] == 1){
  111. throw new \Exception("该用户为系统用户,不允许修改。",0);
  112. }
  113. UsersModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  114. return true;
  115. }
  116. }