Factory.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\FactoryContext;
  5. use app\common\library\ps\FactoryPerm;
  6. use app\common\model\ps\Factory as FactoryModel;
  7. use app\common\model\User as UserModel;
  8. /**
  9. * 当前工厂信息
  10. */
  11. class Factory extends Api
  12. {
  13. protected $noNeedLogin = ['board'];
  14. protected $noNeedRight = '*';
  15. public function index()
  16. {
  17. $factoryId = FactoryContext::id();
  18. if (!$factoryId) {
  19. $this->error('账号未绑定工厂');
  20. }
  21. $factory = FactoryModel::get($factoryId);
  22. if (!$factory) {
  23. $this->error('工厂不存在');
  24. }
  25. $info = $factory->toArray();
  26. unset($info['api_key_hash']);
  27. $info['has_api_key'] = trim((string)$factory['api_key_hash']) !== '';
  28. if ($this->auth->factory_role !== 'owner') {
  29. unset($info['invite_code'], $info['api_key_prefix'], $info['api_key_time'], $info['has_api_key']);
  30. }
  31. $role = (string)$this->auth->factory_role;
  32. $this->success('', [
  33. 'factory' => $info,
  34. 'role' => $role,
  35. 'perms' => $role === 'owner' ? ['*:*:*'] : FactoryPerm::current(),
  36. 'catalog' => FactoryPerm::catalog(),
  37. ]);
  38. }
  39. /**
  40. * 管理员修改本厂资料
  41. */
  42. public function edit()
  43. {
  44. if ($this->auth->factory_role !== 'owner') {
  45. $this->error('只有工厂管理员可以修改本厂资料');
  46. }
  47. $factoryId = FactoryContext::id();
  48. $factory = FactoryModel::get($factoryId);
  49. if (!$factory) {
  50. $this->error('工厂不存在');
  51. }
  52. $name = trim((string)$this->request->post('name', ''));
  53. if ($name === '') {
  54. $this->error('请填写工厂名称');
  55. }
  56. $factory->allowField(['name', 'contact', 'mobile', 'address', 'logo'])->save([
  57. 'name' => mb_substr($name, 0, 100),
  58. 'contact' => mb_substr(trim((string)$this->request->post('contact', '')), 0, 50),
  59. 'mobile' => mb_substr(trim((string)$this->request->post('mobile', '')), 0, 20),
  60. 'address' => mb_substr(trim((string)$this->request->post('address', '')), 0, 255),
  61. 'logo' => trim((string)$this->request->post('logo', '')),
  62. ]);
  63. $info = $factory->toArray();
  64. unset($info['api_key_hash']);
  65. $info['has_api_key'] = trim((string)$factory['api_key_hash']) !== '';
  66. $this->success('已保存', ['factory' => $info]);
  67. }
  68. /**
  69. * 生成或重置本厂 Open API Key(明文只返回一次)
  70. */
  71. public function reset_api_key()
  72. {
  73. if ($this->auth->factory_role !== 'owner') {
  74. $this->error('只有工厂管理员可以管理 API Key');
  75. }
  76. $factoryId = FactoryContext::id();
  77. $factory = FactoryModel::get($factoryId);
  78. if (!$factory) {
  79. $this->error('工厂不存在');
  80. }
  81. $plain = \app\common\library\ps\FactoryApiKey::generate($factory);
  82. $this->success('请立即复制保存,关闭后无法再查看完整 Key', [
  83. 'api_key' => $plain,
  84. 'api_key_prefix' => $factory['api_key_prefix'],
  85. 'api_key_time' => (int)$factory['api_key_time'],
  86. ]);
  87. }
  88. /**
  89. * 本厂成员列表(仅管理员)
  90. */
  91. public function members()
  92. {
  93. $this->assertOwner();
  94. $factoryId = FactoryContext::id();
  95. $rows = UserModel::where('factory_id', $factoryId)
  96. ->field('id,username,nickname,mobile,factory_role,factory_perms,status,logintime,createtime')
  97. ->order('factory_role desc,id asc')
  98. ->select();
  99. $list = [];
  100. foreach ($rows as $row) {
  101. $raw = $row['factory_perms'];
  102. $isOwner = $row['factory_role'] === 'owner';
  103. $list[] = [
  104. 'id' => (int)$row['id'],
  105. 'username' => (string)$row['username'],
  106. 'nickname' => (string)$row['nickname'],
  107. 'mobile' => (string)$row['mobile'],
  108. 'factory_role' => (string)$row['factory_role'],
  109. 'is_owner' => $isOwner,
  110. 'status' => (string)$row['status'],
  111. 'logintime' => (int)$row['logintime'],
  112. 'createtime' => (int)$row['createtime'],
  113. 'perms' => $isOwner ? FactoryPerm::keys() : array_values(array_intersect(FactoryPerm::resolve($raw), FactoryPerm::keys())),
  114. 'customized' => $raw !== null && $raw !== '',
  115. ];
  116. }
  117. $this->success('', [
  118. 'rows' => $list,
  119. 'catalog' => FactoryPerm::catalog(),
  120. 'self_id' => FactoryContext::userId(),
  121. ]);
  122. }
  123. /**
  124. * 保存操作员权限
  125. */
  126. public function members_save()
  127. {
  128. $this->assertOwner();
  129. $user = $this->memberUser();
  130. if ($user['factory_role'] === 'owner') {
  131. $this->error('不能限制工厂管理员的权限');
  132. }
  133. $keys = FactoryPerm::normalize($this->postList('perms'), false);
  134. $user->allowField(['factory_perms'])->save([
  135. 'factory_perms' => json_encode($keys, JSON_UNESCAPED_UNICODE),
  136. ]);
  137. $this->success('已保存该成员权限', [
  138. 'id' => (int)$user->id,
  139. 'perms' => array_values(array_intersect(FactoryPerm::resolve($keys), FactoryPerm::keys())),
  140. ]);
  141. }
  142. /**
  143. * 将操作员移出本厂
  144. */
  145. public function members_remove()
  146. {
  147. $this->assertOwner();
  148. $user = $this->memberUser();
  149. if ($user['factory_role'] === 'owner' || (int)$user->id === FactoryContext::userId()) {
  150. $this->error('不能移出工厂管理员');
  151. }
  152. $user->allowField(['factory_id', 'factory_role', 'factory_perms'])->save([
  153. 'factory_id' => 0,
  154. 'factory_role' => '',
  155. 'factory_perms' => null,
  156. ]);
  157. $this->success('已移出本厂');
  158. }
  159. protected function assertOwner()
  160. {
  161. if (!FactoryContext::isOwner()) {
  162. $this->error('只有工厂管理员可以管理成员权限');
  163. }
  164. }
  165. /**
  166. * @return UserModel
  167. */
  168. protected function memberUser()
  169. {
  170. $userId = (int)$this->request->param('user_id', $this->request->param('id'));
  171. if ($userId <= 0) {
  172. $input = json_decode((string)$this->request->getInput(), true);
  173. if (is_array($input)) {
  174. $userId = (int)($input['user_id'] ?? $input['id'] ?? 0);
  175. }
  176. }
  177. if ($userId <= 0) {
  178. $this->error('请选择成员');
  179. }
  180. $user = UserModel::get($userId);
  181. if (!$user || (int)$user['factory_id'] !== FactoryContext::id()) {
  182. $this->error('成员不存在');
  183. }
  184. return $user;
  185. }
  186. /**
  187. * @param string $name
  188. * @return array
  189. */
  190. protected function postList($name)
  191. {
  192. $val = $this->request->post($name . '/a');
  193. if (is_array($val)) {
  194. return $val;
  195. }
  196. $val = $this->request->param($name);
  197. if (is_array($val)) {
  198. return $val;
  199. }
  200. if (is_string($val) && $val !== '') {
  201. $decoded = json_decode($val, true);
  202. if (is_array($decoded)) {
  203. return $decoded;
  204. }
  205. return array_values(array_filter(array_map('trim', explode(',', $val))));
  206. }
  207. $input = json_decode((string)$this->request->getInput(), true);
  208. if (is_array($input) && isset($input[$name]) && is_array($input[$name])) {
  209. return $input[$name];
  210. }
  211. return [];
  212. }
  213. /**
  214. * 首页看板:本厂数据 + 平台宣传数据
  215. */
  216. public function board()
  217. {
  218. $factoryId = FactoryContext::id();
  219. $today = strtotime(date('Y-m-d'));
  220. $factory = $factoryId ? FactoryModel::get($factoryId) : null;
  221. $printToday = \think\Db::name('ps_print_job')->where('createtime', '>=', $today)->sum('copies');
  222. $printTotal = \think\Db::name('ps_print_job')->sum('copies');
  223. $minePrint = $factoryId
  224. ? \think\Db::name('ps_print_job')->where('factory_id', $factoryId)->sum('copies')
  225. : 0;
  226. $this->success('', [
  227. 'platform' => [
  228. 'factory_count' => (int)\think\Db::name('ps_factory')->where('status', 'normal')->count(),
  229. 'print_today' => (int)$printToday,
  230. 'print_total' => (int)$printTotal,
  231. 'template_count' => (int)\think\Db::name('ps_template')->where('status', 'normal')->count(),
  232. ],
  233. 'mine' => [
  234. 'score' => $factory ? (int)$factory['score'] : 0,
  235. 'product' => $factoryId ? (int)\think\Db::name('ps_product')->where('factory_id', $factoryId)->count() : 0,
  236. 'workorder' => $factoryId ? (int)\think\Db::name('ps_workorder')->where('factory_id', $factoryId)->count() : 0,
  237. 'print' => (int)$minePrint,
  238. 'print_today' => $factoryId
  239. ? (int)\think\Db::name('ps_print_job')->where('factory_id', $factoryId)->where('createtime', '>=', $today)->sum('copies')
  240. : 0,
  241. 'template' => $factoryId
  242. ? (int)\think\Db::name('ps_template')->where('factory_id', $factoryId)->where('status', 'normal')->count()
  243. : 0,
  244. ],
  245. ]);
  246. }
  247. }