UserController.php 18.6 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/**
 * File UserController.php
 *
 * @author Tuan Duong <bacduong@gmail.com>
 * @package Laravue
 * @version 1.0
 */

namespace App\Http\Controllers;

use App\Http\Resources\PermissionResource;
use App\Http\Requests\UsersRequest;
use App\Http\Resources\UserResource;
use App\Laravue\JsonResponse;
use App\Laravue\Models\Permission;
use App\Laravue\Models\Role;
use App\Laravue\Models\User;
use App\Laravue\Models\Users;
use Illuminate\Http\Request;
冯超鹏's avatar
冯超鹏 committed
21
use Illuminate\Support\Facades\Auth;
冯超鹏's avatar
冯超鹏 committed
22 23 24
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
冯超鹏's avatar
冯超鹏 committed
25
use Illuminate\Support\Env;
冯超鹏's avatar
冯超鹏 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use Illuminate\Support\Facades\Hash;
use Validator;

/**
 * Class UserController
 *
 * @package App\Http\Controllers
 */
class UserController extends Controller
{
    const ITEM_PER_PAGE = 15;

    /**
     * Display a listing of the user resource.
     *
冯超鹏's avatar
冯超鹏 committed
41
     * @param  \Illuminate\Http\Request $request
冯超鹏's avatar
冯超鹏 committed
42 43 44 45 46 47 48 49 50 51 52
     * @return \Illuminate\Http\Response|ResourceCollection
     */
    public function index(Request $request)
    {
        $searchParams = $request->all();
        $userQuery = User::query();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $role = Arr::get($searchParams, 'role', '');
        $keyword = Arr::get($searchParams, 'keyword', '');

        if (!empty($role)) {
冯超鹏's avatar
冯超鹏 committed
53 54 55
            $userQuery->whereHas('roles', function ($q) use ($role) {
                $q->where('name', $role);
            });
冯超鹏's avatar
冯超鹏 committed
56 57 58 59 60 61 62 63 64 65 66 67 68
        }

        if (!empty($keyword)) {
            $userQuery->where('name', 'LIKE', '%' . $keyword . '%');
            $userQuery->where('email', 'LIKE', '%' . $keyword . '%');
        }

        return UserResource::collection($userQuery->paginate($limit));
    }

    /**
     * Store a newly created resource in storage.
     *
冯超鹏's avatar
冯超鹏 committed
69
     * @param  \Illuminate\Http\Request $request
冯超鹏's avatar
冯超鹏 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make(
            $request->all(),
            array_merge(
                $this->getValidationRules(),
                [
                    'password' => ['required', 'min:6'],
                    'confirmPassword' => 'same:password',
                ]
            )
        );

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 403);
        } else {
            $params = $request->all();
            $user = User::create([
                'name' => $params['name'],
                'email' => $params['email'],
                'password' => Hash::make($params['password']),
93 94 95 96 97 98 99 100
                'username' => $params['username'],
                'title' => $params['title'],
                'company' => $params['company'],
                'mapcenter' => $params['mapcenter'],
                'provinceid' => $params['province'],
                'areaid' => $params['area'],
                'cityid' => $params['city'],
                'isadmin' => 2,
冯超鹏's avatar
冯超鹏 committed
101
            ]);
102
            $this->timeline('新增了用户!');
冯超鹏's avatar
冯超鹏 committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            $role = Role::findByName($params['role']);
            $user->syncRoles($role);

            return new UserResource($user);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  User $user
     * @return UserResource|\Illuminate\Http\JsonResponse
     */
    public function show(User $user)
    {
        return new UserResource($user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
冯超鹏's avatar
冯超鹏 committed
125
     * @param User $user
冯超鹏's avatar
冯超鹏 committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
     * @return UserResource|\Illuminate\Http\JsonResponse
     */
    public function update(Request $request, User $user)
    {
        if ($user === null) {
            return response()->json(['error' => 'User not found'], 404);
        }
        if ($user->isAdmin()) {
            return response()->json(['error' => 'Admin can not be modified'], 403);
        }

        $validator = Validator::make($request->all(), $this->getValidationRules(false));
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 403);
        } else {
            $email = $request->get('email');
            $found = User::where('email', $email)->first();
            if ($found && $found->id !== $user->id) {
                return response()->json(['error' => 'Email has been taken'], 403);
            }

            $user->name = $request->get('name');
            $user->email = $email;
            $user->save();
            return new UserResource($user);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
冯超鹏's avatar
冯超鹏 committed
158
     * @param User $user
冯超鹏's avatar
冯超鹏 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172
     * @return UserResource|\Illuminate\Http\JsonResponse
     */
    public function updatePermissions(Request $request, User $user)
    {
        if ($user === null) {
            return response()->json(['error' => 'User not found'], 404);
        }

        if ($user->isAdmin()) {
            return response()->json(['error' => 'Admin can not be modified'], 403);
        }

        $permissionIds = $request->get('permissions', []);
        $rolePermissionIds = array_map(
冯超鹏's avatar
冯超鹏 committed
173
            function ($permission) {
冯超鹏's avatar
冯超鹏 committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                return $permission['id'];
            },

            $user->getPermissionsViaRoles()->toArray()
        );

        $newPermissionIds = array_diff($permissionIds, $rolePermissionIds);
        $permissions = Permission::allowed()->whereIn('id', $newPermissionIds)->get();
        $user->syncPermissions($permissions);
        return new UserResource($user);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  User $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        if ($user->isAdmin()) {
            response()->json(['error' => 'Ehhh! Can not delete admin user'], 403);
        }

        try {
            $user->delete();
        } catch (\Exception $ex) {
            response()->json(['error' => $ex->getMessage()], 403);
        }

        return response()->json(null, 204);
    }

    /**
     * Get permissions from role
     *
     * @param User $user
     * @return array|\Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function permissions(User $user)
    {
        try {
            return new JsonResponse([
                'user' => PermissionResource::collection($user->getDirectPermissions()),
                'role' => PermissionResource::collection($user->getPermissionsViaRoles()),
            ]);
        } catch (\Exception $ex) {
            response()->json(['error' => $ex->getMessage()], 403);
        }
    }

    /**
     * @param bool $isNew
     * @return array
     */
    private function getValidationRules($isNew = true)
    {
        return [
            'name' => 'required',
            'email' => $isNew ? 'required|email|unique:users' : 'required|email',
            'roles' => [
                'required',
                'array'
            ],
        ];
    }

冯超鹏's avatar
冯超鹏 committed
241 242 243 244 245 246 247 248 249 250 251 252
    /**
     * @param bool $isNew
     * @return array
     */
    private function getValidationRulescontacts($isNew = true)
    {
        return [
            'contacts_name' => 'required|max:3',
            'contacts_phone' => 'required|regex:/^1[3465789]\d{9}$/|max:11|unique:contactsuser,contacts_phone'
        ];
    }

冯超鹏's avatar
冯超鹏 committed
253 254 255 256 257 258 259 260 261
    /**
     * @param bool $isNew
     * @return array
     * 用户提交表单验证
     */
    private function getValidationRulesuser($isNew = true)
    {
        return [
            'email' => $isNew ? 'required|email|unique:users' : 'required|email',
262
            'username' => 'required|between:2,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,username',
冯超鹏's avatar
冯超鹏 committed
263
            'password' => 'sometimes|required|string|min:6',
264
            'nickname' => 'required|between:2,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name'
冯超鹏's avatar
冯超鹏 committed
265 266 267
        ];
    }

冯超鹏's avatar
冯超鹏 committed
268
//    后台管理用户列表
冯超鹏's avatar
冯超鹏 committed
269 270
    public function HUserList(Request $request)
    {
冯超鹏's avatar
冯超鹏 committed
271
        $limit = $request->input('limit');
冯超鹏's avatar
冯超鹏 committed
272
        $pagenNum = $limit * ($request->input('page') - 1);//页数
冯超鹏's avatar
冯超鹏 committed
273 274
        if (is_null($pagenNum)) {
            return $this->jsonErrorData(105, 'page不能为空');
冯超鹏's avatar
冯超鹏 committed
275
        }
冯超鹏's avatar
冯超鹏 committed
276
        if (is_null($limit)) {
冯超鹏's avatar
冯超鹏 committed
277
            return $this->jsonErrorData(105, 'limit不能为空');
冯超鹏's avatar
冯超鹏 committed
278
        }
279
        $users = DB::table('users as b')
冯超鹏's avatar
冯超鹏 committed
280 281 282
            ->leftjoin('areachina as p', 'b.provinceid', '=', 'p.areaid')
            ->leftjoin('areachina as c', 'b.cityid', '=', 'c.areaid')
            ->leftjoin('areachina as a', 'b.areaid', '=', 'a.areaid')
Administrator's avatar
Administrator committed
283
            ->leftJoin('user_roles AS ur', 'b.user_role_id', '=', 'ur.id')
冯超鹏's avatar
冯超鹏 committed
284
            ->orderBy('b.id', 'desc')
Administrator's avatar
Administrator committed
285 286 287 288 289 290 291 292 293 294 295 296 297
            ->select(
                'b.username',
                'b.id',
                'b.created_at',
                'b.name',
                'b.email',
                'b.state',
                'b.phone_number',
                'ur.name AS role_name',
                'a.area_name as area',
                'c.area_name as city',
                'p.area_name as province'
            )
冯超鹏's avatar
冯超鹏 committed
298 299 300
            ->offset($pagenNum)
            ->limit($limit)
            ->get();
301
        $count = DB::table('users')
冯超鹏's avatar
冯超鹏 committed
302
            ->count();
303
        $davicenum = DB::table('users')
冯超鹏's avatar
冯超鹏 committed
304 305 306
            ->select('id')
            ->get()->toArray();
        $data = [];
307
        foreach (array_column($davicenum, 'id') as $k => $value) {
冯超鹏's avatar
冯超鹏 committed
308
            $countdevice_type = DB::table('device')
309
                ->where('uid', '=', $value)
冯超鹏's avatar
冯超鹏 committed
310
                ->count();
311 312 313
            $usernum['count'] = $countdevice_type;
            $usernum['id'] = $value;
            array_push($data, $usernum);
冯超鹏's avatar
冯超鹏 committed
314
        }
315
        $data = ['users' => $users, 'count' => $count, 'davicenum' => $data];
冯超鹏's avatar
冯超鹏 committed
316
        if ($users) {
冯超鹏's avatar
冯超鹏 committed
317
            return $this->jsonSuccessData($data);
冯超鹏's avatar
冯超鹏 committed
318 319
        } else {
            return $this->jsonErrorData(105, '获取失败');
冯超鹏's avatar
冯超鹏 committed
320 321
        }
    }
322

冯超鹏's avatar
冯超鹏 committed
323
    //用户搜索
324 325
    public function userSeek(Request $request)
    {
冯超鹏's avatar
冯超鹏 committed
326
        $keys = $request->all();
327
        $handle = DB::table('users as b');
冯超鹏's avatar
冯超鹏 committed
328 329 330
        foreach ($keys as $key => $val) {
            if ($key == 'page') {
                unset($keys[$key]);
331
            } elseif ($key == 'limit') {
冯超鹏's avatar
冯超鹏 committed
332 333 334 335
                unset($keys[$key]);
            }
        }
        foreach ($keys as $k => $v) {
336 337 338 339 340 341 342 343
            if ($v == '正常') {
                $v = 2;
            } else if ($v == '禁用') {
                $v = 3;
            } else if ($v == '已删除') {
                $v = 1;
            }
            $keys[$key] && $handle->where('b.' . $k, 'like', '%' . $v . '%');
冯超鹏's avatar
冯超鹏 committed
344 345 346 347 348 349
        }
        $datas = $handle
            ->leftjoin('areachina as p', 'b.provinceid', '=', 'p.areaid')
            ->leftjoin('areachina as c', 'b.cityid', '=', 'c.areaid')
            ->leftjoin('areachina as a', 'b.areaid', '=', 'a.areaid')
            ->orderBy('b.id', 'desc')
350
            ->select('b.username', 'b.id', 'b.created_at', 'b.name', 'b.email', 'b.state', 'a.area_name as area', 'c.area_name as city', 'p.area_name as province')
冯超鹏's avatar
冯超鹏 committed
351
            ->get();
352
        $davicenum = DB::table('users')
冯超鹏's avatar
冯超鹏 committed
353 354 355
            ->select('id')
            ->get()->toArray();
        $data = [];
356
        foreach (array_column($davicenum, 'id') as $k => $value) {
冯超鹏's avatar
冯超鹏 committed
357
            $countdevice_type = DB::table('device')
358
                ->where('uid', '=', $value)
冯超鹏's avatar
冯超鹏 committed
359
                ->count();
360
            $usernum['count'] = $countdevice_type;
冯超鹏's avatar
冯超鹏 committed
361
            $usernum['id'] = $value;
362
            array_push($data, $usernum);
冯超鹏's avatar
冯超鹏 committed
363 364
        }
        $cont = $handle->count();
365
        $data = ['users' => $datas, 'count' => $cont, 'davicenum' => $data];
冯超鹏's avatar
冯超鹏 committed
366 367
        return $this->jsonSuccessData($data);
    }
冯超鹏's avatar
冯超鹏 committed
368

冯超鹏's avatar
冯超鹏 committed
369
    //新增用户
冯超鹏's avatar
冯超鹏 committed
370 371
    public function addUser(Request $request)
    {
冯超鹏's avatar
冯超鹏 committed
372
        $userdata = $request->all();
冯超鹏's avatar
冯超鹏 committed
373
        //获取用户列表
冯超鹏's avatar
冯超鹏 committed
374
        $validator = Validator::make($request->all(), $this->getValidationRulesuser(false));
冯超鹏's avatar
冯超鹏 committed
375 376
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 403);
冯超鹏's avatar
冯超鹏 committed
377
        } else {
冯超鹏's avatar
冯超鹏 committed
378
            $type = new Users();
379
            $this->timeline('新增了用户!');
冯超鹏's avatar
冯超鹏 committed
380
            $arr = $type->getTypeAlldeleteuserToArray($userdata);
冯超鹏's avatar
冯超鹏 committed
381 382 383 384 385 386 387 388 389 390 391
            return $this->jsonSuccessData($arr);
        }
    }

    /*
     * 删除用户
     * type
     * 1 == 逻辑删除用户
     * 2  == 物理删除用户
     * duserid 删除用户的id
     * */
冯超鹏's avatar
冯超鹏 committed
392 393
    public function deleteuser(Request $request)
    {
冯超鹏's avatar
冯超鹏 committed
394
        $type = (int)$request->input('type');
冯超鹏's avatar
冯超鹏 committed
395 396 397
        $duserid = (int)$request->input('duserid');
        if ($type == '') {
            return $this->jsonErrorData(105, 'type参数不能为空');
冯超鹏's avatar
冯超鹏 committed
398
        }
冯超鹏's avatar
冯超鹏 committed
399 400
        if ($duserid == '') {
            return $this->jsonErrorData(105, 'duserid参数不能为空');
冯超鹏's avatar
冯超鹏 committed
401
        }
冯超鹏's avatar
冯超鹏 committed
402
        if ($type == 1) { // 逻辑删除
403
            $this->timeline('将用户放到废纸篓[id' . $duserid . ']');
冯超鹏's avatar
冯超鹏 committed
404
            $users = Users::where('id', '=', $duserid)
405
                ->update(['state' => 3]);
冯超鹏's avatar
冯超鹏 committed
406
            if ($users) {
冯超鹏's avatar
冯超鹏 committed
407
                return $this->jsonSuccessData($users);
冯超鹏's avatar
冯超鹏 committed
408
            } else {
409
                return $this->jsonErrorData(105, '用户操作数据失败');
冯超鹏's avatar
冯超鹏 committed
410
            }
冯超鹏's avatar
冯超鹏 committed
411
        } else if ($type == 2) {
412
            $this->timeline('删除了设备');
冯超鹏's avatar
冯超鹏 committed
413
            $users = Users::where('id', '=', $duserid)
冯超鹏's avatar
冯超鹏 committed
414
                ->delete();
冯超鹏's avatar
冯超鹏 committed
415
            if ($users) {
冯超鹏's avatar
冯超鹏 committed
416
                return $this->jsonSuccessData($users);
冯超鹏's avatar
冯超鹏 committed
417 418
            } else {
                return $this->jsonErrorData(105, '获取用户数据失败');
冯超鹏's avatar
冯超鹏 committed
419 420 421
            }
        }
    }
冯超鹏's avatar
冯超鹏 committed
422

冯超鹏's avatar
冯超鹏 committed
423
    //更新用户
冯超鹏's avatar
冯超鹏 committed
424 425
    public function Upuser(Request $request)
    {
冯超鹏's avatar
冯超鹏 committed
426
        if ($request->isMethod('post')) {
冯超鹏's avatar
冯超鹏 committed
427 428
            $userdata = $request->all();
            $uid = $userdata['userid'];
冯超鹏's avatar
冯超鹏 committed
429 430
            if (!isset($userdata['userid']) || $userdata['userid'] == '') {
                return $this->jsonErrorData(105, '用户id不能为空');
冯超鹏's avatar
冯超鹏 committed
431
            }
432 433 434
            foreach ($userdata as $k => $v) {
                if ($k == 'userid') {
                    unset($userdata[$k]);
冯超鹏's avatar
冯超鹏 committed
435
                }
436 437 438 439 440 441 442
            }
            $re = Users::where('id', '=', (int)$uid)->update($userdata);
            if ($re !== false) {
                return $this->jsonSuccessData($re);
            } else {
                return $this->jsonErrorData(105, '更新失败');
            }
冯超鹏's avatar
冯超鹏 committed
443
        } else {
冯超鹏's avatar
冯超鹏 committed
444
//            获取用户数据
冯超鹏's avatar
冯超鹏 committed
445 446 447
            $userid = (int)$request->input('userid');
            if ($userid == '') {
                return $this->jsonErrorData(105, '用户id不能为空');
冯超鹏's avatar
冯超鹏 committed
448
            }
冯超鹏's avatar
冯超鹏 committed
449
            $userdata = Users::where('id', '=', $userid)
Administrator's avatar
Administrator committed
450
                ->select('username', 'name', 'email', 'title', 'company', 'mapcenter', 'phone_number', 'user_role_id')
冯超鹏's avatar
冯超鹏 committed
451 452
                ->first();
            if ($userdata) {
冯超鹏's avatar
冯超鹏 committed
453
                return $this->jsonSuccessData($userdata);
冯超鹏's avatar
冯超鹏 committed
454 455
            } else {
                return $this->jsonErrorData(105, '获取数据失败');
冯超鹏's avatar
冯超鹏 committed
456
            }
冯超鹏's avatar
冯超鹏 committed
457 458
        }
    }
冯超鹏's avatar
冯超鹏 committed
459 460

    //返回地址列表
冯超鹏's avatar
冯超鹏 committed
461 462
    public function areachina(Request $request)
    {
463
        if ($request->isMethod('post')) {
冯超鹏's avatar
冯超鹏 committed
464
            $areaid = $request->input('areaid');
冯超鹏's avatar
冯超鹏 committed
465 466
            if ($areaid == '') {
                return $this->jsonErrorData('105', '地区id不能为空');
冯超鹏's avatar
冯超鹏 committed
467
            }
Administrator's avatar
Administrator committed
468
            $area =  Db::table('areachina')->where('pid', '=', $areaid)
冯超鹏's avatar
冯超鹏 committed
469 470 471 472
                ->where('status', '=', '1')
                ->select('area_name', 'areaid')
                ->get();
        } else {
Administrator's avatar
Administrator committed
473
            $area =  Db::table('areachina')->where('pid', '=', '0')
冯超鹏's avatar
冯超鹏 committed
474 475
                ->where('status', '=', '1')
                ->select('area_name', 'areaid')
冯超鹏's avatar
冯超鹏 committed
476 477
                ->get();
        }
冯超鹏's avatar
冯超鹏 committed
478
        if ($area) {
冯超鹏's avatar
冯超鹏 committed
479
            return $this->jsonSuccessData($area);
冯超鹏's avatar
冯超鹏 committed
480 481
        } else {
            return $this->jsonErrorData(105, '获取失败');
冯超鹏's avatar
冯超鹏 committed
482 483 484
        }
    }

冯超鹏's avatar
冯超鹏 committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498
    //添加联系人
    public function addcontactsuser(Request $request)
    {
        $data = $request->all();
        if (!is_null($this->isadmin())) {//当前登入管理员
            $validator = Validator::make($request->all(), $this->getValidationRulescontacts(false));
            if ($validator->fails()) {
                return response()->json(['errors' => $validator->errors()], 403);
            } else {
                $data['isadmin'] = 1;
            }
        } else {
            $data['isadmin'] = 2;
        }
冯超鹏's avatar
冯超鹏 committed
499
//        $data['contactsid'] = Auth::id();
500
        $this->timeline($data['isadmin'] == 1 ? '管理员添加了设备联系人' : '用户添加了设备联系人');
冯超鹏's avatar
冯超鹏 committed
501 502 503
        $isadmin = DB::table('contactsuser')->insert($data);
        return $this->jsonSuccessData($isadmin);
    }
冯超鹏's avatar
冯超鹏 committed
504 505

    //查看所有逻辑删除用户 和禁用用户
冯超鹏's avatar
冯超鹏 committed
506
    public function UpPaperBasket($id)
冯超鹏's avatar
冯超鹏 committed
507
    {
508
        return $this->jsonSuccessData(DB::table('users')->where('id', '=', $id)->update(['state' => '2']));
冯超鹏's avatar
冯超鹏 committed
509 510 511 512 513
    }

    //禁用用户
    public function UpuserForbidden($id)
    {
514
        return $this->jsonSuccessData(DB::table('users')->where('id', '=', $id)->update(['state' => '1']));
冯超鹏's avatar
冯超鹏 committed
515 516
    }

冯超鹏's avatar
冯超鹏 committed
517 518 519 520 521
    /*
     * 返回当前登入的联系人列表
     * */
    public function contactslist()
    {
522
        return $this->jsonSuccessData(DB::table('contactsuser')->where([['contactsid', '=', Auth::id()], ['isadmin', '=', is_null($this->isadmin()) ? '2' : '1'], ['isstatus', '=', '1']])->get());
冯超鹏's avatar
冯超鹏 committed
523
    }
冯超鹏's avatar
冯超鹏 committed
524

冯超鹏's avatar
冯超鹏 committed
525 526 527 528 529
    /*
   * 返回废纸篓数量
   * */
    public function paperBasket()
    {
530
        return $this->jsonSuccessData(DB::table('users')->where('state', '=', '3')->count());
冯超鹏's avatar
冯超鹏 committed
531 532
    }

冯超鹏's avatar
冯超鹏 committed
533 534 535
    //返回用户的经纬度
    public function userLocation()
    {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        if (!is_null($this->isadmin())) {
            $davicenum = DB::table('users as b')
                ->leftjoin('device as d', 'b.id', '=', 'd.uid')
                ->leftjoin('device_type as t', 'd.dtype', '=', 't.tid')
                ->selectRaw('b.name,b.id,b.mapcenter,d.devicenum,COUNT(d.id) AS UserDaviceNum,COUNT(IF(d.devicepolice <> 1, true, null)) AS police')
                ->groupBy('b.id')
                ->get()->toArray();
        } else {
            $davicenum = DB::table('users as b')
                ->where('uid','=',Auth::id())
                ->leftjoin('device as d', 'b.id', '=', 'd.uid')
                ->leftjoin('device_type as t', 'd.dtype', '=', 't.tid')
                ->selectRaw('b.name,b.id,b.mapcenter,d.devicenum,COUNT(d.id) AS UserDaviceNum,COUNT(IF(d.devicepolice <> 1, true, null)) AS police')
                ->groupBy('b.id')
                ->get()->toArray();
        }
        return $this->jsonSuccessData($davicenum);
冯超鹏's avatar
冯超鹏 committed
553
    }
冯超鹏's avatar
冯超鹏 committed
554 555 556 557 558 559

    /*
   * 返回废纸篓 和状态
   * */
    public function paperBasketList()
    {
560 561 562
        $paper = DB::table('users')->where('state', '=', '3')->get()->toArray();
        $stutus = DB::table('users')->where('state', '=', '1')->get()->toArray();
        return $this->jsonSuccessData(['paper' => $paper, 'stutus' => $stutus]);
冯超鹏's avatar
冯超鹏 committed
563
    }
564

565 566
    public function textcountuser()
    {
567
        print_r(Auth::id());
568
    }
Administrator's avatar
Administrator committed
569
}