test.js
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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 = '<p class="' + className + '">[' + time + '] ' + message + '</p>';
$('#log-area').prepend(logHtml);
}
// 测试连接
$('#btn-test-connection').on('click', function() {
var $btn = $(this);
$btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 测试中...');
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('<i class="fa fa-plug"></i> 测试连接');
}
});
});
// 同步基础数据
$('#btn-sync-base-data').on('click', function() {
if (!confirm('确定要同步所有基础数据吗?这可能需要一些时间。')) {
return;
}
var $btn = $(this);
$btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 同步中...');
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('<i class="fa fa-refresh"></i> 同步基础数据');
}
});
});
// 同步单个订单
$(document).on('click', '.btn-sync-single', function() {
var orderId = $(this).data('id');
var $btn = $(this);
if (!confirm('确定要同步此订单到用友BIP吗?')) {
return;
}
$btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i>');
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('<i class="fa fa-cloud-upload"></i> 同步');
}
});
});
// 查看同步状态
$(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 + '<br>' +
'同步状态: ' + status;
if (res.data.bip_order_id) {
msg += '<br>用友BIP订单ID: ' + res.data.bip_order_id;
}
if (res.data.sync_time) {
msg += '<br>同步时间: ' + 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('<i class="fa fa-spinner fa-spin"></i> 同步中...');
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('<i class="fa fa-cloud-upload"></i> 同步选中订单');
}
});
});
// 刷新列表
$('#btn-refresh').on('click', function() {
location.reload();
});
}
};
return Controller;
});