Rap 原分销系统代码Web
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.

3e9f8aaf8d72fcb4bba7fcb3981d7cc4fe72516b.svn-base 7.4KB

пре 5 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import {
  2. initData,
  3. download
  4. } from '@/api/data'
  5. import {
  6. parseTime,
  7. downloadFile
  8. } from '@/utils/index'
  9. export default {
  10. data() {
  11. return {
  12. // 表格数据
  13. data: [],
  14. // 排序规则,默认 id 降序, 支持多字段排序 ['id,desc', 'createTime,asc']
  15. sort: ['id,desc'],
  16. // 页码
  17. page: 0,
  18. // 每页数据条数
  19. size: 10,
  20. // 总数据条数
  21. total: 0,
  22. // 请求数据的url
  23. url: '',
  24. // 查询数据的参数
  25. params: {},
  26. // 待查询的对象
  27. query: {},
  28. // 等待时间
  29. time: 50,
  30. // 是否为新增类型的表单
  31. isAdd: false,
  32. // 导出的 Loading
  33. downloadLoading: false,
  34. // 表格 Loading 属性
  35. loading: true,
  36. // 删除 Loading 属性
  37. delLoading: false,
  38. delAllLoading: false,
  39. // 弹窗属性
  40. dialog: false,
  41. // Form 表单
  42. form: {},
  43. // 重置表单
  44. resetForm: {},
  45. // 标题
  46. title: ''
  47. }
  48. },
  49. methods: {
  50. parseTime,
  51. downloadFile,
  52. async init() {
  53. if (!await this.beforeInit()) {
  54. return
  55. }
  56. return new Promise((resolve, reject) => {
  57. this.loading = true
  58. // 请求数据
  59. initData(this.url, this.getQueryParame()).then(data => {
  60. this.total = data.totalElements
  61. this.data = data.content
  62. // time 毫秒后显示表格
  63. setTimeout(() => {
  64. this.loading = false
  65. }, this.time)
  66. resolve(data)
  67. }).catch(err => {
  68. this.loading = false
  69. reject(err)
  70. })
  71. })
  72. },
  73. beforeInit() {
  74. return true
  75. },
  76. getQueryParame: function() {
  77. return {
  78. page: this.page,
  79. size: this.size,
  80. sort: this.sort,
  81. ...this.query,
  82. ...this.params
  83. }
  84. },
  85. // 改变页码
  86. pageChange(e) {
  87. this.page = e - 1
  88. this.init()
  89. },
  90. // 改变每页显示数
  91. sizeChange(e) {
  92. this.page = 0
  93. this.size = e
  94. this.init()
  95. },
  96. // 预防删除第二页最后一条数据时,或者多选删除第二页的数据时,页码错误导致请求无数据
  97. dleChangePage(size) {
  98. if (size === undefined) {
  99. size = 1
  100. }
  101. if (this.data.length === size && this.page !== 0) {
  102. this.page = this.page - 1
  103. }
  104. },
  105. // 查询方法
  106. toQuery() {
  107. this.page = 0
  108. this.init()
  109. },
  110. /**
  111. * 通用的提示封装
  112. */
  113. submitSuccessNotify() {
  114. this.$notify({
  115. title: '提交成功',
  116. type: 'success',
  117. duration: 2500
  118. })
  119. },
  120. addSuccessNotify() {
  121. this.$notify({
  122. title: '新增成功',
  123. type: 'success',
  124. duration: 2500
  125. })
  126. },
  127. editSuccessNotify() {
  128. this.$notify({
  129. title: '编辑成功',
  130. type: 'success',
  131. duration: 2500
  132. })
  133. },
  134. delSuccessNotify() {
  135. this.$notify({
  136. title: '删除成功',
  137. type: 'success',
  138. duration: 2500
  139. })
  140. },
  141. notify(title, type) {
  142. this.$notify({
  143. title: title,
  144. type: type,
  145. duration: 2500
  146. })
  147. },
  148. /**
  149. * 删除前可以调用 beforeDelMethod 做一些操作
  150. */
  151. beforeDelMethod() {
  152. return true
  153. },
  154. /**
  155. * 通用的删除
  156. */
  157. delMethod(id) {
  158. if (!this.beforeDelMethod()) {
  159. return
  160. }
  161. this.delLoading = true
  162. this.crudMethod.del(id).then(() => {
  163. this.delLoading = false
  164. this.$refs[id].doClose()
  165. this.dleChangePage()
  166. this.delSuccessNotify()
  167. this.afterDelMethod()
  168. this.init()
  169. }).catch(() => {
  170. this.delLoading = false
  171. this.$refs[id].doClose()
  172. })
  173. },
  174. afterDelMethod() {},
  175. /**
  176. * 多选删除提示
  177. */
  178. beforeDelAllMethod() {
  179. this.$confirm('你确定删除选中的数据吗?', '提示', {
  180. confirmButtonText: '确定',
  181. cancelButtonText: '取消',
  182. type: 'warning'
  183. }).then(() => {
  184. this.delAllMethod()
  185. })
  186. },
  187. /**
  188. * 多选删除
  189. */
  190. delAllMethod() {
  191. this.delAllLoading = true
  192. const data = this.$refs.table.selection
  193. const ids = []
  194. for (let i = 0; i < data.length; i++) {
  195. ids.push(data[i].id)
  196. }
  197. this.crudMethod.delAll(ids).then(() => {
  198. this.delAllLoading = false
  199. this.dleChangePage(ids.length)
  200. this.init()
  201. this.$notify({
  202. title: '删除成功',
  203. type: 'success',
  204. duration: 2500
  205. })
  206. }).catch(() => {
  207. this.delAllLoading = false
  208. })
  209. },
  210. /**
  211. * 显示新增弹窗前可以调用该方法
  212. */
  213. beforeShowAddForm() {},
  214. /**
  215. * 显示新增弹窗
  216. */
  217. showAddFormDialog() {
  218. this.isAdd = true
  219. this.resetForm = JSON.parse(JSON.stringify(this.form))
  220. this.beforeShowAddForm()
  221. this.dialog = true
  222. },
  223. /**
  224. * 显示编辑弹窗前可以调用该方法
  225. */
  226. beforeShowEditForm(data) {},
  227. /**
  228. * 显示编辑弹窗
  229. */
  230. showEditFormDialog(data = '') {
  231. this.isAdd = false
  232. if (data) {
  233. this.resetForm = JSON.parse(JSON.stringify(this.form))
  234. this.form = JSON.parse(JSON.stringify(data))
  235. }
  236. this.beforeShowEditForm(data)
  237. this.dialog = true
  238. },
  239. /**
  240. * 新增方法
  241. */
  242. addMethod() {
  243. this.crudMethod.add(this.form).then(() => {
  244. this.addSuccessNotify()
  245. this.loading = false
  246. this.afterAddMethod()
  247. this.cancel()
  248. this.init()
  249. }).catch(() => {
  250. this.loading = false
  251. this.afterAddErrorMethod()
  252. })
  253. },
  254. /**
  255. * 新增后可以调用该方法
  256. */
  257. afterAddMethod() {},
  258. /**
  259. * 新增失败后调用该方法
  260. */
  261. afterAddErrorMethod() {},
  262. /**
  263. * 通用的编辑方法
  264. */
  265. editMethod() {
  266. this.crudMethod.edit(this.form).then(() => {
  267. this.editSuccessNotify()
  268. this.loading = false
  269. this.afterEditMethod()
  270. this.cancel()
  271. this.init()
  272. }).catch(() => {
  273. this.loading = false
  274. })
  275. },
  276. /**
  277. * 编辑后可以调用该方法
  278. */
  279. afterEditMethod() {},
  280. /**
  281. * 提交前可以调用该方法
  282. */
  283. beforeSubmitMethod() {
  284. return true
  285. },
  286. /**
  287. * 提交
  288. */
  289. submitMethod() {
  290. if (!this.beforeSubmitMethod()) {
  291. return
  292. }
  293. if (this.$refs['form']) {
  294. this.$refs['form'].validate((valid) => {
  295. if (valid) {
  296. this.loading = true
  297. if (this.isAdd) {
  298. this.addMethod()
  299. } else this.editMethod()
  300. }
  301. })
  302. }
  303. },
  304. /**
  305. * 隐藏弹窗
  306. */
  307. cancel() {
  308. this.dialog = false
  309. if (this.$refs['form']) {
  310. this.$refs['form'].clearValidate()
  311. this.form = this.resetForm
  312. }
  313. },
  314. /**
  315. * 获取弹窗的标题
  316. */
  317. getFormTitle() {
  318. return this.isAdd ? `新增${this.title}` : `编辑${this.title}`
  319. },
  320. /**
  321. * 通用导出
  322. */
  323. downloadMethod() {
  324. this.beforeInit()
  325. this.downloadLoading = true
  326. download(this.url + '/download', this.params).then(result => {
  327. this.downloadFile(result, this.title + '数据', 'xlsx')
  328. this.downloadLoading = false
  329. }).catch(() => {
  330. this.downloadLoading = false
  331. })
  332. }
  333. }
  334. }