123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- namespace app\common\model;
- use app\common\library\Ip;
- use app\common\library\Redis;
- use app\common\service\WaterBookService;
- use app\main\service\AdminService;
- use app\main\service\ApiService;
- use app\main\service\FinancialService;
- use app\main\service\UserService;
- use app\source\model\UserUpdate;
- use think\Cookie;
- use think\Model;
- use think\exception\HttpException;
- use think\Log;
- class User extends Model
- {
- // 表名
- protected $table = 'user';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'sex_text',
- 'is_subscribe_text',
- 'is_pay_text',
- 'vip_endtime_text',
- 'state_text',
- 'isvip'
- ];
- //当前链接的user_id分库
- protected $connectUserId = null;
- //最后一个插入的id
- protected $lastId = 0;
- /**
- * 设置分库链接数据
- *
- * @param $channel_id
- * @param $openid
- * @return $this
- */
- public function setConnect($user_id)
- {
- if ($this->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;
- }
- }
|