User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /**
  34. * 根据邀请码获取会员信息
  35. * @param $invitation_code
  36. * @return array|bool|\PDOStatement|string|Model|null
  37. */
  38. public static function getByInvitationCode($invitation_code)
  39. {
  40. $info = self::where('invitation_code', $invitation_code)
  41. ->find();
  42. return $info;
  43. }
  44. /**
  45. * 获取代理信息
  46. * @param $invitation_code
  47. * @return array|null
  48. */
  49. public static function getAgentInfoByAgentId($agent_id)
  50. {
  51. $data['usdt'] = '';
  52. $data['bank'] = [];
  53. $data['agent_id'] = $agent_id;
  54. $info = self::where('id', $agent_id)
  55. ->where('is_agent', 1)
  56. ->where('is_lock', 0)
  57. ->where('is_delete', 0)
  58. ->find();
  59. if(empty($info)){
  60. //没找到代理时,默认显示系统配置
  61. if((new Config())->getValue('recharge_usdt_switch')){
  62. $data['usdt'] = (new Config())->getValue('recharge_usdt_address');
  63. }
  64. if((new Config())->getValue('recharge_bank_switch')){
  65. $data['bank']['bank_name'] = (new Config())->getValue('recharge_bank_name');
  66. $data['bank']['bank_card'] = (new Config())->getValue('recharge_bank_card');
  67. $data['bank']['account_name'] = (new Config())->getValue('recharge_account_name');
  68. }
  69. $data['agent_id'] = 0;
  70. return $data;
  71. }
  72. if($info['agent_in_usdt']){
  73. $data['usdt'] = $info['agent_usdt_address'];
  74. }
  75. if($info['agent_in_bank']){
  76. $bank_info = $info['agent_bank_info'];
  77. $data['bank']['bank_name'] = $bank_info['bank_name'];
  78. $data['bank']['bank_card'] = $bank_info['bank_card'];
  79. $data['bank']['real_name'] = $bank_info['real_name'];
  80. }
  81. if(empty($data['usdt']) && empty($data['bank'])){
  82. return [];
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 获取个人URL
  88. * @param string $value
  89. * @param array $data
  90. * @return string
  91. */
  92. public function getUrlAttr($value, $data)
  93. {
  94. return "/u/" . $data['id'];
  95. }
  96. /**
  97. * 获取头像
  98. * @param string $value
  99. * @param array $data
  100. * @return string
  101. */
  102. public function getAvatarAttr($value, $data)
  103. {
  104. if (!$value) {
  105. //如果不需要启用首字母头像,请使用
  106. //$value = '/assets/img/avatar.png';
  107. $value = letter_avatar($data['nickname']);
  108. }
  109. return $value;
  110. }
  111. /**
  112. * 获取验证字段数组值
  113. * @param string $value
  114. * @param array $data
  115. * @return object
  116. */
  117. public function getVerificationAttr($value, $data)
  118. {
  119. $value = array_filter((array)json_decode($value, true));
  120. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  121. return (object)$value;
  122. }
  123. /**
  124. * 设置验证字段
  125. * @param mixed $value
  126. * @return string
  127. */
  128. public function setVerificationAttr($value)
  129. {
  130. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  131. return $value;
  132. }
  133. /**
  134. * 变更会员余额
  135. * @param int $money 余额
  136. * @param int $user_id 会员ID
  137. * @param string $memo 备注
  138. */
  139. public static function money($money, $user_id, $memo)
  140. {
  141. Db::startTrans();
  142. try {
  143. $user = self::lock(true)->find($user_id);
  144. if ($user && $money != 0) {
  145. $before = $user->money;
  146. //$after = $user->money + $money;
  147. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  148. //更新会员信息
  149. $user->save(['money' => $after]);
  150. //写入日志
  151. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  152. }
  153. Db::commit();
  154. } catch (\Exception $e) {
  155. Db::rollback();
  156. }
  157. }
  158. /**
  159. * 变更会员积分
  160. * @param int $score 积分
  161. * @param int $user_id 会员ID
  162. * @param string $memo 备注
  163. */
  164. public static function score($score, $user_id, $memo)
  165. {
  166. Db::startTrans();
  167. try {
  168. $user = self::lock(true)->find($user_id);
  169. if ($user && $score != 0) {
  170. $before = $user->score;
  171. $after = $user->score + $score;
  172. $level = self::nextlevel($after);
  173. //更新会员信息
  174. $user->save(['score' => $after, 'level' => $level]);
  175. //写入日志
  176. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  177. }
  178. Db::commit();
  179. } catch (\Exception $e) {
  180. Db::rollback();
  181. }
  182. }
  183. /**
  184. * 根据积分获取等级
  185. * @param int $score 积分
  186. * @return int
  187. */
  188. public static function nextlevel($score = 0)
  189. {
  190. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  191. $level = 1;
  192. foreach ($lv as $key => $value) {
  193. if ($score >= $value) {
  194. $level = $key;
  195. }
  196. }
  197. return $level;
  198. }
  199. }