encryptPassword($NewPassword); $ret = $this->where(['id' => $uid])->update(['password' => $passwd]); return $ret; } public function mobile(){ return $this->hasMany(Mobile::class,'proxy_id'); } public function subOrder(){ return $this->hasMany(MobileOrder::class,'s_id'); } public function proxyOrder(){ return $this->hasMany(MobileOrder::class,'admin_id'); } // 密码加密 protected static function encryptPassword($password, $salt = '', $encrypt = 'md5') { return $encrypt($password . $salt); } public function mobileHoldLog(){ return $this->morphMany(MobileHoldLog::class,'holdable','admin'); } public function mobileOrderRefundLog(){ return $this->morphMany(MobileOrderRefundLog::class,'refunduser','admin'); } public static function createProxy($nickname,$phone){ $user=self::create([ 'nickname'=>$nickname, 'username'=>$phone, 'com_mobile'=>$phone, 'phone'=>$phone, 'com_user'=>$nickname, 'proxy'=>1, 'salt'=>$salt=mt_rand(1000,9999), 'password'=>self::encryptPassword('123456',$salt), ]); $groupId=config('site.proxy_auth_group_id'); if($groupId) { $groupId = AuthGroup::where('id', $groupId)->value('id'); if($groupId){ AuthGroupAccess::insert([ 'uid'=>$user['id'], 'group_id'=>$groupId, ]); } } return $user; } public function scopeProxy(Query $query){ $query->where('proxy',1); } public function scopeSub(Query $query){ $query->where('sub',1); } protected static function init() { self::beforeUpdate(function (self $admin){ $data=$admin->getChangedData(); if(isset($data['status']) && $data['status']=='normal'){ $admin['logintime']=strtotime('-20days'); } }); self::beforeWrite(function (self $admin){ if(!isset($admin['proxy'])){ $admin['proxy']=0; } if(!isset($admin['sub'])){ $admin['sub']=0; } if($admin['sub'] && $admin['proxy']){ throw_user('error admin role'); } if(empty($admin['com_mobile'])){ $admin['com_mobile']=null; } }); self::beforeInsert(function (self $admin){ if(!empty($admin['com_mobile'])){ $has=self::where('com_mobile',$admin['com_mobile'])->find(); if($has){ throw_user($admin['com_mobile']); } } }); self::beforeUpdate(function (self $admin){ if(!empty($admin['com_mobile'])){ $has=self::where('com_mobile',$admin['com_mobile'])->where('id','<>',$admin['id'])->find(); if($has){ throw_user('联系人电话不能重复'); } } }); self::afterDelete(function (self $admin){ MobileAnchor::where('admin_id',$admin['id'])->delete(); Ad::where('upload_admin_id',$admin['id'])->delete(); }); } public static function getAdmins(){ return self::where('proxy',0) ->where('sub',0) ->column('id,nickname'); } public static function getSubs(){ return self::where('proxy',0) ->where('sub',1) ->column('id,nickname'); } public function getIsManagerAttr($_,$admin){ !isset($admin['sub']) && $admin['sub']=0; !isset($admin['proxy']) && $admin['proxy']=0; return !$admin['sub'] && !$admin['proxy']; } public function getIsSubAttr($_,$admin){ !isset($admin['sub']) && $admin['sub']=0; !isset($admin['proxy']) && $admin['proxy']=0; return $admin['sub'] && !$admin['proxy']; } public function getIsProxyAttr($_,$admin){ !isset($admin['sub']) && $admin['sub']=0; !isset($admin['proxy']) && $admin['proxy']=0; return !$admin['sub'] && $admin['proxy']; } public function checkSecpwd($pwd){ $config=config('site.sys_second_pwd'); return $pwd && $pwd===$config; } }