MerchantService.php 22 KB

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