AdminForm.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\AdminRole;
  9. use app\models\AgentApply;
  10. use app\models\AgentBind;
  11. use app\models\AuthRole;
  12. use app\models\District;
  13. use app\models\Md;
  14. use app\models\Option;
  15. use app\models\SaasAdminRole;
  16. use app\models\SaasAuthRole;
  17. use app\models\Supplier;
  18. use app\utils\Notice\NoticeSend;
  19. use Yii;
  20. use Exception;
  21. use yii\base\Model;
  22. use app\models\Admin;
  23. use app\models\SaasUser;
  24. use yii\base\InvalidConfigException;
  25. use yii\db\StaleObjectException;
  26. use yii\db\Exception as DbException;
  27. use app\models\Store;
  28. use app\models\Salesman;
  29. use app\models\AgentFrontBind;
  30. /**
  31. * LoginForm is the model behind the login form.
  32. *
  33. * @property Admin|null $admin This property is read-only.
  34. *
  35. */
  36. class AdminForm extends Model
  37. {
  38. public $id;
  39. public $username;
  40. public $password;
  41. public $email;
  42. public $mobile;
  43. public $name;
  44. public $remark;
  45. public $roles;
  46. public $avatar;
  47. public $rate;
  48. public $loginType = 'username'; // username, email, mobile
  49. public $status = -1;
  50. public $area_level;
  51. public $type; //0 = 区域代理 1 = 产品代理 2 = 前置仓
  52. protected $admin = false;
  53. /**
  54. * @return array the validation rules.
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['mobile', 'id', 'status', 'area_level'], 'integer'],
  60. [['loginType'], 'string', 'max' => 60],
  61. [['email', 'name', 'username', 'password'], 'string', 'max' => 100],
  62. [['avatar', 'remark'], 'string', 'max' => 255],
  63. [['email'], 'email'],
  64. ['roles', 'each', 'rule' => ['string']],
  65. [['rate'], 'number']
  66. ];
  67. }
  68. /**
  69. * Validates the password.
  70. * This method serves as the inline validation for password.
  71. *
  72. * @param string $attribute the attribute currently being validated
  73. */
  74. public function validatePassword($attribute)
  75. {
  76. if (!$this->hasErrors()) {
  77. $admin = $this->getAdmin();
  78. if (!$admin || !$admin->validatePassword($this->password)) {
  79. $this->addError($attribute, '用户名或密码错误.');
  80. }
  81. }
  82. }
  83. /**
  84. * Logs in a user using the provided username and password.
  85. * @return bool whether the user is logged in successfully
  86. */
  87. public function login()
  88. {
  89. return $this->validate(null, false);
  90. }
  91. /**
  92. * Finds admin
  93. *
  94. * @return Admin|null
  95. */
  96. public function getAdmin()
  97. {
  98. if (! $this->admin) {
  99. switch ($this->loginType) {
  100. case 'username':
  101. $this->admin = Admin::findByUsername($this->username);
  102. break;
  103. case 'email':
  104. $this->admin = Admin::findByEmail($this->email);
  105. break;
  106. case 'mobile':
  107. $this->admin = Admin::findByMobile($this->mobile);
  108. break;
  109. }
  110. }
  111. return $this->admin;
  112. }
  113. public function getAdminByMdId($mdId)
  114. {
  115. $admin = Admin::find()->where([
  116. 'type' => 'md',
  117. 'type_id' => $mdId,
  118. 'is_delete' => 0,
  119. ])->one();
  120. if ($admin) {
  121. $this->admin = $admin;
  122. }
  123. return $admin;
  124. }
  125. public function getAdminByMchId($mchId)
  126. {
  127. $admin = Admin::find()->where([
  128. 'type' => Admin::ADMIN_TYPE_MCH,
  129. 'type_id' => $mchId,
  130. 'is_delete' => 0,
  131. ])->one();
  132. if ($admin) {
  133. $this->admin = $admin;
  134. }
  135. return $admin;
  136. }
  137. //代理登陆
  138. public function getAdminByAgent($AdminId)
  139. {
  140. $admin = Admin::find()->where([
  141. 'type' => 'admin',
  142. 'type_id' => 0,
  143. 'is_delete' => 0,
  144. 'id' => $AdminId
  145. ])->one();
  146. if ($admin) {
  147. $this->admin = $admin;
  148. }
  149. return $admin;
  150. }
  151. //前置仓登陆
  152. public function getAdminByFrontAgent($AdminId)
  153. {
  154. $admin = Admin::find()->where([
  155. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT,
  156. 'type_id' => 0,
  157. 'is_delete' => 0,
  158. 'id' => $AdminId
  159. ])->one();
  160. if ($admin) {
  161. $this->admin = $admin;
  162. }
  163. return $admin;
  164. }
  165. //代理商登陆
  166. public function getAdminByGoodsAgent($AdminId)
  167. {
  168. $admin = Admin::find()->where([
  169. 'type' => Admin::ADMIN_TYPE_GOODS_AGENT,
  170. 'type_id' => 0,
  171. 'is_delete' => 0,
  172. 'id' => $AdminId
  173. ])->one();
  174. if ($admin) {
  175. $this->admin = $admin;
  176. }
  177. return $admin;
  178. }
  179. public function getAdminBySupplierId($supplier_id)
  180. {
  181. $supplier = Supplier::findOne(['cloud_supplier_id' => $supplier_id]);
  182. $admin = Admin::find()->where([
  183. 'type' => 'supplier',
  184. 'type_id' => $supplier->id,
  185. 'is_delete' => 0,
  186. ])->one();
  187. if ($admin) {
  188. $this->admin = $admin;
  189. }
  190. return $admin;
  191. }
  192. /**
  193. * 获取员工账号列表
  194. * @return array
  195. * @throws \yii\base\InvalidConfigException
  196. */
  197. public function getAdminList()
  198. {
  199. $type = ADMIN::ADMIN_TYPE_STAFF;
  200. $model = AuthRole::find();
  201. $where = [
  202. 'status' => AuthRole::STATUS_NORMAL,
  203. 'store_id' => get_store_id()
  204. ];
  205. $admin_where = [
  206. 'is_delete' => Admin::ADMIN_NORMAL,
  207. 'type' => $type,
  208. 'type_id' => get_store_id(),
  209. ];
  210. if ((int)$this->type === 1) {
  211. $type = ADMIN::ADMIN_TYPE_SAAS_STAFF;
  212. $model = SaasAuthRole::find();
  213. $where = [
  214. 'status' => AuthRole::STATUS_NORMAL
  215. ];
  216. $admin_where = [
  217. 'is_delete' => Admin::ADMIN_NORMAL,
  218. 'type' => $type
  219. ];
  220. }
  221. $query = Admin::find()
  222. ->where($admin_where)
  223. ->select('id, created_at, username, mobile, remark, email, name, avatar');
  224. $pagination = pagination_make($query);
  225. $admins = $pagination['list'];
  226. foreach ($admins as $key => $admin) {
  227. $admins[$key]['created_at'] = date('Y-m-d H:i:s', $admin['created_at']);
  228. if ((int)$this->type === 1) {
  229. $admins[$key]['roles'] = Admin::findOne($admin['id'])->getSaasRoles()->select('id, name')->all();
  230. } else {
  231. $admins[$key]['roles'] = Admin::findOne($admin['id'])->getRoles()->select('id, name')->all();
  232. }
  233. }
  234. $roles = $model
  235. ->select('id, name')
  236. ->where($where)
  237. ->asArray()
  238. ->all();
  239. return [
  240. 'code' => 0,
  241. 'msg' => 'success',
  242. 'data' => [
  243. 'data' => $admins,
  244. 'roles' => $roles,
  245. 'pageNo' => $pagination['pageNo'],
  246. 'totalCount' => $pagination['totalCount'],
  247. ],
  248. ];
  249. }
  250. /**
  251. * 获取账号列表(saas)
  252. * @return array
  253. * @throws \yii\base\InvalidConfigException
  254. */
  255. public function getAdminListBySaas()
  256. {
  257. $mobile = $this->mobile;
  258. $name = $this->name;
  259. $user_name = $this->username;
  260. $status = $this->status;
  261. $area_level = $this->area_level;
  262. $type = (int)$this->type;
  263. $query = Admin::find()
  264. ->alias('a')
  265. ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id')
  266. ->where(['a.is_delete' => Admin::ADMIN_NORMAL])
  267. ->andWhere(['<>', 'a.username', 'admin'])
  268. ->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');
  269. if (in_array($type, [1, 2])) {
  270. $where = ['is_delete' => 0, 'type' => $type];
  271. if (get_admin()->username !== 'admin' && get_admin()->type === 'admin' && $type === 1) {
  272. //如果为区域代理并且查询的是前置仓、产品代理
  273. //查询当前区域代理的下级中的前置仓、产品代理
  274. $parent_admin_id = get_admin()->id;
  275. $where = array_merge($where, ['parent_admin_id' => $parent_admin_id]);
  276. }
  277. $agent_bind_admin_id = AgentBind::find()->where($where)
  278. ->select('admin_id')->column();
  279. $query->andWhere(['a.id' => $agent_bind_admin_id]);
  280. }
  281. $admin_type = Admin::ADMIN_TYPE_DEFAULT;
  282. if ($type) {
  283. switch ($type) {
  284. case 2:
  285. $admin_type = Admin::ADMIN_TYPE_FRONT_AGENT; //前置仓
  286. break;
  287. case 3:
  288. $admin_type = Admin::ADMIN_TYPE_BD_AGENT; //推广代理
  289. break;
  290. default:
  291. $admin_type = Admin::ADMIN_TYPE_GOODS_AGENT;//产品代理
  292. break;
  293. }
  294. $query->andWhere(['a.type' => $admin_type]);
  295. } else {
  296. $query->andWhere(['a.type' => Admin::ADMIN_TYPE_DEFAULT]);
  297. }
  298. if ($status > -1) {
  299. $query->andWhere(['a.is_enable' => $status]);
  300. }
  301. if (!empty($mobile)) {
  302. $query->andWhere(['like', 'a.mobile', $mobile]);
  303. }
  304. if ($area_level !== null && in_array($area_level, [0, 1, 2, 3])) {
  305. $query->andWhere(['a.area_level' => $area_level]);
  306. }
  307. if (!empty($name)) {
  308. $query->andWhere(['like', 'a.name', $name]);
  309. }
  310. if (!empty($user_name)) {
  311. $query->andWhere(['like', 'a.username', $user_name]);
  312. }
  313. $pagination = pagination_make($query, true, 'id DESC');
  314. $admins = $pagination['list'];
  315. foreach ($admins as &$admin) {
  316. $admin['last_count'] = 0;
  317. $admin['parent_admin'] = '-';
  318. if ($admin['max_app_count'] > 0) {
  319. $store_count = Store::find()->where(['is_delete' => 0])
  320. ->andWhere([
  321. 'or',
  322. ['admin_id' => $admin['id']],
  323. ['salesman_id' => Salesman::find()->where(['admin_id' => $admin['id'], 'is_delete' => 0])->select('id')]
  324. ])->count();
  325. $last_count = $admin['max_app_count'] - $store_count;
  326. $admin['last_count'] = $last_count < 0 ? 0 : $last_count;
  327. }
  328. $admin['area'] = [
  329. 'province_id' => $admin['province_id'],
  330. 'city_id' => $admin['city_id'],
  331. 'district_id' => $admin['district_id']
  332. ];
  333. if ($type && $admin_type == Admin::ADMIN_TYPE_GOODS_AGENT) {
  334. $agent_bind = AgentBind::findOne(['admin_id' => $admin['id'], 'is_delete' => 0, 'type' => 1]);
  335. $parent_admin = Admin::findOne($agent_bind->parent_admin_id);
  336. $admin['parent_admin'] = $parent_admin->name;
  337. $admin['admin_id'] = $parent_admin->id;
  338. }
  339. if ($type && $admin_type == Admin::ADMIN_TYPE_FRONT_AGENT) {
  340. $admin['supplier_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 0, 'status' => 1, 'front_agent_admin_id' => $admin['id']])->count();
  341. $admin['store_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 1, 'status' => 1, 'front_agent_admin_id' => $admin['id']])->count();
  342. $md_front = AgentFrontBind::find()
  343. ->where(['is_delete' => 0, 'type' => 1, 'status' => 1, 'front_agent_admin_id' => $admin['id']])
  344. ->andWhere(['>', 'md_id', 0])->select('md_id')->column();
  345. $admin['md_count'] = Md::find()->where(['id' => $md_front, 'is_delete' => 0])->count();
  346. $admin['driver_reset_time'] = Option::get('driver_reset_time_' . $admin['id'], 0,
  347. 'agent_front', '00:00')['value'];
  348. }
  349. }
  350. return [
  351. 'code' => 0,
  352. 'msg' => 'success',
  353. 'data' => [
  354. 'data' => $admins,
  355. 'pageNo' => $pagination['pageNo'],
  356. 'totalCount' => $pagination['totalCount'],
  357. ],
  358. ];
  359. }
  360. /**
  361. * 修改密码(saas)
  362. *
  363. * @return void
  364. */
  365. public function editPasswordBySaas()
  366. {
  367. $admin = Admin::findOne($this->id);
  368. if (!$admin) {
  369. return [
  370. 'code' => 1,
  371. 'msg' => '找不到数据',
  372. ];
  373. }
  374. if (!$this->password) {
  375. return [
  376. 'code' => 1,
  377. 'msg' => '请提供password参数',
  378. ];
  379. }
  380. $admin->password = Yii::$app->security->generatePasswordHash($this->password);
  381. if ($admin->save()) {
  382. return [
  383. 'code' => 0,
  384. 'msg' => '修改成功',
  385. ];
  386. }
  387. return [
  388. 'code' => 0,
  389. 'msg' => '修改失败',
  390. ];
  391. }
  392. /**
  393. * 添加或编辑账户(saas)
  394. *
  395. * @param [type] $params
  396. * @return array
  397. */
  398. public function addOrEditBySaas($params)
  399. {
  400. //$params['type'] 1 = 产品代理; 2 = 前置仓; 3 = 推广代理;
  401. $t = \Yii::$app->db->beginTransaction();
  402. try {
  403. if (get_admin()->type !== 'admin' && get_admin()->type !== 'saas_staff') {
  404. return [
  405. 'code' => 1,
  406. 'msg' => '身份验证失败,权限错误',
  407. ];
  408. }
  409. if (get_admin()->type === 'admin' && get_admin()->username !== 'admin') {
  410. $params['area_level'] = get_admin()->area_level;
  411. //兼容区域代理下不用选择省市区情况
  412. if ((int)$params['type'] == 1) {//仓库独立出去了type == 2
  413. $params['area']['province_id'] = get_admin()->province_id;
  414. $params['area']['city_id'] = get_admin()->city_id;
  415. $params['area']['district_id'] = get_admin()->district_id;
  416. }
  417. }
  418. //兼容不用选择区域代理情况
  419. if ((int)$params['type'] === 1) {
  420. $areaAdmin = Admin::find()->where(['AND', [
  421. "province_id" => $params['area']['province_id'],
  422. "city_id" => $params['area']['city_id'],
  423. "district_id" => $params['area']['district_id'],
  424. 'area_level' => 1,
  425. 'is_delete' => 0,
  426. 'type' => 'admin',
  427. ], ['<>', 'username', 'admin']])->asArray()->one();
  428. if (empty($areaAdmin)) {
  429. throw new \Exception('当前区域下没有区级代理');
  430. }
  431. $params['admin_id'] = $areaAdmin['id'];
  432. }
  433. if ($params['admin_id']) {
  434. $admin_ = Admin::findOne(['id' => $params['admin_id'], 'type' => 'admin']);
  435. $params['area_level'] = $admin_->area_level;
  436. } else {
  437. $admin_ = get_admin();
  438. }
  439. $isEdit = isset($params['id']) && $params['id'] > 0;
  440. $admin_query = Admin::find()->where(['province_id' => $params['area']['province_id'], 'is_delete' => 0, 'type' => 'admin']);
  441. if ((int)$params['area_level'] === 3) {
  442. $admin_query->andWhere(['area_level' => 3]);
  443. } elseif ((int)$params['area_level'] === 2) {
  444. $admin_query->andWhere(['city_id' => $params['area']['city_id'], 'area_level' => 2]);
  445. } elseif ((int)$params['area_level'] === 1) {
  446. $admin_query->andWhere(['city_id' => $params['area']['city_id'], 'district_id' => $params['area']['district_id'], 'area_level' => 1]);
  447. }
  448. //判断同身份是否存在相同用户
  449. if ($params['saas_user_id']) {
  450. $auth_name = "";
  451. //区域代理
  452. if ($params['area_level'] > 0 && empty($params['type'])) {
  453. $empty_admin_query = Admin::find()->where(['AND',
  454. ['saas_user_id' => $params['saas_user_id'], 'type' => 'admin', 'is_delete' => 0],
  455. ['>', 'area_level', 0]
  456. ]);
  457. $auth_name = "区域代理";
  458. }
  459. if (!empty($params['type'])) {
  460. //产品代理
  461. if (intval($params['type']) === 1) {
  462. $empty_admin_query = Admin::find()->where([
  463. 'saas_user_id' => $params['saas_user_id'],
  464. 'type' => 'goods_agent',
  465. 'is_delete' => 0
  466. ]);
  467. $auth_name = "产品代理";
  468. }
  469. //前置仓
  470. if (intval($params['type']) === 2) {
  471. $empty_admin_query = Admin::find()->where([
  472. 'saas_user_id' => $params['saas_user_id'],
  473. 'type' => 'front_agent',
  474. 'is_delete' => 0
  475. ]);
  476. $auth_name = "前置仓";
  477. }
  478. //推广代理
  479. if (intval($params['type']) === 3) {
  480. $empty_admin_query = Admin::find()->where([
  481. 'saas_user_id' => $params['saas_user_id'],
  482. 'type' => 'bd_agent',
  483. 'is_delete' => 0
  484. ]);
  485. $auth_name = "推广代理";
  486. }
  487. }
  488. if ($isEdit) {
  489. //判断如果是修改则判断不等于自己
  490. if (!empty($empty_admin_query)) {
  491. $admin = $empty_admin_query->andWhere(['<>', 'id', $params['id']])->asArray()->one();
  492. if (!empty($admin)) {
  493. //如果存在则表示存在相同用户给提示
  494. return [
  495. 'code' => 1,
  496. 'msg' => '同用户存在相同的身份:' . $auth_name . ';名称:' . $admin['name'] . ';账户:' . $admin['username'],
  497. ];
  498. }
  499. }
  500. }
  501. }
  502. if (Admin::find()->where(['username' => $params['username'], 'is_delete' => 0])->andWhere(['<>', 'id', $params['id'] ?? 0])->one()) {
  503. return [
  504. 'code' => 1,
  505. 'msg' => '用户名已经存在',
  506. ];
  507. }
  508. // if (! empty($params['mobile'])) {
  509. // if (Admin::find()->where(['mobile' => $params['mobile'], 'is_delete' => 0])->andWhere(['<>', 'id', $params['id'] ?? 0])->one()) {
  510. // return [
  511. // 'code' => 1,
  512. // 'msg' => '手机号已经存在',
  513. // ];
  514. // }
  515. // }
  516. if ($isEdit) {
  517. if (!in_array((int)$params['type'], [1, 2, 3])) {
  518. $is_area_admin = $admin_query->andWhere(['<>', 'id', $params['id']])->one();
  519. if ($is_area_admin) {
  520. return [
  521. 'code' => 1,
  522. 'msg' => '当前区域内已存在同等级代理',
  523. ];
  524. }
  525. }
  526. $admin = Admin::findOne($params['id']);
  527. } else {
  528. if (!in_array((int)$params['type'], [1, 2, 3])) {
  529. $is_area_admin = $admin_query->one();
  530. if ($is_area_admin) {
  531. return [
  532. 'code' => 1,
  533. 'msg' => '当前区域内已存在同等级代理',
  534. ];
  535. }
  536. }
  537. $admin = new Admin();
  538. }
  539. // if ($params['max_price'] < 0) {
  540. // return [
  541. // 'code' => 1,
  542. // 'msg' => '允许设置的最大佣金数据错误',
  543. // ];
  544. // } elseif ($params['username'] == 'admin') {
  545. // return [
  546. // 'code' => 1,
  547. // 'msg' => '超管不允许设置最大佣金',
  548. // ];
  549. // }
  550. //判断是否越级创建产品代理以及中央仓 20240723中央仓独立出去 $params['type'] == 2
  551. if ((int)$params['type'] == 1 && $admin_->username !== 'admin' && $admin_->type === 'admin') {
  552. $province_id = $params['area']['province_id'];
  553. $city_id = $params['area']['city_id'];
  554. $district_id = $params['area']['district_id'];
  555. $open = false;
  556. $error_area = [
  557. 'province_id' => '',
  558. 'city_id' => '',
  559. 'district_id' => ''
  560. ];
  561. if ((int)$admin_->province_id !== (int)$province_id) {
  562. $open = true;
  563. $error_area['province_id'] = $admin_->province_id;
  564. }
  565. switch ((int)$admin_->area_level) {
  566. case 1: //区
  567. if (((int)$admin_->city_id !== (int)$city_id || (int)$admin_->district_id !== (int)$district_id) || (int)$params['area_level'] !== 1) {
  568. $open = true;
  569. $error_area['province_id'] = $admin_->province_id;
  570. $error_area['city_id'] = $admin_->city_id;
  571. $error_area['district_id'] = $admin_->district_id;
  572. }
  573. break;
  574. case 2: //市
  575. if (((int)$admin_->city_id !== (int)$city_id) || (int)$params['area_level'] > 2) {
  576. $open = true;
  577. $error_area['province_id'] = $admin_->province_id;
  578. $error_area['city_id'] = $admin_->city_id;
  579. }
  580. break;
  581. }
  582. if ($open) {
  583. $error_area = array_values($error_area);
  584. $district = District::find()->where(['id' => $error_area])->select('name')->column();
  585. $district_str = implode(' ', $district);
  586. return [
  587. 'code' => 1,
  588. 'msg' => '不允许越级创建,可选范围' . $district_str,
  589. ];
  590. }
  591. }
  592. if (isset($params['is_enable'])) {
  593. if (!in_array($params['is_enable'], [0, 1])) {
  594. return [
  595. 'code' => 1,
  596. 'msg' => '状态参数不正确',
  597. ];
  598. }
  599. $admin->is_enable = $params['is_enable'];
  600. }
  601. if (isset($params['area']['province_id']) && $params['area']['province_id']) {
  602. $admin->province_id = $params['area']['province_id'];
  603. }
  604. if (isset($params['area']['city_id']) && $params['area']['city_id']) {
  605. $admin->city_id = $params['area']['city_id'];
  606. }
  607. if (isset($params['area']['district_id']) && $params['area']['district_id']) {
  608. $admin->district_id = $params['area']['district_id'];
  609. }
  610. if (isset($params['address']) && $params['address']) {
  611. $admin->address = $params['address'];
  612. }
  613. if (isset($params['lat']) && $params['lat']) {
  614. $admin->lat = $params['lat'];
  615. }
  616. if (isset($params['lng']) && $params['lng']) {
  617. $admin->lng = $params['lng'];
  618. }
  619. $admin->area_level = $params['area_level'];
  620. $admin->username = $params['username'];
  621. if (isset($params['password'])) {
  622. $admin->password = Yii::$app->security->generatePasswordHash($params['password']);
  623. }
  624. $admin->mobile = $params['mobile'];
  625. $admin->remark = $params['remark'];
  626. $admin->rate = $params['rate'] ?: 0;
  627. $admin->name = $params['name'];
  628. $admin->max_app_count = $params['max_app_count'];
  629. $admin->saas_user_id = $params['saas_user_id'];
  630. $admin->max_price = 0.00; //去掉代理最大佣金限制设置
  631. if (!$params['expire_time_always']) {
  632. $admin->expire_time = strtotime($params['expire_time']);
  633. } else {
  634. $admin->expire_time = 0;
  635. }
  636. if ((int)$params['type'] === 3) {
  637. $admin->type = Admin::ADMIN_TYPE_BD_AGENT;
  638. }
  639. if ($admin->save()) {
  640. if (in_array((int)$params['type'], [1, 2])) {
  641. if ((int)$params['type'] == 1) {
  642. $parent_admin_id = $admin_->id;
  643. if (!($admin_->username !== 'admin' && $admin_->type === 'admin')) {
  644. return [
  645. 'code' => 1,
  646. 'msg' => '权限错误!',
  647. ];
  648. }
  649. } else {
  650. if ($params['driver_reset_time']) {
  651. Option::set('driver_reset_time_' . $admin->id, $params['driver_reset_time'], 0, 'agent_front');
  652. }
  653. }
  654. $agent_bind = new AgentBind();
  655. $agent_bind->admin_id = $admin->id;
  656. if ($isEdit) {
  657. $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]);
  658. }
  659. $agent_bind->parent_admin_id = $parent_admin_id ?? 0;
  660. $agent_bind->type = (int)$params['type'];
  661. if (!$agent_bind->save()) {
  662. throw new \Exception(json_encode($agent_bind->errors));
  663. }
  664. $admin->type = (int)$params['type'] === 1 ? Admin::ADMIN_TYPE_GOODS_AGENT : Admin::ADMIN_TYPE_FRONT_AGENT;
  665. $admin->type_id = $agent_bind->id;
  666. $admin->save();
  667. }
  668. $t->commit();
  669. return [
  670. 'code' => 0,
  671. 'msg' => '操作成功!',
  672. ];
  673. }
  674. return [
  675. 'code' => 1,
  676. 'msg' => '操作失败!',
  677. ];
  678. } catch (\Exception $e) {
  679. // var_dump($e);die;
  680. $t->rollBack();
  681. return [
  682. 'code' => 1,
  683. 'msg' => $e->getMessage(),
  684. ];
  685. }
  686. }
  687. public function setAgentApplyStatus($params) {
  688. $t = \Yii::$app->db->beginTransaction();
  689. try {
  690. $agent_apply = AgentApply::findOne($params['id']);
  691. if (!$agent_apply) {
  692. throw new \Exception('申请单信息错误');
  693. }
  694. if (in_array($agent_apply->status, [1, 2])) {
  695. throw new \Exception('申请单状态信息错误');
  696. }
  697. if (!in_array($params['status'], [1, 2])) {
  698. throw new \Exception('参数信息错误');
  699. }
  700. $agent_apply->status = (int)$params['status'];
  701. if ((int)$params['status'] === 1) {
  702. $admin = new Admin();
  703. $admin->username = $agent_apply->username;
  704. $admin->saas_user_id = $agent_apply->saas_user_id;
  705. $admin->password = Yii::$app->security->generatePasswordHash($agent_apply->password);
  706. $admin->name = $agent_apply->name;
  707. $admin->province_id = $agent_apply->province_id;
  708. $admin->city_id = $agent_apply->city_id;
  709. $admin->district_id = $agent_apply->district_id;
  710. $admin->address = $agent_apply->address;
  711. $admin->area_level = $agent_apply->area_level;
  712. $admin->type = 'admin';
  713. if ((int)$agent_apply->agent_type === 1) {
  714. $admin->type = "bd_agent";
  715. }
  716. if ((int)$agent_apply->agent_type === 2) {
  717. $admin->type = "goods_agent";
  718. }
  719. $admin->mobile = $agent_apply->mobile;
  720. $admin->address = $agent_apply->address;
  721. $admin->lat = $agent_apply->lat;
  722. $admin->lng = $agent_apply->lng;
  723. if (!$admin->save()) {
  724. throw new \Exception(json_encode($admin->errors));
  725. }
  726. $agent_apply->admin_id = $admin->id;
  727. if ((int)$agent_apply->agent_type === 2) {
  728. $areaAdmin = Admin::find()->where(['AND', [
  729. "province_id" => $agent_apply->province_id,
  730. "city_id" => $agent_apply->city_id,
  731. "district_id" => $agent_apply->district_id,
  732. 'area_level' => 1,
  733. 'is_delete' => 0,
  734. 'type' => 'admin',
  735. ], ['<>', 'username', 'admin']])->asArray()->one();
  736. if (empty($areaAdmin)) {
  737. throw new \Exception('当前区域下没有区域代理');
  738. }
  739. $agent_bind = new AgentBind();
  740. $agent_bind->admin_id = $admin->id;
  741. $agent_bind->parent_admin_id = $areaAdmin['id'];
  742. $agent_bind->type = 1;
  743. if (!$agent_bind->save()) {
  744. throw new \Exception(json_encode($agent_bind->errors));
  745. }
  746. $admin->type_id = $agent_bind->id;
  747. if (!$admin->save()) {
  748. throw new \Exception(json_encode($admin->errors));
  749. }
  750. }
  751. } else {
  752. $agent_apply->reason = $params['reason'];
  753. }
  754. if (!$agent_apply->save()) {
  755. throw new \Exception(json_encode($agent_apply->errors));
  756. }
  757. //申请通过发送短信
  758. $res = NoticeSend::AgentExamine($agent_apply->saas_user_id);
  759. if (isset($res['code']) && $res['code'] !== 0) {
  760. debug_log($res, 'sms.log');
  761. }
  762. $t->commit();
  763. return [
  764. 'code' => 0,
  765. 'msg' => "操作成功"
  766. ];
  767. } catch (\Exception $e) {
  768. $t->rollBack();
  769. return [
  770. 'code' => 0,
  771. 'msg' => $e->getMessage(),
  772. ];
  773. }
  774. }
  775. public function getAgentApplyList($params) {
  776. try {
  777. $status = $params['status'];
  778. $name = $params['name'];
  779. $agent_type = $params['agent_type'];
  780. $query = AgentApply::find()->alias('a')->where(['a.is_delete' => 0])
  781. ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id');
  782. if ((int)$status !== -1 && $status !== null) {
  783. $query->andWhere(['a.status' => $status]);
  784. }
  785. if ($name) {
  786. $query->andWhere(['LIKE', 'a.name', $name]);
  787. }
  788. if ((int)$agent_type !== -1 && $agent_type !== null && in_array($agent_type, [0, 1, 2])) {
  789. $query->andWhere(['a.agent_type' => $agent_type]);
  790. }
  791. $query->select('a.id, a.status, a.agent_type, a.created_at, su.name, su.avatar, a.name, a.username, a.password, a.mobile')
  792. ->orderBy('a.created_at desc');
  793. $page = pagination_make($query);
  794. foreach ($page['list'] as &$item) {
  795. $item['status'] *= 1;
  796. $item['agent_type'] *= 1;
  797. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  798. }
  799. return [
  800. 'code' => 0,
  801. 'msg' => 'success',
  802. 'data' => [
  803. 'data' => $page['list'],
  804. 'pageNo' => $page['pageNo'],
  805. 'totalCount' => $page['totalCount']
  806. ]
  807. ];
  808. } catch (\Exception $e) {
  809. return [
  810. 'code' => 0,
  811. 'msg' => $e->getMessage(),
  812. ];
  813. }
  814. }
  815. /**
  816. * 修改管理员状态
  817. * @param mixed $params
  818. * @return (int|string)[]
  819. * @throws InvalidConfigException
  820. * @throws StaleObjectException
  821. * @throws DbException
  822. */
  823. public function editStatusBySaas($params)
  824. {
  825. if (!isset($params['id']) || !isset($params['is_enable'])) {
  826. return [
  827. 'code' => 1,
  828. 'msg' => '缺少必要参数',
  829. ];
  830. }
  831. if (!in_array($params['is_enable'], [0, 1])) {
  832. return [
  833. 'code' => 1,
  834. 'msg' => '状态参数不正确',
  835. ];
  836. }
  837. $admin = Admin::findOne($params['id']);
  838. if (!$admin) {
  839. return [
  840. 'code' => 1,
  841. 'msg' => '管理员未找到',
  842. ];
  843. }
  844. $admin->is_enable = $params['is_enable'];
  845. $admin->save();
  846. Salesman::updateAll(['status'=>$params['is_enable'],'updated_at'=>time()],['admin_id'=>$params['id'],'is_delete'=>0]);
  847. return [
  848. 'code' => 0,
  849. 'msg' => '操作成功!',
  850. ];
  851. }
  852. /**
  853. * @param int $expireIn
  854. * @return mixed
  855. * @throws \yii\base\InvalidConfigException
  856. */
  857. public function generateToken(int $expireIn = 86400, $store_id = null)
  858. {
  859. $admin = $this->getAdmin();
  860. if (!$admin) {
  861. throw new \Exception('管理员不存在或已被删除');
  862. }
  863. $roles = 'super_admin';
  864. if ($admin->username !== 'admin') {
  865. $roles = $admin->getRoles()->select('id')->column();
  866. if ($admin->type === Admin::ADMIN_TYPE_SAAS_STAFF) {
  867. $roles = $admin->getSaasRoles()->select('id')->column();
  868. }
  869. $roles = implode(',', $roles);
  870. }
  871. $time = time();
  872. $payload = [
  873. 'iss' => Yii::$app->request->hostInfo,
  874. 'iat' => $time,
  875. 'exp' => $time + $expireIn,
  876. 'admin_id' => $admin->id,
  877. 'username' => $admin->username,
  878. 'store_id' => $store_id ?: $admin->store_id,
  879. 'roles' => $roles,
  880. ];
  881. return Yii::$app->jwt->createToken($payload);
  882. }
  883. /**
  884. * 创建账号
  885. * @return array
  886. */
  887. public function createAdmin()
  888. {
  889. try {
  890. if ($this->validate()) {
  891. $admin = new Admin();
  892. if (Admin::findOne(['username' => $this->username, 'is_delete' => 0])) {
  893. return [
  894. 'code' => 1,
  895. 'msg' => '用户名已经存在',
  896. ];
  897. }
  898. $admin->username = $this->username;
  899. if ($this->email) {
  900. if (Admin::findOne(['email' => $this->email, 'is_delete' => 0])) {
  901. return [
  902. 'code' => 1,
  903. 'msg' => '邮箱已经存在',
  904. ];
  905. }
  906. $admin->email = $this->email;
  907. }
  908. if ($this->name) {
  909. $admin->name = $this->name;
  910. }
  911. // if ($this->mobile) {
  912. // if (Admin::findOne(['mobile' => $this->mobile, 'is_delete' => 0])) {
  913. // return [
  914. // 'code' => 1,
  915. // 'msg' => '手机号已经存在',
  916. // ];
  917. // }
  918. // $admin->mobile = $this->mobile;
  919. // }
  920. $admin->password = Yii::$app->security->generatePasswordHash($this->password);
  921. if ($this->remark) {
  922. $admin->remark = $this->remark;
  923. }
  924. $type = Admin::ADMIN_TYPE_STAFF;
  925. if ((int)$this->type === 1) {
  926. $type = Admin::ADMIN_TYPE_SAAS_STAFF;
  927. }
  928. $admin->type = $type;
  929. $admin->type_id = get_store_id();
  930. if ($admin->save()) {
  931. if (count($this->roles) > 0) {
  932. foreach ($this->roles as $roleId) {
  933. $role = AuthRole::findOne($roleId);
  934. if ((int)$this->type === 1) {
  935. $role = SaasAuthRole::findOne($roleId);
  936. if ($role) {
  937. $admin->link('saasRoles', $role);
  938. }
  939. } else {
  940. if ($role) {
  941. $admin->link('roles', $role);
  942. }
  943. }
  944. }
  945. }
  946. return [
  947. 'code' => 0,
  948. 'msg' => '创建成功'
  949. ];
  950. }
  951. return [
  952. 'code' => 1,
  953. 'msg' => $this->getErrorSummary(false)[0],
  954. ];
  955. }
  956. return [
  957. 'code' => 1,
  958. 'msg' => $this->getErrorSummary(false)[0],
  959. ];
  960. } catch (Exception $e) {
  961. return [
  962. 'code' => 1,
  963. 'msg' => '出现错误,账号创建失败',
  964. ];
  965. }
  966. }
  967. public function editAdmin()
  968. {
  969. try {
  970. if ($this->validate()) {
  971. if (! $this->id) {
  972. return [
  973. 'code' => 1,
  974. 'msg' => '缺少账号ID',
  975. ];
  976. }
  977. $admin = Admin::findOne($this->id);
  978. if (Admin::find()->where(['username' => $this->username])->andWhere(['<>', 'id', $this->id])->one()) {
  979. return [
  980. 'code' => 1,
  981. 'msg' => '用户名已经存在',
  982. ];
  983. }
  984. if ($admin->username == 'admin' && $this->username != 'admin') {
  985. return [
  986. 'code' => 1,
  987. 'msg' => '超级管理员账号不允许被修改',
  988. ];
  989. }
  990. $admin->username = $this->username;
  991. if ($this->email) {
  992. if (Admin::find()->where(['email' => $this->email])->andWhere(['<>', 'id', $this->id])->one()) {
  993. return [
  994. 'code' => 1,
  995. 'msg' => '邮箱已经存在',
  996. ];
  997. }
  998. $admin->email = $this->email;
  999. }
  1000. if ($this->name) {
  1001. $admin->name = $this->name;
  1002. }
  1003. if ($this->mobile) {
  1004. // if (Admin::find()->where(['mobile' => $this->mobile])->andWhere(['<>', 'id', $this->id])->one()) {
  1005. // return [
  1006. // 'code' => 1,
  1007. // 'msg' => '手机号已经存在',
  1008. // ];
  1009. // }
  1010. $admin->mobile = $this->mobile;
  1011. }
  1012. if ($this->password) {
  1013. $admin->password = Yii::$app->security->generatePasswordHash($this->password);
  1014. }
  1015. if ($this->remark) {
  1016. $admin->remark = $this->remark;
  1017. }
  1018. if ($admin->save()) {
  1019. if ((int)$this->type === 1) {
  1020. if ($admin->getSaasRoles()->count() > 0) {
  1021. // 先解除该账号所有关联,再创建新关联
  1022. SaasAdminRole::deleteAll(['admin_id' => $this->id]);
  1023. }
  1024. } else {
  1025. if ($admin->getRoles()->count() > 0) {
  1026. // 先解除该账号所有关联,再创建新关联
  1027. AdminRole::deleteAll(['admin_id' => $this->id]);
  1028. }
  1029. }
  1030. if (count($this->roles) > 0) {
  1031. foreach ($this->roles as $roleId) {
  1032. $role = AuthRole::findOne($roleId);
  1033. if ((int)$this->type === 1) {
  1034. $role = SaasAuthRole::findOne($roleId);
  1035. if ($role) {
  1036. $admin->link('saasRoles', $role);
  1037. }
  1038. } else {
  1039. if ($role) {
  1040. $admin->link('roles', $role);
  1041. }
  1042. }
  1043. }
  1044. }
  1045. return [
  1046. 'code' => 0,
  1047. 'msg' => '编辑成功'
  1048. ];
  1049. }
  1050. return [
  1051. 'code' => 1,
  1052. 'msg' => $this->getErrorSummary(false)[0],
  1053. ];
  1054. }
  1055. return [
  1056. 'code' => 1,
  1057. 'msg' => $this->getErrorSummary(false)[0],
  1058. ];
  1059. } catch (Exception $e) {
  1060. return [
  1061. 'code' => 1,
  1062. 'msg' => '出现错误,账号编辑失败' . $e->getMessage(),
  1063. ];
  1064. }
  1065. }
  1066. /**
  1067. * 删除账号(软删除)
  1068. * @return array
  1069. */
  1070. public function deleteAdmin()
  1071. {
  1072. if (! $this->id) {
  1073. return [
  1074. 'code' => 1,
  1075. 'msg' => '请提供账号ID',
  1076. ];
  1077. }
  1078. $admin = Admin::findOne($this->id);
  1079. if (! $admin) {
  1080. return [
  1081. 'code' => 1,
  1082. 'msg' => '账号不存在',
  1083. ];
  1084. }
  1085. $admin->is_delete = Admin::ADMIN_DELETE;
  1086. if ($admin->save()) {
  1087. $agent_bind = AgentBind::findOne(['admin_id' => $this->id]);
  1088. if ($agent_bind) {
  1089. $agent_bind->is_delete = 1;
  1090. if (!$agent_bind->save()) {
  1091. return [
  1092. 'code' => 1,
  1093. 'msg' => implode(';', array_values($agent_bind->firstErrors))
  1094. ];
  1095. }
  1096. }
  1097. Salesman::updateAll(['is_delete'=>1,'updated_at'=>time()],['admin_id'=>$this->id,'is_delete'=>0]);
  1098. AgentApply::deleteAll(['admin_id' => $this->id, 'is_delete' => 0]);
  1099. return [
  1100. 'code' => 0,
  1101. 'msg' => '删除成功',
  1102. ];
  1103. }
  1104. return [
  1105. 'code' => 1,
  1106. 'msg' => $this->getErrorSummary(false)[0],
  1107. ];
  1108. }
  1109. }