MerchantService.php 21 KB

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