123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\AgentModel;
- use App\Models\MechanicModel;
- use App\Models\MerchantCategoryModel;
- use App\Models\MerchantClerkModel;
- use App\Models\MerchantCollectModel;
- use App\Models\MerchantModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\PushService;
- 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.category,lev_a.business_scope,lev_a.service_time,lev_a.deposit,lev_a.service_order_num,lev_a.score_rate,lev_a.city,lev_a.lng,lev_a.lat,lev_a.status';
- $lat = isset($params['lat']) ? $params['lat'] : 0.00;
- $lng = isset($params['lng']) ? $params['lng'] : 0.00;
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
- $order = 'lev_a.id desc';
- if ($sortType == 1) { // 距离
- $order = 'distance asc, lev_a.id desc';
- } else if ($sortType == 2) { // 默认
- $order = 'lev_a.create_time desc, lev_a.id desc';
- } else if ($sortType == 3) { // 评分
- $order = 'lev_a.score_rate desc, lev_a.id desc';
- } else if ($sortType == 4) { // 人气销量
- $order = 'lev_a.service_order_num desc, lev_a.id desc';
- }
- $distanceField = '';
- if ($lat && $lng) {
- $distanceField = "ROUND(
- 6378.138 * 2 * ASIN(
- SQRT(
- POW(
- SIN(
- (
- {$lat} * PI() / 180 - lev_a.`lat` * PI() / 180
- ) / 2
- ),
- 2
- ) + COS({$lat} * PI() / 180) * COS(lev_a.`lat` * PI() / 180) * POW(
- SIN(
- (
- {$lng} * PI() / 180 - lev_a.`lng` * PI() / 180
- ) / 2
- ),
- 2
- )
- )
- ) * 1000
- )";
- $field .= ", {$distanceField} AS distance";
- }
- $distanceLimit = ConfigService::make()->getConfigByCode('distance_limit');
- $distanceLimit = $distanceLimit? $distanceLimit : 0;
- $datas = $this->model->from('merchant as a')
- ->leftJoin('member as b','b.id','=','a.user_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}%")->orWhere('a.business_scope', 'like', "%{$kw}%");
- }
- })
- ->where(function ($query) use ($params,$distanceField,$distanceLimit) {
- // 商户类型
- $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']) ? intval($params['category']) : 0;
- if ($category) {
- $query->where('a.category', $category);
- }
- // 省
- $province = isset($params['province']) ? trim($params['province']) : '';
- $city = isset($params['city']) ? trim($params['city']) : '';
- if ($province && empty($city)) {
- $query->where('a.province', 'like',"{$province}%");
- }
- // 市
- if ($city) {
- $query->where(function($query) use($city){
- $query->where('a.city', $city)->orWhere('a.address','like',"%{$city}%");
- });
- }
- // 是否认证
- $isAuth = isset($params['is_auth']) ? intval($params['is_auth']) : 0;
- if ($isAuth) {
- $query->whereNotNull('a.idcard_imgs');
- }
- // 是否保障金
- $isDeposit = isset($params['deposit']) ? intval($params['deposit']) : 0;
- if ($isDeposit) {
- $query->where('a.deposit', '>', 0);
- }
- // 距离
- $lat = isset($params['lat']) ? $params['lat'] : 0.00;
- $lng = isset($params['lng']) ? $params['lng'] : 0.00;
- $distance = isset($params['distance']) ? intval($params['distance']) : 0;
- $distance = $distance? $distance*1000 : 0;
- $distanceLimit = $distance? $distance : $distanceLimit;
- if($lat>0 && $lng>0 && $distanceLimit>0 && $distanceField) {
- $query->whereRaw("{$distanceField} <= {$distanceLimit}");
- }
- })
- ->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']) : '';
- $item['score_rate'] = max(0, moneyFormat($item['score_rate'], 1));
- if(isset($item['distance'])){
- $item['distance_text'] = $item['lat']>0 && $item['distance']>0? ($item['distance']<200? '200m以内':formatDistance($item['distance'])) : '';
- }
- }
- 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 $position
- * @param int $num
- * @return array|mixed
- */
- public function getRecommendList($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);
- if ($datas && !$refresh) {
- return $datas;
- }
- $field = $field ? $field : 'lev_a.id,lev_a.user_id,lev_a.name,lev_a.logo,lev_a.type,lev_a.category,lev_a.business_scope,lev_a.service_time,lev_a.city,lev_a.lng,lev_a.lat,lev_a.status';
- $lat = isset($params['lat']) ? $params['lat'] : 0.00;
- $lng = isset($params['lng']) ? $params['lng'] : 0.00;
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
- $order = 'lev_a.id desc';
- if ($sortType == 1) {
- $order = 'distance desc, lev_a.id desc';
- } else if ($sortType == 2) {
- $order = 'lev_a.service_order_num desc, lev_a.id desc';
- }
- $distanceField = '';
- if ($lat>0 && $lng>0) {
- $distanceField = "ROUND(
- 6378.138 * 2 * ASIN(
- SQRT(
- POW(
- SIN(
- (
- {$lat} * PI() / 180 - lev_a.`lat` * PI() / 180
- ) / 2
- ),
- 2
- ) + COS({$lat} * PI() / 180) * COS(lev_a.`lat` * PI() / 180) * POW(
- SIN(
- (
- {$lng} * PI() / 180 - lev_a.`lng` * PI() / 180
- ) / 2
- ),
- 2
- )
- )
- ) * 1000
- )";
- $field .= ", {$distanceField} AS distance";
- }
- $distanceLimit = ConfigService::make()->getConfigByCode('distance_limit');
- $distanceLimit = $distanceLimit? $distanceLimit : 0;
- $datas = $this->model->from('merchant as a')
- ->leftJoin('member as b','b.id','=','a.user_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}%")->orWhere('a.business_scope', 'like', "%{$kw}%");
- }
- })
- ->where(function ($query) use ($params,$distanceField,$distanceLimit) {
- // 商户类型
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type) {
- $query->where('a.type', $type);
- }
- // 状态
- $status = isset($params['status']) ? intval($params['status']) : 2;
- if ($status) {
- $query->where('a.status', $status);
- }
- // 经营类目
- $category = isset($params['category']) ? intval($params['category']) : 0;
- if ($category) {
- $query->where('a.category', $category);
- }
- // 省
- $province = isset($params['province']) ? trim($params['province']) : '';
- if ($province) {
- $query->where('a.province', $province);
- }
- // 市
- $city = isset($params['city']) ? trim($params['city']) : '';
- if ($city) {
- $query->where('a.city', $city);
- }
- // 是否认证
- $isAuth = isset($params['is_auth']) ? intval($params['is_auth']) : 0;
- if ($isAuth) {
- $query->whereNotNull('a.idcard_imgs');
- }
- // 是否保障金
- $isDeposit = isset($params['deposit']) ? intval($params['deposit']) : 0;
- if ($isDeposit) {
- $query->where('a.deposit', '>', 0);
- }
- $lat = isset($params['lat']) ? $params['lat'] : 0.00;
- $lng = isset($params['lng']) ? $params['lng'] : 0.00;
- if($lat>0 && $lng>0 && $distanceLimit>0 && $distanceField) {
- $query->whereRaw("{$distanceField} <= {$distanceLimit}");
- }
- })
- ->selectRaw($field)
- ->orderByRaw($order)
- ->paginate($pageSize > 0 ? $pageSize : 9999999)
- ->each(function($item,$k){
- $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
- if(isset($item['distance'])){
- $item['distance_text'] = $item['lat']>0? formatDistance($item['distance']) : '';
- }
- });
- $datas = $datas ? $datas->toArray() : [];
- if ($datas) {
- RedisService::set($cacheKey, $datas, rand(3, 5));
- }
- return $datas;
- }
- /**
- * 获取详情
- * @param $id 商家ID
- * @param string $type
- * @return array|mixed
- */
- public function getInfoById($id, $type='info', $userId=0)
- {
- $cacheKey = "caches:merch:{$type}_{$id}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $field = ['a.id','a.name','a.user_id','a.type','a.views_by_day','a.views','a.views_last_at','a.logo','a.category','a.business_scope','a.service_time','a.settle_type','a.deposit','a.delivery_fee','a.start_total','a.score_rate','a.status','a.trade_status','b.username','b.nickname','b.avatar'];
- if($type == 'home'){
- $field = array_merge($field, ['a.lat','a.lng','a.city','a.address','a.intro','a.albums','a.qualification_imgs','a.other_certificates','b.lat as toLat','b.lng as toLng']);
- }else{
- $field = array_merge($field, ['a.balance','b.withdraw_total','b.balance as b_balance','a.service_order_num','a.service_order_total']);
- }
- $info = $this->model->from('merchant as a')->with(['category'])
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
- ->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['avatar'])){
- $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
- }
- if(isset($info['qualification_imgs'])){
- $info['qualification_imgs'] = $info['qualification_imgs']? json_decode($info['qualification_imgs'], true) : [];
- $info['qualification_imgs'] = $info['qualification_imgs']? get_images_preview($info['qualification_imgs']) : [];
- }
- if(isset($info['other_certificates'])){
- $info['other_certificates'] = $info['other_certificates']? json_decode($info['other_certificates'], true) : [];
- $info['other_certificates'] = $info['other_certificates']? get_images_preview($info['other_certificates']) : [];
- }
- if(isset($info['albums'])){
- $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
- $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
- }
- $info['is_mechanic_apply'] = 0;
- if($type == 'home'){
- $info['distance'] = 0;
- $info['distance_text'] = '';
- $info['myLat'] = request()->post('lat',0);
- $info['myLng'] = request()->post('lng',0);
- if(isset($info['lat']) && $info['lat'] && $info['myLat']){
- $info['distance'] = getDistance($info['lat'], $info['lng'], $info['myLat'], $info['myLng']);
- $info['distance_text'] = $info['distance']? ($info['distance']<200? '距您 200m以内':'距您 '.formatDistance($info['distance'])) : '在您附近';
- }
- $info['is_collect'] = MerchantCollectService::make()->checkCollect($userId, $info['id'], 2);
- $info['is_mechanic_apply'] = (int)MechanicService::make()->checkApply($userId, $info['id']);
- // 浏览量
- $data = ['views_by_day'=>$info['views_last_at']<=date('Y-m-d')?1:$info['views_by_day']+1,'views'=>$info['views']+1,'views_last_at'=>date('Y-m-d H:i:s'),'update_time'=>time()];
- $this->model->where(['id'=> $info['id'],'mark'=>1])->update($data);
- $info['views_by_day'] = $data['views_by_day'];
- $info['views_last_at'] = $data['views_last_at'];
- $info['views'] = $data['views'];
- $info['service_status'] = 2;
- $info['service_time'] = isset($info['service_time']) && $info['service_time']? $info['service_time']:'08点~22点';
- $serviceTime = $info['service_time']? str_replace('~','~',$info['service_time']):'';
- $times = $serviceTime? explode('~',$serviceTime) : [];
- $times[0] = isset($times[0]) && $times[0]? $times[0] : '08点';
- $times[1] = isset($times[1]) && $times[1]? $times[1] : '22点';
- if($times && date('H点')>= $times[0] && date('H点')<=$times[1]){
- $info['service_status'] = 1;
- }
- if($info['trade_status'] != 1){
- $info['service_status'] = 2;
- }
- }else{
- $meals = ConfigService::make()->getConfigByCode('shop_deposit');
- $meals = $meals? str_replace('|','|', $meals) : '';
- $meals = $meals? explode('|', $meals) : [];
- $meals = $meals? $meals : [1000];
- $info['deposit_meals'] = $meals;
- $depositTime = ConfigService::make()->getConfigByCode('shop_deposit_time');
- $depositTime = $depositTime>=0? $depositTime : 0;
- $info['deposit_time'] = $depositTime;
- }
- // 二维码
- $data = ['mid'=> $info['id'],'scene'=>'merch','type'=>'home'];
- $qrcode = MemberService::make()->makeQrcode(json_encode($data));
- $info['qrcode'] = $qrcode? get_image_url($qrcode):'';
- // 邀请入驻
- $data = ['mid'=> $info['id'],'scene'=>'mechanic','type'=>'apply'];
- $qrcode = MemberService::make()->makeQrcode(json_encode($data));
- $info['apply_qrcode'] = $qrcode? get_image_url($qrcode):'';
- // 店铺管理权限
- $info['powers'] = [];
- if($userId){
- $powers = MerchantClerkModel::where(['user_id'=> $userId,'merch_id'=> $info['id'],'mark'=>1])->value('powers');
- $info['powers'] = $powers? explode(',', $powers) : [];
- }
- RedisService::set($cacheKey, $info, 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 string $field
- * @return array|mixed
- */
- public function getCacheInfoByUser($userId, $field = '')
- {
- $cacheKey = "caches:merchant:info:temp_{$userId}" . ($field ? '_' . md5($field) : '');
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $field = $field ? $field : 'id,name,user_id,balance,withdraw_total,mobile,category,service_time,pay_password,deposit,service_order_num,service_order_total,trade_status,settle_type,type,logo,status';
- $data = $this->model::where(['user_id' => $userId, 'mark' => 1])
- ->selectRaw($field)
- ->orderBy('id', 'desc')
- ->first();
- $data = $data ? $data->toArray() : [];
- if ($data) {
- $data['logo'] = $data['logo']? get_image_url($data['logo']):'';
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return $data;
- }
- /**
- * 修改信息
- * @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;
- }
- // 验证是否是技师
- if(MechanicModel::where(['user_id'=> $userId,'status'=>2,'mark'=>1])->value('id')) {
- $this->error = 2687;
- 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 = 2201;
- return false;
- }
- // 是否被冻结
- if($merchId && $status == 3 && $mark){
- $this->error = 2202;
- return false;
- }
- // 验证是否是技师
- if(MechanicModel::where(['user_id'=> $userId,'status'=>2,'mark'=>1])->value('id')) {
- $this->error = 2687;
- 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 = 2213;
- return ['id'=> $merchId];
- }else{
- $this->error = 2214;
- return false;
- }
- }else{
- if($merchId = $this->model->insertGetId($data)){
- $this->error = 2215;
- return ['id'=> $merchId];
- }else{
- $this->error = 2214;
- 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(['balance','deposit','update_time','mark'])->toArray() : [];
- if($info){
- $info['logo'] = isset($info['logo']) && $info['logo']? [['url'=> get_image_url($info['logo'])]] : [];
- $info['alipay_qrcode'] = isset($info['alipay_qrcode']) && $info['alipay_qrcode']? [['url'=> get_image_url($info['alipay_qrcode'])]] : [];
- $info['wxpay_qrcode'] = isset($info['wxpay_qrcode']) && $info['wxpay_qrcode']? [['url'=> get_image_url($info['wxpay_qrcode'])]] : [];
- $file1 = isset($info['qualification_imgs']) && $info['qualification_imgs']? json_decode($info['qualification_imgs'], true) : [];
- $info['file1'] = $file1? get_images_preview($file1) : [];
- $file2 = isset($info['other_certificates']) && $info['other_certificates']? json_decode($info['other_certificates'], true) : [];
- $info['file2'] = $file2? get_images_preview($file2) : [];
- $file3 = isset($info['idcard_imgs']) && $info['idcard_imgs']? json_decode($info['idcard_imgs'], true) : [];
- $info['file3'] = $file3? get_images_preview($file3) : [];
- $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'] = isset($info['category']['id'])? $info['category']['id'] : '';
- }
- unset($info['qualification_imgs']);
- unset($info['other_certificates']);
- unset($info['idcard_imgs']);
- }
- return $info;
- }
- /**
- * @param $userId
- * @param $params
- * @return bool
- */
- public function applyConfirm($userId, $params)
- {
- $id = isset($params['id'])? $params['id'] : 0;
- $info = $this->model->where(['id'=> $id,'mark'=>1])->select('id','user_id','name','status','mark')->first();
- $status = isset($info['status'])? $info['status'] : 0;
- if(empty($info) || $id<=0){
- $this->error = 2230;
- return false;
- }
- if($status != 1){
- $this->error = 2231;
- return false;
- }
- $status = isset($params['status'])? intval($params['status']) : 0;
- $auditRemark = isset($params['audit_remark'])? trim($params['audit_remark']) : '';
- if(!in_array($status,[2,3])){
- $this->error = 2232;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['status'=> $status,'audit_remark'=>$auditRemark,'update_time'=>time()])){
- $this->error = 1040;
- return true;
- }else{
- $this->error = 1041;
- return false;
- }
- }
- /**
- * @param $userId
- * @param $params
- * @return bool
- */
- public function lock($userId, $params)
- {
- $id = isset($params['id'])? $params['id'] : 0;
- $info = $this->model->where(['id'=> $id,'mark'=>1])->select('id','user_id','name','status','mark')->first();
- $status = isset($info['status'])? $info['status'] : 0;
- if(empty($info) || $id<=0){
- $this->error = 2230;
- return false;
- }
- if($status <= 1){
- $this->error = 2234;
- return false;
- }
- $agentInfo = AgentModel::where(['user_id'=> $userId,'mark'=>1])->select(['id','user_id','realname'])->first();
- $agentId = isset($agentInfo['id'])? $agentInfo['id'] : 0;
- if(empty($agentInfo)){
- $this->error = 2024;
- return false;
- }
- $status = isset($params['status'])? intval($params['status']) : 0;
- $remark = isset($params['remark'])? trim($params['remark']) : '';
- if(!in_array($status,[2,3])){
- $this->error = 2232;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['agent_lock'=>1,'agent_lock_id'=> $agentId,'agent_lock_status'=> $status,'agent_lock_remark'=>$remark,'update_time'=>time()])){
- $this->error = 1035;
- return true;
- }else{
- $this->error = 1036;
- return false;
- }
- }
- /**
- * 删除
- * @return array|false
- */
- public function delete()
- {
- // 参数
- $id = request()->post('id');
- if (empty($id)) {
- $this->error = 2014;
- return false;
- }
- $this->error = 1002;
- $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
- return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
- }
- /**
- * @param $userId
- * @param $params
- * @return bool
- */
- public function modify($userId, $params)
- {
- // 用户验证
- $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
- ->select(['id','user_id', 'status'])
- ->first();
- if (!$info) {
- $this->error = 2001;
- return false;
- }
- // 使用状态校验
- if ($info['status'] != 2) {
- $this->error = 2015;
- return false;
- }
- // 密码校验
- $data = ['update_time' => time()];
- $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
- // 手机号验证
- $oldMobile = isset($params['old_mobile']) ? $params['old_mobile'] : '';
- if (isset($params['old_mobile']) && $oldMobile) {
- // 短信验证码
- $code = isset($params['old_code']) ? $params['old_code'] : '';
- if (empty($code)) {
- $this->error = 2013;
- return false;
- }
- if (!SmsService::make()->check($oldMobile, $code, 'modify')) {
- $this->error = 1044;
- return false;
- }
- }
- // 手机号验证
- $mobile = isset($params['mobile']) ? $params['mobile'] : '';
- if (isset($params['mobile']) && $mobile) {
- $data['mobile'] = $mobile;
- $checkInfo = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id','user_id', 'status'])->first();
- if ($checkInfo && $checkInfo['user_id'] != $userId) {
- // $this->error = 2009;
- // return false;
- }
- // 短信验证码
- $code = isset($params['sms_code']) ? $params['sms_code'] : '';
- if (empty($code)) {
- $this->error = 2013;
- return false;
- }
- if (!SmsService::make()->check($mobile, $code, 'modify')) {
- $this->error = SmsService::make()->getError();
- return false;
- }
- }
- if(isset($params['settle_type']) && in_array($params['settle_type'],[0,1,2,3])){
- $data['settle_type'] = intval($params['settle_type']);
- }
- if(isset($params['trade_status']) && in_array($params['trade_status'],[1,2])){
- $data['trade_status'] = intval($params['trade_status']);
- }
- if (isset($params['pay_password']) && $payPassword) {
- $payPassword = get_password($payPassword);
- $data['pay_password'] = $payPassword;
- }
- // 修改数据
- 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;
- }
- /**
- * 缴纳保证金
- * @param $userId 商家用户
- * @param $params
- * @return array|false
- */
- public function deposit($userId, $params)
- {
- $merchId = isset($params['id'])? $params['id'] : 0;
- $money = isset($params['money'])? $params['money'] : 0;
- $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
- if($money<=0){
- $this->error = 2031;
- return false;
- }
- if(!in_array($payType, [10,20])){
- $this->error = 2032;
- return false;
- }
- $info = $this->model->where(['id'=> $merchId,'mark'=>1])
- ->select(['id','name','mobile','balance','deposit','status'])
- ->first();
- $deposit = isset($info['deposit'])? $info['deposit'] : 0;
- $status = isset($info['status'])? $info['status'] : 0;
- if($merchId<=0 || empty($info) || $status != 2){
- $this->error = 2015;
- return false;
- }
- // 充值订单
- $orderNo = get_order_num('DP');
- $data = [
- 'source_order_no'=> $orderNo,
- 'user_id'=> $userId,
- 'merch_id'=> $merchId,
- 'type'=> 14,
- 'coin_type'=> 1,
- 'user_type'=> 2,
- 'money'=> $money,
- 'balance'=> $deposit,
- 'date'=> date('Y-m-d'),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'remark'=> $payType == 10?'微信支付商家保证金':'支付宝支付商家保证金',
- 'status'=> 2,
- 'mark'=> 1,
- ];
- if(!$orderId = AccountLogModel::insertGetId($data)){
- $this->error = 2037;
- return false;
- }
- // 支付方式
- $order = [
- 'order_no'=> $orderNo,
- 'type'=> 0,
- 'pay_type'=> $payType,
- 'pay_money'=> $money,
- 'body'=> '缴纳保证金订单支付',
- ];
- switch($payType){
- case 20: // 支付宝
- $payment = PaymentService::make()->aliPay($info, $order,'deposit');
- if(empty($payment)){
- DB::rollBack();
- $this->error = PaymentService::make()->getError();
- return false;
- }
- break;
- case 10: // 微信支付
- $payment = PaymentService::make()->wechatPay($info, $order,'deposit');
- if(empty($payment)){
- DB::rollBack();
- $this->error = PaymentService::make()->getError();
- return false;
- }
- break;
- default:
- $this->error = 1030;
- return false;
- }
- $this->error = 2038;
- return [
- 'id'=> $orderId,
- 'payment'=> $payment,
- 'total'=> $money,
- 'order_no'=> $orderNo,
- 'pay_type'=> $payType,
- ];
- }
- /**
- * 退还保证金处理
- * @param $userId 用户
- * @param $params
- * @return bool
- */
- public function rebackDeposit($userId, $params)
- {
- $merchId = isset($params['id'])? $params['id'] : 0;
- if($merchId<=0){
- $this->error = 2039;
- return false;
- }
- $info = $this->model->where(['id'=> $merchId,'mark'=>1])
- ->select(['id','name','mobile','balance','deposit','status'])
- ->first();
- $deposit = isset($info['deposit'])? floatval($info['deposit']) : 0;
- $balance = isset($info['balance'])? floatval($info['balance']) : 0;
- $status = isset($info['status'])? $info['status'] : 0;
- if($merchId<=0 || empty($info) || $status != 2){
- $this->error = 2015;
- return false;
- }
- if($deposit<=0){
- $this->error = 2041;
- return false;
- }
- // 时间限制
- $depositTime = ConfigService::make()->getConfigByCode('shop_deposit_time');
- $depositTime = $depositTime>=0? $depositTime : 0;
- $paymentTime = AccountLogModel::where(['merch_id'=> $merchId,'type'=>14,'coin_type'=>1,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('create_time');
- if($depositTime>0 && $paymentTime>0 && $paymentTime + ($depositTime*86400)> time()){
- $this->error = lang(2040,['time'=> $depositTime]);
- return false;
- }
- DB::beginTransaction();
- // 余额
- $updateData = ['balance' => DB::raw("balance + {$deposit}"),'deposit'=>0.00, 'update_time' => time()];
- if(!$this->model->where(['id'=> $merchId])->update($updateData)){
- DB::rollBack();
- $this->error = 2042;
- return false;
- }
- // 退还记录
- $data = [
- 'source_order_no'=> '',
- 'user_id'=> $userId,
- 'merch_id'=> $merchId,
- 'type'=> 14,
- 'coin_type'=> 4,
- 'user_type'=> 2,
- 'money'=> -$deposit,
- 'balance'=> $balance,
- 'date'=> date('Y-m-d'),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'remark'=> '商家保证金退还',
- 'status'=> 1,
- 'mark'=> 1,
- ];
- if(!$id = AccountLogModel::insertGetId($data)){
- DB::rollBack();
- $this->error = 2042;
- return false;
- }
- DB::commit();
- $params = [
- 'title' => '商家保证金退还到账通知',
- 'body' => "您的商家保证金申请退还已成功,请点击查看详情",
- 'type' => 3, // 1-公告通知,2-订单通知,3-交易通知,4-其他
- 'content' => [
- 'pay_time' => ['name' => '退还时间', 'text' => date('Y-m-d H:i:s')],
- 'money' => ['name' => '金额', 'text' => $deposit],
- 'balance' => ['name' => '当前余额', 'text' => moneyFormat($balance + $deposit,2)],
- 'status' => ['name' => '状态', 'text' => '已到账'],
- ],
- 'click_type' => 'payload',
- 'url' => '/pages/account/index?type=3',
- ];
- PushService::make()->pushMessageByUser($userId, $params, 0);
- $this->error = 2043;
- return true;
- }
- }
|