User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Ip;
  4. use app\common\library\Redis;
  5. use app\common\service\WaterBookService;
  6. use app\main\service\AdminService;
  7. use app\main\service\ApiService;
  8. use app\main\service\FinancialService;
  9. use app\main\service\UserService;
  10. use app\source\model\UserUpdate;
  11. use think\Cookie;
  12. use think\Model;
  13. use think\exception\HttpException;
  14. use think\Log;
  15. class User extends Model
  16. {
  17. // 表名
  18. protected $table = 'user';
  19. // 自动写入时间戳字段
  20. protected $autoWriteTimestamp = 'int';
  21. // 定义时间戳字段名
  22. protected $createTime = 'createtime';
  23. protected $updateTime = 'updatetime';
  24. // 追加属性
  25. protected $append = [
  26. 'sex_text',
  27. 'is_subscribe_text',
  28. 'is_pay_text',
  29. 'vip_endtime_text',
  30. 'state_text',
  31. 'isvip'
  32. ];
  33. //当前链接的user_id分库
  34. protected $connectUserId = null;
  35. //最后一个插入的id
  36. protected $lastId = 0;
  37. /**
  38. * 设置分库链接数据
  39. *
  40. * @param $channel_id
  41. * @param $openid
  42. * @return $this
  43. */
  44. public function setConnect($user_id)
  45. {
  46. if ($this->connectUserId != $user_id) {
  47. $database = get_db_connect($this->table, $user_id);
  48. $this->setTable($database['table']);
  49. $this->connect($database);
  50. $this->sequence('id');
  51. $this->connectUserId = $user_id;
  52. }
  53. return $this;
  54. }
  55. public function getSexList()
  56. {
  57. return ['0' => '未知', '1' => '男', '2' => '女'];
  58. }
  59. public function getIsSubscribeList()
  60. {
  61. return ['1' => '已关注', '0' => '未关注'];
  62. }
  63. public function getIsPayList()
  64. {
  65. return ['1' => '已充值', '0' => '未充值'];
  66. }
  67. public function getStateList()
  68. {
  69. return ['0' => '禁用', '1' => '正常'];
  70. }
  71. public function getSexTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : $data['sex'];
  74. $list = $this->getSexList();
  75. return isset($list[$value]) ? $list[$value] : '';
  76. }
  77. public function getIsSubscribeTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : $data['is_subscribe'];
  80. $list = $this->getIsSubscribeList();
  81. return isset($list[$value]) ? $list[$value] : '';
  82. }
  83. public function getIsPayTextAttr($value, $data)
  84. {
  85. $value = $value ? $value : $data['is_pay'];
  86. $list = $this->getIsPayList();
  87. return isset($list[$value]) ? $list[$value] : '';
  88. }
  89. public function getVipEndtimeTextAttr($value, $data)
  90. {
  91. $value = $value ? $value : intval($data['vip_endtime']);
  92. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  93. }
  94. public function getStateTextAttr($value, $data)
  95. {
  96. $value = $value ? $value : $data['state'];
  97. $list = $this->getStateList();
  98. return isset($list[$value]) ? $list[$value] : '';
  99. }
  100. protected function setVipEndtimeAttr($value)
  101. {
  102. return $value && !is_numeric($value) ? strtotime($value) : $value;
  103. }
  104. public function getIsvipAttr($value, $data)
  105. {
  106. $value = $data['vip_endtime'] > time() ? 'VIP' : '否';
  107. return $value;
  108. }
  109. /*
  110. * 获取用户信息
  111. *@param $user_id
  112. *@param $getdb 直接取mysql数据 不走redis
  113. */
  114. public function getUserInfo($user_id, $getdb = 0)
  115. {
  116. if (empty($user_id)) return null;
  117. if (ApiService::instance()->checkApiOn()) {
  118. return \app\source\service\UserService::instance()->getUserInfo($user_id)->toArray();
  119. }
  120. $redis = Redis::instance();
  121. $key = 'UN:' . $user_id;
  122. $user_info = $redis->hgetall($key);
  123. if (!empty($user_info) && $getdb == 0) {
  124. if (!array_key_exists('is_first_unfollow', $user_info)) {
  125. $redis->del($key);
  126. return $this->getUserInfo($user_id, $getdb = 0);
  127. }
  128. } else {
  129. $user_info = [];
  130. $userinfo['avatar'] = '';
  131. $db_data = $this->setConnect($user_id)->find(['id' => $user_id]);
  132. if (!empty($db_data)) {
  133. $db_data = $db_data->toArray();
  134. $redis->hmset($key, $db_data);
  135. //up by wanghy 0407 用户信息过期时间由24小时修改为10小时
  136. $redis->expire($key, 36000);
  137. $user_info = $db_data;
  138. }
  139. }
  140. return $user_info;
  141. }
  142. /*
  143. * 获得用户喜好书籍
  144. */
  145. public function getLike($userId = null){
  146. //获得用户最近阅读
  147. if($userId){
  148. $recentlyRead = model('UserRecentlyRead')->getRecentlyRead(0,10,$userId);
  149. }
  150. $bookIds = [];
  151. $where = [];
  152. $where['state'] = ['eq','1'];
  153. if($userId && !empty($recentlyRead) && $recentlyRead['totalNum']>0) {
  154. foreach($recentlyRead['data'] as $val){
  155. $bookIds[] = $val['book_id'];
  156. }
  157. $bookIdsToStr = implode(',',$bookIds);
  158. $where['id'] = ['not in',$bookIdsToStr];
  159. }
  160. if($userId){
  161. $userInfo = $this->getUserInfo($userId);
  162. }
  163. //0408 去掉用户喜好字段维护
  164. // //如果有用户喜好
  165. // if($userId && !empty($userInfo['book_category_ids'])){
  166. // $where['book_category_id'] = ['in',$userInfo['book_category_ids']];
  167. // }else{
  168. // //如果没有用户喜好
  169. // if(empty($userInfo['sex'])){
  170. // $where['sex'] = ['eq','1'];
  171. // }else{
  172. // $where['sex'] = ['eq',$userInfo['sex']];
  173. // }
  174. // }
  175. if(empty($userInfo['sex'])){
  176. $where['sex'] = ['eq','1'];
  177. }else{
  178. $where['sex'] = ['eq',$userInfo['sex']];
  179. }
  180. $channelId = AdminService::instance()->getAdminExtendModel()->getChannelId($userInfo['channel_id']);
  181. $isWater = WaterBookService::instance()->showBook($channelId,$userId,Ip::ip());
  182. if ($isWater){
  183. $where['classify_white_list'] = ['eq','1'];
  184. }
  185. $totalNum = model('Book')->where($where)->count();
  186. $randNum = $totalNum > 4 ? $totalNum-4 : 0;
  187. if($randNum>0){
  188. $randNum = mt_rand(0,$randNum);
  189. }
  190. $result = collection(model('Book')->where($where)->limit($randNum,4)->select())->toArray();
  191. $bookCate = collection(model('BookCategory')->where('status','normal')->select())->toArray();
  192. $cates = [];
  193. foreach($bookCate as $v){
  194. $cates[$v['id']] = $v['name'];
  195. }
  196. foreach($result as $key=>$val){
  197. if(empty($val['book_category_id'])){
  198. $result[$key]['author'] = '';
  199. }else{
  200. if(array_key_exists($val['book_category_id'],$cates)){
  201. $result[$key]['author'] = $cates[$val['book_category_id']];
  202. }
  203. }
  204. }
  205. return $result;
  206. }
  207. /*
  208. * 获取头像
  209. *
  210. */
  211. public function getUserAvatar($user_id = null)
  212. {
  213. if (!$user_id) {
  214. $user_id = UserService::instance()->getUserInfo()->id;
  215. }
  216. $userinfo = $this->getUserInfo($user_id);
  217. return isset($userinfo['avatar']) ? $userinfo['avatar'] : '/assets/img/frontend/icon/nav_icon_4.png';
  218. }
  219. /*
  220. * 获取当前用户的性别频道
  221. *@param $user_sex 为$this->user->sex;
  222. * @param $adminconfig array
  223. * @param $type true 返回boy|girl|idx fasle 返回 1|2|0
  224. *@return 0 位置 1男 2女
  225. */
  226. public function getUserSex($user_sex=0, $adminconfig, $type = false,$pagesex='boy')
  227. {
  228. if ($user_sex) {
  229. $sex = $user_sex;
  230. } else {
  231. if($adminconfig['page_sex']){
  232. $sex = $adminconfig['page_sex'];
  233. }else{
  234. if($pagesex == 'boy'){
  235. $sex =1;
  236. }else{
  237. $sex =2;
  238. }
  239. }
  240. }
  241. if ($type) {
  242. switch ($sex) {
  243. case '0':
  244. return 'idx';
  245. case '1':
  246. return 'boy';
  247. case '2':
  248. return 'girl';
  249. }
  250. }
  251. return $sex;
  252. }
  253. /**
  254. * 处理自增ID
  255. *
  256. * @param $data
  257. */
  258. protected function autoId($data)
  259. {
  260. if (!isset($data['id'])) {
  261. // 获取redis自增id
  262. $redisAuto = Redis::instanceAuto();
  263. $newUserId = $redisAuto->incr('UID'); //redis自增返回新的user_id
  264. if (!$newUserId) {
  265. Log::error('用户表自增ID获取失败!data:' . json_encode($data));
  266. throw new HttpException(500, '用户表自增ID获取失败!');
  267. }
  268. $data['id'] = $newUserId;
  269. }
  270. return $data;
  271. }
  272. /**
  273. * 插入记录
  274. * @access public
  275. * @param mixed $data 数据
  276. * @param boolean $replace 是否replace
  277. * @param boolean $getLastInsID 返回自增主键
  278. * @param string $sequence 自增序列名
  279. * @return integer|string
  280. */
  281. public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null)
  282. {
  283. $data = $this->autoId($data);
  284. $res = parent::insert($data, $replace, false, $sequence);
  285. if ($res) {
  286. $this->lastId = $data['id'];
  287. if ($getLastInsID) {
  288. return $this->lastId;
  289. }
  290. }
  291. return $res;
  292. }
  293. /**
  294. * 插入记录并获取自增ID
  295. * @access public
  296. * @param mixed $data 数据
  297. * @param boolean $replace 是否replace
  298. * @param string $sequence 自增序列名
  299. * @return integer|string
  300. */
  301. public function insertGetId(array $data, $replace = false, $sequence = null)
  302. {
  303. if (ApiService::instance()->checkApiOn()) {
  304. $userUpdate = new UserUpdate();
  305. $userUpdate->bind($data);
  306. return \app\source\service\UserService::instance()->updateUser($userUpdate)->id;
  307. } else{
  308. $data = $this->autoId($data);
  309. if (parent::insert($data, $replace, false, $sequence)) {
  310. $this->lastId = $data['id'];
  311. return $this->lastId;
  312. } else {
  313. return 0;
  314. }
  315. }
  316. }
  317. /**
  318. * 获取最近插入的ID
  319. * @access public
  320. * @param string $sequence 自增序列名
  321. * @return string
  322. */
  323. public function getLastInsID($sequence = null)
  324. {
  325. return $this->lastId;
  326. }
  327. }