MerchantService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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\AccountLogModel;
  13. use App\Models\MerchantCategoryModel;
  14. use App\Models\MerchantModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. use App\Services\SmsService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 商户服务管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class MerchantService
  25. * @package App\Services\Api
  26. */
  27. class MerchantService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * MerchantService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new MerchantModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 获取缓存列表
  54. * @param $position
  55. * @param int $num
  56. * @return array|mixed
  57. */
  58. public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
  59. {
  60. $page = request()->post('page', 1);
  61. $cacheKey = "caches:merchant:page_{$page}_" . md5(json_encode($params).$pageSize);
  62. $datas = RedisService::get($cacheKey);
  63. $data = isset($datas['data'])? $datas['data'] : [];
  64. if ($datas && $data && !$refresh) {
  65. return [
  66. 'list'=> $data,
  67. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  68. 'pageSize'=>$pageSize
  69. ];
  70. }
  71. $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';
  72. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  73. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  74. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  75. $order = 'lev_a.id desc';
  76. $datas = $this->model->from('merchant as a')
  77. ->leftJoin('member as b','b.id','=','a.user_id')
  78. ->where(['a.mark' => 1,'b.mark'=>1])
  79. ->where(function ($query) use ($params) {
  80. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  81. if ($kw) {
  82. $query->where('a.name', 'like', "%{$kw}%")->orWhere('a.business_scope', 'like', "%{$kw}%");
  83. }
  84. })
  85. ->where(function ($query) use ($params) {
  86. // 商户类型
  87. $type = isset($params['type']) ? intval($params['type']) : 0;
  88. if ($type) {
  89. $query->where('a.type', $type);
  90. }
  91. // 状态
  92. $status = isset($params['status']) && $params['status']>=0 ? intval($params['status']) : 2;
  93. if ($status>0) {
  94. $query->where('a.status', $status);
  95. }else{
  96. $query->whereIn('a.status',[1,2]);
  97. }
  98. // 经营类目
  99. $category = isset($params['category']) ? intval($params['category']) : 0;
  100. if ($category) {
  101. $query->where('a.category', $category);
  102. }
  103. // 省
  104. $province = isset($params['province']) ? trim($params['province']) : '';
  105. $city = isset($params['city']) ? trim($params['city']) : '';
  106. if ($province && empty($city)) {
  107. $query->where('a.province', 'like',"{$province}%");
  108. }
  109. // 市
  110. if ($city) {
  111. $query->where(function($query) use($city){
  112. $query->where('a.city', $city)->orWhere('a.address','like',"%{$city}%");
  113. });
  114. }
  115. // 是否保障金
  116. $isDeposit = isset($params['deposit']) ? intval($params['deposit']) : 0;
  117. if ($isDeposit) {
  118. $query->where('a.deposit', '>', 0);
  119. }
  120. })
  121. ->selectRaw($field)
  122. ->orderByRaw($order)
  123. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  124. $datas = $datas ? $datas->toArray() : [];
  125. if ($datas) {
  126. foreach($datas['data'] as &$item){
  127. $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
  128. $item['score_rate'] = max(0, moneyFormat($item['score_rate'], 1));
  129. }
  130. unset($item);
  131. RedisService::set($cacheKey, $datas, rand(3, 5));
  132. }
  133. return [
  134. 'list'=> isset($datas['data'])? $datas['data'] : [],
  135. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  136. 'pageSize'=>$pageSize
  137. ];
  138. }
  139. /**
  140. * 获取详情
  141. * @param $id 商家ID
  142. * @param string $type
  143. * @return array|mixed
  144. */
  145. public function getInfoById($id, $type='info', $userId=0)
  146. {
  147. $cacheKey = "caches:merch:{$type}_{$id}";
  148. $info = RedisService::get($cacheKey);
  149. if($info){
  150. return $info;
  151. }
  152. $field = ['a.id','a.name','a.user_id','a.type','a.logo','a.category','a.business_scope','a.balance','a.usdt','a.service_time','a.deposit','a.delivery_fee','a.bonus_rate','a.power_rate','a.status','a.trade_status','b.username','b.nickname'];
  153. $info = $this->model->from('merchant as a')->with(['category'])
  154. ->leftJoin('member as b','b.id','=','a.user_id')
  155. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  156. ->select($field)
  157. ->first();
  158. $info = $info? $info->toArray() : [];
  159. if($info){
  160. if(isset($info['logo'])){
  161. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  162. }
  163. if(isset($info['albums'])){
  164. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  165. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  166. }
  167. $info['service_status'] = 2;
  168. $info['service_time'] = isset($info['service_time']) && $info['service_time']? $info['service_time']:'08点~22点';
  169. $serviceTime = $info['service_time']? str_replace('~','~',$info['service_time']):'';
  170. $times = $serviceTime? explode('~',$serviceTime) : [];
  171. $times[0] = isset($times[0]) && $times[0]? $times[0] : '08点';
  172. $times[1] = isset($times[1]) && $times[1]? $times[1] : '22点';
  173. if($times && date('H点')>= $times[0] && date('H点')<=$times[1]){
  174. $info['service_status'] = 1;
  175. }
  176. if($info['trade_status'] != 1){
  177. $info['service_status'] = 2;
  178. }
  179. RedisService::set($cacheKey, $info, rand(3, 5));
  180. }
  181. return $info;
  182. }
  183. /**
  184. * 获取缓存信息
  185. * @param $where
  186. * @param array $field
  187. * @param int $expired
  188. * @return array|mixed
  189. */
  190. public function getCacheInfo($where, $field = [], $expired = 0)
  191. {
  192. $cacheKey = "caches:merchant:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  193. $info = RedisService::get($cacheKey);
  194. if ($info) {
  195. return $info;
  196. }
  197. $defaultField = ['id','user_id', 'name', 'mobile', 'logo', 'category', 'usdt', 'status'];
  198. $field = $field ? $field : $defaultField;
  199. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  200. $info = $info ? $info->toArray() : [];
  201. if ($info) {
  202. if (isset($info['logo'])) {
  203. $info['logo'] = $info['logo']? $info['logo'] : '/images/member/logo.png';
  204. $info['logo_preview'] = $info['logo']? get_image_url($info['logo']) : '';
  205. }
  206. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  207. }
  208. return $info;
  209. }
  210. /**
  211. * 添加或编辑
  212. * @return array
  213. * @since 2020/11/11
  214. * @author laravel开发员
  215. */
  216. public function edit()
  217. {
  218. $data = request()->all();
  219. // 图片处理
  220. $cover = $data['cover'] ? trim($data['cover']) : '';
  221. if (strpos($cover, "temp")) {
  222. $data['cover'] = save_image($cover, 'ad');
  223. } else {
  224. $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
  225. }
  226. // 开始时间
  227. if (isset($data['start_time'])) {
  228. $data['start_time'] = strtotime($data['start_time']);
  229. }
  230. // 结束时间
  231. if (isset($data['end_time'])) {
  232. $data['end_time'] = strtotime($data['end_time']);
  233. }
  234. return parent::edit($data); // TODO: Change the autogenerated stub
  235. }
  236. /**
  237. * 修改信息
  238. * @param $userId
  239. * @param $params
  240. * @return array|false|int[]
  241. */
  242. public function saveInfo($userId, $params)
  243. {
  244. // 验证是否入驻过和入驻状态
  245. $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
  246. $status = isset($info['status'])? $info['status'] : 0;
  247. $mark = isset($info['mark'])? $info['mark'] : 0;
  248. $merchId = isset($info['id'])? $info['id'] : 0;
  249. if($merchId && empty($info)){
  250. $this->error = 2216;
  251. return false;
  252. }
  253. // 是否被冻结
  254. if($merchId && $status == 3 && $mark){
  255. $this->error = 2202;
  256. return false;
  257. }
  258. // 验证账户是否正常
  259. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
  260. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  261. if(empty($userInfo) || $status != 1){
  262. $this->error = 2017;
  263. return false;
  264. }
  265. $type = isset($params['type'])? intval($params['type']) : 0;
  266. $category = isset($params['category'])? intval($params['category']) : 0;
  267. $lng = isset($params['lng'])? floatval($params['lng']) : 0;
  268. $lat = isset($params['lat'])? floatval($params['lat']) : 0;
  269. $address = isset($params['address'])? trim($params['address']) : '';
  270. $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
  271. $file1 = isset($params['file1'])? get_format_images($params['file1']) : '';
  272. $file2 = isset($params['file2'])? get_format_images($params['file2']) : '';
  273. $file3 = isset($params['file3'])? get_format_images($params['file3']) : '';
  274. $alipayQrcodeData = isset($params['alipay_qrcode'])? $params['alipay_qrcode'] : [];
  275. $alipayQrcode = isset($alipayQrcodeData[0]['url'])? get_image_path($alipayQrcodeData[0]['url']) : '';
  276. $wxpayQrcodeData = isset($params['wxpay_qrcode'])? $params['wxpay_qrcode'] : [];
  277. $wxpayQrcode = isset($wxpayQrcodeData[0]['url'])? get_image_path($wxpayQrcodeData[0]['url']) : '';
  278. $logoData = isset($params['logo'])? $params['logo'] : [];
  279. $logo = isset($logoData[0]['url'])? get_image_path($logoData[0]['url']) : '';
  280. $name = isset($params['name'])? $params['name'] : '';
  281. // 验证分类
  282. if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
  283. $this->error = 2203;
  284. return false;
  285. }
  286. // 验证logo
  287. if(empty($logo)){
  288. $this->error = 2204;
  289. return false;
  290. }
  291. // 位置信息
  292. if($type == 1 && (empty($lat) || empty($lng) || empty($address))){
  293. $this->error = 2210;
  294. //return false;
  295. }
  296. // 证书
  297. if(empty($file1)){
  298. $this->error = $type == 1? 2206 : 2211;
  299. return false;
  300. }
  301. if(empty($file2)){
  302. $this->error = 2207;
  303. return false;
  304. }
  305. if(empty($file3)){
  306. $this->error = $type==1? 2208 : 2209;
  307. return false;
  308. }
  309. // 收款码
  310. if(empty($alipayQrcode) || empty($wxpayQrcode)){
  311. $this->error = 2205;
  312. return false;
  313. }
  314. // 入驻数据
  315. $data = [
  316. 'name' => $name,
  317. 'user_id' => $userId,
  318. 'category'=> $category,
  319. 'type'=> $type,
  320. 'logo'=> $logo,
  321. 'albums'=> $albums? $albums : '',
  322. 'qualification_imgs'=> $file1? $file1 : '',
  323. 'other_certificates'=> $file2? $file2 : '',
  324. 'idcard_imgs'=> $file3? $file3 : '',
  325. 'alipay_qrcode'=> $alipayQrcode? $alipayQrcode : '',
  326. 'wxpay_qrcode'=> $wxpayQrcode? $wxpayQrcode : '',
  327. 'lng'=> $lng,
  328. 'lat'=> $lat,
  329. 'address'=> $address,
  330. 'province'=> isset($params['province'])? trim($params['province']) : '',
  331. 'city'=> isset($params['city'])? trim($params['city']) : '',
  332. 'district'=> isset($params['district'])? trim($params['district']) : '',
  333. 'intro'=> isset($params['intro'])? trim($params['intro']) : '',
  334. 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '',
  335. 'business_scope'=> isset($params['business_scope'])? trim($params['business_scope']) : '',
  336. 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
  337. 'create_time'=> time(),
  338. 'update_time'=> time(),
  339. 'status'=> 1,
  340. 'mark'=> 1,
  341. ];
  342. // 写入数据
  343. if($merchId){
  344. if($this->model->where(['id'=> $merchId])->update($data)){
  345. $this->error = 2228;
  346. RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
  347. return ['id'=> $merchId];
  348. }else{
  349. $this->error = 2229;
  350. return false;
  351. }
  352. }else{
  353. if($merchId = $this->model->insertGetId($data)){
  354. $this->error = 2228;
  355. RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
  356. return ['id'=> $merchId];
  357. }else{
  358. $this->error = 2229;
  359. return false;
  360. }
  361. }
  362. }
  363. /**
  364. * 申请入驻
  365. * @param $userId
  366. * @param $params
  367. * @return array|false|int[]
  368. */
  369. public function apply($userId, $params)
  370. {
  371. // 验证是否入驻过和入驻状态
  372. $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
  373. $status = isset($info['status'])? $info['status'] : 0;
  374. $mark = isset($info['mark'])? $info['mark'] : 0;
  375. $merchId = isset($info['id'])? $info['id'] : 0;
  376. if($merchId && $status == 2 && $mark){
  377. $this->error = 2201;
  378. return false;
  379. }
  380. // 是否被冻结
  381. if($merchId && $status == 3 && $mark){
  382. $this->error = 2202;
  383. return false;
  384. }
  385. // 验证账户是否正常
  386. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
  387. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  388. if(empty($userInfo) || $status != 1){
  389. $this->error = 2017;
  390. return false;
  391. }
  392. $type = isset($params['type'])? intval($params['type']) : 1;
  393. $category = isset($params['category'])? intval($params['category']) : 0;
  394. $currency = isset($params['currency'])? trim($params['currency']) : '';
  395. $country = isset($params['country'])? trim($params['country']) : '';
  396. $city = isset($params['city'])? trim($params['city']) : '';
  397. $address = isset($params['address'])? trim($params['address']) : '';
  398. $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
  399. $businessImg = isset($params['business_img'])? get_image_path($params['business_img']) : '';
  400. $logo = isset($params['logo'])? get_image_path($params['logo']) : '';
  401. $name = isset($params['name'])? $params['name'] : '';
  402. // 验证分类
  403. if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
  404. $this->error = 2203;
  405. return false;
  406. }
  407. // 验证logo
  408. if(empty($logo)){
  409. $this->error = 2204;
  410. return false;
  411. }
  412. $mobile = isset($params['mobile'])? trim($params['mobile']) : '';
  413. $telegram = isset($params['telegram'])? trim($params['telegram']) : '';
  414. if(empty($mobile) && empty($telegram)){
  415. $this->error = 2204;
  416. return false;
  417. }
  418. // 地址信息
  419. if(empty($country) || empty($city) || empty($address)){
  420. $this->error = 2210;
  421. return false;
  422. }
  423. if(empty($currency)){
  424. $this->error = 2204;
  425. return false;
  426. }
  427. // 营业执照
  428. if(empty($businessImg)){
  429. $this->error = 2211;
  430. return false;
  431. }
  432. // 入驻数据
  433. $data = [
  434. 'name' => $name,
  435. 'user_id' => $userId,
  436. 'category'=> $category,
  437. 'type'=> $type,
  438. 'logo'=> $logo,
  439. 'albums'=> $albums? $albums : '',
  440. 'business_img'=> $businessImg? $businessImg : '',
  441. 'currency'=> $currency,
  442. 'country'=> $country,
  443. 'city'=> $city,
  444. 'address'=> $address,
  445. 'description'=> isset($params['description'])? trim($params['description']) : '',
  446. 'mobile'=> $mobile,
  447. 'telegram'=> $telegram,
  448. 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
  449. 'create_time'=> time(),
  450. 'update_time'=> time(),
  451. 'status'=> 1,
  452. 'mark'=> 1,
  453. ];
  454. // 写入数据
  455. if($merchId){
  456. if($this->model->where(['id'=> $merchId])->update($data)){
  457. $this->error = 2213;
  458. return ['id'=> $merchId];
  459. }else{
  460. $this->error = 2214;
  461. return false;
  462. }
  463. }else{
  464. if($merchId = $this->model->insertGetId($data)){
  465. $this->error = 2215;
  466. return ['id'=> $merchId];
  467. }else{
  468. $this->error = 2214;
  469. return false;
  470. }
  471. }
  472. }
  473. /**
  474. * 获取商家入驻信息
  475. * @param $userId
  476. * @return mixed
  477. */
  478. public function getApplyInfo($userId)
  479. {
  480. $info = $this->model->with(['category'])->where(['user_id'=> $userId,'mark'=>1])
  481. ->orderBy('id','desc')
  482. ->first();
  483. $info = $info? $info->setHidden(['usdt','update_time','mark'])->toArray() : [];
  484. if($info){
  485. $info['logo'] = isset($info['logo']) && $info['logo']? [['url'=> get_image_url($info['logo'])]] : [];
  486. $info['business_img'] = isset($info['business_img']) && $info['business_img']? [['url'=> get_image_url($info['business_img'])]] : [];
  487. $albums = isset($info['albums']) && $info['albums']? json_decode($info['albums'], true) : [];
  488. $info['albums'] = $albums? get_images_preview($albums) : [];
  489. if(isset($info['category']) && $info['category']){
  490. $info['category_name'] = isset($info['category']['name'])? $info['category']['name'] : '';
  491. $info['category'] = isset($info['category']['id'])? $info['category']['id'] : '';
  492. }
  493. }
  494. return $info;
  495. }
  496. /**
  497. * @param $userId
  498. * @param $params
  499. * @return bool
  500. */
  501. public function modify($userId, $params)
  502. {
  503. // 用户验证
  504. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  505. ->select(['id','user_id', 'status'])
  506. ->first();
  507. $userInfo = isset($info['member'])? $info['member'] : [];
  508. if (!$info) {
  509. $this->error = 2001;
  510. return false;
  511. }
  512. // 使用状态校验
  513. if ($info['status'] != 2) {
  514. $this->error = 2015;
  515. return false;
  516. }
  517. // 密码校验
  518. $data = ['update_time' => time()];
  519. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  520. // 验证账户是否正常
  521. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
  522. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  523. if(empty($userInfo) || $status != 1){
  524. $this->error = 2017;
  525. return false;
  526. }
  527. if($userPayPassword != get_password($payPassword)){
  528. }
  529. // 手机号验证
  530. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  531. if (isset($params['mobile']) && $mobile) {
  532. $data['mobile'] = $mobile;
  533. }
  534. if(isset($params['name']) && $params['name']){
  535. $data['name'] = trim($params['name']);
  536. }
  537. if(isset($params['service_time']) && $params['service_time']){
  538. $data['service_time'] = trim($params['service_time']);
  539. }
  540. // 修改数据
  541. RedisService::clear("caches:merch:detail_{$info['id']}");
  542. RedisService::keyDel("caches:merchant:info:temp*");
  543. $this->model->where(['user_id' => $userId])->update($data);
  544. $this->error = 1008;
  545. return true;
  546. }
  547. }