UserInfo.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\User;
  14. use app\common\model\UserInfo as UserInfoModel;
  15. use app\store\model\Setting as SettingModel;
  16. use think\facade\Cache;
  17. /**
  18. * 用户资料模型类
  19. * Class UserInfo
  20. * @package app\api\model
  21. */
  22. class UserInfo extends UserInfoModel
  23. {
  24. protected $globalScope = [''];
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'store_id',
  31. 'update_time'
  32. ];
  33. /**
  34. * 获取用户信息
  35. * @param $where
  36. * @param array $with
  37. * @return static|array|false|null
  38. */
  39. public static function detail($where, array $with = [])
  40. {
  41. $filter = [];
  42. if (is_array($where)) {
  43. $filter = array_merge($filter, $where);
  44. } else {
  45. $filter['user_id'] = (int)$where;
  46. }
  47. return static::get($filter, $with);
  48. }
  49. /**
  50. * 分配每日解锁用户
  51. * @param $userId
  52. * @return false
  53. */
  54. public static function dayLockedUser($userId)
  55. {
  56. $cacheKey = "caches:locked:day:{$userId}";
  57. if(Cache::get($cacheKey)){
  58. return false;
  59. }
  60. $info = self::where(['user_id'=> $userId])->field('day_locked_time,day_locked_uids')->find();
  61. $dayLockedTime = isset($info['day_locked_time'])? intval($info['day_locked_time']) : 0;
  62. if($dayLockedTime && date('Y-m-d', $dayLockedTime) == date('Y-m-d')){
  63. Cache::set($cacheKey, ['user_id'=> $userId,'ids'=> $info['day_locked_uids'],'date'=> date('Y-m-d H:i:s')], rand(300, 3600));
  64. return false;
  65. }
  66. $trade = SettingModel::getItem('trade');
  67. $locked = isset($trade['locked'])? $trade['locked'] : [];
  68. $lockNum = isset($locked['lock_num'])? $locked['lock_num'] : 0;
  69. $lockNum = $lockNum? $lockNum : 3;
  70. $uids = self::alias('a')
  71. ->leftJoin('user u','u.user_id=a.user_id')
  72. ->where(['u.user_type'=>1,'u.is_delete'=>0,'u.status'=>1])
  73. ->orderRaw('rand()')
  74. ->limit($lockNum)
  75. ->column('a.user_id');
  76. if($uids){
  77. self::where(['user_id'=> $userId])->save(['day_locked_time'=>time(),'day_locked_uids'=> implode(',', $uids)]);
  78. }
  79. Cache::set($cacheKey, ['user_id'=> $userId,'ids'=> $uids,'date'=> date('Y-m-d H:i:s')], rand(300, 3600));
  80. }
  81. }