UserFake.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 虚拟用户
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class UserFake extends Backend
  11. {
  12. /**
  13. * UserFake模型对象
  14. * @var \app\admin\model\shopro\UserFake
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\shopro\UserFake;
  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 index()
  31. {
  32. //当前是否为关联查询
  33. $this->relationSearch = false;
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax())
  37. {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField'))
  40. {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $total = $this->model
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. foreach ($list as $row) {
  54. $row->visible(['id','nickname','avatar']);
  55. }
  56. $list = collection($list)->toArray();
  57. $result = array("total" => $total, "rows" => $list);
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 获取随机虚拟用户
  64. */
  65. public function random_user()
  66. {
  67. $userFake = $this->model->orderRaw('rand()')->find();
  68. if ($userFake) {
  69. $result = [
  70. 'code' => 1,
  71. 'data' => $userFake,
  72. 'msg' => ''
  73. ];
  74. }else{
  75. $result = [
  76. 'code' => 0,
  77. 'data' => null,
  78. 'msg' => '资料管理中添加虚拟用户'
  79. ];
  80. }
  81. return json($result);
  82. }
  83. }