MemberSettingService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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:member: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. }
  65. return $type? (isset($datas[$type])? $datas[$type] : $default) : $datas;
  66. }
  67. /**
  68. * 设置消息参数
  69. * @param $userId
  70. * @param $params
  71. * @return false
  72. */
  73. public function setMsgData($userId, $params)
  74. {
  75. $type = isset($params['type'])? trim($params['type']) : '';
  76. $value = isset($params['value'])? intval($params['value']) : 0;
  77. $types = ['receive_app','receive_custom','receive_order','receive_account'];
  78. if(!in_array($type, $types)){
  79. $this->error = 1021;
  80. return false;
  81. }
  82. // 参数值验证
  83. if(!in_array($value, [1,2])){
  84. $this->error =1022;
  85. return false;
  86. }
  87. $data = [
  88. 'user_id'=> $userId,
  89. 'update_time'=> time(),
  90. 'mark'=> 1,
  91. ];
  92. // 更新设置
  93. $data[$type] = $value;
  94. if($id = $this->model->where(['user_id'=> $userId])->value('id')){
  95. $id = $this->model->where(['id'=> $id])->update($data);
  96. }else{
  97. $id = $this->model->insertGetId($data);
  98. }
  99. return $id;
  100. }
  101. }