connectUserId != $user_id) { $database = get_db_connect($this->table, $user_id); $this->setTable($database['table']); $this->connect($database); $this->sequence('id'); $this->connectUserId = $user_id; } return $this; } public function getSexList() { return ['0' => '未知', '1' => '男', '2' => '女']; } public function getIsSubscribeList() { return ['1' => '已关注', '0' => '未关注']; } public function getIsPayList() { return ['1' => '已充值', '0' => '未充值']; } public function getStateList() { return ['0' => '禁用', '1' => '正常']; } public function getSexTextAttr($value, $data) { $value = $value ? $value : $data['sex']; $list = $this->getSexList(); return isset($list[$value]) ? $list[$value] : ''; } public function getIsSubscribeTextAttr($value, $data) { $value = $value ? $value : $data['is_subscribe']; $list = $this->getIsSubscribeList(); return isset($list[$value]) ? $list[$value] : ''; } public function getIsPayTextAttr($value, $data) { $value = $value ? $value : $data['is_pay']; $list = $this->getIsPayList(); return isset($list[$value]) ? $list[$value] : ''; } public function getVipEndtimeTextAttr($value, $data) { $value = $value ? $value : intval($data['vip_endtime']); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getStateTextAttr($value, $data) { $value = $value ? $value : $data['state']; $list = $this->getStateList(); return isset($list[$value]) ? $list[$value] : ''; } protected function setVipEndtimeAttr($value) { return $value && !is_numeric($value) ? strtotime($value) : $value; } public function getIsvipAttr($value, $data) { $value = $data['vip_endtime'] > time() ? 'VIP' : '否'; return $value; } /* * 获取用户信息 *@param $user_id *@param $getdb 直接取mysql数据 不走redis */ public function getUserInfo($user_id, $getdb = 0) { if (empty($user_id)) return null; if (ApiService::instance()->checkApiOn()) { return \app\source\service\UserService::instance()->getUserInfo($user_id)->toArray(); } $redis = Redis::instance(); $key = 'UN:' . $user_id; $user_info = $redis->hgetall($key); if (!empty($user_info) && $getdb == 0) { if (!array_key_exists('is_first_unfollow', $user_info)) { $redis->del($key); return $this->getUserInfo($user_id, $getdb = 0); } } else { $user_info = []; $userinfo['avatar'] = ''; $db_data = $this->setConnect($user_id)->find(['id' => $user_id]); if (!empty($db_data)) { $db_data = $db_data->toArray(); $redis->hmset($key, $db_data); //up by wanghy 0407 用户信息过期时间由24小时修改为10小时 $redis->expire($key, 36000); $user_info = $db_data; } } return $user_info; } /* * 获得用户喜好书籍 */ public function getLike($userId = null){ //获得用户最近阅读 if($userId){ $recentlyRead = model('UserRecentlyRead')->getRecentlyRead(0,10,$userId); } $bookIds = []; $where = []; $where['state'] = ['eq','1']; if($userId && !empty($recentlyRead) && $recentlyRead['totalNum']>0) { foreach($recentlyRead['data'] as $val){ $bookIds[] = $val['book_id']; } $bookIdsToStr = implode(',',$bookIds); $where['id'] = ['not in',$bookIdsToStr]; } if($userId){ $userInfo = $this->getUserInfo($userId); } //0408 去掉用户喜好字段维护 // //如果有用户喜好 // if($userId && !empty($userInfo['book_category_ids'])){ // $where['book_category_id'] = ['in',$userInfo['book_category_ids']]; // }else{ // //如果没有用户喜好 // if(empty($userInfo['sex'])){ // $where['sex'] = ['eq','1']; // }else{ // $where['sex'] = ['eq',$userInfo['sex']]; // } // } if(empty($userInfo['sex'])){ $where['sex'] = ['eq','1']; }else{ $where['sex'] = ['eq',$userInfo['sex']]; } $channelId = AdminService::instance()->getAdminExtendModel()->getChannelId($userInfo['channel_id']); $isWater = WaterBookService::instance()->showBook($channelId,$userId,Ip::ip()); if ($isWater){ $where['classify_white_list'] = ['eq','1']; } $totalNum = model('Book')->where($where)->count(); $randNum = $totalNum > 4 ? $totalNum-4 : 0; if($randNum>0){ $randNum = mt_rand(0,$randNum); } $result = collection(model('Book')->where($where)->limit($randNum,4)->select())->toArray(); $bookCate = collection(model('BookCategory')->where('status','normal')->select())->toArray(); $cates = []; foreach($bookCate as $v){ $cates[$v['id']] = $v['name']; } foreach($result as $key=>$val){ if(empty($val['book_category_id'])){ $result[$key]['author'] = ''; }else{ if(array_key_exists($val['book_category_id'],$cates)){ $result[$key]['author'] = $cates[$val['book_category_id']]; } } } return $result; } /* * 获取头像 * */ public function getUserAvatar($user_id = null) { if (!$user_id) { $user_id = UserService::instance()->getUserInfo()->id; } $userinfo = $this->getUserInfo($user_id); return isset($userinfo['avatar']) ? $userinfo['avatar'] : '/assets/img/frontend/icon/nav_icon_4.png'; } /* * 获取当前用户的性别频道 *@param $user_sex 为$this->user->sex; * @param $adminconfig array * @param $type true 返回boy|girl|idx fasle 返回 1|2|0 *@return 0 位置 1男 2女 */ public function getUserSex($user_sex=0, $adminconfig, $type = false,$pagesex='boy') { if ($user_sex) { $sex = $user_sex; } else { if($adminconfig['page_sex']){ $sex = $adminconfig['page_sex']; }else{ if($pagesex == 'boy'){ $sex =1; }else{ $sex =2; } } } if ($type) { switch ($sex) { case '0': return 'idx'; case '1': return 'boy'; case '2': return 'girl'; } } return $sex; } /** * 处理自增ID * * @param $data */ protected function autoId($data) { if (!isset($data['id'])) { // 获取redis自增id $redisAuto = Redis::instanceAuto(); $newUserId = $redisAuto->incr('UID'); //redis自增返回新的user_id if (!$newUserId) { Log::error('用户表自增ID获取失败!data:' . json_encode($data)); throw new HttpException(500, '用户表自增ID获取失败!'); } $data['id'] = $newUserId; } return $data; } /** * 插入记录 * @access public * @param mixed $data 数据 * @param boolean $replace 是否replace * @param boolean $getLastInsID 返回自增主键 * @param string $sequence 自增序列名 * @return integer|string */ public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null) { $data = $this->autoId($data); $res = parent::insert($data, $replace, false, $sequence); if ($res) { $this->lastId = $data['id']; if ($getLastInsID) { return $this->lastId; } } return $res; } /** * 插入记录并获取自增ID * @access public * @param mixed $data 数据 * @param boolean $replace 是否replace * @param string $sequence 自增序列名 * @return integer|string */ public function insertGetId(array $data, $replace = false, $sequence = null) { if (ApiService::instance()->checkApiOn()) { $userUpdate = new UserUpdate(); $userUpdate->bind($data); return \app\source\service\UserService::instance()->updateUser($userUpdate)->id; } else{ $data = $this->autoId($data); if (parent::insert($data, $replace, false, $sequence)) { $this->lastId = $data['id']; return $this->lastId; } else { return 0; } } } /** * 获取最近插入的ID * @access public * @param string $sequence 自增序列名 * @return string */ public function getLastInsID($sequence = null) { return $this->lastId; } }