User.php 4.2 KB

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