| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\api\controller\ps;
- use app\common\controller\Api;
- use app\common\library\ps\FactoryContext;
- use app\common\model\ps\Factory as FactoryModel;
- /**
- * 当前工厂信息
- */
- class Factory extends Api
- {
- protected $noNeedRight = '*';
- public function index()
- {
- $factoryId = FactoryContext::id();
- if (!$factoryId) {
- $this->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']);
- }
- $this->success('', [
- 'factory' => $info,
- 'role' => $this->auth->factory_role,
- ]);
- }
- /**
- * 管理员修改本厂资料
- */
- 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'],
- ]);
- }
- }
|