Admin.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\service\Qiyu;
  4. use think\db\Query;
  5. use think\Model;
  6. use think\Session;
  7. /**
  8. *@method static Query|static filterAdmin()
  9. * @property bool is_seller
  10. * @property bool is_manager
  11. */
  12. class Admin extends Model
  13. {
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $readonly=['user_type'];
  20. protected $append=[];
  21. protected $hidden=['salt','password','token'];
  22. const UT_ADMIN=0;
  23. const UT_SELLER=1;
  24. /**
  25. * 重置用户密码
  26. * @author baiyouwen
  27. */
  28. public function resetPassword($uid, $NewPassword)
  29. {
  30. $passwd = $this->encryptPassword($NewPassword);
  31. $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);
  32. return $ret;
  33. }
  34. // 密码加密
  35. protected static function encryptPassword($password, $salt = '', $encrypt = 'md5')
  36. {
  37. return $encrypt($password . $salt);
  38. }
  39. protected static function init()
  40. {
  41. self::beforeUpdate(function (self $admin){
  42. $data=$admin->getChangedData();
  43. if(isset($data['status']) && $data['status']=='normal'){
  44. $admin['logintime']=strtotime('-20days');
  45. }
  46. });
  47. self::beforeWrite(function (self $admin){
  48. });
  49. self::afterInsert(function (self $admin){
  50. if(empty($admin['user_type'])){
  51. $admin['user_type']=0;
  52. }
  53. $kf_id=Qiyu::instance()->create($admin,$_SERVER['admin_pass']??null);
  54. $admin['kf_id']=$kf_id;
  55. });
  56. self::beforeUpdate(function (self $admin){
  57. Qiyu::instance()->update($admin,$_SERVER['admin_pass']??null);
  58. });
  59. self::afterDelete(function (self $admin){
  60. Qiyu::instance()->delete($admin);
  61. });
  62. }
  63. public function getIsManagerAttr($_,$admin){
  64. return !$admin['user_type']==self::UT_ADMIN;
  65. }
  66. public function getIsSellerAttr($_,$admin){
  67. return $admin['user_type']==self::UT_SELLER;
  68. }
  69. public function moneyLog(){
  70. return $this->hasMany(AdminMoneyLog::class);
  71. }
  72. public function getShareLinkAttr($_m,$model){
  73. return str_replace('{frommanager}',$model['id'],config('site.sell_share_url')?:'');
  74. return sprintf('%s/register',request()->domain(),);
  75. }
  76. public function scopeFilterAdmin(Query $query){
  77. $query->where('user_type',self::UT_ADMIN);
  78. }
  79. }