User.php 4.1 KB

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