request->get('page_no', 1); $pageSize = $this->request->get('page_size', 15); $phone = $this->request->get('phone', ''); $where = []; if ($phone) { $where[] = ['phone', 'like', '%' . $phone . '%']; } $count = AssetUser::where($where)->count(); $lists = AssetUser::where($where) ->order('id', 'desc') ->page($pageNo, $pageSize) ->select(); return $this->data([ 'count' => $count, 'lists' => $lists, 'page_no' => $pageNo, 'page_size' => $pageSize, ]); } /** * @notes 添加账号 */ public function add() { $params = $this->request->post(); if (empty($params['phone'])) { return $this->fail('手机号不能为空'); } $exist = AssetUser::where('phone', $params['phone'])->find(); if ($exist) { return $this->fail('手机号已存在'); } // Default password 123456 $password = empty($params['password']) ? '123456' : $params['password']; $passwordHash = password_hash($password, PASSWORD_DEFAULT); $user = AssetUser::create([ 'phone' => $params['phone'], 'password' => $passwordHash, 'status' => $params['status'] ?? 1, 'remark' => trim((string)($params['remark'] ?? '')), ]); return $this->success('添加成功', ['id' => $user->id]); } /** * @notes 编辑账号 */ public function edit() { $params = $this->request->post(); if (empty($params['id'])) { return $this->fail('缺少参数'); } $user = AssetUser::find($params['id']); if (!$user) { return $this->fail('账号不存在'); } if (!empty($params['phone']) && $params['phone'] != $user->phone) { $exist = AssetUser::where('phone', $params['phone'])->find(); if ($exist) { return $this->fail('手机号已存在'); } $user->phone = $params['phone']; } if (!empty($params['password'])) { $user->password = password_hash($params['password'], PASSWORD_DEFAULT); } if (isset($params['status'])) { $user->status = $params['status']; } if (array_key_exists('remark', $params)) { $user->remark = trim((string)$params['remark']); } $user->save(); return $this->success('修改成功'); } /** * @notes 删除账号 */ public function delete() { $id = $this->request->post('id'); if (empty($id)) { return $this->fail('缺少参数'); } AssetUser::destroy($id); // 也需要删除关联的资源记录 \app\common\model\AssetUserResource::where('user_id', $id)->delete(); return $this->success('删除成功'); } }