UserInfo.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\common\model\UserInfo as UserInfoModel;
  14. use app\store\model\Setting as SettingModel;
  15. use think\facade\Cache;
  16. /**
  17. * 用户资料模型类
  18. * Class UserInfo
  19. * @package app\api\model
  20. */
  21. class UserInfo extends UserInfoModel
  22. {
  23. protected $globalScope = [''];
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'store_id',
  30. 'update_time'
  31. ];
  32. /**
  33. * 获取用户信息
  34. * @param $where
  35. * @param array $with
  36. * @return static|array|false|null
  37. */
  38. public static function detail($where, array $with = [])
  39. {
  40. $filter = [];
  41. if (is_array($where)) {
  42. $filter = array_merge($filter, $where);
  43. } else {
  44. $filter['user_id'] = (int)$where;
  45. }
  46. return static::get($filter, $with);
  47. }
  48. /**
  49. * 分配每日解锁用户
  50. * @param $userId
  51. * @return false
  52. */
  53. public static function dayLockedUser($userId)
  54. {
  55. $cacheKey = "caches:locked:day:{$userId}";
  56. if(Cache::get($cacheKey)){
  57. return false;
  58. }
  59. $info = self::where(['user_id'=> $userId])->field('day_locked_time,day_locked_uids')->find();
  60. $dayLockedTime = isset($info['day_locked_time'])? intval($info['day_locked_time']) : 0;
  61. if($dayLockedTime && date('Y-m-d', $dayLockedTime) == date('Y-m-d')){
  62. Cache::set($cacheKey, ['user_id'=> $userId,'ids'=> $info['day_locked_uids'],'date'=> date('Y-m-d H:i:s')], rand(300, 3600));
  63. return false;
  64. }
  65. $platform = SettingModel::getItem('platform');
  66. $locked = isset($platform['locked'])? $platform['locked'] : [];
  67. $lockNum = isset($locked['lock_num'])? $locked['lock_num'] : 0;
  68. $lockNum = $lockNum? $lockNum : 3;
  69. $uids = self::alias('a')
  70. ->leftJoin('user u','u.user_id=a.user_id')
  71. ->where(['u.user_type'=>1,'u.is_delete'=>0,'u.status'=>1])
  72. ->orderRaw('rand()')
  73. ->limit($lockNum)
  74. ->column('a.user_id');
  75. if($uids){
  76. self::where(['user_id'=> $userId])->save(['day_locked_time'=>time(),'day_locked_uids'=> implode(',', $uids)]);
  77. }
  78. Cache::set($cacheKey, ['user_id'=> $userId,'ids'=> $uids,'date'=> date('Y-m-d H:i:s')], rand(300, 3600));
  79. }
  80. }