index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <script setup lang="ts">
  2. import { ref, reactive, onMounted } from "vue";
  3. import { ElMessage, ElMessageBox } from "element-plus";
  4. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  5. import Refresh from "~icons/ep/refresh";
  6. import Link from "~icons/ep/link";
  7. import Remove from "~icons/ep/remove";
  8. import Search from "~icons/ep/search";
  9. import {
  10. syncErpShops,
  11. getErpShopList,
  12. bindErpShop,
  13. unbindErpShop,
  14. autoBindErpShop,
  15. unbindAllErpShop
  16. } from "@/api/erpShop";
  17. import { getDeviceList } from "@/api/device";
  18. defineOptions({
  19. name: "ErpShopList"
  20. });
  21. const loading = ref(false);
  22. const syncing = ref(false);
  23. const autoBinding = ref(false);
  24. const unbindingAll = ref(false);
  25. const form = reactive({
  26. keyword: "",
  27. boundStatus: undefined as number | undefined
  28. });
  29. const dataList = ref<any[]>([]);
  30. const total = ref(0);
  31. const pagination = reactive({ currentPage: 1, pageSize: 10 });
  32. const deviceOptions = ref<any[]>([]);
  33. const bindDialogVisible = ref(false);
  34. const currentErpShop = ref<any>(null);
  35. const selectedDeviceId = ref<number | null>(null);
  36. const bindLoading = ref(false);
  37. async function fetchList() {
  38. loading.value = true;
  39. try {
  40. const res = await getErpShopList({
  41. keyword: form.keyword || undefined,
  42. boundStatus: form.boundStatus,
  43. page: pagination.currentPage,
  44. pageSize: pagination.pageSize
  45. });
  46. if (res.code === 200) {
  47. dataList.value = res.data?.list || [];
  48. total.value = res.data?.total || 0;
  49. await fetchBoundStatus();
  50. }
  51. } finally {
  52. loading.value = false;
  53. }
  54. }
  55. function resetForm() {
  56. form.keyword = "";
  57. form.boundStatus = undefined;
  58. pagination.currentPage = 1;
  59. fetchList();
  60. }
  61. function handleSearch() {
  62. pagination.currentPage = 1;
  63. fetchList();
  64. }
  65. function handlePageChange(page: number) {
  66. pagination.currentPage = page;
  67. fetchList();
  68. }
  69. function handleSizeChange(size: number) {
  70. pagination.currentPage = 1;
  71. pagination.pageSize = size;
  72. fetchList();
  73. }
  74. async function fetchBoundStatus() {
  75. try {
  76. const res = await getDeviceList({ page: 1, pageSize: 1000 });
  77. if (res.code === 200) {
  78. const devices = res.data?.list || res.data || [];
  79. dataList.value.forEach((erp: any) => {
  80. const bound = devices.find((d: any) => d.erpShopId === erp.erpShopId);
  81. erp._boundDevice = bound || null;
  82. });
  83. }
  84. } catch {
  85. // ignore
  86. }
  87. }
  88. async function handleSync() {
  89. syncing.value = true;
  90. try {
  91. const res = await syncErpShops();
  92. if (res.code === 200) {
  93. ElMessage.success(res.message || "同步任务已启动");
  94. } else {
  95. ElMessage.error(res.message || "同步失败");
  96. }
  97. } catch {
  98. ElMessage.error("同步请求失败,请稍后重试");
  99. } finally {
  100. syncing.value = false;
  101. }
  102. }
  103. async function handleAutoBind() {
  104. autoBinding.value = true;
  105. try {
  106. const res = await autoBindErpShop();
  107. if (res.code === 200) {
  108. ElMessage.success(res.message || "自动绑定完成");
  109. await fetchList();
  110. } else {
  111. ElMessage.error(res.message || "自动绑定失败");
  112. }
  113. } catch {
  114. ElMessage.error("自动绑定请求失败");
  115. } finally {
  116. autoBinding.value = false;
  117. }
  118. }
  119. async function handleUnbindAll() {
  120. try {
  121. await ElMessageBox.confirm(
  122. "确认解除所有设备与 ERP 店铺的绑定?此操作不可恢复。",
  123. "一键解绑",
  124. { type: "error", confirmButtonText: "确认解绑", cancelButtonText: "取消" }
  125. );
  126. } catch { return; }
  127. unbindingAll.value = true;
  128. try {
  129. const res = await unbindAllErpShop();
  130. if (res.code === 200) {
  131. ElMessage.success(res.message || `已解绑 ${res.data} 台设备`);
  132. await fetchList();
  133. } else {
  134. ElMessage.error(res.message || "解绑失败");
  135. }
  136. } catch {
  137. ElMessage.error("解绑请求失败");
  138. } finally {
  139. unbindingAll.value = false;
  140. }
  141. }
  142. function openBindDialog(row: any) {
  143. currentErpShop.value = row;
  144. selectedDeviceId.value = row._boundDevice?.id || null;
  145. bindDialogVisible.value = true;
  146. getDeviceList({ page: 1, pageSize: 1000 }).then(res => {
  147. if (res.code === 200) {
  148. deviceOptions.value = res.data?.list || res.data || [];
  149. }
  150. });
  151. }
  152. async function handleBind() {
  153. if (!selectedDeviceId.value) {
  154. ElMessage.warning("请选择设备");
  155. return;
  156. }
  157. bindLoading.value = true;
  158. try {
  159. const res = await bindErpShop(selectedDeviceId.value, currentErpShop.value.erpShopId);
  160. if (res.code === 200) {
  161. ElMessage.success("绑定成功");
  162. bindDialogVisible.value = false;
  163. await fetchList();
  164. } else {
  165. ElMessage.error(res.message || "绑定失败");
  166. }
  167. } finally {
  168. bindLoading.value = false;
  169. }
  170. }
  171. async function handleUnbind(row: any) {
  172. if (!row._boundDevice) return;
  173. try {
  174. await ElMessageBox.confirm(
  175. `确认解除设备「${row._boundDevice.deviceId || row._boundDevice.name}」与 ERP 店铺「${row.shopName}」的绑定?`,
  176. "解除绑定",
  177. { type: "warning" }
  178. );
  179. const res = await unbindErpShop(row._boundDevice.id);
  180. if (res.code === 200) {
  181. ElMessage.success("解绑成功");
  182. await fetchList();
  183. } else {
  184. ElMessage.error(res.message || "解绑失败");
  185. }
  186. } catch {
  187. // 取消操作
  188. }
  189. }
  190. onMounted(() => {
  191. fetchList();
  192. });
  193. </script>
  194. <template>
  195. <div class="main">
  196. <!-- 搜索表单区域 -->
  197. <el-form
  198. :inline="true"
  199. :model="form"
  200. class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
  201. >
  202. <el-form-item label="绑定状态:">
  203. <el-select
  204. v-model="form.boundStatus"
  205. placeholder="全部"
  206. clearable
  207. class="w-[140px]!"
  208. @change="handleSearch"
  209. >
  210. <el-option label="已绑定" :value="1" />
  211. <el-option label="未绑定" :value="0" />
  212. </el-select>
  213. </el-form-item>
  214. <el-form-item label="店铺名称:">
  215. <el-input
  216. v-model="form.keyword"
  217. placeholder="搜索店铺名称或卖家账号"
  218. clearable
  219. class="w-[240px]!"
  220. @keyup.enter="handleSearch"
  221. />
  222. </el-form-item>
  223. <el-form-item>
  224. <el-button
  225. type="primary"
  226. :icon="useRenderIcon(Search)"
  227. :loading="loading"
  228. @click="handleSearch"
  229. >
  230. 搜索
  231. </el-button>
  232. <el-button :icon="useRenderIcon(Refresh)" @click="resetForm">
  233. 重置
  234. </el-button>
  235. </el-form-item>
  236. </el-form>
  237. <!-- 操作按钮 + 表格 -->
  238. <div class="px-6 pt-4">
  239. <div class="flex items-center justify-between mb-4">
  240. <h2 class="text-lg font-semibold">ERP 店铺管理</h2>
  241. <div class="flex items-center gap-2">
  242. <el-button
  243. type="primary"
  244. :icon="useRenderIcon(Refresh)"
  245. :loading="syncing"
  246. @click="handleSync"
  247. >
  248. 从 ERP 同步
  249. </el-button>
  250. <el-button
  251. type="success"
  252. :loading="autoBinding"
  253. @click="handleAutoBind"
  254. >
  255. 一键绑定
  256. </el-button>
  257. <el-button
  258. type="danger"
  259. :loading="unbindingAll"
  260. @click="handleUnbindAll"
  261. >
  262. 一键解绑
  263. </el-button>
  264. </div>
  265. </div>
  266. <el-table
  267. v-loading="loading"
  268. :data="dataList"
  269. border
  270. stripe
  271. style="width: 100%"
  272. empty-text="暂无数据,请先同步ERP店铺"
  273. :row-style="{ height: '64px' }"
  274. >
  275. <el-table-column prop="erpShopId" label="ERP店铺ID" width="160" align="center" />
  276. <el-table-column prop="shopName" label="店铺名称" min-width="180" show-overflow-tooltip />
  277. <el-table-column prop="nickName" label="卖家账号" min-width="160" show-overflow-tooltip />
  278. <el-table-column prop="shopCode" label="设备ID(shopCode)" width="180" align="center" show-overflow-tooltip />
  279. <el-table-column label="绑定设备" min-width="180">
  280. <template #default="{ row }">
  281. <el-popover
  282. v-if="row._boundDevice"
  283. trigger="hover"
  284. placement="right"
  285. :width="260"
  286. >
  287. <template #reference>
  288. <el-tag type="success" size="small" style="cursor:default">
  289. {{ row._boundDevice.deviceId }}-{{ row._boundDevice.name || '未命名' }}
  290. </el-tag>
  291. </template>
  292. <div class="text-sm">
  293. <div class="font-medium text-base mb-3">{{ row._boundDevice.name || row._boundDevice.deviceId }}</div>
  294. <div class="grid grid-cols-[60px_1fr] gap-y-2 gap-x-2 text-xs">
  295. <span class="text-gray-400">设备名称</span>
  296. <span>{{ row._boundDevice.name || '-' }}</span>
  297. <span class="text-gray-400">设备ID</span>
  298. <span>{{ row._boundDevice.deviceId || '-' }}</span>
  299. <span class="text-gray-400">所属门店</span>
  300. <span>{{ row._boundDevice.shopName || row._boundDevice.shopId || '-' }}</span>
  301. <span class="text-gray-400">地址</span>
  302. <span>{{ row._boundDevice.address || '-' }}</span>
  303. </div>
  304. </div>
  305. </el-popover>
  306. <span v-else class="text-gray-400">未绑定</span>
  307. </template>
  308. </el-table-column>
  309. <el-table-column label="操作" width="160" fixed="right" align="center">
  310. <template #default="{ row }">
  311. <el-button
  312. v-if="!row._boundDevice"
  313. link
  314. type="primary"
  315. :icon="useRenderIcon(Link)"
  316. size="small"
  317. @click="openBindDialog(row)"
  318. >
  319. 绑定
  320. </el-button>
  321. <el-button
  322. v-else
  323. link
  324. type="danger"
  325. :icon="useRenderIcon(Remove)"
  326. size="small"
  327. @click="handleUnbind(row)"
  328. >
  329. 解绑
  330. </el-button>
  331. </template>
  332. </el-table-column>
  333. </el-table>
  334. <div class="flex justify-end mt-4">
  335. <el-pagination
  336. v-model:current-page="pagination.currentPage"
  337. v-model:page-size="pagination.pageSize"
  338. :total="total"
  339. :page-sizes="[10, 20, 50, 100]"
  340. layout="total, sizes, prev, pager, next, jumper"
  341. background
  342. @current-change="handlePageChange"
  343. @size-change="handleSizeChange"
  344. />
  345. </div>
  346. </div>
  347. <!-- 绑定对话框 -->
  348. <el-dialog
  349. v-model="bindDialogVisible"
  350. title="绑定设备"
  351. width="500px"
  352. :close-on-click-modal="false"
  353. >
  354. <el-form label-width="100px">
  355. <el-form-item label="ERP 店铺">
  356. <el-input
  357. :model-value="currentErpShop?.shopName + ' (' + currentErpShop?.erpShopId + ')'"
  358. disabled
  359. />
  360. </el-form-item>
  361. <el-form-item label="选择设备">
  362. <el-select
  363. v-model="selectedDeviceId"
  364. placeholder="请选择要绑定的设备"
  365. clearable
  366. filterable
  367. class="w-full!"
  368. >
  369. <el-option
  370. v-for="d in deviceOptions"
  371. :key="d.id"
  372. :label="d.deviceId + ' - ' + (d.name || '未命名')"
  373. :value="d.id"
  374. />
  375. </el-select>
  376. </el-form-item>
  377. </el-form>
  378. <template #footer>
  379. <el-button @click="bindDialogVisible = false">取消</el-button>
  380. <el-button type="primary" :loading="bindLoading" @click="handleBind">确认绑定</el-button>
  381. </template>
  382. </el-dialog>
  383. </div>
  384. </template>