师傅接单

This commit is contained in:
gcd 2025-04-01 20:56:11 +08:00
parent 7ace9cc65c
commit 11ec750e49
7 changed files with 169 additions and 1 deletions

View File

@ -2,7 +2,11 @@
namespace app\services; namespace app\services;
use app\admin\model\Order;
use app\admin\model\OrderDispatch; use app\admin\model\OrderDispatch;
use app\api\library\ApiException;
use think\Db;
use think\Hook;
class OrderDispatchService extends BaseService class OrderDispatchService extends BaseService
{ {
@ -20,4 +24,82 @@ class OrderDispatchService extends BaseService
->order('id desc') ->order('id desc')
->paginate($pageSize); ->paginate($pageSize);
} }
/**
* 师傅接单/拒接
* @param int $workerId 师傅id
* @param int $orderDispatchId 派单id
* @param string $type 类型accept=接单,reject=拒接
* @return true
*/
public function orderConfirm(int $workerId, int $orderDispatchId, string $type)
{
$orderDispatch = $this->getOrderDispatchModel()
->where('worker_id', $workerId)
->where('id', $orderDispatchId)
->find();
if (!$orderDispatch) {
$this->apiError('订单不存在');
} }
if ($orderDispatch->status !== OrderDispatch::STATUS_TOGET) {
$this->apiError('该订单已被接单');
}
//接单或拒接
$orderDispatchStatus = $type == 'accept' ? OrderDispatch::STATUS_GOTIT : OrderDispatch::STATUS_REFUSED;
Db::startTrans();
try {
//接单
$orderDispatch->status = $orderDispatchStatus;
$orderDispatch->save();
$orderDispatchChangeParams = [
'dispatch' => $orderDispatch,
'remark' => $type == 'accept' ? '师傅接单' : '师傅拒接',
];
Hook::listen('order_dispatch_change', $orderDispatchChangeParams);
//拒接,更新订单状态
if ($type == 'reject') {
$order = $this->getOrderModel()->find($orderDispatch->order_id);
$order->status = Order::STATUS_DISPATCHING;
$order->save();
$orderChangeParams['order'] = $order;
$orderChangeParams['role'] = 2;
$orderChangeParams['auth'] = $this->getWorkerModel()->find($orderDispatch->worker_id);
$orderChangeParams['remark'] = '任务被师傅拒接[OrderDispatchId' . $orderDispatch->id . '],订单状态回退';
Hook::listen('order_change', $orderChangeParams);
}
Db::commit();
} catch (ApiException $e) {
Db::rollback();
$this->apiError($e->getMessage());
} catch (\Exception $e) {
Db::rollback();
$this->apiError('操作失败', $e, [
'msg' => '师傅接单或拒接操作失败',
'workerId' => $workerId,
'orderDispatchId' => $orderDispatchId,
'type' => $type,
]);
}
return true;
}
}

View File

@ -2,7 +2,8 @@
namespace app\services; namespace app\services;
use app\admin\model\Order;
class OrderService extends BaseService class OrderService extends BaseService
{ {
} }

View File

@ -9,6 +9,11 @@ use EasyWeChat\MiniProgram\Application;
class WorkerService extends BaseService class WorkerService extends BaseService
{ {
/**
* 绑定手机号
* @param string $code
* @param string $vendorToken
*/
public function bindPhoneNumber(string $code, string $vendorToken) public function bindPhoneNumber(string $code, string $vendorToken)
{ {
$phone = $this->getPhoneNumber($code); $phone = $this->getPhoneNumber($code);
@ -33,6 +38,11 @@ class WorkerService extends BaseService
return $worker->id; return $worker->id;
} }
/**
* 微信登录
* @param string $code
* @return array
*/
public function login(string $code) public function login(string $code)
{ {
$app = $this->getMiniProgramApp(); $app = $this->getMiniProgramApp();
@ -75,6 +85,11 @@ class WorkerService extends BaseService
return Factory::miniProgram($config); return Factory::miniProgram($config);
} }
/**
* 解密微信手机号
* @param string $code
* @return mixed
*/
public function getPhoneNumber(string $code) public function getPhoneNumber(string $code)
{ {
//getPhoneNumber 方法通过魔术方法 __call 获取 //getPhoneNumber 方法通过魔术方法 __call 获取
@ -91,6 +106,10 @@ class WorkerService extends BaseService
return $phoneInfo['phone_info']['phoneNumber']; return $phoneInfo['phone_info']['phoneNumber'];
} }
/**
* 通过手机号查询师傅信息
* @param string $phone
*/
private function getByTel(string $phone) private function getByTel(string $phone)
{ {
return $this->getWorkerModel()->where('tel', $phone)->find(); return $this->getWorkerModel()->where('tel', $phone)->find();

View File

@ -31,6 +31,11 @@ class WorkerVendorService extends BaseService
return $vendor->id; return $vendor->id;
} }
/**
* 通过师傅id和平台查询师傅账号表信息
* @param int $workerId
* @param string $platform
*/
public function getByWorkerIdAndPlatform(int $workerId, string $platform) public function getByWorkerIdAndPlatform(int $workerId, string $platform)
{ {
return $this->getWorkerVendorModel()->where(['worker_id' => $workerId, 'platform' => $platform])->find(); return $this->getWorkerVendorModel()->where(['worker_id' => $workerId, 'platform' => $platform])->find();

View File

@ -8,9 +8,29 @@ class OrderDispatch extends WorkerApi
{ {
protected $noNeedLogin = []; protected $noNeedLogin = [];
/**
* 待接单列表
* @return void
*/
public function index() public function index()
{ {
$res = $this->getOrderDispatchService()->dispatchList($this->user['id'], $this->request->request('page_size', 20)); $res = $this->getOrderDispatchService()->dispatchList($this->user['id'], $this->request->request('page_size', 20));
$this->success('获取成功', $res); $this->success('获取成功', $res);
} }
/**
* 接单/拒接
* @return void
*/
public function orderConfirm()
{
$params = $this->request->request();
$validate = $this->validate($params, \app\worker\validate\OrderDispatch::class . '.orderConfirm');
if ($validate !== true) {
$this->error($validate);
}
$res = $this->getOrderDispatchService()->orderConfirm($this->user['id'], $params['order_dispatch_id'], $params['type']);
$this->success('操作成功', $res);
}
} }

View File

@ -8,6 +8,10 @@ class Worker extends WorkerApi
{ {
protected $noNeedLogin = ['login', 'bindPhoneNumber', 'guestLogin']; protected $noNeedLogin = ['login', 'bindPhoneNumber', 'guestLogin'];
/**
* 微信登录
* @return void
*/
public function login() public function login()
{ {
$params = $this->request->request(); $params = $this->request->request();
@ -27,6 +31,10 @@ class Worker extends WorkerApi
$this->error('请绑定手机号', ['vendor_token' => $this->getTokenByUserId($workerVendor['id'])]); $this->error('请绑定手机号', ['vendor_token' => $this->getTokenByUserId($workerVendor['id'])]);
} }
/**
* 绑定手机号
* @return void
*/
public function bindPhoneNumber() public function bindPhoneNumber()
{ {
$params = $this->request->request(); $params = $this->request->request();
@ -41,18 +49,30 @@ class Worker extends WorkerApi
$this->success('绑定成功', $this->user); $this->success('绑定成功', $this->user);
} }
/**
* 游客登录
* @return void
*/
public function guestLogin() public function guestLogin()
{ {
$this->workerLogin(9); $this->workerLogin(9);
$this->success('登录成功', $this->user); $this->success('登录成功', $this->user);
} }
/**
* 退出登录
* @return void
*/
public function logout() public function logout()
{ {
$this->workerLogout(); $this->workerLogout();
$this->success('操作成功'); $this->success('操作成功');
} }
/**
* 获取当前登录师傅信息
* @return void
*/
public function show() public function show()
{ {
$this->success('操作成功', $this->user); $this->success('操作成功', $this->user);

View File

@ -0,0 +1,21 @@
<?php
namespace app\worker\validate;
use think\Validate;
class OrderDispatch extends Validate
{
protected $rule = [
'type|确认类型' => 'require|in:accept,reject',
'order_dispatch_id|订单派单id' => 'require|number',
];
protected $message = [
];
protected $scene = [
'orderConfirm' => ['type', 'order_dispatch_id'],
];
}