增加批量导入组织成员

Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
vilson 2019-07-11 17:08:31 +08:00
parent 2c3a5bdd8a
commit 0f4208a2cd
7 changed files with 208 additions and 43 deletions

View File

@ -3,6 +3,12 @@
namespace app\common\Model; namespace app\common\Model;
use Exception;
use service\RandomService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
/** /**
* 部门成员 * 部门成员
* Class ProjectMember * Class ProjectMember
@ -18,9 +24,9 @@ class DepartmentMember extends CommonModel
* @param int $isOwner 是否拥有者 * @param int $isOwner 是否拥有者
* @param int $isPrincipal 是否负责人 * @param int $isPrincipal 是否负责人
* @return DepartmentMember|MemberAccount * @return DepartmentMember|MemberAccount
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public function inviteMember($accountCode, $departmentCode = '', $isOwner = 0, $isPrincipal = 0) public function inviteMember($accountCode, $departmentCode = '', $isOwner = 0, $isPrincipal = 0)
{ {
@ -28,11 +34,11 @@ class DepartmentMember extends CommonModel
if ($departmentCode) { if ($departmentCode) {
$department = Department::where(['code' => $departmentCode])->find(); $department = Department::where(['code' => $departmentCode])->find();
if (!$department) { if (!$department) {
throw new \Exception('该部门不存在', 1); throw new Exception('该部门不存在', 1);
} }
$hasJoined = self::where(['account_code' => $accountCode, 'department_code' => $departmentCode])->find(); $hasJoined = self::where(['account_code' => $accountCode, 'department_code' => $departmentCode])->find();
if ($hasJoined) { if ($hasJoined) {
throw new \Exception('已加入该部门', 2); throw new Exception('已加入该部门', 2);
} }
$data = [ $data = [
'code' => createUniqueCode('departmentMember'), 'code' => createUniqueCode('departmentMember'),
@ -53,8 +59,8 @@ class DepartmentMember extends CommonModel
} else { } else {
try { try {
$result = MemberAccount::inviteMember($accountCode, $orgCode); $result = MemberAccount::inviteMember($accountCode, $orgCode);
} catch (\Exception $e) { } catch (Exception $e) {
throw new \Exception($e->getMessage(), 3); throw new Exception($e->getMessage(), 3);
} }
return $result; return $result;
} }
@ -64,20 +70,20 @@ class DepartmentMember extends CommonModel
* @param $accountCode * @param $accountCode
* @param $departmentCode * @param $departmentCode
* @return bool * @return bool
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public function removeMember($accountCode, $departmentCode) public function removeMember($accountCode, $departmentCode)
{ {
$orgCode = getCurrentOrganizationCode(); $orgCode = getCurrentOrganizationCode();
$department = Department::where(['code' => $departmentCode])->find(); $department = Department::where(['code' => $departmentCode])->find();
if (!$department) { if (!$department) {
throw new \Exception('该部门不存在', 1); throw new Exception('该部门不存在', 1);
} }
$hasJoined = self::where(['account_code' => $accountCode, 'department_code' => $departmentCode])->find(); $hasJoined = self::where(['account_code' => $accountCode, 'department_code' => $departmentCode])->find();
if (!$hasJoined) { if (!$hasJoined) {
throw new \Exception('尚未加入该部门', 2); throw new Exception('尚未加入该部门', 2);
} }
$result = $hasJoined->delete(); $result = $hasJoined->delete();
$department_codes = self::where(['account_code' => $accountCode, 'organization_code' => $orgCode])->column('department_code'); $department_codes = self::where(['account_code' => $accountCode, 'organization_code' => $orgCode])->column('department_code');
@ -86,4 +92,88 @@ class DepartmentMember extends CommonModel
MemberAccount::update(['department_code' => $department_codes], ['code' => $accountCode]); MemberAccount::update(['department_code' => $department_codes], ['code' => $accountCode]);
return $result; return $result;
} }
/**
* 导入成员
* @param \think\File $file
* @return bool
* @throws Exception
*/
public function uploadFile(\think\File $file)
{
try {
$data = importExcel($file->getInfo()['tmp_name']);
} catch (Exception $e) {
return error('201', $e->getMessage());
}
$count = 0;
if ($data) {
$organizationCode = getCurrentOrganizationCode();
foreach ($data as $key => $item) {
if ($key > 3) {
$name = trim($item['A']);
$email = trim($item['B']);
$departments = trim($item['C']);
$position = trim($item['D']);
$mobile = trim($item['E']);
$password = trim($item['F']);
$description = trim($item['G']);
$member = Member::where(['email' => $email])->find();
if (!$member) {
//注册新账号
$memberData = [
'email' => $email,
'name' => $name,
'account' => RandomService::alnumLowercase(),
'avatar' => 'https://static.vilson.xyz/cover.png',
'status' => 1,
'code' => createUniqueCode('member'),
'password' => $password ? md5($password) : '',
// 'mobile' => $mobile,
];
try {
$result = Member::createMember($memberData);
} catch (Exception $e) {
return error(1, $e->getMessage());
}
$member = Member::get($result->id);
$memberAccount = MemberAccount::inviteMember($member['code'], $organizationCode, $position, $mobile, '', $description);
if (!isError($memberAccount)) {
$count++;
}
} else {
$memberAccount = MemberAccount::where(['member_code' => $member['code'], 'organization_code' => $organizationCode])->find();
}
if ($departments) {
$departmentList = explode(';', $departments);
if ($departmentList) {
foreach ($departmentList as $departmentItems) {
$departmentNames = explode('/', $departmentItems);
if ($departmentNames) {
$department = null;
$pcode = '';
foreach ($departmentNames as $key => $departmentName) {
$department = Department::where(['name' => $departmentNames, 'pcode' => $pcode, 'organization_code' => $organizationCode])->find();
if (!$department) {
break;
}
$pcode = $department['code'];
}
if ($department) {
try {
$this->inviteMember($memberAccount['code'], $department['code']);
} catch (Exception $e) {
return error(2, $e->getMessage());
}
}
}
}
}
}
}
}
}
return $count;
}
} }

View File

@ -2,12 +2,18 @@
namespace app\common\Model; namespace app\common\Model;
use Exception;
use PDOStatement; use PDOStatement;
use service\JwtService; use service\JwtService;
use service\NodeService; use service\NodeService;
use service\RandomService; use service\RandomService;
use think\Db; use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
use think\File; use think\File;
use think\Model;
class Member extends CommonModel class Member extends CommonModel
{ {
@ -23,7 +29,17 @@ class Member extends CommonModel
$list = MemberAccount::where(['member_code' => $member['code']])->order('id asc')->select()->toArray(); $list = MemberAccount::where(['member_code' => $member['code']])->order('id asc')->select()->toArray();
$organizationList = []; $organizationList = [];
if ($list) { if ($list) {
foreach ($list as $item) { foreach ($list as &$item) {
$departments = '';
$departmentCodes = $item['department_code'];
if ($departmentCodes) {
$departmentCodes = explode(',', $departmentCodes);
foreach ($departmentCodes as $departmentCode) {
$department = Department::where(['code' => $departmentCode])->field('name')->find();
$departments .= "{$department['name']} ";
}
}
$item['department'] = $departments;
$organization = Organization::where(['code' => $item['organization_code']])->find(); $organization = Organization::where(['code' => $item['organization_code']])->find();
if ($organization) { if ($organization) {
$organizationList[] = $organization; $organizationList[] = $organization;
@ -51,9 +67,9 @@ class Member extends CommonModel
/** /**
* @param $memberData * @param $memberData
* @return Member * @return Member
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public static function createMember($memberData) public static function createMember($memberData)
{ {
@ -128,10 +144,10 @@ class Member extends CommonModel
/** /**
* 钉钉登录 * 钉钉登录
* @param $userInfo * @param $userInfo
* @return Member|array|PDOStatement|string|\think\Model|null * @return Member|array|PDOStatement|string|Model|null
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public static function dingtalkLogin($userInfo) public static function dingtalkLogin($userInfo)
{ {
@ -186,8 +202,8 @@ class Member extends CommonModel
* @param File $file * @param File $file
* @return array|bool * @return array|bool
* @throws \think\Exception * @throws \think\Exception
* @throws \think\exception\PDOException * @throws PDOException
* @throws \Exception * @throws Exception
*/ */
public function uploadImg(File $file) public function uploadImg(File $file)
{ {

View File

@ -2,8 +2,13 @@
namespace app\common\Model; namespace app\common\Model;
use Exception;
use service\NodeService; use service\NodeService;
use think\Db; use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
use think\File; use think\File;
class MemberAccount extends CommonModel class MemberAccount extends CommonModel
@ -13,9 +18,9 @@ class MemberAccount extends CommonModel
/** /**
* 获取当前用户菜单 * 获取当前用户菜单
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public static function getAuthMenuList() public static function getAuthMenuList()
{ {
@ -29,12 +34,16 @@ class MemberAccount extends CommonModel
* 邀请成员 * 邀请成员
* @param $memberCode * @param $memberCode
* @param $organizationCode * @param $organizationCode
* @param string $position
* @param string $mobile
* @param string $department
* @param string $description
* @return MemberAccount * @return MemberAccount
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws DbException
* @throws \think\exception\DbException * @throws ModelNotFoundException
*/ */
public static function inviteMember($memberCode, $organizationCode) public static function inviteMember($memberCode, $organizationCode, $position = '', $mobile = '', $department = '', $description = '')
{ {
$hasJoined = MemberAccount::where(['member_code' => $memberCode, 'organization_code' => $organizationCode])->find(); $hasJoined = MemberAccount::where(['member_code' => $memberCode, 'organization_code' => $organizationCode])->find();
if ($hasJoined) { if ($hasJoined) {
@ -50,8 +59,9 @@ class MemberAccount extends CommonModel
$authId = $auth['id'];//权限id $authId = $auth['id'];//权限id
} }
$data = [ $data = [
'position' => '资深工程师', 'position' => $position,
'department' => '某某公司-某某某事业群-某某平台部-某某技术部', 'department' => $department ?? '某某公司-某某某事业群-某某平台部-某某技术部',
'description' => $description ?? '',
'code' => createUniqueCode('memberAccount'), 'code' => createUniqueCode('memberAccount'),
'member_code' => $memberCode, 'member_code' => $memberCode,
'organization_code' => $organizationCode, 'organization_code' => $organizationCode,
@ -60,6 +70,7 @@ class MemberAccount extends CommonModel
'status' => 1, 'status' => 1,
'create_time' => nowTime(), 'create_time' => nowTime(),
'name' => $memberDate['name'], 'name' => $memberDate['name'],
'mobile' => $mobile,
'email' => $memberDate['email'], 'email' => $memberDate['email'],
]; ];
return MemberAccount::create($data); return MemberAccount::create($data);
@ -69,8 +80,8 @@ class MemberAccount extends CommonModel
* @param File $file * @param File $file
* @return array|bool * @return array|bool
* @throws \think\Exception * @throws \think\Exception
* @throws \think\exception\PDOException * @throws PDOException
* @throws \Exception * @throws Exception
*/ */
public function uploadImg(File $file) public function uploadImg(File $file)
{ {
@ -100,7 +111,7 @@ class MemberAccount extends CommonModel
/** /**
* @param $accountCode * @param $accountCode
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function del($accountCode) public function del($accountCode)
{ {
@ -116,9 +127,9 @@ class MemberAccount extends CommonModel
DepartmentMember::where(['account_code' => $accountCode, 'organization_code' => $orgCode])->delete(); DepartmentMember::where(['account_code' => $accountCode, 'organization_code' => $orgCode])->delete();
} }
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (Exception $e) {
Db::rollback(); Db::rollback();
throw new \Exception($e->getMessage(), 201); throw new Exception($e->getMessage(), 201);
} }
return true; return true;
} }

View File

@ -65,6 +65,7 @@ class Project extends CommonModel
'name' => $name, 'name' => $name,
'description' => $description, 'description' => $description,
'organization_code' => $orgCode, 'organization_code' => $orgCode,
'task_board_theme' => 'simple',
'cover' => FileService::getFilePrefix() . 'static/image/default/project-cover.png' 'cover' => FileService::getFilePrefix() . 'static/image/default/project-cover.png'
]; ];
$result = self::create($project); $result = self::create($project);

View File

@ -5,7 +5,12 @@ namespace app\project\controller;
use app\common\Model\Member; use app\common\Model\Member;
use app\common\Model\MemberAccount; use app\common\Model\MemberAccount;
use controller\BasicApi; use controller\BasicApi;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Request; use think\facade\Request;
use think\response\Download;
/** /**
* 部门成员 * 部门成员
@ -21,7 +26,7 @@ class DepartmentMember extends BasicApi
} }
/** /**
* @throws \think\exception\DbException * @throws DbException
*/ */
public function index() public function index()
{ {
@ -46,9 +51,9 @@ class DepartmentMember extends BasicApi
/** /**
* 邀请成员查询 * 邀请成员查询
* @throws \think\db\exception\DataNotFoundException * @throws DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws ModelNotFoundException
* @throws \think\exception\DbException * @throws DbException
*/ */
public function searchInviteMember() public function searchInviteMember()
{ {
@ -119,7 +124,7 @@ class DepartmentMember extends BasicApi
} }
try { try {
$this->model->inviteMember($data['accountCode'], $data['departmentCode']); $this->model->inviteMember($data['accountCode'], $data['departmentCode']);
} catch (\Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage(), $e->getCode());; $this->error($e->getMessage(), $e->getCode());;
} }
$this->success(''); $this->success('');
@ -136,9 +141,31 @@ class DepartmentMember extends BasicApi
} }
try { try {
$this->model->removeMember($data['accountCode'], $data['departmentCode']); $this->model->removeMember($data['accountCode'], $data['departmentCode']);
} catch (\Exception $e) { } catch (Exception $e) {
$this->error($e->getMessage(), $e->getCode());; $this->error($e->getMessage(), $e->getCode());;
} }
$this->success(''); $this->success('');
} }
/**
* 下载导入成员模板
* @return Download
*/
public function _downloadTemplate()
{
return download(env('root_path') . 'data/template/importMember.xlsx', '批量导入成员模板.xlsx');
}
/**
* 上传文件
*/
public function uploadFile()
{
$count = $this->model->uploadFile(Request::file('file'));
if (isError($count)) {
$this->error($count['msg']);
}
$this->success('', $count);
}
} }

View File

@ -3,6 +3,7 @@
namespace app\project\controller; namespace app\project\controller;
use app\common\Model\CommonModel; use app\common\Model\CommonModel;
use app\common\Model\Department;
use app\common\Model\Member; use app\common\Model\Member;
use app\common\Model\MemberAccount; use app\common\Model\MemberAccount;
use app\common\Model\Notify; use app\common\Model\Notify;
@ -58,6 +59,18 @@ class Index extends BasicApi
$member = getCurrentMember(); $member = getCurrentMember();
$memberAccount = MemberAccount::where(['member_code' => $member['code'], 'organization_code' => $organizationCode])->find(); $memberAccount = MemberAccount::where(['member_code' => $member['code'], 'organization_code' => $organizationCode])->find();
$member = Member::where(['account' => $member['account']])->order('id asc')->find()->toArray(); $member = Member::where(['account' => $member['account']])->order('id asc')->find()->toArray();
$departments = '';
$departmentCodes = $memberAccount['department_code'];
if ($departmentCodes) {
$departmentCodes = explode(',', $departmentCodes);
foreach ($departmentCodes as $departmentCode) {
$department = Department::where(['code' => $departmentCode])->field('name')->find();
$departments .= "{$department['name']} ";
}
}
$member['position'] = $memberAccount['position'];
$member['department'] = $departments;
$member['account_id'] = $memberAccount['id']; $member['account_id'] = $memberAccount['id'];
$member['is_owner'] = $memberAccount['is_owner']; $member['is_owner'] = $memberAccount['is_owner'];
$member['authorize'] = $memberAccount['authorize']; $member['authorize'] = $memberAccount['authorize'];
@ -66,7 +79,7 @@ class Index extends BasicApi
setCurrentOrganizationCode($organizationCode); setCurrentOrganizationCode($organizationCode);
$list = MemberAccount::getAuthMenuList(); $list = MemberAccount::getAuthMenuList();
$this->success('', $list); $this->success('', ['menuList' => $list, 'member' => $member]);
} }
$this->error('请选择组织'); $this->error('请选择组织');
} }
@ -101,6 +114,10 @@ class Index extends BasicApi
$params = Request::only('mobile,mail,idcard,name,realname,avatar,id'); $params = Request::only('mobile,mail,idcard,name,realname,avatar,id');
$memberModel = new Member(); $memberModel = new Member();
$result = $memberModel->_edit($params, ['id' => Request::post('id')]); $result = $memberModel->_edit($params, ['id' => Request::post('id')]);
if (isset($params['avatar'])) {
$member = Member::get($params['id']);
MemberAccount::update(['avatar' => $params['avatar']], ['member_code' => $member['code']]);
}
if ($result) { if ($result) {
$this->success('基本信息更新成功'); $this->success('基本信息更新成功');
} }
@ -114,6 +131,9 @@ class Index extends BasicApi
*/ */
public function editPassword() public function editPassword()
{ {
var_dump(11);
die;
$memberModel = new Member(); $memberModel = new Member();
$params = Request::only('password,newPassword,confirmPassword,id'); $params = Request::only('password,newPassword,confirmPassword,id');
$member = $memberModel->field('password')->get($params['id'])->toArray(); $member = $memberModel->field('password')->get($params['id'])->toArray();

Binary file not shown.