User.php 4.5 KB

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