| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\MemberSettingModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 用户参数设置服务管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class MemberSettingService
- * @package App\Services\Common
- */
- class MemberSettingService extends BaseService
- {
- protected static $instance=null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * MemberSettingService constructor.
- */
- public function __construct()
- {
- $this->model = new MemberSettingModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获取用户设置参数
- * @param $userId 用户ID
- * @param int $type 参数字段,空为所有参数
- * @param int $default 有字段时,为空返回的默认值
- * @return array|int|mixed
- */
- public function getSetting($userId, $type=0, $default=0)
- {
- $cacheKey = "caches:member:setting:{$userId}_{$type}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $type? (isset($datas[$type])? $datas[$type] : $default) : $datas;
- }
- $datas = $this->model->where(['user_id'=> $userId, 'mark'=> 1])->first();
- $datas = $datas? $datas->setHidden(['id','update_time','mark'])->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(10, 20));
- }
- return $type? (isset($datas[$type])? $datas[$type] : $default) : $datas;
- }
- /**
- * 设置消息参数
- * @param $userId
- * @param $params
- * @return false
- */
- public function setMsgData($userId, $params)
- {
- $type = isset($params['type'])? trim($params['type']) : '';
- $value = isset($params['value'])? intval($params['value']) : 0;
- $types = ['receive_app','receive_custom','receive_order','receive_account'];
- if(!in_array($type, $types)){
- $this->error = 1021;
- return false;
- }
- // 参数值验证
- if(!in_array($value, [1,2])){
- $this->error =1022;
- return false;
- }
- $data = [
- 'user_id'=> $userId,
- 'update_time'=> time(),
- 'mark'=> 1,
- ];
- // 更新设置
- $data[$type] = $value;
- if($id = $this->model->where(['user_id'=> $userId])->value('id')){
- $id = $this->model->where(['id'=> $id])->update($data);
- }else{
- $id = $this->model->insertGetId($data);
- }
- return $id;
- }
- }
|