You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

138 lines
3.5 KiB

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