lefengyang
2026-08-20 d01a28cb030b2cb0ff1d42273c877a0f728519a6
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
<template>
  <div class="bid-task-adder">
    <h2>测试数据添加器</h2>
 
    <!-- 历史记录选择 -->
    <HistorySelector
      v-if="history.length > 0"
      v-model="selectedHistoryItem"
      :history-items="history"
      @selected="handleSelected"
    />
 
 
    <form @submit.prevent="addBidTask">
      <div class="form-group">
        <label for="goodsId">商品ID</label>
        <input
            id="goodsId"
            v-model="formData.goodsId"
            type="text"
            required
            placeholder="例如:T000001"
        />
      </div>
 
      <div class="form-group">
        <label for="bid">出价</label>
        <input
            id="bid"
            v-model="formData.bid"
            type="text"
            required
            placeholder="例如:600.00"
        />
      </div>
 
      <div class="form-group">
        <label for="taskType">任务类型</label>
        <select id="taskType" v-model.number="formData.taskType" required>
          <option value="0">0 - 竞拍</option>
          <option value="1">1 - 一口价购买</option>
        </select>
      </div>
 
      <div class="form-group">
        <label for="taskName">取名</label>
        <input
            id="taskName"
            v-model="formData.name"
            type="text"
        />
      </div>
 
      <button type="submit" :disabled="isLoading">
        {{ isLoading ? '提交中...' : '添加出价任务' }}
      </button>
      <button @click="clearHistory">清除历史记录</button>
    </form>
 
    <div v-if="response" class="response">
      <h3>响应结果</h3>
      <pre>{{ JSON.stringify(response, null, 2) }}</pre>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import './App.scss'
import {onMounted, reactive, ref} from 'vue';
import {BidFormData, Db} from "/src-com";
import HistorySelector from './components/HistorySelector.vue';
import {copyObject} from "gs-base";
import {Delete} from "gs-idb-pro";
 
 
interface ResponseData {
  code: number;
  message: string;
  data: any;
}
 
const formData = reactive<BidFormData>({
  goodsId: '',
  bid: '',
  taskType: 0,
  params: {}
});
 
const paramsJson = ref('');
const isLoading = ref(false);
const response = ref<ResponseData | null>(null);
const history = ref<BidFormData[]>([]);
const selectedHistoryItem = ref<BidFormData | null>(null);
 
onMounted(async () => {
  history.value = await Db.bidTaskAddedHistory.all();
  // 如果有历史记录,设置最后一条为默认值
  if (history.value.length > 0) {
    const lastItem = history.value[history.value.length - 1];
    loadHistoryDataFromItem(lastItem);
  }
});
 
function handleSelected(e) {
  loadHistoryDataFromItem(e);
}
 
// 从历史数据项加载数据到表单
const loadHistoryDataFromItem = (item: BidFormData) => {
  formData.goodsId = item.goodsId;
  formData.bid = item.bid;
  formData.taskType = item.taskType;
  formData.name = item.name;
  formData.params = item.params || {};
  paramsJson.value = JSON.stringify(formData.params, null, 2);
};
 
// 清除历史记录
const clearHistory = async () => {
  try {
    await Db.bidTaskAddedHistory.cursor(() => ({modify: Delete}))
    history.value = [];
  } catch (e: any) {
    console.log(e)
  }
}
 
 
const addBidTask = async () => {
  try {
    // 解析params JSON
    if (paramsJson.value) {
      formData.params = JSON.parse(paramsJson.value);
    } else {
      formData.params = {};
    }
    try {
      const obj = copyObject(formData)
      await Db.bidTaskAddedHistory.add(obj);
      history.value.push(obj)
    } catch (e: any) {
      console.log(e, formData)
    }
 
    isLoading.value = true;
    response.value = null;
 
    const res = await fetch('https://adminapi.wakuwaku.asia/api/task/addBid', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(formData)
    });
 
    const result = await res.json();
    response.value = result;
 
    if (result.code === 200) {
      // 重置表单
      formData.goodsId = '';
      formData.bid = '';
      formData.taskType = 0;
      paramsJson.value = '';
    }
  } catch (error) {
    response.value = {
      code: 500,
      message: `请求失败:${error instanceof Error ? error.message : String(error)}`,
      data: null
    };
  } finally {
    isLoading.value = false;
  }
};
</script>