CloseLoginForm.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\helpers\Tool;
  7. use common\helpers\user\Info;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\Period;
  10. use common\models\Region;
  11. use common\models\User;
  12. use common\models\UserClose;
  13. use common\models\UserInfo;
  14. use common\models\UserNetwork;
  15. use common\models\UserRelation;
  16. use common\models\UserSystem;
  17. use yii\base\Exception;
  18. /**
  19. * Login form
  20. */
  21. class CloseLoginForm extends Model
  22. {
  23. public $userIds;
  24. public $userName;
  25. public $remark;
  26. public $type; // 1:单独关闭个人,2:按推荐网络关闭,3:按安置网络关闭,4:按体系关闭
  27. public $isClose; // 0: 允许登录, 1:禁止登录
  28. public $areaSelected; //地区选择
  29. private $_userId;
  30. private $_userSystem;
  31. public static $types = [
  32. 1 => '按指定会员',
  33. 2 => '按开拓网络',
  34. 3 => '按安置网络',
  35. 4 => '按体系',
  36. 5 => '按地区',
  37. ];
  38. public static $closeSwitch = [
  39. 0 => '允许登录',
  40. 1 => '禁止登录',
  41. ];
  42. public function init()
  43. {
  44. parent::init();
  45. $this->adminOperateLogger = new AdminOperate([
  46. 'fetchClass' => User::class,
  47. ]);
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['userIds', 'userName', 'type', 'isClose', 'areaSelected', 'remark'], 'trim'],
  56. [['userIds', 'userName', 'type', 'isClose', 'areaSelected'], 'required'],
  57. [['userName'], 'initUserId'],
  58. [['type'], 'isType'],
  59. ];
  60. }
  61. /**
  62. * 指定校验场景
  63. * @return array
  64. */
  65. public function scenarios()
  66. {
  67. $parentScenarios = parent::scenarios();
  68. $customScenarios = [
  69. // 单独禁止登录或加入黑名单
  70. 'single' => ['userName', 'type', 'isClose', 'remark'],
  71. // 批量禁止
  72. 'batch' => ['userIds', 'type', 'isClose', 'remark'],
  73. // 按地区关闭
  74. 'area' => ['type', 'isClose', 'areaSelected', 'remark'],
  75. ];
  76. return array_merge($parentScenarios, $customScenarios);
  77. }
  78. public function attributeLabels()
  79. {
  80. return [
  81. 'userName' => '会员编号',
  82. 'type' => '类型',
  83. 'remark' => '备注',
  84. ];
  85. }
  86. /**
  87. * 初始化会员ID
  88. * @param $attribute
  89. */
  90. public function initUserId($attribute){
  91. $this->_userId = Info::getUserIdByUserName($this->userName);
  92. if(!$this->_userId){
  93. $this->addError($attribute, 'Member does not exist'); // 会员不存在
  94. }
  95. }
  96. /**
  97. * 状态类型校验
  98. * @param $attribute
  99. */
  100. public function isType($attribute){
  101. if($this->type == 4){
  102. $this->_userSystem = UserSystem::getSystemByUserId($this->_userId);
  103. if($this->_userSystem){
  104. if($this->_userSystem['LEADER_UID'] != $this->_userId){
  105. $this->addError($attribute, '按体系管理登录必须是体系领导人');
  106. }
  107. } else {
  108. $this->addError($attribute, '体系不存在');
  109. }
  110. }
  111. }
  112. /**
  113. * 批量处理
  114. * @return array|null
  115. * @throws \yii\db\Exception
  116. */
  117. public function batch(){
  118. if(!$this->validate()){
  119. return null;
  120. }
  121. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,ALLOW_LOGIN']);
  122. $db = \Yii::$app->db;
  123. $transaction = $db->beginTransaction();
  124. try {
  125. if($this->isClose){
  126. $closedAt = Date::nowTime();
  127. } else {
  128. $closedAt = 0;
  129. }
  130. $allUidIn = implode("','", $this->userIds);
  131. User::updateAll(['ALLOW_LOGIN'=>!$this->isClose], "ID IN ('".$allUidIn."')");
  132. $transaction->commit();
  133. } catch (Exception $e) {
  134. $transaction->rollBack();
  135. $this->addError('batch', $e->getMessage());
  136. return null;
  137. }
  138. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,ALLOW_LOGIN']);
  139. $this->adminOperateLogger->setBatchField('ID')->setOptObjField('ID')->clean()->save([
  140. 'optType' => self::$types[$this->type].self::$closeSwitch[$this->isClose],
  141. 'remark' => $this->remark,
  142. ]);
  143. return $this->userIds;
  144. }
  145. /**
  146. * 按照地区关闭
  147. * @return bool|null
  148. * @throws \yii\db\Exception
  149. */
  150. public function area(){
  151. if(!$this->validate()){
  152. return null;
  153. }
  154. $db = \Yii::$app->db;
  155. $transaction = $db->beginTransaction();
  156. try {
  157. if($this->isClose){
  158. $closedAt = Date::nowTime();
  159. } else {
  160. $closedAt = 0;
  161. }
  162. $where = '';
  163. $params = [];
  164. if(isset($this->areaSelected[0]) && $this->areaSelected[0]){
  165. $where .= 'PROVINCE=:PROVINCE';
  166. $params[':PROVINCE'] = $this->areaSelected[0];
  167. }
  168. if(isset($this->areaSelected[1]) && $this->areaSelected[1]){
  169. $where .= ' AND CITY=:CITY';
  170. $params[':CITY'] = $this->areaSelected[1];
  171. }
  172. if(isset($this->areaSelected[2]) && $this->areaSelected[1]){
  173. $where .= ' AND COUNTY=:COUNTY';
  174. $params[':COUNTY'] = $this->areaSelected[2];
  175. }
  176. $allUser = User::findAllAsArray($where,$params,'ID');
  177. $allUserIds = array_column($allUser, 'ID');
  178. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($allUserIds, 'ID',['select'=>'ID,ALLOW_LOGIN']);
  179. User::updateAll(['ALLOW_LOGIN'=>!$this->isClose], $where,$params);
  180. $transaction->commit();
  181. } catch (Exception $e) {
  182. $transaction->rollBack();
  183. $this->addError('area', $e->getMessage());
  184. return null;
  185. }
  186. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($allUserIds, 'ID', ['select' => 'ID,ALLOW_LOGIN']);
  187. $province = isset($this->areaSelected[0]) ? Region::getCnName($this->areaSelected[0]) : '';
  188. $city = isset($this->areaSelected[1]) ? Region::getCnName($this->areaSelected[1]) : '';
  189. $county = isset($this->areaSelected[2]) ? Region::getCnName($this->areaSelected[2]) : '';
  190. $this->adminOperateLogger->setBatchField('ID')->setOptObjField('ID')->clean()->save([
  191. 'optType' => self::$types[$this->type] . $province . $city . $county . self::$closeSwitch[$this->isClose],
  192. 'remark' => $this->remark,
  193. ]);
  194. return true;
  195. }
  196. }