<?php
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\model\Staff as StaffModel;
|
|
use app\model\Order as OrderModel;
|
|
use app\model\Product;
|
|
use app\service\CardService;
|
|
|
|
class Staff extends BaseController
|
|
{
|
|
protected $middleware = [
|
|
'StaffAuth' => ['except' => ['login']],
|
|
];
|
|
|
|
public function login()
|
|
{
|
|
$username = $this->request->post('username', '');
|
|
$password = $this->request->post('password', '');
|
|
|
|
$staff = StaffModel::where('username', $username)
|
|
->where('status', 1)
|
|
->find();
|
|
|
|
if (!$staff || !password_verify($password, $staff->password)) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '账号或密码错误']);
|
|
}
|
|
|
|
$secret = config('app.app_secret', 'bar_order_secret_key_2026');
|
|
$expire = time() + 86400;
|
|
$sign = hash_hmac('sha256', $staff->id . '|' . $expire, $secret);
|
|
$token = base64_encode($staff->id . '|' . $expire . '|' . $sign);
|
|
|
|
$staff->last_login = date('Y-m-d H:i:s');
|
|
$staff->save();
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'data' => [
|
|
'token' => $token,
|
|
'nickname' => $staff->nickname,
|
|
'staffId' => $staff->id,
|
|
],
|
|
'msg' => '登录成功',
|
|
]);
|
|
}
|
|
|
|
public function orders()
|
|
{
|
|
$status = $this->request->get('status', 0);
|
|
$orders = OrderModel::with('items')
|
|
->where('status', intval($status))
|
|
->order('remind_count', 'desc')
|
|
->order('submitted_at', 'asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
$list = array_map(function ($o) {
|
|
return [
|
|
'id' => $o['id'],
|
|
'orderNo' => $o['order_no'],
|
|
'cardNo' => $o['card_no'],
|
|
'status' => $o['status'],
|
|
'note' => $o['note'] ?? '',
|
|
'remindCount' => $o['remind_count'] ?? 0,
|
|
'submittedAt' => date('H:i', strtotime($o['submitted_at'])),
|
|
'items' => array_map(function ($i) {
|
|
return [
|
|
'name' => $i['product_name'],
|
|
'emoji' => $i['emoji'] ?? '',
|
|
'alc' => 0.0,
|
|
'qty' => $i['quantity'],
|
|
];
|
|
}, $o['items'] ?? []),
|
|
];
|
|
}, $orders);
|
|
|
|
return json(['code' => 0, 'data' => $list, 'msg' => 'ok']);
|
|
}
|
|
|
|
public function detail()
|
|
{
|
|
$id = $this->request->get('id', 0);
|
|
$order = OrderModel::with('items')->find(intval($id));
|
|
|
|
if (!$order) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '订单不存在']);
|
|
}
|
|
|
|
$items = [];
|
|
foreach ($order->items as $item) {
|
|
$product = Product::find($item->product_id);
|
|
$items[] = [
|
|
'name' => $item->product_name,
|
|
'emoji' => $item->emoji ?? '',
|
|
'alc' => $product ? floatval($product->alc) : 0.0,
|
|
'qty' => $item->quantity,
|
|
'recipe' => $product ? $product->recipe : '',
|
|
];
|
|
}
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'data' => [
|
|
'id' => $order->id,
|
|
'orderNo' => $order->order_no,
|
|
'cardNo' => $order->card_no,
|
|
'status' => $order->status,
|
|
'note' => $order->note ?? '',
|
|
'remindCount' => $order->remind_count ?? 0,
|
|
'submittedAt' => date('H:i', strtotime($order->submitted_at)),
|
|
'items' => $items,
|
|
],
|
|
'msg' => 'ok',
|
|
]);
|
|
}
|
|
|
|
// BUG-02: 增加状态机校验 — confirm仅允许 status=0
|
|
public function confirm()
|
|
{
|
|
$id = $this->request->param('id', 0);
|
|
$order = OrderModel::find(intval($id));
|
|
if (!$order) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '订单不存在']);
|
|
}
|
|
if ($order->status !== 0) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '仅新订单可接单']);
|
|
}
|
|
|
|
$order->status = 1;
|
|
$order->remind_count = 0;
|
|
$order->staff_id = $this->request->staffId ?? null;
|
|
$order->handled_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
|
|
return json(['code' => 0, 'data' => null, 'msg' => '已接单']);
|
|
}
|
|
|
|
// BUG-02: 增加状态机校验 — done仅允许 status=1
|
|
public function done(CardService $cardService)
|
|
{
|
|
$id = $this->request->param('id', 0);
|
|
$order = OrderModel::find(intval($id));
|
|
if (!$order) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '订单不存在']);
|
|
}
|
|
if ($order->status !== 1) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '仅进行中订单可结单']);
|
|
}
|
|
|
|
$order->status = 2;
|
|
$order->save();
|
|
|
|
$released = $cardService->release($order->card_no);
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'data' => ['released' => $released],
|
|
'msg' => '已结单',
|
|
]);
|
|
}
|
|
|
|
// BUG-02: 增加状态机校验 — cancel仅允许 status=0
|
|
public function cancel()
|
|
{
|
|
$id = $this->request->param('id', 0);
|
|
$order = OrderModel::find(intval($id));
|
|
if (!$order) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '订单不存在']);
|
|
}
|
|
if ($order->status !== 0) {
|
|
return json(['code' => -1, 'data' => null, 'msg' => '仅新订单可取消']);
|
|
}
|
|
|
|
$order->status = 3;
|
|
$order->save();
|
|
|
|
return json(['code' => 0, 'data' => null, 'msg' => '已取消']);
|
|
}
|
|
}
|