MerchantService.php 21 KB

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