User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class User extends Base
  6. {
  7. use PublicApi;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->check_config();
  12. }
  13. public function index()
  14. {
  15. }
  16. /**
  17. * 获取用户列表
  18. *
  19. * @param Request $request
  20. * @return \think\response\Json
  21. */
  22. public function get_list(Request $request)
  23. {
  24. // 参数校验
  25. $param = $request->param();
  26. $validate = validate($request->controller());
  27. if (!$validate->scene($request->action())->check($param)) {
  28. return json([
  29. 'code' => 1001,
  30. 'msg' => '参数错误: ' . $validate->getError(),
  31. ]);
  32. }
  33. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  34. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  35. // 查询条件组装
  36. $where = [];
  37. if (isset($param['id'])) {
  38. $where['user_id'] = (int)$param['id'];
  39. }
  40. if (isset($param['group_id'])) {
  41. $where['group_id'] = (int)$param['group_id'];
  42. }
  43. if (isset($param['time_end']) && isset($param['time_start'])) {
  44. $where['user_reg_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  45. }elseif (isset($param['time_end'])) {
  46. $where['user_reg_time'] = ['<=', (int)$param['time_end']];
  47. }elseif (isset($param['time_start'])) {
  48. $where['user_reg_time'] = ['>=', (int)$param['time_start']];
  49. }
  50. if (isset($param['phone']) && strlen($param['phone']) > 0) {
  51. $where['user_phone'] = ['like', '%' . $this->format_sql_string($param['phone']) . '%'];
  52. }
  53. if (isset($param['qq']) && strlen($param['qq']) > 0) {
  54. $where['user_qq'] = ['like', '%' . $this->format_sql_string($param['qq']) . '%'];
  55. }
  56. if (isset($param['email']) && strlen($param['email']) > 0) {
  57. $where['user_email'] = ['like', '%' . $this->format_sql_string($param['email']) . '%'];
  58. }
  59. if (isset($param['nickname']) && strlen($param['nickname']) > 0) {
  60. $where['user_nickname'] = ['like', '%' . $this->format_sql_string($param['nickname']) . '%'];
  61. }
  62. if (isset($param['name']) && strlen($param['name']) > 0) {
  63. $where['user_name'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  64. }
  65. // 数据获取
  66. $total = model('User')->getCountByCond($where);
  67. $list = [];
  68. if ($total > 0) {
  69. // 排序
  70. $order = "user_reg_time DESC";
  71. $field = 'user_id,user_name,user_nick_name,user_phone,user_reg_time';
  72. if (strlen($param['orderby']) > 0) {
  73. $order = 'user_' . $param['orderby'] . " DESC";
  74. }
  75. $list = model('User')->getListByCond($offset, $limit, $where, $order, $field, []);
  76. }
  77. // 返回
  78. return json([
  79. 'code' => 1,
  80. 'msg' => '获取成功',
  81. 'info' => [
  82. 'offset' => $offset,
  83. 'limit' => $limit,
  84. 'total' => $total,
  85. 'rows' => $list,
  86. ],
  87. ]);
  88. }
  89. /**
  90. * 用户详细信息
  91. *
  92. * @param Request $request
  93. * @return \think\response\Json
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function get_detail(Request $request)
  99. {
  100. // 参数校验
  101. $param = $request->param();
  102. $validate = validate($request->controller());
  103. if (!$validate->scene($request->action())->check($param)) {
  104. return json([
  105. 'code' => 1001,
  106. 'msg' => '参数错误: ' . $validate->getError(),
  107. ]);
  108. }
  109. $result = Db::table('mac_user')->where(['user_id' => $param['id']])->find();
  110. // 返回
  111. return json([
  112. 'code' => 1,
  113. 'msg' => '获取成功',
  114. 'info' => $result
  115. ]);
  116. }
  117. }