| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\AdminRole;
- use app\models\AgentApply;
- use app\models\AgentBind;
- use app\models\AuthRole;
- use app\models\District;
- use app\models\Md;
- use app\models\Option;
- use app\models\SaasAdminRole;
- use app\models\SaasAuthRole;
- use app\models\Supplier;
- use app\utils\Notice\NoticeSend;
- use Yii;
- use Exception;
- use yii\base\Model;
- use app\models\Admin;
- use app\models\SaasUser;
- use yii\base\InvalidConfigException;
- use yii\db\StaleObjectException;
- use yii\db\Exception as DbException;
- use app\models\Store;
- use app\models\Salesman;
- use app\models\AgentFrontBind;
- /**
- * LoginForm is the model behind the login form.
- *
- * @property Admin|null $admin This property is read-only.
- *
- */
- class AdminForm extends Model
- {
- public $id;
- public $username;
- public $password;
- public $email;
- public $mobile;
- public $name;
- public $remark;
- public $roles;
- public $avatar;
- public $rate;
- public $loginType = 'username'; // username, email, mobile
- public $status = -1;
- public $area_level;
- public $type; //0 = 区域代理 1 = 产品代理 2 = 前置仓
- protected $admin = false;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['mobile', 'id', 'status', 'area_level'], 'integer'],
- [['loginType'], 'string', 'max' => 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],
- ];
- }
- }
|