Users.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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($user)
  49. {
  50. $data['usdt'] = '';
  51. $data['bank'] = [];
  52. $data['agent_id'] = $user['agent_id'];
  53. if(empty($data['agent_id'])){
  54. $parent_info = self::where('id', $user['parent_id'])
  55. ->where('is_agent', 1)
  56. ->where('is_lock', 0)
  57. ->where('is_delete', 0)
  58. ->find();
  59. $data['agent_id'] = $parent_info['agent_id'] ?? 0;
  60. }
  61. $info = self::where('id', $data['agent_id'])
  62. ->where('is_agent', 1)
  63. ->where('is_lock', 0)
  64. ->where('is_delete', 0)
  65. ->find();
  66. if(empty($info)){
  67. //没找到代理时,默认显示系统配置
  68. if((new Config())->getValue('recharge_usdt_switch')){
  69. $data['usdt'] = (new Config())->getValue('recharge_usdt_address');
  70. }
  71. if((new Config())->getValue('recharge_bank_switch')){
  72. $data['bank']['bank_name'] = (new Config())->getValue('recharge_bank_name');
  73. $data['bank']['bank_card'] = (new Config())->getValue('recharge_bank_card');
  74. $data['bank']['account_name'] = (new Config())->getValue('recharge_real_name');
  75. }
  76. $data['agent_id'] = 0;
  77. return $data;
  78. }
  79. if($info['agent_in_usdt']){
  80. $data['usdt'] = $info['agent_usdt_address'];
  81. }
  82. if($info['agent_in_bank']){
  83. $bank_info = json_decode($info['agent_bank_info'], true) ;
  84. $data['bank']['bank_name'] = $bank_info['bank_name']?? '';
  85. $data['bank']['bank_card'] = $bank_info['bank_card']?? '';
  86. $data['bank']['account_name'] = $bank_info['account_name']?? '';
  87. }
  88. if(empty($data['usdt']) && empty($data['bank'])){
  89. return [];
  90. }
  91. return $data;
  92. }
  93. /**
  94. * 获取代理信息
  95. * @param $invitation_code
  96. * @return array|null
  97. */
  98. public static function getAgentWithdrawInfoByAgentId($agent_id)
  99. {
  100. $data['agent_id'] = $agent_id;
  101. $info = self::where('id', $agent_id)
  102. ->where('is_agent', 1)
  103. ->where('is_lock', 0)
  104. ->where('is_delete', 0)
  105. ->find();
  106. if(empty($info)){
  107. //没找到代理时,默认显示系统配置
  108. $data['usdt'] = (new Config())->getValue('withdraw_usdt_switch');
  109. $data['bank'] = (new Config())->getValue('withdraw_bank_switch');
  110. $data['agent_id'] = 0;
  111. return $data;
  112. }
  113. $data['usdt'] = $info['agent_out_usdt'];
  114. $data['bank'] = $info['agent_out_bank'];
  115. return $data;
  116. }
  117. /**
  118. * 获取头像
  119. * @param string $value
  120. * @param array $data
  121. * @return string
  122. */
  123. public function getAvatarAttr($value, $data)
  124. {
  125. if (!$value) {
  126. //如果不需要启用首字母头像,请使用
  127. //$value = '/assets/img/avatar.png';
  128. $value = letter_avatar($data['nickname']);
  129. }
  130. return $value;
  131. }
  132. /**
  133. * 获取验证字段数组值
  134. * @param string $value
  135. * @param array $data
  136. * @return object
  137. */
  138. public function getVerificationAttr($value, $data)
  139. {
  140. $value = array_filter((array)json_decode($value, true));
  141. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  142. return (object)$value;
  143. }
  144. /**
  145. * 设置验证字段
  146. * @param mixed $value
  147. * @return string
  148. */
  149. public function setVerificationAttr($value)
  150. {
  151. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  152. return $value;
  153. }
  154. /**
  155. * 变更会员余额
  156. * @param int $money 余额
  157. * @param int $user_id 会员ID
  158. * @param string $memo 备注
  159. */
  160. public function setInvitationCode($user_id)
  161. {
  162. $inviteCode = $this->generateInviteCode();
  163. $this->where('id', $user_id)->update(['invitation_code' => $inviteCode]);
  164. return $inviteCode;
  165. }
  166. /**
  167. * 生成随机邀请码
  168. * @param $len
  169. * @return string
  170. */
  171. public function generateInviteCode($len = 4)
  172. {
  173. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  174. $inviteCode = '';
  175. for ($i = 0; $i < $len; $i++) {
  176. $inviteCode .= $characters[mt_rand(0, strlen($characters) - 1)];
  177. }
  178. //检查是否重复
  179. if($this->where('invitation_code', $inviteCode)->count()){
  180. return $this->generateInviteCode($len);
  181. }
  182. return $inviteCode;
  183. }
  184. }