MerchantService.php 33 KB

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