Admin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\MobileHoldLog;
  4. use app\common\model\MobileOrderRefundLog;
  5. use think\db\Query;
  6. use think\Model;
  7. use think\Session;
  8. /**
  9. *@method static Query|static proxy()
  10. *@method static Query|static sub()
  11. */
  12. class Admin extends Model
  13. {
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $readonly=['proxy','sub'];
  20. protected $append=['is_proxy','is_sub','is_manager'];
  21. protected $hidden=['salt','password','token'];
  22. /**
  23. * 重置用户密码
  24. * @author baiyouwen
  25. */
  26. public function resetPassword($uid, $NewPassword)
  27. {
  28. $passwd = $this->encryptPassword($NewPassword);
  29. $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);
  30. return $ret;
  31. }
  32. public function mobile(){
  33. return $this->hasMany(Mobile::class,'proxy_id');
  34. }
  35. public function subOrder(){
  36. return $this->hasMany(MobileOrder::class,'s_id');
  37. }
  38. public function proxyOrder(){
  39. return $this->hasMany(MobileOrder::class,'admin_id');
  40. }
  41. // 密码加密
  42. protected static function encryptPassword($password, $salt = '', $encrypt = 'md5')
  43. {
  44. return $encrypt($password . $salt);
  45. }
  46. public function mobileHoldLog(){
  47. return $this->morphMany(MobileHoldLog::class,'holdable','admin');
  48. }
  49. public function mobileOrderRefundLog(){
  50. return $this->morphMany(MobileOrderRefundLog::class,'refunduser','admin');
  51. }
  52. public static function createProxy($nickname,$phone){
  53. $user=self::create([
  54. 'nickname'=>$nickname,
  55. 'username'=>$phone,
  56. 'com_mobile'=>$phone,
  57. 'phone'=>$phone,
  58. 'com_user'=>$nickname,
  59. 'proxy'=>1,
  60. 'salt'=>$salt=mt_rand(1000,9999),
  61. 'password'=>self::encryptPassword('123456',$salt),
  62. ]);
  63. $groupId=config('site.proxy_auth_group_id');
  64. if($groupId) {
  65. $groupId = AuthGroup::where('id', $groupId)->value('id');
  66. if($groupId){
  67. AuthGroupAccess::insert([
  68. 'uid'=>$user['id'],
  69. 'group_id'=>$groupId,
  70. ]);
  71. }
  72. }
  73. return $user;
  74. }
  75. public function scopeProxy(Query $query){
  76. $query->where('proxy',1);
  77. }
  78. public function scopeSub(Query $query){
  79. $query->where('sub',1);
  80. }
  81. protected static function init()
  82. {
  83. self::beforeUpdate(function (self $admin){
  84. $data=$admin->getChangedData();
  85. if(isset($data['status']) && $data['status']=='normal'){
  86. $admin['logintime']=strtotime('-20days');
  87. }
  88. });
  89. self::beforeWrite(function (self $admin){
  90. if(!isset($admin['proxy'])){
  91. $admin['proxy']=0;
  92. }
  93. if(!isset($admin['sub'])){
  94. $admin['sub']=0;
  95. }
  96. if($admin['sub'] && $admin['proxy']){
  97. throw_user('error admin role');
  98. }
  99. if(empty($admin['com_mobile'])){
  100. $admin['com_mobile']=null;
  101. }
  102. });
  103. self::beforeInsert(function (self $admin){
  104. if(!empty($admin['com_mobile'])){
  105. $has=self::where('com_mobile',$admin['com_mobile'])->find();
  106. if($has){
  107. throw_user($admin['com_mobile']);
  108. }
  109. }
  110. });
  111. self::beforeUpdate(function (self $admin){
  112. if(!empty($admin['com_mobile'])){
  113. $has=self::where('com_mobile',$admin['com_mobile'])->where('id','<>',$admin['id'])->find();
  114. if($has){
  115. throw_user('联系人电话不能重复');
  116. }
  117. }
  118. });
  119. self::afterDelete(function (self $admin){
  120. MobileAnchor::where('admin_id',$admin['id'])->delete();
  121. Ad::where('upload_admin_id',$admin['id'])->delete();
  122. });
  123. }
  124. public static function getAdmins(){
  125. return self::where('proxy',0)
  126. ->where('sub',0)
  127. ->column('id,nickname');
  128. }
  129. public static function getSubs(){
  130. return self::where('proxy',0)
  131. ->where('sub',1)
  132. ->column('id,nickname');
  133. }
  134. public function getIsManagerAttr($_,$admin){
  135. !isset($admin['sub']) && $admin['sub']=0;
  136. !isset($admin['proxy']) && $admin['proxy']=0;
  137. return !$admin['sub'] && !$admin['proxy'];
  138. }
  139. public function getIsSubAttr($_,$admin){
  140. !isset($admin['sub']) && $admin['sub']=0;
  141. !isset($admin['proxy']) && $admin['proxy']=0;
  142. return $admin['sub'] && !$admin['proxy'];
  143. }
  144. public function getIsProxyAttr($_,$admin){
  145. !isset($admin['sub']) && $admin['sub']=0;
  146. !isset($admin['proxy']) && $admin['proxy']=0;
  147. return !$admin['sub'] && $admin['proxy'];
  148. }
  149. public function checkSecpwd($pwd){
  150. $config=config('site.sys_second_pwd');
  151. return $pwd && $pwd===$config;
  152. }
  153. }