You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.4 KiB

4 weeks ago
3 weeks ago
4 weeks ago
  1. <template>
  2. <div>
  3. <el-form :inline="true" label-position="top" style="margin-top: 10px">
  4. <el-form-item >
  5. <el-button type="primary" @click="openUserPicker">选择账号</el-button>
  6. <el-button type="primary" :loading="saving" @click="save">保存</el-button>
  7. </el-form-item>
  8. </el-form>
  9. <div class="rq">
  10. <el-table :data="rows" border style="width: 100%" height="400">
  11. <el-table-column prop="userName" label="账号" min-width="180" />
  12. <el-table-column label="启用" width="100" align="center">
  13. <template slot-scope="{ row }">
  14. <el-select v-model="row.active" size="mini" style="width: 80px">
  15. <el-option label="Y" value="Y" />
  16. <el-option label="N" value="N" />
  17. </el-select>
  18. </template>
  19. </el-table-column>
  20. <el-table-column label="备注" min-width="220">
  21. <template slot-scope="{ row }">
  22. <el-input v-model="row.remark" size="mini" />
  23. </template>
  24. </el-table-column>
  25. <el-table-column label="操作" width="80" align="center">
  26. <template slot-scope="scope">
  27. <el-link type="danger" @click="removeRow(scope.$index)">删除</el-link>
  28. </template>
  29. </el-table-column>
  30. </el-table>
  31. </div>
  32. <el-dialog
  33. title="选择账号"
  34. :visible.sync="pickerVisible"
  35. width="900px"
  36. top="6vh"
  37. append-to-body
  38. :close-on-click-modal="false">
  39. <el-form :inline="true" label-position="top">
  40. <el-form-item label="账号">
  41. <el-input v-model="pickerQuery.username" clearable placeholder="请输入账号" style="width: 180px" />
  42. </el-form-item>
  43. <el-form-item label="名称">
  44. <el-input v-model="pickerQuery.userDisplay" clearable placeholder="请输入名称" style="width: 180px" />
  45. </el-form-item>
  46. <el-form-item label=" ">
  47. <el-button type="primary" @click="fetchPickerRows">查询</el-button>
  48. <el-button @click="resetPickerQuery">重置</el-button>
  49. </el-form-item>
  50. </el-form>
  51. <el-table
  52. ref="pickerTable"
  53. :data="pickerRows"
  54. border
  55. height="420"
  56. v-loading="pickerLoading"
  57. @selection-change="onPickerSelectionChange">
  58. <el-table-column type="selection" width="55" />
  59. <el-table-column prop="username" label="账号" min-width="180" />
  60. <el-table-column prop="userDisplay" label="名称" min-width="180" />
  61. </el-table>
  62. <div slot="footer" class="dialog-footer">
  63. <el-button @click="pickerVisible = false">取消</el-button>
  64. <el-button type="primary" @click="confirmPickUsers">确认添加</el-button>
  65. </div>
  66. </el-dialog>
  67. </div>
  68. </template>
  69. <script>
  70. import {
  71. listPoCustomerUserScope,
  72. savePoCustomerUserScope,
  73. searchSiteUserList
  74. } from '@/api/order/poCustomer'
  75. export default {
  76. name: 'ComPoCustomerUserScope',
  77. props: {
  78. site: { type: String, required: true },
  79. customerCode: { type: String, default: '' }
  80. },
  81. data () {
  82. return {
  83. rows: [],
  84. saving: false,
  85. pickerVisible: false,
  86. pickerLoading: false,
  87. pickerRows: [],
  88. pickerSelection: [],
  89. pickerQuery: {
  90. username: '',
  91. userDisplay: ''
  92. }
  93. }
  94. },
  95. watch: {
  96. customerCode: {
  97. immediate: true,
  98. handler () { this.load() }
  99. }
  100. },
  101. methods: {
  102. load () {
  103. if (!this.customerCode) {
  104. this.rows = []
  105. return
  106. }
  107. listPoCustomerUserScope({ site: this.site, customerCode: this.customerCode }).then(({ data }) => {
  108. const rows = (data && data.code === 0 && data.rows) ? data.rows : []
  109. this.rows = rows.map(this.normalizeScopeRow)
  110. }).catch(() => {
  111. this.rows = []
  112. })
  113. },
  114. normalizeScopeRow (row) {
  115. return {
  116. userName: (row && row.userName ? String(row.userName).trim() : ''),
  117. active: row && row.active === 'N' ? 'N' : 'Y',
  118. remark: row && row.remark ? String(row.remark) : ''
  119. }
  120. },
  121. openUserPicker () {
  122. if (!this.customerCode) {
  123. this.$message.warning('请先选择Customer')
  124. return
  125. }
  126. this.pickerVisible = true
  127. this.pickerSelection = []
  128. this.fetchPickerRows()
  129. },
  130. resetPickerQuery () {
  131. this.pickerQuery = { username: '', userDisplay: '' }
  132. this.fetchPickerRows()
  133. },
  134. fetchPickerRows () {
  135. this.pickerLoading = true
  136. searchSiteUserList({
  137. site: this.site,
  138. username: this.pickerQuery.username || '',
  139. userDisplay: this.pickerQuery.userDisplay || ''
  140. }).then(({ data }) => {
  141. if (data && data.code === 0) {
  142. this.pickerRows = data.rows || []
  143. this.$nextTick(() => {
  144. if (this.$refs.pickerTable) {
  145. this.$refs.pickerTable.clearSelection()
  146. }
  147. })
  148. } else {
  149. this.pickerRows = []
  150. this.$message.error((data && data.msg) || '加载账号清单失败')
  151. }
  152. }).catch(() => {
  153. this.pickerRows = []
  154. this.$message.error('加载账号清单失败')
  155. }).finally(() => {
  156. this.pickerLoading = false
  157. })
  158. },
  159. onPickerSelectionChange (selection) {
  160. this.pickerSelection = selection || []
  161. },
  162. confirmPickUsers () {
  163. if (!this.pickerSelection.length) {
  164. this.$message.warning('请至少选择一个账号')
  165. return
  166. }
  167. const exists = new Set(this.rows.map(r => (r.userName || '').toLowerCase()))
  168. let added = 0
  169. let ignored = 0
  170. this.pickerSelection.forEach(user => {
  171. const userName = user && user.username ? String(user.username).trim() : ''
  172. if (!userName) {
  173. return
  174. }
  175. const key = userName.toLowerCase()
  176. if (exists.has(key)) {
  177. ignored++
  178. return
  179. }
  180. this.rows.push({
  181. userName,
  182. active: 'Y',
  183. remark: ''
  184. })
  185. exists.add(key)
  186. added++
  187. })
  188. this.pickerVisible = false
  189. if (added > 0 && ignored > 0) {
  190. this.$message.success(`已添加 ${added} 个账号,忽略 ${ignored} 个已存在账号`)
  191. } else if (added > 0) {
  192. this.$message.success(`已添加 ${added} 个账号`)
  193. } else {
  194. this.$message.warning('所选账号均已存在,未新增')
  195. }
  196. },
  197. removeRow (index) {
  198. this.rows.splice(index, 1)
  199. },
  200. save () {
  201. if (!this.customerCode) return
  202. const dedupMap = new Map()
  203. this.rows.forEach(row => {
  204. const userName = row && row.userName ? String(row.userName).trim() : ''
  205. if (!userName) {
  206. return
  207. }
  208. const key = userName.toLowerCase()
  209. if (dedupMap.has(key)) {
  210. return
  211. }
  212. dedupMap.set(key, {
  213. userName,
  214. active: row.active === 'N' ? 'N' : 'Y',
  215. remark: row.remark ? String(row.remark) : ''
  216. })
  217. })
  218. const payloadRows = Array.from(dedupMap.values())
  219. this.saving = true
  220. savePoCustomerUserScope({
  221. site: this.site,
  222. customerCode: this.customerCode,
  223. rows: payloadRows
  224. }).then(({ data }) => {
  225. this.saving = false
  226. if (data && data.code === 0) {
  227. this.$message.success('保存成功')
  228. this.load()
  229. } else {
  230. this.$message.error((data && data.msg) || '保存失败')
  231. }
  232. }).catch(() => {
  233. this.saving = false
  234. this.$message.error('保存失败')
  235. })
  236. }
  237. }
  238. }
  239. </script>