| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace addons\BusinessCard\backend\controllers;
- use addons\BusinessCard\common\enums\CardEnums;
- use addons\BusinessCard\common\models\BusinessCardPosition;
- use addons\BusinessCard\common\models\BusinessCardUser;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Exception;
- use Yii;
- class DepartmentController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 部门列表
- *
- * @author hpei
- * @return array|mixed
- */
- public function actionIndex() {
- if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id], ['=', 'status' , StatusEnum::ENABLED], ['=', 'parent_id', '0']],
- 'order' => 'sort',
- 'with' => ['departmentPosition'],
- 'limit' => 10,
- ];
- $list = BusinessCardPosition::listPage($params);
- return $this->success('操作成功', $list);
- }
- return $this->render('index');
- }
- /**
- * 编辑职位
- *
- * @author hpei
- * @return string
- */
- public function actionEditPosition() {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name'], 'required'],
- [['id', 'is_default_position'], 'integer'],
- [['is_default_position'], 'in', 'range' => [0, 1]],
- [['id', 'parent_id'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $params['mall_id'] = $this->mall_id;
- $res = BusinessCardPosition::setData($params);
- if (!$res) return $this->error(BusinessCardPosition::getStaticError());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功', ['id' => $res]);
- }
- /**
- * 编辑部门
- *
- * @author hpei
- * @return string
- */
- public function actionEditDepartment() {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name', 'position'], 'required'],
- [['sort'], 'integer'],
- [['id'], 'safe']
- ]);
- if (empty($params['position'])) {
- return $this->error("部门职位必须填写一个!");
- }
- $positions = array_unique(array_column($params['position'],'name'));
- if (count($params['position']) !== count($positions)) return $this->error("有相同职位,请重新填写!");
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = BusinessCardPosition::newSaveData($params);
- if (!$res) return $this->error(BusinessCardPosition::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 修改成员职位
- *
- * @author hpei
- * @return string
- */
- public function actionEditDepartmentMember() {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['position_id', 'user_ids'], 'required'],
- [['position_id'], 'integer'],
- [['user_ids', 'department_id', 'role_type'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $data = ['mall_id' => $this->mall_id, 'position_id' => $params['position_id']];
- if (isset($params['department_id']) && !empty($params['department_id'])) $data['department_id'] = $params['department_id'];
- if (isset($params['role_type']) && !empty($params['role_type'])) $data['role_type'] = $params['role_type'];
- $res = BusinessCardUser::batchSaves(['user_id' => is_array($params['user_ids']) ? $params['user_ids'] : [$params['user_ids']]], $data);
- if (!$res) return $this->error(BusinessCardUser::getStaticError());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- /**
- * 部门成员
- *
- * @author hpei
- * @return array|mixed
- */
- public function actionMember() {
- if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where = [['=', 'mall_id', $this->mall_id], ['<>','status', StatusEnum::DELETE]];
- // 根据用户id/昵称/手机号查询
- if (isset($get['keyword']) && !empty($get['keyword'])) {
- // 获取用户的ids
- $uids = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->andWhere(['or',['id' => $get['keyword']], ['mobile' => $get['keyword']], ['nickname' => $get['keyword']]])
- ->asArray()->column();
- !empty($uids) && $where[] = ['in', 'user_id', $uids];
- }
- // 根据部门id查询
- if (isset($get['department_id']) && !empty($get['department_id'])) $where[] = ['=', 'department_id', $get['department_id']];
- if (isset($get['role']) && !empty($get['role'])) $where[] = ['=', 'role_type', $get['role']];
- $with = ['user' => function ($query) {// 查询用户信息
- $query->select('id,mobile,nickname,avatar_url');
- }, 'parent' => function ($query) {// 查询推荐人信息
- $query->select('id,mobile,nickname,avatar_url');
- }];
- $params = ['limit' => 10, 'where' => $where, 'order' => 'id desc', 'select' => ['id', 'status', 'user_id', 'parent_id', 'department_id', 'position_id', 'role_type'], 'with' => $with];
- $list = BusinessCardUser::listPage($params);
- // 获取角色,部门,职位列表
- $roleList = CardEnums::getRoleTypeMap();
- $departmentList = array_column(BusinessCardPosition::getDepartment($this->mall_id), null, 'id');
- $positionList = array_column(BusinessCardPosition::getPosition($this->mall_id, ['name', 'id', 'parent_id']), null, 'id');
- $list['roleList'] = $roleList;// 角色类型
- $list['departmentList'] = $departmentList;// 部门列表
- $list['positionList'] = $positionList;// 职位列表
- return $this->success('操作成功', $list);
- }
- return $this->render('member');
- }
- /**
- * 修改成员权限
- *
- * @author hpei
- * @param int user_id role_type
- * @return string
- */
- public function actionSetRole() {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'role_type', 'id'], 'required'],
- [['user_id', 'role_type', 'id'], 'integer'],
- [['role_type'], 'in', 'range' => array_values(array_keys(CardEnums::getRoleTypeMap()))]
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $data = ['user_id' => $params['user_id'], 'role_type' => $params['role_type'], 'id' => $params['id']];
- $res = BusinessCardUser::setData($data);
- if (!$res) return $this->error(BusinessCardUser::getStaticError());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- public function actionDel(){
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- BusinessCardPosition::del($this->mall_id,$params['id']);
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- public function actionChangeIsDefaultPosition()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'is_default_position'], 'required'],
- [['id', 'is_default_position'], 'integer'],
- [['is_default_position'], 'in', 'range' => [0, 1]],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $params['mall_id'] = $this->mall_id;
- BusinessCardPosition::changeIsDefaultPosition($params);
- } catch (\Exception $e) {
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- }
|