Admin.php 2.3 KB

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