Users.php 5.8 KB

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