文件
xionggeshop/application/admin/controller/shopro_wechat_trade/Sync.php
可乐加冰 35762c67e6 ```
feat(shopro_wechat_trade): 增加微信发货/确认收货同步页面功能

增加统计信息显示、筛选搜索功能和详细信息弹窗,优化用户体验

- 添加buildQuery方法用于构建查询条件,支持状态、同步类型和订单号筛选
- 添加getStats方法获取各状态统计数据并在页面显示统计栏
- 在index页面增加类型筛选、订单号搜索、重置和仅看失败按钮
- 实现detail方法用于获取同步记录详情
- 增加详细的CSS样式和前端交互逻辑
- 添加防止重复重试成功记录的验证
- 格式化JSON数据显示和错误信息展示
```
2026-10-08 15:44:59 +08:00

96 行
3.0 KiB
PHP

<?php
namespace app\admin\controller\shopro_wechat_trade;
use addons\shopro_wechat_trade\library\WxShippingService;
use app\common\controller\Backend;
use think\Db;
class Sync extends Backend
{
protected function buildQuery($status = '', $syncType = '', $orderSn = '')
{
$query = Db::name('shopro_wechat_trade_sync');
if ($status !== '') {
$query->where('status', $status);
}
if ($syncType !== '') {
$query->where('sync_type', $syncType);
}
if ($orderSn !== '') {
$query->where('order_sn', 'like', '%' . $orderSn . '%');
}
return $query;
}
protected function getStats()
{
$rows = Db::name('shopro_wechat_trade_sync')
->field('status, COUNT(*) AS cnt')
->group('status')
->select();
$stats = ['success' => 0, 'pending' => 0, 'failed' => 0, 'total' => 0];
foreach ($rows as $row) {
$key = $row['status'] ?? '';
if (isset($stats[$key])) {
$stats[$key] = intval($row['cnt']);
}
$stats['total'] += intval($row['cnt']);
}
return $stats;
}
public function index()
{
if ($this->request->isAjax()) {
$page = max(1, intval($this->request->get('page', 1)));
$limit = max(1, min(100, intval($this->request->get('limit', 20))));
$status = trim($this->request->get('status', ''));
$syncType = trim($this->request->get('sync_type', ''));
$orderSn = trim($this->request->get('order_sn', ''));
$query = $this->buildQuery($status, $syncType, $orderSn)->order('id', 'desc');
$total = (clone $query)->count();
$list = $query->page($page, $limit)->select();
return json(['total' => $total, 'rows' => $list]);
}
$this->view->assign('stats', $this->getStats());
return $this->view->fetch();
}
public function detail()
{
$id = intval($this->request->get('id'));
$row = Db::name('shopro_wechat_trade_sync')->where('id', $id)->find();
if (!$row) {
$this->error('记录不存在');
}
$this->success('', null, $row);
}
public function retry()
{
$id = intval($this->request->post('id'));
$row = Db::name('shopro_wechat_trade_sync')->where('id', $id)->find();
if (!$row) {
$this->error('记录不存在');
}
if ($row['status'] === 'success') {
$this->error('该记录已同步成功,无需重试');
}
try {
$service = new WxShippingService();
if ($row['sync_type'] === 'confirm') {
$service->syncOrderConfirm($row['order_id']);
} else {
$service->syncOrderShipping($row['order_id'], $row['order_item_id']);
}
$this->success('重试成功');
} catch (\Exception $e) {
$this->error('重试失败:' . $e->getMessage());
}
}
}