Admin.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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::beforeInsert(function (self $admin){
  50. Qiyu::instance()->create($admin,$_SERVER['admin_pass']??null);
  51. });
  52. self::beforeUpdate(function (self $admin){
  53. Qiyu::instance()->update($admin,$_SERVER['admin_pass']??null);
  54. });
  55. }
  56. public function getIsManagerAttr($_,$admin){
  57. return !$admin['user_type']==self::UT_ADMIN;
  58. }
  59. public function getIsSellerAttr($_,$admin){
  60. return $admin['user_type']==self::UT_SELLER;
  61. }
  62. public function moneyLog(){
  63. return $this->hasMany(AdminMoneyLog::class);
  64. }
  65. public function getShareLinkAttr($_m,$model){
  66. return str_replace('{frommanager}',$model['id'],config('site.sell_share_url')?:'');
  67. return sprintf('%s/register',request()->domain(),);
  68. }
  69. public function scopeFilterAdmin(Query $query){
  70. $query->where('user_type',self::UT_ADMIN);
  71. }
  72. }