form.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import {dibootApi} from '@/utils/dibootApi'
  2. import more from './more'
  3. export default {
  4. mixins: [more],
  5. data() {
  6. return {
  7. // 主键字段名
  8. primaryKey: 'id',
  9. // 请求接口基础路径
  10. baseApi: '/',
  11. // 新建接口
  12. createApi: '',
  13. // 更新接口
  14. updateApiPrefix: '',
  15. // 标题
  16. title: '',
  17. // 存储当前对象form数据
  18. form: {},
  19. // 当前form是否包含上传
  20. isUpload: false,
  21. // 确认提交
  22. confirmSubmit: false,
  23. /**
  24. * 所有文件的集合都放置与fileWrapper对象中,提交的时候会自动遍历
  25. * 格式如下:
  26. * fileWrapper: {
  27. * singleImageList: [],
  28. * multiImageList: [],
  29. * singleFileList: [],
  30. * multiFileList: []
  31. * }
  32. */
  33. fileWrapper: {},
  34. /**
  35. * uuid集合
  36. */
  37. fileUuidList: [],
  38. /**
  39. *
  40. * 激活的颜色:主要用于checkbox、radio等,保持风格统一
  41. */
  42. activeColor: this.$color.success
  43. }
  44. },
  45. /**
  46. * 打开表单
  47. * @param id ;/test?id=1
  48. */
  49. onLoad(option) {
  50. this.open(option.id)
  51. },
  52. methods: {
  53. /**
  54. * 打开
  55. * @param {Object} id
  56. */
  57. async open(id) {
  58. if (id === undefined) {
  59. // 没有id数据则认为是新建
  60. this.title = '新建'
  61. this.afterOpen(id)
  62. } else {
  63. uni.showLoading({
  64. title: '加载中'
  65. });
  66. try{
  67. // 否则作为更新处理
  68. const res = await dibootApi.get(`${this.baseApi}/${id}`)
  69. if (res.code === 0) {
  70. this.form = res.data
  71. this.title = '更新'
  72. this.afterOpen(id)
  73. } else {
  74. uni.showToast({
  75. title: res.msg
  76. });
  77. }
  78. } finally {
  79. uni.hideLoading()
  80. }
  81. }
  82. await this.attachMore()
  83. },
  84. afterOpen(id) {
  85. },
  86. /** *
  87. * 提交前的验证流程
  88. * @returns {Promise<any>}
  89. */
  90. validate() {
  91. return new Promise((resolve, reject) => {
  92. // rules存在,进行校验
  93. if(this.$refs.uForm.rules && Object.keys(this.$refs.uForm.rules).length > 0) {
  94. this.$refs.uForm.validate(valid => {
  95. valid ? resolve(true) : reject(false)
  96. });
  97. } else {
  98. resolve(true)
  99. }
  100. })
  101. },
  102. /** *
  103. * 提交前对数据的处理(在验证正确之后的处理)
  104. * @param values 提交的参数
  105. */
  106. async enhance(values) {},
  107. /** *
  108. * 新建记录的提交
  109. * @param values 提交的参数
  110. * @returns {Promise<string>}
  111. */
  112. async add(values) {
  113. const createApi = this.createApi ? this.createApi : '/'
  114. const res = await dibootApi.post(`${this.baseApi}${createApi}`, values)
  115. if (res.code === 0) {
  116. return {
  117. data: res.data,
  118. msg: '添加成功'
  119. }
  120. } else {
  121. throw new Error(res.msg)
  122. }
  123. },
  124. /** *
  125. * 更新记录的提交
  126. * @param values
  127. * @returns {Promise<string>}
  128. */
  129. async update(values) {
  130. const updateApiPrefix = this.updateApiPrefix ? this.updateApiPrefix : ''
  131. const res = await dibootApi.put(`${this.baseApi}${updateApiPrefix}/${this.form[this.primaryKey]}`, values)
  132. if (res.code === 0) {
  133. return {
  134. data: res.data,
  135. msg: '更新记录成功'
  136. }
  137. } else {
  138. throw new Error(res.msg)
  139. }
  140. },
  141. /** *
  142. * 表单提交事件
  143. * @returns {Promise<void>}
  144. */
  145. async onSubmit() {
  146. this.confirmSubmit = true
  147. uni.showLoading({
  148. title: '提交中...'
  149. });
  150. try {
  151. const valid = await this.validate()
  152. if(!valid) {
  153. uni.hideLoading()
  154. return
  155. }
  156. await this.enhance()
  157. let result = {}
  158. if (this.form[this.primaryKey] === undefined) {
  159. // 新增该记录
  160. result = await this.add(this.form)
  161. } else {
  162. // 更新该记录
  163. result = await this.update(this.form)
  164. }
  165. // 执行提交成功后的一系列后续操作
  166. this.submitSuccess(result)
  167. } catch (e) {
  168. // 执行提交失败后的一系列后续操作
  169. this.submitFailed(e)
  170. console.log(e)
  171. } finally {
  172. uni.hideLoading()
  173. this.confirmSubmit = false
  174. }
  175. },
  176. /** *
  177. * 提交成功之后的处理
  178. * @param msg
  179. */
  180. submitSuccess(result) {
  181. uni.showToast({
  182. title: '操作成功',
  183. duration: 2000,
  184. success: ()=>{
  185. uni.navigateBack({
  186. delta: 1
  187. });
  188. }
  189. });
  190. },
  191. /** *
  192. * 提交失败之后的处理
  193. * @param e
  194. */
  195. submitFailed(e) {
  196. // 如果是字符串,直接提示
  197. let msg
  198. if (typeof e === 'string') {
  199. msg = e
  200. } else if (typeof e === 'boolean') {
  201. msg = ''
  202. } else {
  203. msg = e.message || e.msg
  204. }
  205. if(msg) {
  206. uni.showToast({ title: msg, icon: 'error'})
  207. }
  208. },
  209. /**
  210. * 文件转化
  211. *
  212. * @param {Object} data
  213. */
  214. fileFormatter(data) {
  215. return {
  216. uid: data.uuid,
  217. filePath: data.accessUrl,
  218. url: `${this.$cons.host()}${data.accessUrl}/image`
  219. }
  220. },
  221. /**
  222. * 将属性值转化为数组
  223. * @param fieldName
  224. * @param separator
  225. */
  226. transformStr2Arr(fieldName, separator = ',') {
  227. this.$set(this.form, fieldName, this.strSplit(this.form[fieldName], separator))
  228. },
  229. /**
  230. * 字符串分割
  231. * @param str
  232. * @param separator
  233. */
  234. strSplit(str, separator = ',') {
  235. return str ? str.split(',') : []
  236. },
  237. /**
  238. * 设置文件uuid
  239. * @private
  240. */
  241. __setFileUuidList__() {
  242. if (!this.isUpload) {
  243. return
  244. }
  245. // 如果包含上传功能,那么设置uuid
  246. this.fileUuidList = []
  247. const fileWrapperKeys = Object.keys(this.fileWrapper)
  248. if (fileWrapperKeys.length === 0) {
  249. return
  250. }
  251. for (const fileWrapperKey of fileWrapperKeys) {
  252. const tempFileList = this.fileWrapper[fileWrapperKey]
  253. if (tempFileList && tempFileList.length && tempFileList.length > 0) {
  254. this.fileUuidList.push(...tempFileList.map(item => item.uid))
  255. }
  256. }
  257. this.form['fileUuidList'] = this.fileUuidList
  258. },
  259. /**
  260. * 初始化fileWrapper
  261. * @private
  262. */
  263. __defaultFileWrapperKeys__() {
  264. const fileWrapperKeys = Object.keys(this.fileWrapper)
  265. if (fileWrapperKeys.length > 0) {
  266. for (const fileWrapperKey of fileWrapperKeys) {
  267. this.fileWrapper[fileWrapperKey] = []
  268. }
  269. } else {
  270. this.fileWrapper = {}
  271. }
  272. this.fileUuidList = []
  273. }
  274. }
  275. }