1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- /**
- * Author: luzheng.liu
- * Time: 2021/2/4 19:38
- */
- namespace app\common\service;
- use app\api\exception\ApiException;
- use app\api\model\AdminModel;
- use think\Exception;
- class AdminService {
- public function encodePasswd(string $passwd) {
- return md5($passwd . '-Bjx14Nb3Le9ghOmM');
- }
- public function addAdmin($input) {
- $model = new AdminModel();
- if (empty($input['password'])) {
- throw new ApiException('密码不为空');
- }
- $info = $model::where(['account' => $input['account']])->find();
- if ($info !== null){
- throw new ApiException('管理员账号不能重复!');
- }
- $info = $model::where(['admin_code' => $input['adminCode']])->find();
- if ($info !== null){
- throw new ApiException('管理员工号不能重复!');
- }
- try {
- $model->startTrans();
- $id = $model->insertGetId([
- 'name' => $input['name'],
- 'account' => $input['account'],
- 'role_id' => $input['roleId'],
- 'password' => $this->encodePasswd($input['password']),
- 'mobile' => $input['mobile'],
- 'admin_code' => $input['adminCode'],
- 'status' => $input['status'] ?? 1,
- 'mac_address' => $input['macAddress'] ?? ''
- ]);
- if(!empty($input['storeIds'])) {
- $model->saveStoreRole($input['storeIds'], (int)$id);
- }
- $model->saveGroupRole($input['groupIds'], (int)$id);
- $model->commit();
- } catch (Exception $e) {
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- return $id;
- }
- public function editAdmin($input) {
- $model = new AdminModel();
- $id = (int)$input['id'];
- $rs = $model::where([['id', '<>', $id], ['admin_code', '=', $input['adminCode']]])->find();
- if (!empty($rs)) {
- throw new ApiException('工号不为空');
- }
- try {
- $model->startTrans();
- $data = [
- 'name' => $input['name'],
- 'account' => $input['account'],
- 'role_id' => $input['roleId'],
- 'mobile' => $input['mobile'],
- 'admin_code' => $input['adminCode'],
- 'status' => $input['status'] ?? 1,
- ];
- if (!empty($input['password'])) {
- $data['password'] = $this->encodePasswd($input['password']);
- }
- $model::where(['id' => $id])->update($data);
- if(!empty($input['storeIds'])) {
- $model->saveStoreRole($input['storeIds'], $id, true);
- }
- $model->saveGroupRole($input['groupIds'], $id,true);
- $model->commit();
- }catch (\Exception $e){
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- }
- }
|