Users.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 会员模型
  7. */
  8. class Users 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. ];
  20. /**
  21. * @param $code //国际区号
  22. * @param $mobile //手机号
  23. * @return array
  24. */
  25. public static function getByCodeAndMobile($code, $mobile)
  26. {
  27. $info = self::where('code', $code)
  28. ->where('mobile', $mobile)
  29. ->find();
  30. return $info;
  31. }
  32. /**
  33. * 根据邀请码获取会员信息
  34. * @param $invitation_code
  35. * @return array|bool|\PDOStatement|string|Model|null
  36. */
  37. public static function getByInvitationCode($invitation_code)
  38. {
  39. $info = self::where('invitation_code', $invitation_code)
  40. ->find();
  41. return $info;
  42. }
  43. /**
  44. * 获取代理信息
  45. * @param $invitation_code
  46. * @return array|null
  47. */
  48. public static function getAgentRechargeInfoByAgentId($agent_id)
  49. {
  50. $data['usdt'] = '';
  51. $data['bank'] = [];
  52. $data['agent_id'] = $agent_id;
  53. $info = self::where('id', $agent_id)
  54. ->where('is_agent', 1)
  55. ->where('is_lock', 0)
  56. ->where('is_delete', 0)
  57. ->find();
  58. if(empty($info)){
  59. //没找到代理时,默认显示系统配置
  60. if((new Config())->getValue('recharge_usdt_switch')){
  61. $data['usdt'] = (new Config())->getValue('recharge_usdt_address');
  62. }
  63. if((new Config())->getValue('recharge_bank_switch')){
  64. $data['bank']['bank_name'] = (new Config())->getValue('recharge_bank_name');
  65. $data['bank']['bank_card'] = (new Config())->getValue('recharge_bank_card');
  66. $data['bank']['account_name'] = (new Config())->getValue('recharge_account_name');
  67. }
  68. $data['agent_id'] = 0;
  69. return $data;
  70. }
  71. if($info['agent_in_usdt']){
  72. $data['usdt'] = $info['agent_usdt_address'];
  73. }
  74. if($info['agent_in_bank']){
  75. $bank_info = $info['agent_bank_info'];
  76. $data['bank']['bank_name'] = $bank_info['bank_name'];
  77. $data['bank']['bank_card'] = $bank_info['bank_card'];
  78. $data['bank']['real_name'] = $bank_info['real_name'];
  79. }
  80. if(empty($data['usdt']) && empty($data['bank'])){
  81. return [];
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 获取代理信息
  87. * @param $invitation_code
  88. * @return array|null
  89. */
  90. public static function getAgentWithdrawInfoByAgentId($agent_id)
  91. {
  92. $data['agent_id'] = $agent_id;
  93. $info = self::where('id', $agent_id)
  94. ->where('is_agent', 1)
  95. ->where('is_lock', 0)
  96. ->where('is_delete', 0)
  97. ->find();
  98. if(empty($info)){
  99. //没找到代理时,默认显示系统配置
  100. $data['usdt'] = (new Config())->getValue('withdraw_usdt_switch');
  101. $data['bank'] = (new Config())->getValue('withdraw_bank_switch');
  102. $data['agent_id'] = 0;
  103. return $data;
  104. }
  105. $data['usdt'] = $info['agent_out_usdt'];
  106. $data['bank'] = $info['agent_out_bank'];
  107. return $data;
  108. }
  109. /**
  110. * 获取头像
  111. * @param string $value
  112. * @param array $data
  113. * @return string
  114. */
  115. public function getAvatarAttr($value, $data)
  116. {
  117. if (!$value) {
  118. //如果不需要启用首字母头像,请使用
  119. //$value = '/assets/img/avatar.png';
  120. $value = letter_avatar($data['nickname']);
  121. }
  122. return $value;
  123. }
  124. /**
  125. * 获取验证字段数组值
  126. * @param string $value
  127. * @param array $data
  128. * @return object
  129. */
  130. public function getVerificationAttr($value, $data)
  131. {
  132. $value = array_filter((array)json_decode($value, true));
  133. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  134. return (object)$value;
  135. }
  136. /**
  137. * 设置验证字段
  138. * @param mixed $value
  139. * @return string
  140. */
  141. public function setVerificationAttr($value)
  142. {
  143. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  144. return $value;
  145. }
  146. /**
  147. * 变更会员余额
  148. * @param int $money 余额
  149. * @param int $user_id 会员ID
  150. * @param string $memo 备注
  151. */
  152. public function setInvitationCode($user_id)
  153. {
  154. $inviteCode = $this->generateInviteCode();
  155. $this->where('id', $user_id)->update(['invitation_code' => $inviteCode]);
  156. return $inviteCode;
  157. }
  158. /**
  159. * 生成随机邀请码
  160. * @param $len
  161. * @return string
  162. */
  163. protected function generateInviteCode($len = 4)
  164. {
  165. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  166. $inviteCode = '';
  167. for ($i = 0; $i < $len; $i++) {
  168. $inviteCode .= $characters[mt_rand(0, strlen($characters) - 1)];
  169. }
  170. //检查是否重复
  171. if($this->where('invitation_code', $inviteCode)->count()){
  172. return $this->generateInviteCode($len);
  173. }
  174. return $inviteCode;
  175. }
  176. }