User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $value = '';
  42. }
  43. return $value;
  44. }
  45. /**
  46. * 获取会员的组别
  47. */
  48. public function getGroupAttr($value, $data)
  49. {
  50. return UserGroup::get($data['group_id']);
  51. }
  52. /**
  53. * 获取验证字段数组值
  54. * @param string $value
  55. * @param array $data
  56. * @return object
  57. */
  58. public function getVerificationAttr($value, $data)
  59. {
  60. $value = array_filter((array)json_decode($value, true));
  61. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  62. return (object)$value;
  63. }
  64. /**
  65. * 设置验证字段
  66. * @param mixed $value
  67. * @return string
  68. */
  69. public function setVerificationAttr($value)
  70. {
  71. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  72. return $value;
  73. }
  74. /**
  75. * 变更会员余额
  76. * @param int $money 余额
  77. * @param int $user_id 会员ID
  78. * @param string $memo 备注
  79. */
  80. public static function money($money, $user_id, $memo)
  81. {
  82. Db::startTrans();
  83. try {
  84. $user = self::lock(true)->find($user_id);
  85. if ($user && $money != 0) {
  86. $before = $user->money;
  87. //$after = $user->money + $money;
  88. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  89. //更新会员信息
  90. $user->save(['money' => $after]);
  91. //写入日志
  92. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  93. }
  94. Db::commit();
  95. } catch (\Exception $e) {
  96. Db::rollback();
  97. }
  98. }
  99. /**
  100. * 变更会员积分
  101. * @param int $score 积分
  102. * @param int $user_id 会员ID
  103. * @param string $memo 备注
  104. */
  105. public static function score($score, $user_id, $memo)
  106. {
  107. Db::startTrans();
  108. try {
  109. $user = self::lock(true)->find($user_id);
  110. if ($user && $score != 0) {
  111. $before = $user->score;
  112. $after = $user->score + $score;
  113. $level = self::nextlevel($after);
  114. //更新会员信息
  115. $user->save(['score' => $after, 'level' => $level]);
  116. //写入日志
  117. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  118. }
  119. Db::commit();
  120. } catch (\Exception $e) {
  121. Db::rollback();
  122. }
  123. }
  124. /**
  125. * 根据积分获取等级
  126. * @param int $score 积分
  127. * @return int
  128. */
  129. public static function nextlevel($score = 0)
  130. {
  131. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  132. $level = 1;
  133. foreach ($lv as $key => $value) {
  134. if ($score >= $value) {
  135. $level = $key;
  136. }
  137. }
  138. return $level;
  139. }
  140. }