60], [['email', 'name', 'username', 'password'], 'string', 'max' => 100], [['avatar', 'remark'], 'string', 'max' => 255], [['email'], 'email'], ['roles', 'each', 'rule' => ['string']], [['rate'], 'number'] ]; } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated */ public function validatePassword($attribute) { if (!$this->hasErrors()) { $admin = $this->getAdmin(); if (!$admin || !$admin->validatePassword($this->password)) { $this->addError($attribute, '用户名或密码错误.'); } } } /** * Logs in a user using the provided username and password. * @return bool whether the user is logged in successfully */ public function login() { return $this->validate(null, false); } /** * Finds admin * * @return Admin|null */ public function getAdmin() { if (! $this->admin) { switch ($this->loginType) { case 'username': $this->admin = Admin::findByUsername($this->username); break; case 'email': $this->admin = Admin::findByEmail($this->email); break; case 'mobile': $this->admin = Admin::findByMobile($this->mobile); break; } } return $this->admin; } public function getAdminByMdId($mdId) { $admin = Admin::find()->where([ 'type' => 'md', 'type_id' => $mdId, 'is_delete' => 0, ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } public function getAdminByMchId($mchId) { $admin = Admin::find()->where([ 'type' => Admin::ADMIN_TYPE_MCH, 'type_id' => $mchId, 'is_delete' => 0, ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } //代理登陆 public function getAdminByAgent($AdminId) { $admin = Admin::find()->where([ 'type' => 'admin', 'type_id' => 0, 'is_delete' => 0, 'id' => $AdminId ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } //前置仓登陆 public function getAdminByFrontAgent($AdminId) { $admin = Admin::find()->where([ 'type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'type_id' => 0, 'is_delete' => 0, 'id' => $AdminId ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } //代理商登陆 public function getAdminByGoodsAgent($AdminId) { $admin = Admin::find()->where([ 'type' => Admin::ADMIN_TYPE_GOODS_AGENT, 'type_id' => 0, 'is_delete' => 0, 'id' => $AdminId ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } public function getAdminBySupplierId($supplier_id) { $supplier = Supplier::findOne(['cloud_supplier_id' => $supplier_id]); $admin = Admin::find()->where([ 'type' => 'supplier', 'type_id' => $supplier->id, 'is_delete' => 0, ])->one(); if ($admin) { $this->admin = $admin; } return $admin; } /** * 获取员工账号列表 * @return array * @throws \yii\base\InvalidConfigException */ public function getAdminList() { $type = ADMIN::ADMIN_TYPE_STAFF; $model = AuthRole::find(); $where = [ 'status' => AuthRole::STATUS_NORMAL, 'store_id' => get_store_id() ]; $admin_where = [ 'is_delete' => Admin::ADMIN_NORMAL, 'type' => $type, 'type_id' => get_store_id(), ]; if ((int)$this->type === 1) { $type = ADMIN::ADMIN_TYPE_SAAS_STAFF; $model = SaasAuthRole::find(); $where = [ 'status' => AuthRole::STATUS_NORMAL ]; $admin_where = [ 'is_delete' => Admin::ADMIN_NORMAL, 'type' => $type ]; } $query = Admin::find() ->where($admin_where) ->select('id, created_at, username, mobile, remark, email, name, avatar'); $pagination = pagination_make($query); $admins = $pagination['list']; foreach ($admins as $key => $admin) { $admins[$key]['created_at'] = date('Y-m-d H:i:s', $admin['created_at']); if ((int)$this->type === 1) { $admins[$key]['roles'] = Admin::findOne($admin['id'])->getSaasRoles()->select('id, name')->all(); } else { $admins[$key]['roles'] = Admin::findOne($admin['id'])->getRoles()->select('id, name')->all(); } } $roles = $model ->select('id, name') ->where($where) ->asArray() ->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $admins, 'roles' => $roles, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ], ]; } /** * 获取账号列表(saas) * @return array * @throws \yii\base\InvalidConfigException */ public function getAdminListBySaas() { $mobile = $this->mobile; $name = $this->name; $user_name = $this->username; $status = $this->status; $area_level = $this->area_level; $type = (int)$this->type; $query = Admin::find() ->alias('a') ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id') ->where(['a.is_delete' => Admin::ADMIN_NORMAL]) ->andWhere(['<>', 'a.username', 'admin']) ->select('a.lat,a.lng,a.area_level,a.province_id,a.city_id,a.district_id,a.id, a.created_at, a.username, a.mobile, a.name, a.remark, a.max_app_count, a.expire_time, a.saas_user_id, a.max_price, a.rate, a.is_enable, su.name as saas_user_name, a.address'); if (in_array($type, [1, 2])) { $where = ['is_delete' => 0, 'type' => $type]; if (get_admin()->username !== 'admin' && get_admin()->type === 'admin' && $type === 1) { //如果为区域代理并且查询的是前置仓、产品代理 //查询当前区域代理的下级中的前置仓、产品代理 $parent_admin_id = get_admin()->id; $where = array_merge($where, ['parent_admin_id' => $parent_admin_id]); } $agent_bind_admin_id = AgentBind::find()->where($where) ->select('admin_id')->column(); $query->andWhere(['a.id' => $agent_bind_admin_id]); } $admin_type = Admin::ADMIN_TYPE_DEFAULT; if ($type) { switch ($type) { case 2: $admin_type = Admin::ADMIN_TYPE_FRONT_AGENT; //前置仓 break; case 3: $admin_type = Admin::ADMIN_TYPE_BD_AGENT; //推广代理 break; default: $admin_type = Admin::ADMIN_TYPE_GOODS_AGENT;//产品代理 break; } $query->andWhere(['a.type' => $admin_type]); } else { $query->andWhere(['a.type' => Admin::ADMIN_TYPE_DEFAULT]); } if ($status > -1) { $query->andWhere(['a.is_enable' => $status]); } if (!empty($mobile)) { $query->andWhere(['like', 'a.mobile', $mobile]); } if ($area_level !== null && in_array($area_level, [0, 1, 2, 3])) { $query->andWhere(['a.area_level' => $area_level]); } if (!empty($name)) { $query->andWhere(['like', 'a.name', $name]); } if (!empty($user_name)) { $query->andWhere(['like', 'a.username', $user_name]); } $pagination = pagination_make($query, true, 'id DESC'); $admins = $pagination['list']; foreach ($admins as &$admin) { $admin['last_count'] = 0; $admin['parent_admin'] = '-'; if ($admin['max_app_count'] > 0) { $store_count = Store::find()->where(['is_delete' => 0]) ->andWhere([ 'or', ['admin_id' => $admin['id']], ['salesman_id' => Salesman::find()->where(['admin_id' => $admin['id'], 'is_delete' => 0])->select('id')] ])->count(); $last_count = $admin['max_app_count'] - $store_count; $admin['last_count'] = $last_count < 0 ? 0 : $last_count; } $admin['area'] = [ 'province_id' => $admin['province_id'], 'city_id' => $admin['city_id'], 'district_id' => $admin['district_id'] ]; if ($type && $admin_type == Admin::ADMIN_TYPE_GOODS_AGENT) { $agent_bind = AgentBind::findOne(['admin_id' => $admin['id'], 'is_delete' => 0, 'type' => 1]); $parent_admin = Admin::findOne($agent_bind->parent_admin_id); $admin['parent_admin'] = $parent_admin->name; $admin['admin_id'] = $parent_admin->id; } if ($type && $admin_type == Admin::ADMIN_TYPE_FRONT_AGENT) { $admin['supplier_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 0, 'status' => 1, 'front_agent_admin_id' => $admin['id']])->count(); $admin['store_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 1, 'status' => 1, 'front_agent_admin_id' => $admin['id']])->count(); $md_front = AgentFrontBind::find() ->where(['is_delete' => 0, 'type' => 1, 'status' => 1, 'front_agent_admin_id' => $admin['id']]) ->andWhere(['>', 'md_id', 0])->select('md_id')->column(); $admin['md_count'] = Md::find()->where(['id' => $md_front, 'is_delete' => 0])->count(); $admin['driver_reset_time'] = Option::get('driver_reset_time_' . $admin['id'], 0, 'agent_front', '00:00')['value']; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $admins, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ], ]; } /** * 修改密码(saas) * * @return void */ public function editPasswordBySaas() { $admin = Admin::findOne($this->id); if (!$admin) { return [ 'code' => 1, 'msg' => '找不到数据', ]; } if (!$this->password) { return [ 'code' => 1, 'msg' => '请提供password参数', ]; } $admin->password = Yii::$app->security->generatePasswordHash($this->password); if ($admin->save()) { return [ 'code' => 0, 'msg' => '修改成功', ]; } return [ 'code' => 0, 'msg' => '修改失败', ]; } /** * 添加或编辑账户(saas) * * @param [type] $params * @return array */ public function addOrEditBySaas($params) { //$params['type'] 1 = 产品代理; 2 = 前置仓; 3 = 推广代理; $t = \Yii::$app->db->beginTransaction(); try { if (get_admin()->type !== 'admin' && get_admin()->type !== 'saas_staff') { return [ 'code' => 1, 'msg' => '身份验证失败,权限错误', ]; } if (get_admin()->type === 'admin' && get_admin()->username !== 'admin') { $params['area_level'] = get_admin()->area_level; //兼容区域代理下不用选择省市区情况 if ((int)$params['type'] == 1) {//仓库独立出去了type == 2 $params['area']['province_id'] = get_admin()->province_id; $params['area']['city_id'] = get_admin()->city_id; $params['area']['district_id'] = get_admin()->district_id; } } //兼容不用选择区域代理情况 if ((int)$params['type'] === 1) { $areaAdmin = Admin::find()->where(['AND', [ "province_id" => $params['area']['province_id'], "city_id" => $params['area']['city_id'], "district_id" => $params['area']['district_id'], 'area_level' => 1, 'is_delete' => 0, 'type' => 'admin', ], ['<>', 'username', 'admin']])->asArray()->one(); if (empty($areaAdmin)) { throw new \Exception('当前区域下没有区级代理'); } $params['admin_id'] = $areaAdmin['id']; } if ($params['admin_id']) { $admin_ = Admin::findOne(['id' => $params['admin_id'], 'type' => 'admin']); $params['area_level'] = $admin_->area_level; } else { $admin_ = get_admin(); } $isEdit = isset($params['id']) && $params['id'] > 0; $admin_query = Admin::find()->where(['province_id' => $params['area']['province_id'], 'is_delete' => 0, 'type' => 'admin']); if ((int)$params['area_level'] === 3) { $admin_query->andWhere(['area_level' => 3]); } elseif ((int)$params['area_level'] === 2) { $admin_query->andWhere(['city_id' => $params['area']['city_id'], 'area_level' => 2]); } elseif ((int)$params['area_level'] === 1) { $admin_query->andWhere(['city_id' => $params['area']['city_id'], 'district_id' => $params['area']['district_id'], 'area_level' => 1]); } //判断同身份是否存在相同用户 if ($params['saas_user_id']) { $auth_name = ""; //区域代理 if ($params['area_level'] > 0 && empty($params['type'])) { $empty_admin_query = Admin::find()->where(['AND', ['saas_user_id' => $params['saas_user_id'], 'type' => 'admin', 'is_delete' => 0], ['>', 'area_level', 0] ]); $auth_name = "区域代理"; } if (!empty($params['type'])) { //产品代理 if (intval($params['type']) === 1) { $empty_admin_query = Admin::find()->where([ 'saas_user_id' => $params['saas_user_id'], 'type' => 'goods_agent', 'is_delete' => 0 ]); $auth_name = "产品代理"; } //前置仓 if (intval($params['type']) === 2) { $empty_admin_query = Admin::find()->where([ 'saas_user_id' => $params['saas_user_id'], 'type' => 'front_agent', 'is_delete' => 0 ]); $auth_name = "前置仓"; } //推广代理 if (intval($params['type']) === 3) { $empty_admin_query = Admin::find()->where([ 'saas_user_id' => $params['saas_user_id'], 'type' => 'bd_agent', 'is_delete' => 0 ]); $auth_name = "推广代理"; } } if ($isEdit) { //判断如果是修改则判断不等于自己 if (!empty($empty_admin_query)) { $admin = $empty_admin_query->andWhere(['<>', 'id', $params['id']])->asArray()->one(); if (!empty($admin)) { //如果存在则表示存在相同用户给提示 return [ 'code' => 1, 'msg' => '同用户存在相同的身份:' . $auth_name . ';名称:' . $admin['name'] . ';账户:' . $admin['username'], ]; } } } } if (Admin::find()->where(['username' => $params['username'], 'is_delete' => 0])->andWhere(['<>', 'id', $params['id'] ?? 0])->one()) { return [ 'code' => 1, 'msg' => '用户名已经存在', ]; } // if (! empty($params['mobile'])) { // if (Admin::find()->where(['mobile' => $params['mobile'], 'is_delete' => 0])->andWhere(['<>', 'id', $params['id'] ?? 0])->one()) { // return [ // 'code' => 1, // 'msg' => '手机号已经存在', // ]; // } // } if ($isEdit) { if (!in_array((int)$params['type'], [1, 2, 3])) { $is_area_admin = $admin_query->andWhere(['<>', 'id', $params['id']])->one(); if ($is_area_admin) { return [ 'code' => 1, 'msg' => '当前区域内已存在同等级代理', ]; } } $admin = Admin::findOne($params['id']); } else { if (!in_array((int)$params['type'], [1, 2, 3])) { $is_area_admin = $admin_query->one(); if ($is_area_admin) { return [ 'code' => 1, 'msg' => '当前区域内已存在同等级代理', ]; } } $admin = new Admin(); } // if ($params['max_price'] < 0) { // return [ // 'code' => 1, // 'msg' => '允许设置的最大佣金数据错误', // ]; // } elseif ($params['username'] == 'admin') { // return [ // 'code' => 1, // 'msg' => '超管不允许设置最大佣金', // ]; // } //判断是否越级创建产品代理以及中央仓 20240723中央仓独立出去 $params['type'] == 2 if ((int)$params['type'] == 1 && $admin_->username !== 'admin' && $admin_->type === 'admin') { $province_id = $params['area']['province_id']; $city_id = $params['area']['city_id']; $district_id = $params['area']['district_id']; $open = false; $error_area = [ 'province_id' => '', 'city_id' => '', 'district_id' => '' ]; if ((int)$admin_->province_id !== (int)$province_id) { $open = true; $error_area['province_id'] = $admin_->province_id; } switch ((int)$admin_->area_level) { case 1: //区 if (((int)$admin_->city_id !== (int)$city_id || (int)$admin_->district_id !== (int)$district_id) || (int)$params['area_level'] !== 1) { $open = true; $error_area['province_id'] = $admin_->province_id; $error_area['city_id'] = $admin_->city_id; $error_area['district_id'] = $admin_->district_id; } break; case 2: //市 if (((int)$admin_->city_id !== (int)$city_id) || (int)$params['area_level'] > 2) { $open = true; $error_area['province_id'] = $admin_->province_id; $error_area['city_id'] = $admin_->city_id; } break; } if ($open) { $error_area = array_values($error_area); $district = District::find()->where(['id' => $error_area])->select('name')->column(); $district_str = implode(' ', $district); return [ 'code' => 1, 'msg' => '不允许越级创建,可选范围' . $district_str, ]; } } if (isset($params['is_enable'])) { if (!in_array($params['is_enable'], [0, 1])) { return [ 'code' => 1, 'msg' => '状态参数不正确', ]; } $admin->is_enable = $params['is_enable']; } if (isset($params['area']['province_id']) && $params['area']['province_id']) { $admin->province_id = $params['area']['province_id']; } if (isset($params['area']['city_id']) && $params['area']['city_id']) { $admin->city_id = $params['area']['city_id']; } if (isset($params['area']['district_id']) && $params['area']['district_id']) { $admin->district_id = $params['area']['district_id']; } if (isset($params['address']) && $params['address']) { $admin->address = $params['address']; } if (isset($params['lat']) && $params['lat']) { $admin->lat = $params['lat']; } if (isset($params['lng']) && $params['lng']) { $admin->lng = $params['lng']; } $admin->area_level = $params['area_level']; $admin->username = $params['username']; if (isset($params['password'])) { $admin->password = Yii::$app->security->generatePasswordHash($params['password']); } $admin->mobile = $params['mobile']; $admin->remark = $params['remark']; $admin->rate = $params['rate'] ?: 0; $admin->name = $params['name']; $admin->max_app_count = $params['max_app_count']; $admin->saas_user_id = $params['saas_user_id']; $admin->max_price = 0.00; //去掉代理最大佣金限制设置 if (!$params['expire_time_always']) { $admin->expire_time = strtotime($params['expire_time']); } else { $admin->expire_time = 0; } if ((int)$params['type'] === 3) { $admin->type = Admin::ADMIN_TYPE_BD_AGENT; } if ($admin->save()) { if (in_array((int)$params['type'], [1, 2])) { if ((int)$params['type'] == 1) { $parent_admin_id = $admin_->id; if (!($admin_->username !== 'admin' && $admin_->type === 'admin')) { return [ 'code' => 1, 'msg' => '权限错误!', ]; } } else { if ($params['driver_reset_time']) { Option::set('driver_reset_time_' . $admin->id, $params['driver_reset_time'], 0, 'agent_front'); } } $agent_bind = new AgentBind(); $agent_bind->admin_id = $admin->id; if ($isEdit) { $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]); } $agent_bind->parent_admin_id = $parent_admin_id ?? 0; $agent_bind->type = (int)$params['type']; if (!$agent_bind->save()) { throw new \Exception(json_encode($agent_bind->errors)); } $admin->type = (int)$params['type'] === 1 ? Admin::ADMIN_TYPE_GOODS_AGENT : Admin::ADMIN_TYPE_FRONT_AGENT; $admin->type_id = $agent_bind->id; $admin->save(); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!', ]; } return [ 'code' => 1, 'msg' => '操作失败!', ]; } catch (\Exception $e) { // var_dump($e);die; $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function setAgentApplyStatus($params) { $t = \Yii::$app->db->beginTransaction(); try { $agent_apply = AgentApply::findOne($params['id']); if (!$agent_apply) { throw new \Exception('申请单信息错误'); } if (in_array($agent_apply->status, [1, 2])) { throw new \Exception('申请单状态信息错误'); } if (!in_array($params['status'], [1, 2])) { throw new \Exception('参数信息错误'); } $agent_apply->status = (int)$params['status']; if ((int)$params['status'] === 1) { $admin = new Admin(); $admin->username = $agent_apply->username; $admin->saas_user_id = $agent_apply->saas_user_id; $admin->password = Yii::$app->security->generatePasswordHash($agent_apply->password); $admin->name = $agent_apply->name; $admin->province_id = $agent_apply->province_id; $admin->city_id = $agent_apply->city_id; $admin->district_id = $agent_apply->district_id; $admin->address = $agent_apply->address; $admin->area_level = $agent_apply->area_level; $admin->type = 'admin'; if ((int)$agent_apply->agent_type === 1) { $admin->type = "bd_agent"; } if ((int)$agent_apply->agent_type === 2) { $admin->type = "goods_agent"; } $admin->mobile = $agent_apply->mobile; $admin->address = $agent_apply->address; $admin->lat = $agent_apply->lat; $admin->lng = $agent_apply->lng; if (!$admin->save()) { throw new \Exception(json_encode($admin->errors)); } $agent_apply->admin_id = $admin->id; if ((int)$agent_apply->agent_type === 2) { $areaAdmin = Admin::find()->where(['AND', [ "province_id" => $agent_apply->province_id, "city_id" => $agent_apply->city_id, "district_id" => $agent_apply->district_id, 'area_level' => 1, 'is_delete' => 0, 'type' => 'admin', ], ['<>', 'username', 'admin']])->asArray()->one(); if (empty($areaAdmin)) { throw new \Exception('当前区域下没有区域代理'); } $agent_bind = new AgentBind(); $agent_bind->admin_id = $admin->id; $agent_bind->parent_admin_id = $areaAdmin['id']; $agent_bind->type = 1; if (!$agent_bind->save()) { throw new \Exception(json_encode($agent_bind->errors)); } $admin->type_id = $agent_bind->id; if (!$admin->save()) { throw new \Exception(json_encode($admin->errors)); } } } else { $agent_apply->reason = $params['reason']; } if (!$agent_apply->save()) { throw new \Exception(json_encode($agent_apply->errors)); } //申请通过发送短信 $res = NoticeSend::AgentExamine($agent_apply->saas_user_id); if (isset($res['code']) && $res['code'] !== 0) { debug_log($res, 'sms.log'); } $t->commit(); return [ 'code' => 0, 'msg' => "操作成功" ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 0, 'msg' => $e->getMessage(), ]; } } public function getAgentApplyList($params) { try { $status = $params['status']; $name = $params['name']; $agent_type = $params['agent_type']; $query = AgentApply::find()->alias('a')->where(['a.is_delete' => 0]) ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id'); if ((int)$status !== -1 && $status !== null) { $query->andWhere(['a.status' => $status]); } if ($name) { $query->andWhere(['LIKE', 'a.name', $name]); } if ((int)$agent_type !== -1 && $agent_type !== null && in_array($agent_type, [0, 1, 2])) { $query->andWhere(['a.agent_type' => $agent_type]); } $query->select('a.id, a.status, a.agent_type, a.created_at, su.name, su.avatar, a.name, a.username, a.password, a.mobile') ->orderBy('a.created_at desc'); $page = pagination_make($query); foreach ($page['list'] as &$item) { $item['status'] *= 1; $item['agent_type'] *= 1; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $page['list'], 'pageNo' => $page['pageNo'], 'totalCount' => $page['totalCount'] ] ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage(), ]; } } /** * 修改管理员状态 * @param mixed $params * @return (int|string)[] * @throws InvalidConfigException * @throws StaleObjectException * @throws DbException */ public function editStatusBySaas($params) { if (!isset($params['id']) || !isset($params['is_enable'])) { return [ 'code' => 1, 'msg' => '缺少必要参数', ]; } if (!in_array($params['is_enable'], [0, 1])) { return [ 'code' => 1, 'msg' => '状态参数不正确', ]; } $admin = Admin::findOne($params['id']); if (!$admin) { return [ 'code' => 1, 'msg' => '管理员未找到', ]; } $admin->is_enable = $params['is_enable']; $admin->save(); Salesman::updateAll(['status'=>$params['is_enable'],'updated_at'=>time()],['admin_id'=>$params['id'],'is_delete'=>0]); return [ 'code' => 0, 'msg' => '操作成功!', ]; } /** * @param int $expireIn * @return mixed * @throws \yii\base\InvalidConfigException */ public function generateToken(int $expireIn = 86400, $store_id = null) { $admin = $this->getAdmin(); if (!$admin) { throw new \Exception('管理员不存在或已被删除'); } $roles = 'super_admin'; if ($admin->username !== 'admin') { $roles = $admin->getRoles()->select('id')->column(); if ($admin->type === Admin::ADMIN_TYPE_SAAS_STAFF) { $roles = $admin->getSaasRoles()->select('id')->column(); } $roles = implode(',', $roles); } $time = time(); $payload = [ 'iss' => Yii::$app->request->hostInfo, 'iat' => $time, 'exp' => $time + $expireIn, 'admin_id' => $admin->id, 'username' => $admin->username, 'store_id' => $store_id ?: $admin->store_id, 'roles' => $roles, ]; return Yii::$app->jwt->createToken($payload); } /** * 创建账号 * @return array */ public function createAdmin() { try { if ($this->validate()) { $admin = new Admin(); if (Admin::findOne(['username' => $this->username, 'is_delete' => 0])) { return [ 'code' => 1, 'msg' => '用户名已经存在', ]; } $admin->username = $this->username; if ($this->email) { if (Admin::findOne(['email' => $this->email, 'is_delete' => 0])) { return [ 'code' => 1, 'msg' => '邮箱已经存在', ]; } $admin->email = $this->email; } if ($this->name) { $admin->name = $this->name; } // if ($this->mobile) { // if (Admin::findOne(['mobile' => $this->mobile, 'is_delete' => 0])) { // return [ // 'code' => 1, // 'msg' => '手机号已经存在', // ]; // } // $admin->mobile = $this->mobile; // } $admin->password = Yii::$app->security->generatePasswordHash($this->password); if ($this->remark) { $admin->remark = $this->remark; } $type = Admin::ADMIN_TYPE_STAFF; if ((int)$this->type === 1) { $type = Admin::ADMIN_TYPE_SAAS_STAFF; } $admin->type = $type; $admin->type_id = get_store_id(); if ($admin->save()) { if (count($this->roles) > 0) { foreach ($this->roles as $roleId) { $role = AuthRole::findOne($roleId); if ((int)$this->type === 1) { $role = SaasAuthRole::findOne($roleId); if ($role) { $admin->link('saasRoles', $role); } } else { if ($role) { $admin->link('roles', $role); } } } } return [ 'code' => 0, 'msg' => '创建成功' ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } catch (Exception $e) { return [ 'code' => 1, 'msg' => '出现错误,账号创建失败', ]; } } public function editAdmin() { try { if ($this->validate()) { if (! $this->id) { return [ 'code' => 1, 'msg' => '缺少账号ID', ]; } $admin = Admin::findOne($this->id); if (Admin::find()->where(['username' => $this->username])->andWhere(['<>', 'id', $this->id])->one()) { return [ 'code' => 1, 'msg' => '用户名已经存在', ]; } if ($admin->username == 'admin' && $this->username != 'admin') { return [ 'code' => 1, 'msg' => '超级管理员账号不允许被修改', ]; } $admin->username = $this->username; if ($this->email) { if (Admin::find()->where(['email' => $this->email])->andWhere(['<>', 'id', $this->id])->one()) { return [ 'code' => 1, 'msg' => '邮箱已经存在', ]; } $admin->email = $this->email; } if ($this->name) { $admin->name = $this->name; } if ($this->mobile) { // if (Admin::find()->where(['mobile' => $this->mobile])->andWhere(['<>', 'id', $this->id])->one()) { // return [ // 'code' => 1, // 'msg' => '手机号已经存在', // ]; // } $admin->mobile = $this->mobile; } if ($this->password) { $admin->password = Yii::$app->security->generatePasswordHash($this->password); } if ($this->remark) { $admin->remark = $this->remark; } if ($admin->save()) { if ((int)$this->type === 1) { if ($admin->getSaasRoles()->count() > 0) { // 先解除该账号所有关联,再创建新关联 SaasAdminRole::deleteAll(['admin_id' => $this->id]); } } else { if ($admin->getRoles()->count() > 0) { // 先解除该账号所有关联,再创建新关联 AdminRole::deleteAll(['admin_id' => $this->id]); } } if (count($this->roles) > 0) { foreach ($this->roles as $roleId) { $role = AuthRole::findOne($roleId); if ((int)$this->type === 1) { $role = SaasAuthRole::findOne($roleId); if ($role) { $admin->link('saasRoles', $role); } } else { if ($role) { $admin->link('roles', $role); } } } } return [ 'code' => 0, 'msg' => '编辑成功' ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } catch (Exception $e) { return [ 'code' => 1, 'msg' => '出现错误,账号编辑失败' . $e->getMessage(), ]; } } /** * 删除账号(软删除) * @return array */ public function deleteAdmin() { if (! $this->id) { return [ 'code' => 1, 'msg' => '请提供账号ID', ]; } $admin = Admin::findOne($this->id); if (! $admin) { return [ 'code' => 1, 'msg' => '账号不存在', ]; } $admin->is_delete = Admin::ADMIN_DELETE; if ($admin->save()) { $agent_bind = AgentBind::findOne(['admin_id' => $this->id]); if ($agent_bind) { $agent_bind->is_delete = 1; if (!$agent_bind->save()) { return [ 'code' => 1, 'msg' => implode(';', array_values($agent_bind->firstErrors)) ]; } } Salesman::updateAll(['is_delete'=>1,'updated_at'=>time()],['admin_id'=>$this->id,'is_delete'=>0]); AgentApply::deleteAll(['admin_id' => $this->id, 'is_delete' => 0]); return [ 'code' => 0, 'msg' => '删除成功', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } }