文件
xionggeshop/application/admin/controller/shopro_wechat_trade/Sync.php
可乐加冰 bf1f525545 ```
feat(shopro_wechat_trade): 微信同步页面优化及数据格式化功能

- 新增formatListItem方法对同步记录数据进行格式化处理,
  包括同步类型、状态、发货模式和错误信息的中文映射
- 实现统计项点击筛选功能,添加高亮状态指示器
- 重构前端JavaScript代码,使用预格式化数据字段提升显示性能
- 添加详情弹窗功能,支持查看完整的同步请求响应内容
- 优化重试按钮交互,增加确认提示防止误操作
- 更新页面布局样式,改善用户界面体验
```
2026-10-08 15:49:33 +08:00

142 行
4.7 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;
}
protected function formatListItem(array $row)
{
$typeMap = [
'shipping' => '发货同步',
'confirm' => '确认收货同步',
];
$row['sync_type_text'] = $typeMap[$row['sync_type'] ?? ''] ?? ($row['sync_type'] ?? '-');
$status = $row['status'] ?? '';
$retryCount = intval($row['retry_count'] ?? 0);
if ($status === 'success') {
$row['status_text'] = '成功';
} elseif ($status === 'failed') {
$row['status_text'] = '失败';
} elseif ($status === 'pending') {
$row['status_text'] = $retryCount > 0 ? '待重试' : '处理中';
} else {
$row['status_text'] = $status ?: '-';
}
if (($row['sync_type'] ?? '') !== 'shipping') {
$row['delivery_mode_text'] = '-';
} else {
$modeMap = [1 => '统一发货', 2 => '分拆发货'];
$row['delivery_mode_text'] = $modeMap[intval($row['delivery_mode'] ?? 0)] ?? ('模式' . $row['delivery_mode']);
}
if ($status === 'success') {
$row['error_text'] = '-';
} else {
$parts = [];
if ($row['errcode'] !== null && $row['errcode'] !== '') {
$parts[] = '[' . $row['errcode'] . ']';
}
if (!empty($row['errmsg'])) {
$parts[] = $row['errmsg'];
}
$row['error_text'] = $parts ? implode(' ', $parts) : '-';
}
return $row;
}
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();
foreach ($list as $key => $row) {
$list[$key] = $this->formatListItem($row);
}
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, $this->formatListItem($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());
}
}
}