User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 会员模型
  7. */
  8. class User extends Model
  9. {
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  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. Db::startTrans();
  82. try {
  83. $user = self::lock(true)->find($user_id);
  84. if ($user && $money != 0) {
  85. $before = $user->money;
  86. //$after = $user->money + $money;
  87. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  88. //更新会员信息
  89. $user->save(['money' => $after]);
  90. //写入日志
  91. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  92. }
  93. Db::commit();
  94. } catch (\Exception $e) {
  95. Db::rollback();
  96. }
  97. }
  98. /**
  99. * 变更会员积分
  100. * @param int $score 积分
  101. * @param int $user_id 会员ID
  102. * @param string $memo 备注
  103. */
  104. public static function score($score, $user_id, $memo)
  105. {
  106. Db::startTrans();
  107. try {
  108. $user = self::lock(true)->find($user_id);
  109. if ($user && $score != 0) {
  110. $before = $user->score;
  111. $after = $user->score + $score;
  112. $level = self::nextlevel($after);
  113. //更新会员信息
  114. $user->save(['score' => $after, 'level' => $level]);
  115. //写入日志
  116. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  117. }
  118. Db::commit();
  119. } catch (\Exception $e) {
  120. Db::rollback();
  121. }
  122. }
  123. /**
  124. * 根据积分获取等级
  125. * @param int $score 积分
  126. * @return int
  127. */
  128. public static function nextlevel($score = 0)
  129. {
  130. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  131. $level = 1;
  132. foreach ($lv as $key => $value) {
  133. if ($score >= $value) {
  134. $level = $key;
  135. }
  136. }
  137. return $level;
  138. }
  139. }