MerchantService.php 21 KB

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