// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AgentModel; use App\Services\BaseService; use App\Services\RedisService; /** * 代理管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class AgentService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new AgentModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; if ($status > 0) { $where['a.status'] = $status; } $list = $this->model->with(['user'])->from('agent as a') ->leftJoin('member as b', 'b.id', '=', 'a.user_id') ->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('a.real_name', 'like', "%{$keyword}%") ->orWhere('a.phone', 'like', "%{$keyword}%"); } }) ->select(['a.*']) ->orderBy('a.create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 申请 * @param $userId * @param $params * @return mixed */ public function apply($userId, $params) { $realname = isset($params['real_name']) ? trim($params['real_name']) : ''; $phone = isset($params['phone']) ? trim($params['phone']) : ''; $idcard = isset($params['idcard']) && $params['idcard']? trim($params['idcard']) : ''; $data = [ 'user_id' => $userId, 'real_name' => $realname, 'phone' => $phone, 'idcard' => $idcard, 'income' => 0, 'balance' => 0, 'withdraw_total' => 0, 'order_count' => 0, 'confirm_remark' => '', 'create_time' => time(), 'update_time' => time(), 'status' => 2, 'mark' => 1, ]; if($id = $this->model->where(['user_id'=>$userId])->value('id')){ $this->model->where(['id'=>$id])->update($data); }else{ if (!$id = $this->model->insertGetId($data)) { $this->error = '代理申请失败'; return false; } } RedisService::keyDel("caches:members:info_*"); RedisService::keyDel("caches:agents:info*"); $this->error = '代理申请成功,请耐心等候审核~'; return ['id' => $id]; } /** * 代理信息 * @param $id * @param $userId * @return array|mixed */ public function getInfo($uid) { $cacheKey = "caches:agents:info_{$uid}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['user_id'=> $uid,'mark'=>1]; $info = $this->model->with(['user'])->where($where)->first(); $info = $info? $info->toArray() : []; if($info){ RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } }