123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace app\admin\model;
- use app\common\model\MobileHoldLog;
- use app\common\model\MobileOrderRefundLog;
- use think\db\Query;
- use think\Model;
- use think\Session;
- /**
- *@method static Query|static proxy()
- *@method static Query|static sub()
- */
- class Admin extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $readonly=['proxy','sub'];
- protected $append=['is_proxy','is_sub','is_manager'];
- protected $hidden=['salt','password','token'];
- /**
- * 重置用户密码
- * @author baiyouwen
- */
- public function resetPassword($uid, $NewPassword)
- {
- $passwd = $this->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;
- }
- }
|