error('账号未绑定工厂'); } $factory = FactoryModel::get($factoryId); if (!$factory) { $this->error('工厂不存在'); } $info = $factory->toArray(); unset($info['api_key_hash']); $info['has_api_key'] = trim((string)$factory['api_key_hash']) !== ''; if ($this->auth->factory_role !== 'owner') { unset($info['invite_code'], $info['api_key_prefix'], $info['api_key_time'], $info['has_api_key']); } $role = (string)$this->auth->factory_role; $this->success('', [ 'factory' => $info, 'role' => $role, 'perms' => $role === 'owner' ? ['*:*:*'] : FactoryPerm::current(), 'catalog' => FactoryPerm::catalog(), ]); } /** * 管理员修改本厂资料 */ public function edit() { if ($this->auth->factory_role !== 'owner') { $this->error('只有工厂管理员可以修改本厂资料'); } $factoryId = FactoryContext::id(); $factory = FactoryModel::get($factoryId); if (!$factory) { $this->error('工厂不存在'); } $name = trim((string)$this->request->post('name', '')); if ($name === '') { $this->error('请填写工厂名称'); } $factory->allowField(['name', 'contact', 'mobile', 'address', 'logo'])->save([ 'name' => mb_substr($name, 0, 100), 'contact' => mb_substr(trim((string)$this->request->post('contact', '')), 0, 50), 'mobile' => mb_substr(trim((string)$this->request->post('mobile', '')), 0, 20), 'address' => mb_substr(trim((string)$this->request->post('address', '')), 0, 255), 'logo' => trim((string)$this->request->post('logo', '')), ]); $info = $factory->toArray(); unset($info['api_key_hash']); $info['has_api_key'] = trim((string)$factory['api_key_hash']) !== ''; $this->success('已保存', ['factory' => $info]); } /** * 生成或重置本厂 Open API Key(明文只返回一次) */ public function reset_api_key() { if ($this->auth->factory_role !== 'owner') { $this->error('只有工厂管理员可以管理 API Key'); } $factoryId = FactoryContext::id(); $factory = FactoryModel::get($factoryId); if (!$factory) { $this->error('工厂不存在'); } $plain = \app\common\library\ps\FactoryApiKey::generate($factory); $this->success('请立即复制保存,关闭后无法再查看完整 Key', [ 'api_key' => $plain, 'api_key_prefix' => $factory['api_key_prefix'], 'api_key_time' => (int)$factory['api_key_time'], ]); } /** * 本厂成员列表(仅管理员) */ public function members() { $this->assertOwner(); $factoryId = FactoryContext::id(); $rows = UserModel::where('factory_id', $factoryId) ->field('id,username,nickname,mobile,factory_role,factory_perms,status,logintime,createtime') ->order('factory_role desc,id asc') ->select(); $list = []; foreach ($rows as $row) { $raw = $row['factory_perms']; $isOwner = $row['factory_role'] === 'owner'; $list[] = [ 'id' => (int)$row['id'], 'username' => (string)$row['username'], 'nickname' => (string)$row['nickname'], 'mobile' => (string)$row['mobile'], 'factory_role' => (string)$row['factory_role'], 'is_owner' => $isOwner, 'status' => (string)$row['status'], 'logintime' => (int)$row['logintime'], 'createtime' => (int)$row['createtime'], 'perms' => $isOwner ? FactoryPerm::keys() : array_values(array_intersect(FactoryPerm::resolve($raw), FactoryPerm::keys())), 'customized' => $raw !== null && $raw !== '', ]; } $this->success('', [ 'rows' => $list, 'catalog' => FactoryPerm::catalog(), 'self_id' => FactoryContext::userId(), ]); } /** * 保存操作员权限 */ public function members_save() { $this->assertOwner(); $user = $this->memberUser(); if ($user['factory_role'] === 'owner') { $this->error('不能限制工厂管理员的权限'); } $keys = FactoryPerm::normalize($this->postList('perms'), false); $user->allowField(['factory_perms'])->save([ 'factory_perms' => json_encode($keys, JSON_UNESCAPED_UNICODE), ]); $this->success('已保存该成员权限', [ 'id' => (int)$user->id, 'perms' => array_values(array_intersect(FactoryPerm::resolve($keys), FactoryPerm::keys())), ]); } /** * 将操作员移出本厂 */ public function members_remove() { $this->assertOwner(); $user = $this->memberUser(); if ($user['factory_role'] === 'owner' || (int)$user->id === FactoryContext::userId()) { $this->error('不能移出工厂管理员'); } $user->allowField(['factory_id', 'factory_role', 'factory_perms'])->save([ 'factory_id' => 0, 'factory_role' => '', 'factory_perms' => null, ]); $this->success('已移出本厂'); } protected function assertOwner() { if (!FactoryContext::isOwner()) { $this->error('只有工厂管理员可以管理成员权限'); } } /** * @return UserModel */ protected function memberUser() { $userId = (int)$this->request->param('user_id', $this->request->param('id')); if ($userId <= 0) { $input = json_decode((string)$this->request->getInput(), true); if (is_array($input)) { $userId = (int)($input['user_id'] ?? $input['id'] ?? 0); } } if ($userId <= 0) { $this->error('请选择成员'); } $user = UserModel::get($userId); if (!$user || (int)$user['factory_id'] !== FactoryContext::id()) { $this->error('成员不存在'); } return $user; } /** * @param string $name * @return array */ protected function postList($name) { $val = $this->request->post($name . '/a'); if (is_array($val)) { return $val; } $val = $this->request->param($name); if (is_array($val)) { return $val; } if (is_string($val) && $val !== '') { $decoded = json_decode($val, true); if (is_array($decoded)) { return $decoded; } return array_values(array_filter(array_map('trim', explode(',', $val)))); } $input = json_decode((string)$this->request->getInput(), true); if (is_array($input) && isset($input[$name]) && is_array($input[$name])) { return $input[$name]; } return []; } /** * 首页看板:本厂数据 + 平台宣传数据 */ public function board() { $factoryId = FactoryContext::id(); $today = strtotime(date('Y-m-d')); $factory = $factoryId ? FactoryModel::get($factoryId) : null; $printToday = \think\Db::name('ps_print_job')->where('createtime', '>=', $today)->sum('copies'); $printTotal = \think\Db::name('ps_print_job')->sum('copies'); $minePrint = $factoryId ? \think\Db::name('ps_print_job')->where('factory_id', $factoryId)->sum('copies') : 0; $this->success('', [ 'platform' => [ 'factory_count' => (int)\think\Db::name('ps_factory')->where('status', 'normal')->count(), 'print_today' => (int)$printToday, 'print_total' => (int)$printTotal, 'template_count' => (int)\think\Db::name('ps_template')->where('status', 'normal')->count(), ], 'mine' => [ 'score' => $factory ? (int)$factory['score'] : 0, 'product' => $factoryId ? (int)\think\Db::name('ps_product')->where('factory_id', $factoryId)->count() : 0, 'workorder' => $factoryId ? (int)\think\Db::name('ps_workorder')->where('factory_id', $factoryId)->count() : 0, 'print' => (int)$minePrint, 'print_today' => $factoryId ? (int)\think\Db::name('ps_print_job')->where('factory_id', $factoryId)->where('createtime', '>=', $today)->sum('copies') : 0, 'template' => $factoryId ? (int)\think\Db::name('ps_template')->where('factory_id', $factoryId)->where('status', 'normal')->count() : 0, ], ]); } }