新大宝股票管理后台
李凌
4 days ago 0dca2cc0ba3a58728d86ca0af07c92eab2b44c48
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
<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <a-table
        bordered
        :loading="loading"
        :columns="columns"
        :data-source="datalist"
        :pagination="false"
        rowKey="id"
        :customRow="customRow"
      >
        <span slot="enabled" slot-scope="text, record">
          <a-tag :color="record.enabled === 1 ? 'green' : 'red'">
            {{ record.enabled === 1 ? '开启' : '关闭' }}
          </a-tag>
        </span>
        <template slot="action" slot-scope="text, record">
          <a-button
            type="link"
            size="small"
            @click="toggleEnabled(record)"
            :loading="record._loading"
          >
            {{ record.enabled === 1 ? '关闭' : '开启' }}
          </a-button>
        </template>
        <span slot="dragHandle" class="drag-handle">
          <a-icon type="menu" />
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>
 
<script>
import { payOptionList, payOptionUpdateSort, payOptionSetEnabled } from '@/api/capital'
 
export default {
  name: 'PayOption',
  data() {
    return {
      columns: [
        {
          title: '排序',
          key: 'sort',
          width: 60,
          align: 'center',
          scopedSlots: { customRender: 'dragHandle' },
        },
        {
          title: '名称',
          dataIndex: 'name',
          align: 'center',
        },
        {
          title: '排序序号',
          dataIndex: 'sortOrder',
          align: 'center',
          width: 120,
        },
        {
          title: '状态',
          dataIndex: 'enabled',
          align: 'center',
          width: 100,
          scopedSlots: { customRender: 'enabled' },
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          width: 120,
          scopedSlots: { customRender: 'action' },
        },
      ],
      loading: false,
      datalist: [],
      dragIndex: null,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      payOptionList()
        .then((res) => {
          this.loading = false
          if (res.status === 0 && Array.isArray(res.data)) {
            this.datalist = (res.data || [])
              .slice()
              .sort((a, b) => (a.sortOrder || 0) - (b.sortOrder || 0))
          } else {
            this.datalist = []
          }
        })
        .catch(() => {
          this.loading = false
          this.datalist = []
        })
    },
    customRow(record, index) {
      return {
        attrs: {
          draggable: true,
        },
        on: {
          dragstart: (e) => this.onDragStart(e, index),
          dragover: (e) => this.onDragOver(e, index),
          drop: (e) => this.onDrop(e, index),
        },
      }
    },
    onDragStart(e, index) {
      this.dragIndex = index
      e.dataTransfer.effectAllowed = 'move'
      e.dataTransfer.setData('text/plain', index)
      try {
        e.target.classList.add('drag-over')
      } catch (_) {}
    },
    onDragOver(e, index) {
      e.preventDefault()
      e.dataTransfer.dropEffect = 'move'
    },
    onDrop(e, dropIndex) {
      e.preventDefault()
      try {
        e.target.classList.remove('drag-over')
      } catch (_) {}
      const dragIndex = this.dragIndex
      if (dragIndex == null || dragIndex === dropIndex) return
      const list = this.datalist.slice()
      const [item] = list.splice(dragIndex, 1)
      list.splice(dropIndex, 0, item)
      this.datalist = list
      this.dragIndex = null
      const orderedIds = list.map((i) => i.id)
      // console.log(orderedIds, 'orderedIds');
      // return
      payOptionUpdateSort(orderedIds)
        .then((res) => {
          if (res.status === 0) {
            this.$message.success('排序已更新')
          } else {
            this.$message.error(res.msg || '排序更新失败')
            this.getList()
          }
        })
        .catch(() => {
          this.$message.error('排序更新失败')
          this.getList()
        })
    },
    toggleEnabled(record) {
      const enabled = record.enabled === 1 ? 0 : 1
      this.$set(record, '_loading', true)
      payOptionSetEnabled({ id: record.id, enabled })
        .then((res) => {
          this.$set(record, '_loading', false)
          if (res.status === 0) {
            record.enabled = enabled
            this.$message.success(enabled === 1 ? '已开启' : '已关闭')
          } else {
            this.$message.error(res.msg || '操作失败')
          }
        })
        .catch(() => {
          this.$set(record, '_loading', false)
          this.$message.error('操作失败')
        })
    },
  },
}
</script>
 
<style scoped>
.drag-handle {
  cursor: move;
  color: #999;
}
.drag-handle:hover {
  color: #1890ff;
}
</style>