MemberSettingService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberSettingModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 用户参数设置服务管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class MemberSettingService
  20. * @package App\Services\Common
  21. */
  22. class MemberSettingService extends BaseService
  23. {
  24. protected static $instance=null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * MemberSettingService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new MemberSettingModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 获取用户设置参数
  48. * @param $userId 用户ID
  49. * @param int $type 参数字段,空为所有参数
  50. * @param int $default 有字段时,为空返回的默认值
  51. * @return array|int|mixed
  52. */
  53. public function getSetting($userId, $type=0, $default=0)
  54. {
  55. $cacheKey = "caches:m_setting:{$userId}_{$type}";
  56. $datas = RedisService::get($cacheKey);
  57. if($datas){
  58. return $type? (isset($datas[$type])? $datas[$type] : $default) : $datas;
  59. }
  60. $datas = $this->model->where(['user_id'=> $userId, 'mark'=> 1])->first();
  61. $datas = $datas? $datas->setHidden(['id','update_time','mark'])->toArray() : [];
  62. if($datas){
  63. RedisService::set($cacheKey, $datas, rand(10, 20));
  64. }else{
  65. $datas = ['receive_app'=>1,'receive_custom'=>1,'receive_order'=>1,'receive_account'=>1];
  66. }
  67. return $type? (isset($datas[$type])? $datas[$type] : $default) : $datas;
  68. }
  69. /**
  70. * 设置消息参数
  71. * @param $userId
  72. * @param $params
  73. * @return false
  74. */
  75. public function setMsgData($userId, $params)
  76. {
  77. $type = isset($params['type'])? trim($params['type']) : '';
  78. $value = isset($params['value'])? intval($params['value']) : 0;
  79. $types = ['receive_app','receive_custom','receive_order','receive_account'];
  80. if(!in_array($type, $types)){
  81. $this->error = 1021;
  82. return false;
  83. }
  84. // 频繁操作
  85. if(RedisService::get("caches:m_setting:lock_{$userId}_{$type}")){
  86. $this->error = 1034;
  87. return false;
  88. }
  89. // 参数值验证
  90. if(!in_array($value, [1,2])){
  91. $this->error =1022;
  92. return false;
  93. }
  94. $data = [
  95. 'user_id'=> $userId,
  96. 'update_time'=> time(),
  97. 'mark'=> 1,
  98. ];
  99. // 更新设置
  100. $data[$type] = $value;
  101. RedisService::set("caches:m_setting:lock_{$userId}_{$type}", rand(2,3));
  102. if($id = $this->model->where(['user_id'=> $userId])->value('id')){
  103. $id = $this->model->where(['id'=> $id])->update($data);
  104. }else{
  105. $id = $this->model->insertGetId($data);
  106. }
  107. RedisService::clear("caches:m_setting:{$userId}_{$type}");
  108. RedisService::clear("caches:m_setting:{$userId}_0");
  109. RedisService::clear("caches:message:topList_{$userId}");
  110. RedisService::clear("caches:m_setting:lock_{$userId}_{$type}");
  111. return $id;
  112. }
  113. }