// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountLogModel; use App\Models\MerchantCategoryModel; use App\Models\MerchantModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use App\Services\SmsService; use Illuminate\Support\Facades\DB; /** * 商户服务管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MerchantService * @package App\Services\Api */ class MerchantService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MerchantService constructor. */ public function __construct() { $this->model = new MerchantModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取缓存列表 * @param $position * @param int $num * @return array|mixed */ public function getDataList($params, $pageSize = 15, $refresh = false, $field = '') { $page = request()->post('page', 1); $cacheKey = "caches:merchant:page_{$page}_" . md5(json_encode($params).$pageSize); $datas = RedisService::get($cacheKey); $data = isset($datas['data'])? $datas['data'] : []; if ($datas && $data && !$refresh) { return [ 'list'=> $data, 'total'=> isset($datas['total'])? $datas['total'] : 0, 'pageSize'=>$pageSize ]; } $field = $field ? $field : 'lev_a.id,lev_a.user_id,lev_a.name,lev_a.logo,lev_a.type,lev_a.country,lev_a.city,lev_a.category_id,lev_c.name as category_name,lev_a.description,lev_a.service_time,lev_a.receive_num,lev_a.status'; $order = 'lev_a.id desc'; $sortType = isset($params['sort_type'])? $params['sort_type'] : 0; if($sortType == 1){ $order = 'lev_a.receive_num desc, lev_a.create_time desc'; } $datas = $this->model->from('merchant as a') ->leftJoin('member as b','b.id','=','a.user_id') ->leftJoin('merchant_category as c','c.id','=','a.category_id') ->where(['a.mark' => 1,'b.mark'=>1]) ->where(function ($query) use ($params) { $kw = isset($params['kw']) ? trim($params['kw']) : ''; if ($kw) { $query->where('a.name', 'like', "%{$kw}%"); } }) ->where(function ($query) use ($params) { // 商户类型 $type = isset($params['type']) ? intval($params['type']) : 0; if ($type) { $query->where('a.type', $type); } // 状态 $status = isset($params['status']) && $params['status']>=0 ? intval($params['status']) : 2; if ($status>0) { $query->where('a.status', $status); }else{ $query->whereIn('a.status',[1,2]); } // 经营类目 $category = isset($params['category_id']) ? intval($params['category_id']) : 0; if ($category) { $query->where('a.category_id', $category); } // 市 $city = isset($params['city']) ? trim($params['city']) : ''; if ($city) { $query->where(function($query) use($city){ $query->where('a.city', $city)->orWhere('a.address','like',"%{$city}%"); }); } $locale = isset($params['locale']) ? trim($params['locale']) : ''; if ($locale) { $query->where(function($query) use($locale){ $query->where('a.country_code', $locale); }); } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $datas = $datas ? $datas->toArray() : []; if ($datas) { foreach($datas['data'] as &$item){ $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : ''; } unset($item); RedisService::set($cacheKey, $datas, rand(3, 5)); } return [ 'list'=> isset($datas['data'])? $datas['data'] : [], 'total'=> isset($datas['total'])? $datas['total'] : 0, 'pageSize'=>$pageSize ]; } /** * 获取详情 * @param $id 商家ID * @param string $type * @return array|mixed */ public function getInfoById($userId, $type='info', $id=0) { $cacheKey = "caches:merch:{$type}_{$userId}_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['a.mark'=>1,'b.mark'=>1]; if($id){ $where['a.id'] = $id; }else if($userId){ $where['a.user_id'] = $userId; } $field = ['a.id','a.name','a.user_id','a.type','a.logo','a.category_id','a.business_img','a.albums','a.country','a.city','a.address','a.usdt','a.service_time','a.withdraw_total','a.status','b.username','b.nickname']; $info = $this->model->from('merchant as a')->with(['category']) ->leftJoin('member as b','b.id','=','a.user_id') ->where($where) ->select($field) ->first(); $info = $info? $info->toArray() : []; if($info){ if(isset($info['logo'])){ $info['logo'] = $info['logo']? get_image_url($info['logo']) : ''; } if(isset($info['business_img']) && $info['business_img']){ $info['business_img'] = get_image_url($info['business_img']); } if(isset($info['albums'])){ $info['albums'] = $info['albums']? json_decode($info['albums'], true) : []; $info['albums'] = $info['albums']? get_images_preview($info['albums']) : []; } // 收款二维码 $data = ['mid'=> $info['id'],'scene'=>'merch','type'=>'payment']; $qrcode = MemberService::make()->makeQrcode(json_encode($data)); $info['qrcode'] = $qrcode? get_image_url($qrcode):''; $info['day_usdt'] = moneyFormat(AccountLogService::make()->getCountDataByDate($info['id'],1,2,1),2); $info['total_usdt'] = moneyFormat(AccountLogService::make()->getCountDataByDate($info['id'],1,2,0),2); RedisService::set($cacheKey, $info, rand(2, 3)); } return $info; } /** * 获取缓存信息 * @param $where * @param array $field * @param int $expired * @return array|mixed */ public function getCacheInfo($where, $field = [], $expired = 0) { $cacheKey = "caches:merchant:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired); $info = RedisService::get($cacheKey); if ($info) { return $info; } $defaultField = ['id','user_id', 'name', 'mobile', 'logo', 'category', 'usdt', 'status']; $field = $field ? $field : $defaultField; $info = $this->model->where($where)->where('mark', 1)->select($field)->first(); $info = $info ? $info->toArray() : []; if ($info) { if (isset($info['logo'])) { $info['logo'] = $info['logo']? $info['logo'] : '/images/member/logo.png'; $info['logo_preview'] = $info['logo']? get_image_url($info['logo']) : ''; } RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5)); } return $info; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); // 图片处理 $cover = $data['cover'] ? trim($data['cover']) : ''; if (strpos($cover, "temp")) { $data['cover'] = save_image($cover, 'ad'); } else { $data['cover'] = str_replace(IMG_URL, "", $data['cover']); } // 开始时间 if (isset($data['start_time'])) { $data['start_time'] = strtotime($data['start_time']); } // 结束时间 if (isset($data['end_time'])) { $data['end_time'] = strtotime($data['end_time']); } return parent::edit($data); // TODO: Change the autogenerated stub } /** * 修改信息 * @param $userId * @param $params * @return array|false|int[] */ public function saveInfo($userId, $params) { // 验证是否入驻过和入驻状态 $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first(); $status = isset($info['status'])? $info['status'] : 0; $mark = isset($info['mark'])? $info['mark'] : 0; $merchId = isset($info['id'])? $info['id'] : 0; if($merchId && empty($info)){ $this->error = 2216; return false; } // 是否被冻结 if($merchId && $status == 3 && $mark){ $this->error = 2202; return false; } // 验证账户是否正常 $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']); $status = isset($userInfo['status'])? $userInfo['status'] : 0; if(empty($userInfo) || $status != 1){ $this->error = 2017; return false; } $type = isset($params['type'])? intval($params['type']) : 0; $category = isset($params['category'])? intval($params['category']) : 0; $lng = isset($params['lng'])? floatval($params['lng']) : 0; $lat = isset($params['lat'])? floatval($params['lat']) : 0; $address = isset($params['address'])? trim($params['address']) : ''; $albums = isset($params['albums'])? get_format_images($params['albums']) : ''; $file1 = isset($params['file1'])? get_format_images($params['file1']) : ''; $file2 = isset($params['file2'])? get_format_images($params['file2']) : ''; $file3 = isset($params['file3'])? get_format_images($params['file3']) : ''; $alipayQrcodeData = isset($params['alipay_qrcode'])? $params['alipay_qrcode'] : []; $alipayQrcode = isset($alipayQrcodeData[0]['url'])? get_image_path($alipayQrcodeData[0]['url']) : ''; $wxpayQrcodeData = isset($params['wxpay_qrcode'])? $params['wxpay_qrcode'] : []; $wxpayQrcode = isset($wxpayQrcodeData[0]['url'])? get_image_path($wxpayQrcodeData[0]['url']) : ''; $logoData = isset($params['logo'])? $params['logo'] : []; $logo = isset($logoData[0]['url'])? get_image_path($logoData[0]['url']) : ''; $name = isset($params['name'])? $params['name'] : ''; // 验证分类 if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){ $this->error = 2203; return false; } // 验证logo if(empty($logo)){ $this->error = 2204; return false; } // 位置信息 if($type == 1 && (empty($lat) || empty($lng) || empty($address))){ $this->error = 2210; //return false; } // 证书 if(empty($file1)){ $this->error = $type == 1? 2206 : 2211; return false; } if(empty($file2)){ $this->error = 2207; return false; } if(empty($file3)){ $this->error = $type==1? 2208 : 2209; return false; } // 收款码 if(empty($alipayQrcode) || empty($wxpayQrcode)){ $this->error = 2205; return false; } // 入驻数据 $data = [ 'name' => $name, 'user_id' => $userId, 'category'=> $category, 'type'=> $type, 'logo'=> $logo, 'albums'=> $albums? $albums : '', 'qualification_imgs'=> $file1? $file1 : '', 'other_certificates'=> $file2? $file2 : '', 'idcard_imgs'=> $file3? $file3 : '', 'alipay_qrcode'=> $alipayQrcode? $alipayQrcode : '', 'wxpay_qrcode'=> $wxpayQrcode? $wxpayQrcode : '', 'lng'=> $lng, 'lat'=> $lat, 'address'=> $address, 'province'=> isset($params['province'])? trim($params['province']) : '', 'city'=> isset($params['city'])? trim($params['city']) : '', 'district'=> isset($params['district'])? trim($params['district']) : '', 'intro'=> isset($params['intro'])? trim($params['intro']) : '', 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '', 'business_scope'=> isset($params['business_scope'])? trim($params['business_scope']) : '', 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '', 'create_time'=> time(), 'update_time'=> time(), 'status'=> 1, 'mark'=> 1, ]; // 写入数据 if($merchId){ if($this->model->where(['id'=> $merchId])->update($data)){ $this->error = 2228; RedisService::keyDel("caches:merchant:info:temp_{$userId}*"); return ['id'=> $merchId]; }else{ $this->error = 2229; return false; } }else{ if($merchId = $this->model->insertGetId($data)){ $this->error = 2228; RedisService::keyDel("caches:merchant:info:temp_{$userId}*"); return ['id'=> $merchId]; }else{ $this->error = 2229; return false; } } } /** * 申请入驻 * @param $userId * @param $params * @return array|false|int[] */ public function apply($userId, $params) { // 验证是否入驻过和入驻状态 $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first(); $status = isset($info['status'])? $info['status'] : 0; $mark = isset($info['mark'])? $info['mark'] : 0; $merchId = isset($info['id'])? $info['id'] : 0; if($merchId && $status == 2 && $mark){ $this->error = 2801; return false; } // 是否被冻结 if($merchId && $status == 4){ $this->error = 2802; return false; } // 验证账户是否正常 $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']); $status = isset($userInfo['status'])? $userInfo['status'] : 0; if(empty($userInfo) || $status != 1){ $this->error = 2017; return false; } $type = isset($params['type'])? intval($params['type']) : 1; $category = isset($params['categoty_id'])? intval($params['categoty_id']) : 0; $currency = isset($params['currency'])? trim($params['currency']) : ''; $country = isset($params['country'])? trim($params['country']) : ''; $city = isset($params['city'])? trim($params['city']) : ''; $address = isset($params['address'])? trim($params['address']) : ''; $albums = isset($params['albums'])? get_format_images($params['albums']) : ''; $businessImg = isset($params['business_img'])? get_image_path($params['business_img']) : ''; $logo = isset($params['logo'])? get_image_path($params['logo']) : ''; $name = isset($params['name'])? $params['name'] : ''; // 验证分类 if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){ $this->error = 2803; return false; } // 验证logo if(empty($logo)){ $this->error = 2804; return false; } $mobile = isset($params['mobile'])? trim($params['mobile']) : ''; $telegram = isset($params['telegram'])? trim($params['telegram']) : ''; if(empty($mobile) && empty($telegram)){ $this->error = 2805; return false; } // 地址信息 if(empty($country) || empty($city) || empty($address)){ $this->error = 2806; return false; } if(empty($currency)){ $this->error = 2807; return false; } // 营业执照 if(empty($businessImg)){ $this->error = 2808; return false; } // 入驻数据 $data = [ 'name' => $name, 'user_id' => $userId, 'category_id'=> $category, 'type'=> $type, 'logo'=> $logo, 'albums'=> $albums? $albums : '', 'business_img'=> $businessImg? $businessImg : '', 'currency'=> $currency, 'country'=> $country, 'city'=> $city, 'address'=> $address, 'description'=> isset($params['description'])? trim($params['description']) : '', 'mobile'=> $mobile, 'telegram'=> $telegram, 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '', 'create_time'=> time(), 'update_time'=> time(), 'status'=> 1, 'mark'=> 1, ]; // 写入数据 if($merchId){ if($this->model->where(['id'=> $merchId])->update($data)){ $this->error = 2809; return ['id'=> $merchId]; }else{ $this->error = 2810; return false; } }else{ if($merchId = $this->model->insertGetId($data)){ $this->error = 2811; return ['id'=> $merchId]; }else{ $this->error = 2810; return false; } } } /** * 获取商家入驻信息 * @param $userId * @return mixed */ public function getApplyInfo($userId) { $info = $this->model->with(['category'])->where(['user_id'=> $userId,'mark'=>1]) ->orderBy('id','desc') ->first(); $info = $info? $info->setHidden(['usdt','update_time','mark'])->toArray() : []; if($info){ $info['logo'] = isset($info['logo']) && $info['logo']? get_image_url($info['logo']) : ''; $info['business_img'] = isset($info['business_img']) && $info['business_img']? get_image_url($info['business_img']) : ''; $albums = isset($info['albums']) && $info['albums']? json_decode($info['albums'], true) : []; $info['albums'] = $albums? get_images_preview($albums) : []; if(isset($info['category']) && $info['category']){ $info['category_name'] = isset($info['category']['name'])? $info['category']['name'] : ''; $info['category_id'] = isset($info['category']['id'])? $info['category']['id'] : ''; } } return $info; } /** * 修改 * @param $userId * @param $params * @return bool */ public function modify($userId, $params) { // 用户验证 $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1]) ->select(['id','user_id', 'status']) ->first(); if (!$info || $info['status'] != 2) { $this->error = 2812; return false; } // 验证账户是否正常 $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']); $status = isset($userInfo['status'])? $userInfo['status'] : 0; if(empty($userInfo) || $status != 1){ $this->error = 2017; return false; } // 手机号 $mobile = isset($params['mobile']) ? $params['mobile'] : ''; if (isset($params['mobile']) && $mobile) { $data['mobile'] = $mobile; } $telegram = isset($params['telegram']) ? $params['telegram'] : ''; if (isset($params['telegram']) && $telegram) { $data['telegram'] = $telegram; } if(isset($params['name']) && $params['name']){ $data['name'] = trim($params['name']); } if(isset($params['service_time']) && $params['service_time']){ $data['service_time'] = trim($params['service_time']); } $address = isset($params['address'])? trim($params['address']) : ''; $albums = isset($params['albums'])? get_format_images($params['albums']) : ''; $logo = isset($params['logo'])? get_image_path($params['logo']) : ''; if(isset($params['address']) && $address){ $data['address'] = $address; } if(isset($params['logo']) && $logo){ $data['logo'] = $logo; } if(isset($params['albums']) && $albums){ $data['albums'] = $albums; } // 修改数据 RedisService::clear("caches:merch:detail_{$info['id']}"); RedisService::keyDel("caches:merchant:info:temp*"); $this->model->where(['user_id' => $userId])->update($data); $this->error = 1008; return true; } }