| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Model;
- /**
- * 会员模型
- */
- class Users extends Model
- {
- // 表名
- protected $name = 'users';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = false;
- // 追加属性
- protected $append = [
- ];
- /**
- * @param $code //国际区号
- * @param $mobile //手机号
- * @return array
- */
- public static function getByCodeAndMobile($code, $mobile)
- {
- $info = self::where('code', $code)
- ->where('mobile', $mobile)
- ->find();
- return $info;
- }
- /**
- * 根据邀请码获取会员信息
- * @param $invitation_code
- * @return array|bool|\PDOStatement|string|Model|null
- */
- public static function getByInvitationCode($invitation_code)
- {
- $info = self::where('invitation_code', $invitation_code)
- ->find();
- return $info;
- }
- /**
- * 获取代理信息
- * @param $invitation_code
- * @return array|null
- */
- public static function getAgentRechargeInfoByAgentId($user)
- {
- $data['usdt'] = '';
- $data['bank'] = [];
- $data['agent_id'] = $user['agent_id'];
- if(empty($data['agent_id'])){
- $parent_info = self::where('id', $user['parent_id'])
- ->where('is_agent', 1)
- ->where('is_lock', 0)
- ->where('is_delete', 0)
- ->find();
- $data['agent_id'] = $parent_info['agent_id'] ?? 0;
- }
- $info = self::where('id', $data['agent_id'])
- ->where('is_agent', 1)
- ->where('is_lock', 0)
- ->where('is_delete', 0)
- ->find();
- if(empty($info)){
- //没找到代理时,默认显示系统配置
- if((new Config())->getValue('recharge_usdt_switch')){
- $data['usdt'] = (new Config())->getValue('recharge_usdt_address');
- }
- if((new Config())->getValue('recharge_bank_switch')){
- $data['bank']['bank_name'] = (new Config())->getValue('recharge_bank_name');
- $data['bank']['bank_card'] = (new Config())->getValue('recharge_bank_card');
- $data['bank']['account_name'] = (new Config())->getValue('recharge_real_name');
- }
- $data['agent_id'] = 0;
- return $data;
- }
- if($info['agent_in_usdt']){
- $data['usdt'] = $info['agent_usdt_address'];
- }
- if($info['agent_in_bank']){
- $bank_info = json_decode($info['agent_bank_info'], true) ;
- $data['bank']['bank_name'] = $bank_info['bank_name']?? '';
- $data['bank']['bank_card'] = $bank_info['bank_card']?? '';
- $data['bank']['account_name'] = $bank_info['account_name']?? '';
- }
- if(empty($data['usdt']) && empty($data['bank'])){
- return [];
- }
- return $data;
- }
- /**
- * 获取代理信息
- * @param $invitation_code
- * @return array|null
- */
- public static function getAgentWithdrawInfoByAgentId($agent_id)
- {
- $data['agent_id'] = $agent_id;
- $info = self::where('id', $agent_id)
- ->where('is_agent', 1)
- ->where('is_lock', 0)
- ->where('is_delete', 0)
- ->find();
- if(empty($info)){
- //没找到代理时,默认显示系统配置
- $data['usdt'] = (new Config())->getValue('withdraw_usdt_switch');
- $data['bank'] = (new Config())->getValue('withdraw_bank_switch');
- $data['agent_id'] = 0;
- return $data;
- }
- $data['usdt'] = $info['agent_out_usdt'];
- $data['bank'] = $info['agent_out_bank'];
- return $data;
- }
- /**
- * 获取头像
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getAvatarAttr($value, $data)
- {
- if (!$value) {
- //如果不需要启用首字母头像,请使用
- //$value = '/assets/img/avatar.png';
- $value = letter_avatar($data['nickname']);
- }
- return $value;
- }
- /**
- * 获取验证字段数组值
- * @param string $value
- * @param array $data
- * @return object
- */
- public function getVerificationAttr($value, $data)
- {
- $value = array_filter((array)json_decode($value, true));
- $value = array_merge(['email' => 0, 'mobile' => 0], $value);
- return (object)$value;
- }
- /**
- * 设置验证字段
- * @param mixed $value
- * @return string
- */
- public function setVerificationAttr($value)
- {
- $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
- return $value;
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public function setInvitationCode($user_id)
- {
- $inviteCode = $this->generateInviteCode();
- $this->where('id', $user_id)->update(['invitation_code' => $inviteCode]);
- return $inviteCode;
- }
- /**
- * 生成随机邀请码
- * @param $len
- * @return string
- */
- public function generateInviteCode($len = 4)
- {
- $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $inviteCode = '';
- for ($i = 0; $i < $len; $i++) {
- $inviteCode .= $characters[mt_rand(0, strlen($characters) - 1)];
- }
- //检查是否重复
- if($this->where('invitation_code', $inviteCode)->count()){
- return $this->generateInviteCode($len);
- }
- return $inviteCode;
- }
- }
|