MerchantService.php 22 KB

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