114 lines
4.9 KiB
PHP
114 lines
4.9 KiB
PHP
<?php
|
||
|
||
namespace app\controller\api\store\order;
|
||
|
||
use app\common\repositories\store\order\StoreOrderCreateRepository;
|
||
use app\common\repositories\store\order\StoreOrderRepository;
|
||
use app\common\repositories\system\merchant\MerchantRepository;
|
||
use crmeb\basic\BaseController;
|
||
use crmeb\services\LockService;
|
||
|
||
class OnlinePay extends BaseController{
|
||
/**
|
||
* Common: 在线买单 - 搜索商户
|
||
* Author: wu-hui
|
||
* Time: 2024/01/12 11:53
|
||
* @return mixed
|
||
*/
|
||
public function searchMer(){
|
||
$search = $this->request->params(['search_text','mer_id','lat','lng']);
|
||
// if(empty($search['search_text'])) return app('json')->fail('请输入搜索内容!');
|
||
// 内容查询
|
||
$list = app()->make(MerchantRepository::class)
|
||
->getSearch([])
|
||
->where('is_del',0)
|
||
->where('mer_state', 1)
|
||
->where('status', 1)
|
||
->where('is_online_payment', 1)
|
||
->when((isset($search['lng']) && $search['lng'] > 0) && (isset($search['lat']) && $search['lat'] > 0),function($query) use ($search){
|
||
$distance = "ROUND((ST_DISTANCE(point(`long`, lat), point({$search['lng']}, {$search['lat']})) * 111195),2)";
|
||
$query->field("mer_id,mer_name,mer_address,mer_avatar,{$distance} as distance")
|
||
->orderRaw("{$distance} ASC");
|
||
},function($query){
|
||
$query->field('mer_id,mer_name,mer_address,mer_avatar');
|
||
})
|
||
->where(function($query) use ($search){
|
||
$query->where('mer_name','like',"%{$search['search_text']}%")
|
||
->whereOr('mer_address','like',"%{$search['search_text']}%");
|
||
})
|
||
->when((int)$search['mer_id'] > 0,function($query) use ($search){
|
||
$query->where('mer_id',$search['mer_id']);
|
||
})
|
||
->order('create_time desc,mer_id desc')
|
||
->limit(20)
|
||
->select()->each(function($item){
|
||
$item->distance = $item->distance ?? 0;
|
||
$item->distance_text = '';
|
||
if($item->distance > 0){
|
||
if ($item->distance < 100) $item->distance_text = '100m以内';
|
||
else $item->distance_text = sprintf("%.2f",$item->distance / 1000).'km';
|
||
}
|
||
|
||
return $item;
|
||
});;
|
||
|
||
return app('json')->success($list);
|
||
}
|
||
/**
|
||
* Common: 在线买单 - 订单生成及发起支付
|
||
* Author: wu-hui
|
||
* Time: 2024/01/12 16:07
|
||
* @param StoreOrderCreateRepository $orderCreateRepository
|
||
* @return mixed
|
||
*/
|
||
public function createOrder(StoreOrderCreateRepository $orderCreateRepository){
|
||
// 参数获取
|
||
$payInfo = $this->request->params(['money','pay_type', 'mer_id', 'return_url']);
|
||
if (!in_array($payInfo['pay_type'], StoreOrderRepository::PAY_TYPE, true)) return app('json')->fail('请选择正确的支付方式');
|
||
if ((float)$payInfo['money'] <= 0) return app('json')->fail('支付金额必须大于0!');
|
||
if ((int)$payInfo['mer_id'] <= 0) return app('json')->fail('请选择商户!');
|
||
// 判断:当前商户是否支持买单 商户类别:0=普通商户,1=酒道馆,2=供应商
|
||
$isOnlinePayment = (int)app()->make(MerchantRepository::class)->getSearch([])
|
||
->where('mer_id', (int)$payInfo['mer_id'])
|
||
->value('is_online_payment');
|
||
if($isOnlinePayment != 1) return app('json')->fail('该商户不支持当前操作!');
|
||
// 发起支付
|
||
$groupOrder = app()
|
||
->make(LockService::class)
|
||
->exec('online_order.create',function() use ($payInfo,$orderCreateRepository){
|
||
$payType = array_search($payInfo['pay_type'],StoreOrderRepository::PAY_TYPE);
|
||
|
||
return $orderCreateRepository->onlinePayment($payType,$payInfo,$this->request->userInfo());
|
||
});
|
||
|
||
if ($groupOrder['pay_price'] == 0) {
|
||
app()->make(StoreOrderRepository::class)->paySuccess($groupOrder);
|
||
return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
|
||
}
|
||
try {
|
||
|
||
return app()->make(StoreOrderRepository::class)->pay($payInfo['pay_type'], $this->request->userInfo(), $groupOrder, $payInfo['return_url'], $this->request->isApp());
|
||
} catch (\Exception $e) {
|
||
|
||
return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
|
||
}
|
||
}
|
||
/**
|
||
* Common: 获取买单记录
|
||
* Author: wu-hui
|
||
* Time: 2024/01/15 9:23
|
||
* @return mixed
|
||
*/
|
||
public function getRecord(){
|
||
$params = $this->request->params(['uid','mer_id']);
|
||
$params['uid'] = $this->request->uid();
|
||
[$page, $limit] = $this->getPage();
|
||
|
||
$data = app()->make(StoreOrderRepository::class)->getOnlineOrderList((array)$params,(int)$page,(int)$limit);
|
||
|
||
return app('json')->success($data);
|
||
}
|
||
|
||
|
||
}
|