* @version : HUOSDK 8.0 */ namespace huo\model\user; use huo\model\agent\AgentRateModel; use huo\model\common\CommonModel; use huo\model\member\MemberModel; use huolib\constant\AgentConst; use huolib\constant\CommonConst; use huolib\constant\MemConst; use think\Cache; class UserModel extends CommonModel { CONST USER_STATUS_UNVERIFIED = 1; CONST USER_STATUS_ACTIVATED = 2; CONST USER_STATUS_BLOCKED = 3; CONST USER_SWITCH_YES = 1; CONST USER_SWITCH_NO = 2; protected $name = 'user'; protected $type = ['ext_info' => 'array']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $agent_role = '23'; protected $cache_tag = 'agent_id_name'; /** * 关联agent_ext表 * * @return \think\model\relation\HasOne */ public function ext() { return $this->hasOne(AgentExtModel::className(), 'agent_id', 'id', [], 'left')->setEagerlyType(0); } /** * 获取余额 * * @return \think\model\relation\HasOne */ public function remain() { return $this->hasOne(AgentExtModel::className(), 'agent_id', 'id') ->field('agent_id, share_total,frozen_amount,share_remain'); } /** * 关联父渠道 * * @return \think\model\relation\HasOne */ public function parent() { return $this->hasOne(UserModel::className(), 'id', 'parent_id')->field('id,user_nicename,user_login'); } /** * 关联玩家 * * @return \think\model\relation\HasOne */ public function mem() { return $this->hasOne(MemberModel::className(), 'id', 'mem_id')->field('id,username,nickname,mobile,real_name'); } /** * 折扣 * * @return \think\model\relation\HasOne */ public function rate() { return $this->hasOne(AgentRateModel::className(), 'agent_id', 'id', [], 'left') ->field('agent_rate,sub_agent_rate') ->setEagerlyType(0); } /** * 设置用户支付密码 * * @param string $value * * @return string */ public function setPayPwdAttr($value) { if (!empty($value)) { return cmf_password($value); } return $value; } /** * 设置用户密码 * * @param string $value * * @return string */ public function setUserPassAttr($value = '') { if (!empty($value)) { return cmf_password($value); } return $value; } /** * 获取状态文字 * * @param $value * * @return mixed */ public function getStatusTextAttr($value) { $_user_statuses = array( self::USER_STATUS_UNVERIFIED => '未验证', self::USER_STATUS_ACTIVATED => '正常', self::USER_STATUS_BLOCKED => '已拉黑' ); return isset($_user_statuses[$value]) ? $_user_statuses[$value] : ''; } /** * 获取切量文字 * * @param $value * * @return mixed */ public function getSwitchTextAttr($value) { $_user_switchs = array( self::USER_SWITCH_YES => '切量', self::USER_SWITCH_NO => '不切' ); return isset($_user_switchs[$value]) ? $_user_switchs[$value] : ''; } /** * 通过代理ID获取信息 * * @param $user_id * * @return array|false */ public function getUserInfo($user_id) { $_map = array(); if (empty($user_id) || $user_id < 0) { return false; } $_map['id'] = $user_id; $_info = $this->where($_map)->find(); if (false === $_info) { return false; } if (is_object($_info)) { return $_info->toArray(); } else { return $_info; } } /** * @param array $data * * @return bool|mixed */ public function addData($data = []) { $_data['user_login'] = get_val($data, 'user_login', ''); $_data['user_pass'] = get_val($data, 'user_pass', ''); $_data['pay_pwd'] = get_val($data, 'pay_pwd', ''); $_data['user_nicename'] = get_val($data, 'user_nicename', ''); $_data['user_email'] = get_val($data, 'user_email', ''); $_data['user_url'] = get_val($data, 'user_url', ''); $_data['avatar'] = get_val($data, 'avatar', ''); $_data['sex'] = get_val($data, 'sex', MemConst::GENDER_N); $_data['birthday'] = get_val($data, 'birthday', 0); $_data['signature'] = get_val($data, 'signature', ''); $_data['last_login_ip'] = get_val($data, 'last_login_ip', request()->ip()); $_data['last_login_time'] = get_val($data, 'last_login_time', time()); $_data['user_activation_key'] = get_val($data, 'user_activation_key', ''); $_data['user_status'] = get_val($data, 'user_status', MemConst::STATUS_NORMAL); $_data['score'] = get_val($data, 'score', 0); $_data['role_id'] = get_val($data, 'role_id', 0); $_data['coin'] = get_val($data, 'coin', 0); $_data['mobile'] = get_val($data, 'mobile', ''); $_data['qq'] = get_val($data, 'qq', ''); $_data['linkman'] = get_val($data, 'linkman', ''); $_data['parent_id'] = get_val($data, 'parent_id', 1); $_data['mem_id'] = get_val($data, 'mem_id', 0); $_data['cp_id'] = get_val($data, 'cp_id', 0); $_ext_info = get_val($_data, 'ext_info', []); $_agent_charge_type = get_val($data, 'agent_charge_type', ''); if (!empty($_agent_charge_type)) { $_ext_info['agent_charge_type'] = $_agent_charge_type; } $_price = get_val($data, 'price', ''); if (!empty($_price)) { $_ext_info['price'] = $_price; } $_wx_id = get_val($data, 'wx_id', ''); if (!empty($_wx_id)) { $_ext_info['wx_id'] = $_wx_id; } if ($_obj = self::create($_data, true)) { $_ext_data['agent_id'] = $_obj->id; /* 写入扩展 */ $_obj->ext()->save($_ext_data); if (isset($_data['role_id']) && AgentConst::AGENT_ROLE_MP_MEMBER != $_data['role_id']) { /* 小游戏玩家渠道不更新该缓存 20200313 chenbingling*/ Cache::clear($this->cache_tag); } return $_obj->id; } else { return false; } } /** * 更新数据 * * @param array $data * * @param int $user_id * * @return bool|mixed */ public function updateData($data = [], $user_id) { $_map['id'] = $user_id; $_data = $data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { Cache::clear($this->cache_tag); return true; } } /** * 通过ID获取名称 * * @param $id * * @return mixed */ public function getNameById($id) { if (empty($id)) { return ''; } $_map['id'] = $id; return $this->where($_map)->value('user_login'); } /** * @param mixed $agent_ids * @param bool $inc_me * * @return array */ public function getSubAgents($agent_ids, $inc_me = false, $role = 0) { if (!(is_array($agent_ids) || is_numeric($agent_ids))) { return []; } $_agent_ids = $agent_ids; if (is_numeric($agent_ids)) { $_agent_ids = [$agent_ids]; } $_map['parent_id'] = ['in', $_agent_ids]; if (!empty($role)) { $_map['role_id'] = $role; } $_id_arr = $this->where($_map)->column('id'); if ($inc_me) { $_id_arr = array_merge($_agent_ids, $_id_arr); } return $_id_arr; } /** * @param mixed $role_ids * * @return array */ public function getIdsByRoleId($role_ids) { if (!(is_array($role_ids) || is_numeric($role_ids))) { return []; } $_role_ids = $role_ids; if (is_numeric($role_ids)) { $_role_ids = [$role_ids]; } $_map['role_id'] = ['in', $_role_ids]; $_id_arr = $this->where($_map)->column('id'); return $_id_arr; } /** * 渠道ID 名称对应表 * * @param array $where * @param bool $inc_nice * * @return array */ public function getIdNames($where, $inc_nice = false) { $_tag = $this->cache_tag; $_cache_key = md5($_tag.json_encode($where)); $_field = 'user_login'; if ($inc_nice) { $_cache_key = 'inc_nice'.$_cache_key; $_field = "concat(user_login,'(',user_nicename,')') as name"; } $_agents = $this->where($where) ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag) ->column($_field, 'id'); return $_agents; } /** * 获取总渠道列表 * @param $where * @param false $inc_nice * @return array|false|string */ public function getNames($where, $inc_nice = false) { $_tag = $this->cache_tag; $_cache_key = md5($_tag . json_encode($where)); $_field = 'user_login'; if ($inc_nice) { $_cache_key = 'inc_nice' . $_cache_key; $_field = "user_nicename as name"; } return $this->where($where) ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag) ->column($_field, 'id'); } /** * 总计 * * @param int $_start_time * @param int $_end_time * * @param array $map * * @return int */ public function agentTotal($_start_time = 0, $_end_time = 0, $map = []) { $_role_types = [AgentConst::ROLE_TYPE_GROUP, AgentConst::ROLE_TYPE_AGENT]; $_role_ids = (new RoleModel())->getIdsByRoleType($_role_types); $where['role_id'] = ['in', $_role_ids]; if (!empty($_start_time) && !empty($_end_time)) { $where['create_time'] = ['between', [$_start_time, $_end_time]]; } return $this->where($where)->where($map)->count('id'); } /** * 昨日新增 * * @return int */ public function agentYesterdayCount() { return $this->getDayAddedCount(strtotime('yesterday')); } /** * 今日新增 * * @return int */ public function agentTodayCount() { return $this->getDayAddedCount(strtotime('today')); } /** * 一天新增 * * @param int $time 时间戳,0点 * * @return int */ public function getDayAddedCount($time) { // $where['role_id'] = ['like', "%{$this->agent_role}%"]; // $where['create_time'] = ['between', [$time, $time + 24 * 3600 - 1]]; return $this->agentTotal($time, $time + 24 * 3600 - 1); // return $this->where($where)->count('id'); } /** * 上线的渠道数 * * @param array $where * * @return int */ public function onlineCount($where) { $_map = $where; $_map['user_status'] = MemConst::STATUS_NORMAL; return $this->agentTotal(0, time(), $_map); // return $this->where($_map)->count('id'); } /** * 校验支付密码是否正确 * * @param string $pay_password 提交的支付密码 * * @return bool */ public function verifyPayPassword($pay_password) { if (cmf_password($pay_password) == $this->pay_pwd) { return true; } return false; } /** * 根据用户名获取ID * * @param $user_login * * @return mixed */ public function getIdByName($user_login) { return $this->where(['user_login' => $user_login])->value('id'); } /** * 根据用户昵称获取IDs * * @param $user_nicename * * @return mixed */ public function getIdsByNickName($user_nicename) { return $this->where(['user_nicename' => $user_nicename])->column('id'); } /** * 根据玩家ID获取渠道ID * * @param int $mem_id 玩家ID * * @return int */ public function getIdByMemId($mem_id) { return $this->where(['mem_id' => $mem_id])->value('id'); } /** * 根据渠道ID获取玩家ID * * @param int $id 玩家渠道ID * * @return int */ public function getMemIdById($id) { return $this->where(['id' => $id])->value('mem_id'); } /** * 根据玩家ID获取渠道信息 * * @param $mem_id * * @return array|bool|false|\PDOStatement|string|\think\Model */ public function getInfoByMemId($mem_id) { $_map['mem_id'] = $mem_id; $_info = $this->useGlobalScope(false)->where($_map)->find(); if (false === $_info) { return false; } if (is_object($_info)) { return $_info->toArray(); } else { return $_info; } } /** * 获取子渠道 * * @param $parent_id * @param array $where * * @return array */ public function getIdsByParentId($parent_id, $where = []) { $_map = $where; $_map['parent_id'] = $parent_id; return $this->where($_map)->column('id'); } /** * 获取子渠道 * * @param $mem_ids * @param array $where * * @return array */ public function getIdsByMemIds($mem_ids, $where = []) { if (empty($mem_ids)) { return []; } $_map = $where; $_map['mem_id'] = ['in', $mem_ids]; return $this->where($_map)->column('id'); } /** * 获取子渠道 * * @param $ids * @param array $where * * @return array */ public function getMemIdsByIds($ids, $where = []) { if (empty($mem_ids)) { return []; } $_map = $where; $_map['id'] = ['in', $ids]; return $this->where($_map)->column('mem_id'); } /** * 根据cp_id获取一级渠道id * * @param $cp_id * @param int $role_id * * @return array */ public function getIdsByCpId($cp_id, $role_id = AgentConst::AGENT_ROLE_MP_GROUP) { $_map = [ 'role_id' => $role_id, 'cp_id' => $cp_id ]; return $this->where($_map)->column('id'); } /*** * 获取父id * * @param $id * * @return array */ public function getParentIdById($id) { $_map['id'] = $id; return $this->where($_map)->value('parent_id'); } /*** * 获取父id * * @param $id * * @return array */ public function getRoleIdById($id) { $_map['id'] = $id; return $this->where($_map)->value('role_id'); } }