Users.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\MoneyLog;
  4. use app\common\model\ScoreLog;
  5. use think\Model;
  6. class Users extends Model
  7. {
  8. // 表名
  9. protected $name = 'users';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. 'prevtime_text',
  18. 'logintime_text',
  19. 'jointime_text'
  20. ];
  21. public function getOriginData()
  22. {
  23. return $this->origin;
  24. }
  25. protected static function init()
  26. {
  27. self::beforeUpdate(function ($row) {
  28. $changed = $row->getChangedData();
  29. //如果有修改密码
  30. if (isset($changed['password'])) {
  31. if ($changed['password']) {
  32. $salt = \fast\Random::alnum();
  33. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  34. $row->salt = $salt;
  35. } else {
  36. unset($row->password);
  37. }
  38. }
  39. });
  40. self::beforeUpdate(function ($row) {
  41. $changedata = $row->getChangedData();
  42. $origin = $row->getOriginData();
  43. if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (double)$changedata['money'] !== (double)$origin['money'])) {
  44. MoneyLog::create(['user_id' => $row['id'], 'money' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => '管理员变更金额']);
  45. }
  46. if (isset($changedata['score']) && (int)$changedata['score'] !== (int)$origin['score']) {
  47. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  48. }
  49. });
  50. }
  51. public function getStatusList()
  52. {
  53. return ['1' => __('Open'), '0' => __('Close')];
  54. }
  55. public function getTypeList()
  56. {
  57. return [0 => __('New'), 1 => __('Reduce')];
  58. }
  59. public function parent()
  60. {
  61. return $this->hasOne('Users','id','parent_id',[],'LEFT')->setEagerlyType(0)->field('id,parent_id,moblie');
  62. }
  63. public function agent()
  64. {
  65. return $this->hasOne('Users','id','agent_id',[],'LEFT')->setEagerlyType(0)->field('id,agent_id,moblie');
  66. }
  67. public function getPrevtimeTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : ($data['prevtime'] ?? "");
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. public function getLogintimeTextAttr($value, $data)
  73. {
  74. $value = $value ? $value : ($data['logintime'] ?? "");
  75. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  76. }
  77. public function getJointimeTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : ($data['jointime'] ?? "");
  80. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  81. }
  82. protected function setPrevtimeAttr($value)
  83. {
  84. return $value && !is_numeric($value) ? strtotime($value) : $value;
  85. }
  86. protected function setLogintimeAttr($value)
  87. {
  88. return $value && !is_numeric($value) ? strtotime($value) : $value;
  89. }
  90. protected function setJointimeAttr($value)
  91. {
  92. return $value && !is_numeric($value) ? strtotime($value) : $value;
  93. }
  94. protected function setBirthdayAttr($value)
  95. {
  96. return $value ? $value : null;
  97. }
  98. public function group()
  99. {
  100. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  101. }
  102. /**
  103. * @param $code //国际区号
  104. * @param $mobile //手机号
  105. * @return array
  106. */
  107. public static function getByParentInfo($user_id)
  108. {
  109. return self::where('id', $user_id)->where('is_delete', 0)->find();
  110. }
  111. }