MerchantService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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,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. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  73. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  74. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  75. $order = 'lev_a.id desc';
  76. $datas = $this->model->from('merchant as a')
  77. ->leftJoin('member as b','b.id','=','a.user_id')
  78. ->where(['a.mark' => 1,'b.mark'=>1])
  79. ->where(function ($query) use ($params) {
  80. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  81. if ($kw) {
  82. $query->where('a.name', 'like', "%{$kw}%")->orWhere('a.business_scope', 'like', "%{$kw}%");
  83. }
  84. })
  85. ->where(function ($query) use ($params) {
  86. // 商户类型
  87. $type = isset($params['type']) ? intval($params['type']) : 0;
  88. if ($type) {
  89. $query->where('a.type', $type);
  90. }
  91. // 状态
  92. $status = isset($params['status']) && $params['status']>=0 ? intval($params['status']) : 2;
  93. if ($status>0) {
  94. $query->where('a.status', $status);
  95. }else{
  96. $query->whereIn('a.status',[1,2]);
  97. }
  98. // 经营类目
  99. $category = isset($params['category']) ? intval($params['category']) : 0;
  100. if ($category) {
  101. $query->where('a.category', $category);
  102. }
  103. // 省
  104. $province = isset($params['province']) ? trim($params['province']) : '';
  105. $city = isset($params['city']) ? trim($params['city']) : '';
  106. if ($province && empty($city)) {
  107. $query->where('a.province', 'like',"{$province}%");
  108. }
  109. // 市
  110. if ($city) {
  111. $query->where(function($query) use($city){
  112. $query->where('a.city', $city)->orWhere('a.address','like',"%{$city}%");
  113. });
  114. }
  115. // 是否保障金
  116. $isDeposit = isset($params['deposit']) ? intval($params['deposit']) : 0;
  117. if ($isDeposit) {
  118. $query->where('a.deposit', '>', 0);
  119. }
  120. })
  121. ->selectRaw($field)
  122. ->orderByRaw($order)
  123. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  124. $datas = $datas ? $datas->toArray() : [];
  125. if ($datas) {
  126. foreach($datas['data'] as &$item){
  127. $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
  128. $item['score_rate'] = max(0, moneyFormat($item['score_rate'], 1));
  129. }
  130. unset($item);
  131. RedisService::set($cacheKey, $datas, rand(3, 5));
  132. }
  133. return [
  134. 'list'=> isset($datas['data'])? $datas['data'] : [],
  135. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  136. 'pageSize'=>$pageSize
  137. ];
  138. }
  139. /**
  140. * 获取详情
  141. * @param $id 商家ID
  142. * @param string $type
  143. * @return array|mixed
  144. */
  145. public function getInfoById($id, $type='info', $userId=0)
  146. {
  147. $cacheKey = "caches:merch:{$type}_{$id}";
  148. $info = RedisService::get($cacheKey);
  149. if($info){
  150. return $info;
  151. }
  152. $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'];
  153. $info = $this->model->from('merchant as a')->with(['category'])
  154. ->leftJoin('member as b','b.id','=','a.user_id')
  155. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  156. ->select($field)
  157. ->first();
  158. $info = $info? $info->toArray() : [];
  159. if($info){
  160. if(isset($info['logo'])){
  161. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  162. }
  163. if(isset($info['albums'])){
  164. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  165. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  166. }
  167. $info['service_status'] = 2;
  168. $info['service_time'] = isset($info['service_time']) && $info['service_time']? $info['service_time']:'08点~22点';
  169. $serviceTime = $info['service_time']? str_replace('~','~',$info['service_time']):'';
  170. $times = $serviceTime? explode('~',$serviceTime) : [];
  171. $times[0] = isset($times[0]) && $times[0]? $times[0] : '08点';
  172. $times[1] = isset($times[1]) && $times[1]? $times[1] : '22点';
  173. if($times && date('H点')>= $times[0] && date('H点')<=$times[1]){
  174. $info['service_status'] = 1;
  175. }
  176. if($info['trade_status'] != 1){
  177. $info['service_status'] = 2;
  178. }
  179. RedisService::set($cacheKey, $info, rand(3, 5));
  180. }
  181. return $info;
  182. }
  183. /**
  184. * 获取缓存信息
  185. * @param $where
  186. * @param array $field
  187. * @param int $expired
  188. * @return array|mixed
  189. */
  190. public function getCacheInfo($where, $field = [], $expired = 0)
  191. {
  192. $cacheKey = "caches:merchant:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  193. $info = RedisService::get($cacheKey);
  194. if ($info) {
  195. return $info;
  196. }
  197. $defaultField = ['id','user_id', 'name', 'mobile', 'logo', 'category', 'usdt', 'status'];
  198. $field = $field ? $field : $defaultField;
  199. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  200. $info = $info ? $info->toArray() : [];
  201. if ($info) {
  202. if (isset($info['logo'])) {
  203. $info['logo'] = $info['logo']? $info['logo'] : '/images/member/logo.png';
  204. $info['logo_preview'] = $info['logo']? get_image_url($info['logo']) : '';
  205. }
  206. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  207. }
  208. return $info;
  209. }
  210. /**
  211. * 添加或编辑
  212. * @return array
  213. * @since 2020/11/11
  214. * @author laravel开发员
  215. */
  216. public function edit()
  217. {
  218. $data = request()->all();
  219. // 图片处理
  220. $cover = $data['cover'] ? trim($data['cover']) : '';
  221. if (strpos($cover, "temp")) {
  222. $data['cover'] = save_image($cover, 'ad');
  223. } else {
  224. $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
  225. }
  226. // 开始时间
  227. if (isset($data['start_time'])) {
  228. $data['start_time'] = strtotime($data['start_time']);
  229. }
  230. // 结束时间
  231. if (isset($data['end_time'])) {
  232. $data['end_time'] = strtotime($data['end_time']);
  233. }
  234. return parent::edit($data); // TODO: Change the autogenerated stub
  235. }
  236. /**
  237. * 修改信息
  238. * @param $userId
  239. * @param $params
  240. * @return array|false|int[]
  241. */
  242. public function saveInfo($userId, $params)
  243. {
  244. // 验证是否入驻过和入驻状态
  245. $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
  246. $status = isset($info['status'])? $info['status'] : 0;
  247. $mark = isset($info['mark'])? $info['mark'] : 0;
  248. $merchId = isset($info['id'])? $info['id'] : 0;
  249. if($merchId && empty($info)){
  250. $this->error = 2216;
  251. return false;
  252. }
  253. // 是否被冻结
  254. if($merchId && $status == 3 && $mark){
  255. $this->error = 2202;
  256. return false;
  257. }
  258. // 验证是否是技师
  259. if(MechanicModel::where(['user_id'=> $userId,'status'=>2,'mark'=>1])->value('id')) {
  260. $this->error = 2687;
  261. return false;
  262. }
  263. // 验证账户是否正常
  264. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
  265. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  266. if(empty($userInfo) || $status != 1){
  267. $this->error = 2017;
  268. return false;
  269. }
  270. $type = isset($params['type'])? intval($params['type']) : 0;
  271. $category = isset($params['category'])? intval($params['category']) : 0;
  272. $lng = isset($params['lng'])? floatval($params['lng']) : 0;
  273. $lat = isset($params['lat'])? floatval($params['lat']) : 0;
  274. $address = isset($params['address'])? trim($params['address']) : '';
  275. $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
  276. $file1 = isset($params['file1'])? get_format_images($params['file1']) : '';
  277. $file2 = isset($params['file2'])? get_format_images($params['file2']) : '';
  278. $file3 = isset($params['file3'])? get_format_images($params['file3']) : '';
  279. $alipayQrcodeData = isset($params['alipay_qrcode'])? $params['alipay_qrcode'] : [];
  280. $alipayQrcode = isset($alipayQrcodeData[0]['url'])? get_image_path($alipayQrcodeData[0]['url']) : '';
  281. $wxpayQrcodeData = isset($params['wxpay_qrcode'])? $params['wxpay_qrcode'] : [];
  282. $wxpayQrcode = isset($wxpayQrcodeData[0]['url'])? get_image_path($wxpayQrcodeData[0]['url']) : '';
  283. $logoData = isset($params['logo'])? $params['logo'] : [];
  284. $logo = isset($logoData[0]['url'])? get_image_path($logoData[0]['url']) : '';
  285. $name = isset($params['name'])? $params['name'] : '';
  286. // 验证分类
  287. if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
  288. $this->error = 2203;
  289. return false;
  290. }
  291. // 验证logo
  292. if(empty($logo)){
  293. $this->error = 2204;
  294. return false;
  295. }
  296. // 位置信息
  297. if($type == 1 && (empty($lat) || empty($lng) || empty($address))){
  298. $this->error = 2210;
  299. //return false;
  300. }
  301. // 证书
  302. if(empty($file1)){
  303. $this->error = $type == 1? 2206 : 2211;
  304. return false;
  305. }
  306. if(empty($file2)){
  307. $this->error = 2207;
  308. return false;
  309. }
  310. if(empty($file3)){
  311. $this->error = $type==1? 2208 : 2209;
  312. return false;
  313. }
  314. // 收款码
  315. if(empty($alipayQrcode) || empty($wxpayQrcode)){
  316. $this->error = 2205;
  317. return false;
  318. }
  319. // 入驻数据
  320. $data = [
  321. 'name' => $name,
  322. 'user_id' => $userId,
  323. 'category'=> $category,
  324. 'type'=> $type,
  325. 'logo'=> $logo,
  326. 'albums'=> $albums? $albums : '',
  327. 'qualification_imgs'=> $file1? $file1 : '',
  328. 'other_certificates'=> $file2? $file2 : '',
  329. 'idcard_imgs'=> $file3? $file3 : '',
  330. 'alipay_qrcode'=> $alipayQrcode? $alipayQrcode : '',
  331. 'wxpay_qrcode'=> $wxpayQrcode? $wxpayQrcode : '',
  332. 'lng'=> $lng,
  333. 'lat'=> $lat,
  334. 'address'=> $address,
  335. 'province'=> isset($params['province'])? trim($params['province']) : '',
  336. 'city'=> isset($params['city'])? trim($params['city']) : '',
  337. 'district'=> isset($params['district'])? trim($params['district']) : '',
  338. 'intro'=> isset($params['intro'])? trim($params['intro']) : '',
  339. 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '',
  340. 'business_scope'=> isset($params['business_scope'])? trim($params['business_scope']) : '',
  341. 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
  342. 'create_time'=> time(),
  343. 'update_time'=> time(),
  344. 'status'=> 1,
  345. 'mark'=> 1,
  346. ];
  347. // 写入数据
  348. if($merchId){
  349. if($this->model->where(['id'=> $merchId])->update($data)){
  350. $this->error = 2228;
  351. RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
  352. return ['id'=> $merchId];
  353. }else{
  354. $this->error = 2229;
  355. return false;
  356. }
  357. }else{
  358. if($merchId = $this->model->insertGetId($data)){
  359. $this->error = 2228;
  360. RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
  361. return ['id'=> $merchId];
  362. }else{
  363. $this->error = 2229;
  364. return false;
  365. }
  366. }
  367. }
  368. /**
  369. * 申请入驻
  370. * @param $userId
  371. * @param $params
  372. * @return array|false|int[]
  373. */
  374. public function apply($userId, $params)
  375. {
  376. // 验证是否入驻过和入驻状态
  377. $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
  378. $status = isset($info['status'])? $info['status'] : 0;
  379. $mark = isset($info['mark'])? $info['mark'] : 0;
  380. $merchId = isset($info['id'])? $info['id'] : 0;
  381. if($merchId && $status == 2 && $mark){
  382. $this->error = 2201;
  383. return false;
  384. }
  385. // 是否被冻结
  386. if($merchId && $status == 3 && $mark){
  387. $this->error = 2202;
  388. return false;
  389. }
  390. // 验证是否是技师
  391. if(MechanicModel::where(['user_id'=> $userId,'status'=>2,'mark'=>1])->value('id')) {
  392. $this->error = 2687;
  393. return false;
  394. }
  395. // 验证账户是否正常
  396. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
  397. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  398. if(empty($userInfo) || $status != 1){
  399. $this->error = 2017;
  400. return false;
  401. }
  402. $type = isset($params['type'])? intval($params['type']) : 0;
  403. $category = isset($params['category'])? intval($params['category']) : 0;
  404. $lng = isset($params['lng'])? floatval($params['lng']) : 0;
  405. $lat = isset($params['lat'])? floatval($params['lat']) : 0;
  406. $address = isset($params['address'])? trim($params['address']) : '';
  407. $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
  408. $file1 = isset($params['file1'])? get_format_images($params['file1']) : '';
  409. $file2 = isset($params['file2'])? get_format_images($params['file2']) : '';
  410. $file3 = isset($params['file3'])? get_format_images($params['file3']) : '';
  411. $alipayQrcodeData = isset($params['alipay_qrcode'])? $params['alipay_qrcode'] : [];
  412. $alipayQrcode = isset($alipayQrcodeData[0]['url'])? get_image_path($alipayQrcodeData[0]['url']) : '';
  413. $wxpayQrcodeData = isset($params['wxpay_qrcode'])? $params['wxpay_qrcode'] : [];
  414. $wxpayQrcode = isset($wxpayQrcodeData[0]['url'])? get_image_path($wxpayQrcodeData[0]['url']) : '';
  415. $logoData = isset($params['logo'])? $params['logo'] : [];
  416. $logo = isset($logoData[0]['url'])? get_image_path($logoData[0]['url']) : '';
  417. $name = isset($params['name'])? $params['name'] : '';
  418. // 验证分类
  419. if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
  420. $this->error = 2203;
  421. return false;
  422. }
  423. // 验证logo
  424. if(empty($logo)){
  425. $this->error = 2204;
  426. return false;
  427. }
  428. // 位置信息
  429. if($type == 1 && (empty($lat) || empty($lng) || empty($address))){
  430. $this->error = 2210;
  431. //return false;
  432. }
  433. // 证书
  434. if(empty($file1)){
  435. $this->error = $type == 1? 2206 : 2211;
  436. return false;
  437. }
  438. if(empty($file2)){
  439. $this->error = 2207;
  440. return false;
  441. }
  442. if(empty($file3)){
  443. $this->error = $type==1? 2208 : 2209;
  444. return false;
  445. }
  446. // 收款码
  447. if(empty($alipayQrcode) || empty($wxpayQrcode)){
  448. $this->error = 2205;
  449. return false;
  450. }
  451. // 入驻数据
  452. $data = [
  453. 'name' => $name,
  454. 'user_id' => $userId,
  455. 'category'=> $category,
  456. 'type'=> $type,
  457. 'logo'=> $logo,
  458. 'albums'=> $albums? $albums : '',
  459. 'qualification_imgs'=> $file1? $file1 : '',
  460. 'other_certificates'=> $file2? $file2 : '',
  461. 'idcard_imgs'=> $file3? $file3 : '',
  462. 'alipay_qrcode'=> $alipayQrcode? $alipayQrcode : '',
  463. 'wxpay_qrcode'=> $wxpayQrcode? $wxpayQrcode : '',
  464. 'lng'=> $lng,
  465. 'lat'=> $lat,
  466. 'address'=> $address,
  467. 'province'=> isset($params['province'])? trim($params['province']) : '',
  468. 'city'=> isset($params['city'])? trim($params['city']) : '',
  469. 'district'=> isset($params['district'])? trim($params['district']) : '',
  470. 'intro'=> isset($params['intro'])? trim($params['intro']) : '',
  471. 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '',
  472. 'business_scope'=> isset($params['business_scope'])? trim($params['business_scope']) : '',
  473. 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
  474. 'create_time'=> time(),
  475. 'update_time'=> time(),
  476. 'status'=> 1,
  477. 'mark'=> 1,
  478. ];
  479. // 写入数据
  480. if($merchId){
  481. if($this->model->where(['id'=> $merchId])->update($data)){
  482. $this->error = 2213;
  483. return ['id'=> $merchId];
  484. }else{
  485. $this->error = 2214;
  486. return false;
  487. }
  488. }else{
  489. if($merchId = $this->model->insertGetId($data)){
  490. $this->error = 2215;
  491. return ['id'=> $merchId];
  492. }else{
  493. $this->error = 2214;
  494. return false;
  495. }
  496. }
  497. }
  498. /**
  499. * 获取商家入驻信息
  500. * @param $userId
  501. * @return mixed
  502. */
  503. public function getApplyInfo($userId)
  504. {
  505. $info = $this->model->with(['category'])->where(['user_id'=> $userId,'mark'=>1])
  506. ->orderBy('id','desc')
  507. ->first();
  508. $info = $info? $info->setHidden(['balance','deposit','update_time','mark'])->toArray() : [];
  509. if($info){
  510. $info['logo'] = isset($info['logo']) && $info['logo']? [['url'=> get_image_url($info['logo'])]] : [];
  511. $info['alipay_qrcode'] = isset($info['alipay_qrcode']) && $info['alipay_qrcode']? [['url'=> get_image_url($info['alipay_qrcode'])]] : [];
  512. $info['wxpay_qrcode'] = isset($info['wxpay_qrcode']) && $info['wxpay_qrcode']? [['url'=> get_image_url($info['wxpay_qrcode'])]] : [];
  513. $file1 = isset($info['qualification_imgs']) && $info['qualification_imgs']? json_decode($info['qualification_imgs'], true) : [];
  514. $info['file1'] = $file1? get_images_preview($file1) : [];
  515. $file2 = isset($info['other_certificates']) && $info['other_certificates']? json_decode($info['other_certificates'], true) : [];
  516. $info['file2'] = $file2? get_images_preview($file2) : [];
  517. $file3 = isset($info['idcard_imgs']) && $info['idcard_imgs']? json_decode($info['idcard_imgs'], true) : [];
  518. $info['file3'] = $file3? get_images_preview($file3) : [];
  519. $albums = isset($info['albums']) && $info['albums']? json_decode($info['albums'], true) : [];
  520. $info['albums'] = $albums? get_images_preview($albums) : [];
  521. if(isset($info['category']) && $info['category']){
  522. $info['category_name'] = isset($info['category']['name'])? $info['category']['name'] : '';
  523. $info['category'] = isset($info['category']['id'])? $info['category']['id'] : '';
  524. }
  525. unset($info['qualification_imgs']);
  526. unset($info['other_certificates']);
  527. unset($info['idcard_imgs']);
  528. }
  529. return $info;
  530. }
  531. /**
  532. * @param $userId
  533. * @param $params
  534. * @return bool
  535. */
  536. public function applyConfirm($userId, $params)
  537. {
  538. $id = isset($params['id'])? $params['id'] : 0;
  539. $info = $this->model->where(['id'=> $id,'mark'=>1])->select('id','user_id','name','status','mark')->first();
  540. $status = isset($info['status'])? $info['status'] : 0;
  541. if(empty($info) || $id<=0){
  542. $this->error = 2230;
  543. return false;
  544. }
  545. if($status != 1){
  546. $this->error = 2231;
  547. return false;
  548. }
  549. $status = isset($params['status'])? intval($params['status']) : 0;
  550. $auditRemark = isset($params['audit_remark'])? trim($params['audit_remark']) : '';
  551. if(!in_array($status,[2,3])){
  552. $this->error = 2232;
  553. return false;
  554. }
  555. if($this->model->where(['id'=> $id])->update(['status'=> $status,'audit_remark'=>$auditRemark,'update_time'=>time()])){
  556. $this->error = 1040;
  557. return true;
  558. }else{
  559. $this->error = 1041;
  560. return false;
  561. }
  562. }
  563. /**
  564. * @param $userId
  565. * @param $params
  566. * @return bool
  567. */
  568. public function lock($userId, $params)
  569. {
  570. $id = isset($params['id'])? $params['id'] : 0;
  571. $info = $this->model->where(['id'=> $id,'mark'=>1])->select('id','user_id','name','status','mark')->first();
  572. $status = isset($info['status'])? $info['status'] : 0;
  573. if(empty($info) || $id<=0){
  574. $this->error = 2230;
  575. return false;
  576. }
  577. if($status <= 1){
  578. $this->error = 2234;
  579. return false;
  580. }
  581. $agentInfo = AgentModel::where(['user_id'=> $userId,'mark'=>1])->select(['id','user_id','realname'])->first();
  582. $agentId = isset($agentInfo['id'])? $agentInfo['id'] : 0;
  583. if(empty($agentInfo)){
  584. $this->error = 2024;
  585. return false;
  586. }
  587. $status = isset($params['status'])? intval($params['status']) : 0;
  588. $remark = isset($params['remark'])? trim($params['remark']) : '';
  589. if(!in_array($status,[2,3])){
  590. $this->error = 2232;
  591. return false;
  592. }
  593. if($this->model->where(['id'=> $id])->update(['agent_lock'=>1,'agent_lock_id'=> $agentId,'agent_lock_status'=> $status,'agent_lock_remark'=>$remark,'update_time'=>time()])){
  594. $this->error = 1035;
  595. return true;
  596. }else{
  597. $this->error = 1036;
  598. return false;
  599. }
  600. }
  601. /**
  602. * 删除
  603. * @return array|false
  604. */
  605. public function delete()
  606. {
  607. // 参数
  608. $id = request()->post('id');
  609. if (empty($id)) {
  610. $this->error = 2014;
  611. return false;
  612. }
  613. $this->error = 1002;
  614. $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  615. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  616. }
  617. /**
  618. * @param $userId
  619. * @param $params
  620. * @return bool
  621. */
  622. public function modify($userId, $params)
  623. {
  624. // 用户验证
  625. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  626. ->select(['id','user_id', 'status'])
  627. ->first();
  628. if (!$info) {
  629. $this->error = 2001;
  630. return false;
  631. }
  632. // 使用状态校验
  633. if ($info['status'] != 2) {
  634. $this->error = 2015;
  635. return false;
  636. }
  637. // 密码校验
  638. $data = ['update_time' => time()];
  639. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  640. // 手机号验证
  641. $oldMobile = isset($params['old_mobile']) ? $params['old_mobile'] : '';
  642. if (isset($params['old_mobile']) && $oldMobile) {
  643. // 短信验证码
  644. $code = isset($params['old_code']) ? $params['old_code'] : '';
  645. if (empty($code)) {
  646. $this->error = 2013;
  647. return false;
  648. }
  649. if (!SmsService::make()->check($oldMobile, $code, 'modify')) {
  650. $this->error = 1044;
  651. return false;
  652. }
  653. }
  654. // 手机号验证
  655. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  656. if (isset($params['mobile']) && $mobile) {
  657. $data['mobile'] = $mobile;
  658. $checkInfo = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id','user_id', 'status'])->first();
  659. if ($checkInfo && $checkInfo['user_id'] != $userId) {
  660. // $this->error = 2009;
  661. // return false;
  662. }
  663. // 短信验证码
  664. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  665. if (empty($code)) {
  666. $this->error = 2013;
  667. return false;
  668. }
  669. if (!SmsService::make()->check($mobile, $code, 'modify')) {
  670. $this->error = SmsService::make()->getError();
  671. return false;
  672. }
  673. }
  674. if(isset($params['settle_type']) && in_array($params['settle_type'],[0,1,2,3])){
  675. $data['settle_type'] = intval($params['settle_type']);
  676. }
  677. if(isset($params['trade_status']) && in_array($params['trade_status'],[1,2])){
  678. $data['trade_status'] = intval($params['trade_status']);
  679. }
  680. if (isset($params['pay_password']) && $payPassword) {
  681. $payPassword = get_password($payPassword);
  682. $data['pay_password'] = $payPassword;
  683. }
  684. // 修改数据
  685. RedisService::clear("caches:merch:detail_{$info['id']}");
  686. RedisService::keyDel("caches:merchant:info:temp*");
  687. $this->model->where(['user_id' => $userId])->update($data);
  688. $this->error = 1008;
  689. return true;
  690. }
  691. /**
  692. * 缴纳保证金
  693. * @param $userId 商家用户
  694. * @param $params
  695. * @return array|false
  696. */
  697. public function deposit($userId, $params)
  698. {
  699. $merchId = isset($params['id'])? $params['id'] : 0;
  700. $money = isset($params['money'])? $params['money'] : 0;
  701. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  702. if($money<=0){
  703. $this->error = 2031;
  704. return false;
  705. }
  706. if(!in_array($payType, [10,20])){
  707. $this->error = 2032;
  708. return false;
  709. }
  710. $info = $this->model->where(['id'=> $merchId,'mark'=>1])
  711. ->select(['id','name','mobile','balance','deposit','status'])
  712. ->first();
  713. $deposit = isset($info['deposit'])? $info['deposit'] : 0;
  714. $status = isset($info['status'])? $info['status'] : 0;
  715. if($merchId<=0 || empty($info) || $status != 2){
  716. $this->error = 2015;
  717. return false;
  718. }
  719. // 充值订单
  720. $orderNo = get_order_num('DP');
  721. $data = [
  722. 'source_order_no'=> $orderNo,
  723. 'user_id'=> $userId,
  724. 'merch_id'=> $merchId,
  725. 'type'=> 14,
  726. 'coin_type'=> 1,
  727. 'user_type'=> 2,
  728. 'money'=> $money,
  729. 'balance'=> $deposit,
  730. 'date'=> date('Y-m-d'),
  731. 'create_time'=> time(),
  732. 'update_time'=> time(),
  733. 'remark'=> $payType == 10?'微信支付商家保证金':'支付宝支付商家保证金',
  734. 'status'=> 2,
  735. 'mark'=> 1,
  736. ];
  737. if(!$orderId = AccountLogModel::insertGetId($data)){
  738. $this->error = 2037;
  739. return false;
  740. }
  741. // 支付方式
  742. $order = [
  743. 'order_no'=> $orderNo,
  744. 'type'=> 0,
  745. 'pay_type'=> $payType,
  746. 'pay_money'=> $money,
  747. 'body'=> '缴纳保证金订单支付',
  748. ];
  749. switch($payType){
  750. case 20: // 支付宝
  751. $payment = PaymentService::make()->aliPay($info, $order,'deposit');
  752. if(empty($payment)){
  753. DB::rollBack();
  754. $this->error = PaymentService::make()->getError();
  755. return false;
  756. }
  757. break;
  758. case 10: // 微信支付
  759. $payment = PaymentService::make()->wechatPay($info, $order,'deposit');
  760. if(empty($payment)){
  761. DB::rollBack();
  762. $this->error = PaymentService::make()->getError();
  763. return false;
  764. }
  765. break;
  766. default:
  767. $this->error = 1030;
  768. return false;
  769. }
  770. $this->error = 2038;
  771. return [
  772. 'id'=> $orderId,
  773. 'payment'=> $payment,
  774. 'total'=> $money,
  775. 'order_no'=> $orderNo,
  776. 'pay_type'=> $payType,
  777. ];
  778. }
  779. /**
  780. * 退还保证金处理
  781. * @param $userId 用户
  782. * @param $params
  783. * @return bool
  784. */
  785. public function rebackDeposit($userId, $params)
  786. {
  787. $merchId = isset($params['id'])? $params['id'] : 0;
  788. if($merchId<=0){
  789. $this->error = 2039;
  790. return false;
  791. }
  792. $info = $this->model->where(['id'=> $merchId,'mark'=>1])
  793. ->select(['id','name','mobile','balance','deposit','status'])
  794. ->first();
  795. $deposit = isset($info['deposit'])? floatval($info['deposit']) : 0;
  796. $balance = isset($info['balance'])? floatval($info['balance']) : 0;
  797. $status = isset($info['status'])? $info['status'] : 0;
  798. if($merchId<=0 || empty($info) || $status != 2){
  799. $this->error = 2015;
  800. return false;
  801. }
  802. if($deposit<=0){
  803. $this->error = 2041;
  804. return false;
  805. }
  806. // 时间限制
  807. $depositTime = ConfigService::make()->getConfigByCode('shop_deposit_time');
  808. $depositTime = $depositTime>=0? $depositTime : 0;
  809. $paymentTime = AccountLogModel::where(['merch_id'=> $merchId,'type'=>14,'coin_type'=>1,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('create_time');
  810. if($depositTime>0 && $paymentTime>0 && $paymentTime + ($depositTime*86400)> time()){
  811. $this->error = lang(2040,['time'=> $depositTime]);
  812. return false;
  813. }
  814. DB::beginTransaction();
  815. // 余额
  816. $updateData = ['balance' => DB::raw("balance + {$deposit}"),'deposit'=>0.00, 'update_time' => time()];
  817. if(!$this->model->where(['id'=> $merchId])->update($updateData)){
  818. DB::rollBack();
  819. $this->error = 2042;
  820. return false;
  821. }
  822. // 退还记录
  823. $data = [
  824. 'source_order_no'=> '',
  825. 'user_id'=> $userId,
  826. 'merch_id'=> $merchId,
  827. 'type'=> 14,
  828. 'coin_type'=> 4,
  829. 'user_type'=> 2,
  830. 'money'=> -$deposit,
  831. 'balance'=> $balance,
  832. 'date'=> date('Y-m-d'),
  833. 'create_time'=> time(),
  834. 'update_time'=> time(),
  835. 'remark'=> '商家保证金退还',
  836. 'status'=> 1,
  837. 'mark'=> 1,
  838. ];
  839. if(!$id = AccountLogModel::insertGetId($data)){
  840. DB::rollBack();
  841. $this->error = 2042;
  842. return false;
  843. }
  844. DB::commit();
  845. $params = [
  846. 'title' => '商家保证金退还到账通知',
  847. 'body' => "您的商家保证金申请退还已成功,请点击查看详情",
  848. 'type' => 3, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  849. 'content' => [
  850. 'pay_time' => ['name' => '退还时间', 'text' => date('Y-m-d H:i:s')],
  851. 'money' => ['name' => '金额', 'text' => $deposit],
  852. 'balance' => ['name' => '当前余额', 'text' => moneyFormat($balance + $deposit,2)],
  853. 'status' => ['name' => '状态', 'text' => '已到账'],
  854. ],
  855. 'click_type' => 'payload',
  856. 'url' => '/pages/account/index?type=3',
  857. ];
  858. PushService::make()->pushMessageByUser($userId, $params, 0);
  859. $this->error = 2043;
  860. return true;
  861. }
  862. }