define(['jquery', 'bootstrap', 'backend'], function ($, undefined, Backend) { var Controller = { index: function() { // 添加日志 function addLog(message, type) { type = type || 'info'; var time = new Date().toLocaleTimeString(); var className = type === 'error' ? 'text-danger' : (type === 'success' ? 'text-success' : 'text-info'); var logHtml = '

[' + time + '] ' + message + '

'; $('#log-area').prepend(logHtml); } // 测试连接 $('#btn-test-connection').on('click', function() { var $btn = $(this); $btn.prop('disabled', true).html(' 测试中...'); addLog('开始测试连接...', 'info'); $.ajax({ url: Fast.api.fixurl('antis/yonyou_bip/test_connection'), type: 'GET', dataType: 'json', success: function(res) { if (res.code == 1) { addLog('✓ 连接成功!Token长度: ' + res.data.token_length, 'success'); addLog('Token预览: ' + res.data.token_preview, 'info'); layer.msg('连接成功!', {icon: 1}); } else { addLog('✗ 连接失败: ' + res.msg, 'error'); layer.msg('连接失败: ' + res.msg, {icon: 2}); } }, error: function(xhr) { var errorMsg = '网络错误'; try { if (xhr.responseJSON && xhr.responseJSON.msg) { errorMsg = xhr.responseJSON.msg; } else if (xhr.responseText) { var response = JSON.parse(xhr.responseText); errorMsg = response.msg || errorMsg; } } catch(e) { errorMsg = '请求失败,请检查网络连接'; } addLog('✗ 连接失败: ' + errorMsg, 'error'); layer.msg('连接失败: ' + errorMsg, {icon: 2}); }, complete: function() { $btn.prop('disabled', false).html(' 测试连接'); } }); }); // 同步基础数据 $('#btn-sync-base-data').on('click', function() { if (!confirm('确定要同步所有基础数据吗?这可能需要一些时间。')) { return; } var $btn = $(this); $btn.prop('disabled', true).html(' 同步中...'); addLog('开始同步基础数据...', 'info'); $.ajax({ url: Fast.api.fixurl('antis/yonyou_bip/sync_all_base_data'), type: 'POST', dataType: 'json', success: function(res) { if (res.code == 1) { addLog('✓ 同步完成!成功: ' + res.data.total_success + ' 条,失败: ' + res.data.total_fail, 'success'); layer.msg(res.msg, {icon: 1}); } else { addLog('✗ 同步失败: ' + res.msg, 'error'); layer.msg('同步失败: ' + res.msg, {icon: 2}); } }, error: function(xhr) { var errorMsg = '网络错误'; try { if (xhr.responseJSON && xhr.responseJSON.msg) { errorMsg = xhr.responseJSON.msg; } else if (xhr.responseText) { var response = JSON.parse(xhr.responseText); errorMsg = response.msg || errorMsg; } } catch(e) { errorMsg = '请求失败,请检查网络连接'; } addLog('✗ 同步失败: ' + errorMsg, 'error'); layer.msg('同步失败: ' + errorMsg, {icon: 2}); }, complete: function() { $btn.prop('disabled', false).html(' 同步基础数据'); } }); }); // 同步单个订单 $(document).on('click', '.btn-sync-single', function() { var orderId = $(this).data('id'); var $btn = $(this); if (!confirm('确定要同步此订单到用友BIP吗?')) { return; } $btn.prop('disabled', true).html(''); addLog('开始同步订单 ID: ' + orderId, 'info'); $.ajax({ url: Fast.api.fixurl('antis/yonyou_bip/sync_sales_order'), type: 'POST', dataType: 'json', data: {order_id: orderId}, success: function(res) { if (res.code == 1) { addLog('✓ 订单同步成功!订单ID: ' + orderId, 'success'); layer.msg('同步成功', {icon: 1}); setTimeout(function() { location.reload(); }, 1500); } else { addLog('✗ 订单同步失败: ' + res.msg, 'error'); layer.msg('同步失败: ' + res.msg, {icon: 2}); } }, error: function(xhr) { var errorMsg = '网络错误'; try { if (xhr.responseJSON && xhr.responseJSON.msg) { errorMsg = xhr.responseJSON.msg; } else if (xhr.responseText) { var response = JSON.parse(xhr.responseText); errorMsg = response.msg || errorMsg; } } catch(e) { errorMsg = '请求失败,请检查网络连接'; } addLog('✗ 订单同步失败: ' + errorMsg, 'error'); layer.msg('同步失败: ' + errorMsg, {icon: 2}); }, complete: function() { $btn.prop('disabled', false).html(' 同步'); } }); }); // 查看同步状态 $(document).on('click', '.btn-check-status', function() { var orderId = $(this).data('id'); $.ajax({ url: Fast.api.fixurl('antis/yonyou_bip/get_sync_status'), type: 'GET', dataType: 'json', data: {order_id: orderId}, success: function(res) { if (res.code == 1) { var status = res.data.sync_status == 1 ? '已同步' : '未同步'; var msg = '订单编号: ' + res.data.order_code + '
' + '同步状态: ' + status; if (res.data.bip_order_id) { msg += '
用友BIP订单ID: ' + res.data.bip_order_id; } if (res.data.sync_time) { msg += '
同步时间: ' + res.data.sync_time; } layer.msg(msg, {time: 3000}); } else { layer.msg('查询失败: ' + res.msg, {icon: 2}); } } }); }); // 全选/取消全选 $('#checkAll').on('change', function() { $('.order-checkbox').prop('checked', $(this).prop('checked')); }); // 同步选中订单 $('#btn-sync-selected').on('click', function() { var selectedIds = []; $('.order-checkbox:checked').each(function() { selectedIds.push($(this).val()); }); if (selectedIds.length == 0) { layer.msg('请选择要同步的订单', {icon: 0}); return; } if (!confirm('确定要同步选中的 ' + selectedIds.length + ' 个订单吗?')) { return; } var $btn = $(this); $btn.prop('disabled', true).html(' 同步中...'); addLog('开始批量同步订单,数量: ' + selectedIds.length, 'info'); $.ajax({ url: Fast.api.fixurl('antis/yonyou_bip/batch_sync_sales_order'), type: 'POST', dataType: 'json', data: {order_ids: selectedIds}, success: function(res) { if (res.code == 1) { addLog('✓ 批量同步完成!成功: ' + res.data.success + ' 条,失败: ' + res.data.fail, 'success'); if (res.data.errors && res.data.errors.length > 0) { res.data.errors.forEach(function(error) { addLog(' - ' + error, 'error'); }); } layer.msg(res.msg, {icon: 1, time: 3000}); setTimeout(function() { location.reload(); }, 2000); } else { addLog('✗ 批量同步失败: ' + res.msg, 'error'); layer.msg('同步失败: ' + res.msg, {icon: 2}); } }, error: function(xhr) { var errorMsg = '网络错误'; try { if (xhr.responseJSON && xhr.responseJSON.msg) { errorMsg = xhr.responseJSON.msg; } else if (xhr.responseText) { var response = JSON.parse(xhr.responseText); errorMsg = response.msg || errorMsg; } } catch(e) { errorMsg = '请求失败,请检查网络连接'; } addLog('✗ 批量同步失败: ' + errorMsg, 'error'); layer.msg('同步失败: ' + errorMsg, {icon: 2}); }, complete: function() { $btn.prop('disabled', false).html(' 同步选中订单'); } }); }); // 刷新列表 $('#btn-refresh').on('click', function() { location.reload(); }); } }; return Controller; });