Rfiduser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. *
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Rfiduser extends Backend
  11. {
  12. /**
  13. * Rfiduser模型对象
  14. * @var \app\admin\model\Rfiduser
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Rfiduser;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. 新增
  29. */
  30. public function add()
  31. {
  32. if ($this->request->isPost()) {
  33. $param = input('');
  34. // 当前时间和 token 过期时间
  35. $now = time();
  36. $todayEnd = strtotime(date('Y-m-d 23:59:59'));
  37. // 生成初始 token
  38. $token = md5($param['row']['username'] . $now . uniqid());
  39. $arr = [
  40. 'nickname' => $param['row']['nickname'],
  41. 'username' => $param['row']['username'],
  42. 'password' => md5($param['row']['password']),
  43. 'token' => $token, // 初始 token
  44. 'token_expire_time' => date('Y-m-d H:i:s', $todayEnd), // 当天结束时间
  45. 'building' => '', // 栋
  46. 'room' => '', // 房间
  47. 'pen' => '', // 栏位
  48. 'sys_rq' => date('Y-m-d H:i:s') // 系统创建时间
  49. ];
  50. $add_result = Db::name("rfid_user")->insert($arr);
  51. if ($add_result) {
  52. $this->success('新增成功');
  53. } else {
  54. $this->error('新增失败');
  55. }
  56. } else {
  57. return $this->view->fetch();
  58. }
  59. }
  60. /**
  61. 修改
  62. */
  63. public function edit($ids = null)
  64. {
  65. if ($this->request->isPost()) {
  66. $param = input('');
  67. $arr = [
  68. 'nickname' => $param['row']['nickname'],
  69. 'username' => $param['row']['username'],
  70. 'password' => md5($param['row']['password'])
  71. ];
  72. $res = Db::name("rfid_user")->where('id',$ids)->update($arr);
  73. if ($res == 0) {
  74. $this->success('修改成功');
  75. } else {
  76. $this->error('修改失败');
  77. }
  78. }else{
  79. $row = Db::name("rfid_user")->where('id', $ids)->find();
  80. if (!$row) {
  81. $this->error("记录未找到");
  82. }
  83. $this->assign("row", $row);
  84. return $this->view->fetch();
  85. }
  86. }
  87. }