User.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 getAgentRechargeInfoByAgentId($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. * 获取代理信息
  88. * @param $invitation_code
  89. * @return array|null
  90. */
  91. public static function getAgentWithdrawInfoByAgentId($agent_id)
  92. {
  93. $data['agent_id'] = $agent_id;
  94. $info = self::where('id', $agent_id)
  95. ->where('is_agent', 1)
  96. ->where('is_lock', 0)
  97. ->where('is_delete', 0)
  98. ->find();
  99. if(empty($info)){
  100. //没找到代理时,默认显示系统配置
  101. $data['usdt'] = (new Config())->getValue('withdraw_usdt_switch');
  102. $data['bank'] = (new Config())->getValue('withdraw_bank_switch');
  103. $data['agent_id'] = 0;
  104. return $data;
  105. }
  106. $data['usdt'] = $info['agent_out_usdt'];
  107. $data['bank'] = $info['agent_out_bank'];
  108. return $data;
  109. }
  110. /**
  111. * 获取个人URL
  112. * @param string $value
  113. * @param array $data
  114. * @return string
  115. */
  116. public function getUrlAttr($value, $data)
  117. {
  118. return "/u/" . $data['id'];
  119. }
  120. /**
  121. * 获取头像
  122. * @param string $value
  123. * @param array $data
  124. * @return string
  125. */
  126. public function getAvatarAttr($value, $data)
  127. {
  128. if (!$value) {
  129. //如果不需要启用首字母头像,请使用
  130. //$value = '/assets/img/avatar.png';
  131. $value = letter_avatar($data['nickname']);
  132. }
  133. return $value;
  134. }
  135. /**
  136. * 获取验证字段数组值
  137. * @param string $value
  138. * @param array $data
  139. * @return object
  140. */
  141. public function getVerificationAttr($value, $data)
  142. {
  143. $value = array_filter((array)json_decode($value, true));
  144. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  145. return (object)$value;
  146. }
  147. /**
  148. * 设置验证字段
  149. * @param mixed $value
  150. * @return string
  151. */
  152. public function setVerificationAttr($value)
  153. {
  154. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  155. return $value;
  156. }
  157. /**
  158. * 变更会员余额
  159. * @param int $money 余额
  160. * @param int $user_id 会员ID
  161. * @param string $memo 备注
  162. */
  163. public static function money($money, $user_id, $memo)
  164. {
  165. Db::startTrans();
  166. try {
  167. $user = self::lock(true)->find($user_id);
  168. if ($user && $money != 0) {
  169. $before = $user->money;
  170. //$after = $user->money + $money;
  171. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  172. //更新会员信息
  173. $user->save(['money' => $after]);
  174. //写入日志
  175. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  176. }
  177. Db::commit();
  178. } catch (\Exception $e) {
  179. Db::rollback();
  180. }
  181. }
  182. /**
  183. * 变更会员积分
  184. * @param int $score 积分
  185. * @param int $user_id 会员ID
  186. * @param string $memo 备注
  187. */
  188. public static function score($score, $user_id, $memo)
  189. {
  190. Db::startTrans();
  191. try {
  192. $user = self::lock(true)->find($user_id);
  193. if ($user && $score != 0) {
  194. $before = $user->score;
  195. $after = $user->score + $score;
  196. $level = self::nextlevel($after);
  197. //更新会员信息
  198. $user->save(['score' => $after, 'level' => $level]);
  199. //写入日志
  200. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  201. }
  202. Db::commit();
  203. } catch (\Exception $e) {
  204. Db::rollback();
  205. }
  206. }
  207. /**
  208. * 根据积分获取等级
  209. * @param int $score 积分
  210. * @return int
  211. */
  212. public static function nextlevel($score = 0)
  213. {
  214. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  215. $level = 1;
  216. foreach ($lv as $key => $value) {
  217. if ($score >= $value) {
  218. $level = $key;
  219. }
  220. }
  221. return $level;
  222. }
  223. }