User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 会员模型
  6. */
  7. class User extends Model
  8. {
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $pk = 'id';
  15. // 追加属性
  16. protected $append = [
  17. 'url',
  18. ];
  19. /**
  20. * 获取个人URL
  21. * @param string $value
  22. * @param array $data
  23. * @return string
  24. */
  25. public function getUrlAttr($value, $data)
  26. {
  27. return "/u/" . $data['id'];
  28. }
  29. /**
  30. * 获取头像
  31. * @param string $value
  32. * @param array $data
  33. * @return string
  34. */
  35. public function getAvatarAttr($value, $data)
  36. {
  37. if (!$value) {
  38. //如果不需要启用首字母头像,请使用
  39. //$value = '/assets/img/avatar.png';
  40. $value = letter_avatar($data['nickname']);
  41. }
  42. return $value;
  43. }
  44. /**
  45. * 获取会员的组别
  46. */
  47. public function getGroupAttr($value, $data)
  48. {
  49. return UserGroup::get($data['group_id']);
  50. }
  51. /**
  52. * 获取验证字段数组值
  53. * @param string $value
  54. * @param array $data
  55. * @return object
  56. */
  57. public function getVerificationAttr($value, $data)
  58. {
  59. $value = array_filter((array)json_decode($value, true));
  60. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  61. return (object)$value;
  62. }
  63. /**
  64. * 设置验证字段
  65. * @param mixed $value
  66. * @return string
  67. */
  68. public function setVerificationAttr($value)
  69. {
  70. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  71. return $value;
  72. }
  73. /**
  74. * 变更会员余额
  75. * @param int $money 余额
  76. * @param int $user_id 会员ID
  77. * @param string $memo 备注
  78. */
  79. public static function money($money, $user_id, $memo)
  80. {
  81. $user = self::get($user_id);
  82. if ($user && $money != 0) {
  83. $before = $user->money;
  84. $after = $user->money + $money;
  85. //更新会员信息
  86. $user->save(['money' => $after]);
  87. //写入日志
  88. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  89. }
  90. }
  91. /**
  92. * 变更会员积分
  93. * @param int $score 积分
  94. * @param int $user_id 会员ID
  95. * @param string $memo 备注
  96. */
  97. public static function score($score, $user_id, $memo)
  98. {
  99. $user = self::get($user_id);
  100. if ($user && $score != 0) {
  101. $before = $user->score;
  102. $after = $user->score + $score;
  103. $level = self::nextlevel($after);
  104. //更新会员信息
  105. $user->save(['score' => $after, 'level' => $level]);
  106. //写入日志
  107. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  108. }
  109. }
  110. /**
  111. * 根据积分获取等级
  112. * @param int $score 积分
  113. * @return int
  114. */
  115. public static function nextlevel($score = 0)
  116. {
  117. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  118. $level = 1;
  119. foreach ($lv as $key => $value) {
  120. if ($score >= $value) {
  121. $level = $key;
  122. }
  123. }
  124. return $level;
  125. }
  126. }