addEditDialog.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div class="add-edit-dialog-container">
  3. <el-dialog
  4. :title="title"
  5. :visible.sync="visible"
  6. width="600px"
  7. :before-close="close"
  8. :close-on-click-modal="false"
  9. >
  10. <el-form :model="formData" ref="formData" :rules="rules" label-width="90px">
  11. <el-form-item label="名称:" prop="imgName">
  12. <el-input placeholder="请输入" v-model="formData.imgName"></el-input>
  13. </el-form-item>
  14. <el-form-item label="图片:" class="plate-icon" prop="file">
  15. <!-- <p class="color666">图片大小不能超过3MB</p> -->
  16. <Upload
  17. :fileList="fileList"
  18. @changeUploadFile="changeUploadFile"
  19. @validateUpload="validateUpload"
  20. :limitUpload="1"
  21. :maximumSize="maximumSize"
  22. :uploadType="1"
  23. ref="upload"
  24. ></Upload>
  25. </el-form-item>
  26. <el-form-item label="跳转链接:" prop="url">
  27. <el-input placeholder="跳转链接" v-model="formData.url"></el-input>
  28. </el-form-item>
  29. <el-form-item label="跳转类型:" prop="jumpType">
  30. <el-select v-model="formData.jumpType" placeholder="请选择">
  31. <el-option label="pdf" :value="1"></el-option>
  32. <el-option label="网页链接" :value="2"></el-option>
  33. </el-select>
  34. </el-form-item>
  35. <el-form-item label="显示位置:" prop="type">
  36. <el-select v-model="formData.type" placeholder="请选择">
  37. <el-option label="工作台banner" :value="1"></el-option>
  38. <el-option label="物业端banner" :value="2"></el-option>
  39. <el-option label="4.0首页banner" :value="3"></el-option>
  40. </el-select>
  41. </el-form-item>
  42. <el-form-item label="排序:" prop="serialNo">
  43. <el-input placeholder="请排序" v-model="formData.serialNo" class="sort-input" maxlength="20"></el-input>
  44. </el-form-item>
  45. <!-- <el-form-item label="启用状态:">
  46. <el-switch
  47. v-model="formData.statuz"
  48. active-text="开"
  49. inactive-text="关"
  50. :active-value="1"
  51. :inactive-value="0"
  52. ></el-switch> -->
  53. </el-form-item>
  54. </el-form>
  55. <div slot="footer" class="dialog-footer">
  56. <el-button @click="close()">取 消</el-button>
  57. <el-button
  58. type="primary"
  59. @click="submitForm('formData')"
  60. :disabled="submitBtndisabled"
  61. >确 定</el-button>
  62. </div>
  63. </el-dialog>
  64. </div>
  65. </template>
  66. <script>
  67. import Upload from '@/apps/mobile/components/uploadAuto/index';
  68. import { addBanner, editBanner } from '@/apps/mobile/api/banner/index';
  69. import { validPositiveInteger, validHttps } from '@/apps/mobile/utils/validate';
  70. import { staticUrl } from '@/config.js';
  71. import { staticPath } from '@/apps//mobile/utils/util';
  72. export default {
  73. components: {
  74. Upload,
  75. },
  76. data() {
  77. // http(s)校验
  78. var checkHttps = (rule, value, callback) => {
  79. if (value && !validHttps(value)) {
  80. callback(new Error('格式不正确'));
  81. } else {
  82. callback();
  83. }
  84. };
  85. // 排序校验
  86. var checkSort = (rule, value, callback) => {
  87. if (value && !validPositiveInteger(value)) {
  88. callback(new Error('格式不正确'));
  89. } else {
  90. callback();
  91. }
  92. };
  93. return {
  94. jumpType: null,
  95. type: null,
  96. visible: false,
  97. title: '新增',
  98. formData: { file: null },
  99. submitBtndisabled: false,
  100. fileList: [], // 图片列表
  101. maximumSize: 3, //图片大小 不超过3MB
  102. limitUpload: 1,
  103. rules: {
  104. imgName: [
  105. { required: true, message: '请输入名称', trigger: 'blur' },
  106. { max: 30, message: '长度最大30个字符', trigger: 'blur' },
  107. ],
  108. jumpType: [{ required: true, message: '请选择跳转类型', trigger: 'change' }],
  109. type: [{ required: true, message: '请选择显示位置', trigger: 'blur' }],
  110. serialNo: [
  111. { validator: checkSort, message: '请输入正整数', trigger: 'blur' }
  112. ],
  113. file: [{ required: true, message: '请上传图片', trigger: 'blur' }],
  114. url: [
  115. { validator: checkHttps, message: '请输入正确的链接地址', trigger: 'blur' }
  116. ],
  117. },
  118. };
  119. },
  120. methods: {
  121. open(type, data) {
  122. this.type = type;
  123. let obj1 = {
  124. file: null,
  125. };
  126. if (type === 'edit') {
  127. this.title = '编辑';
  128. console.log(data, 'daaaaa');
  129. if (data.image) {
  130. obj1.file = true;
  131. // if(!this.pathReg.test(data.image)) {
  132. // data.image = staticUrl + data.image
  133. // }
  134. let url = staticPath(data.image);
  135. let obj = {
  136. name: '',
  137. url,
  138. response: {
  139. data: { pathUrl: data.image },
  140. },
  141. };
  142. this.fileList.push(obj);
  143. }
  144. this.formData = Object.assign({ ...data, ...obj1 });
  145. console.log(this.formData, 'this.formData');
  146. } else {
  147. this.title = '新增';
  148. this.formData = obj1;
  149. }
  150. this.visible = true;
  151. },
  152. close() {
  153. this.type = null;
  154. this.$refs['formData'].resetFields();
  155. this.$refs.upload.clearFiles(); // 清空已上传的文件列表
  156. this.fileList = [];
  157. this.submitBtndisabled = false;
  158. this.visible = false;
  159. },
  160. submitForm(formName) {
  161. this.$refs[formName].validate(valid => {
  162. if (valid) {
  163. this.submitBtndisabled = true;
  164. let message = '';
  165. let image = null;
  166. if (this.fileList.length > 0) {
  167. console.log(this.fileList, 'this.fileList');
  168. image = this.fileList[0].response.data.pathUrl;
  169. }
  170. let obj = Object.assign({ ...this.formData, image });
  171. console.log(obj, 'objj');
  172. if (this.type === 'add') {
  173. message = '新增成功';
  174. delete obj.file;
  175. obj.createBy = 0; //创建者ID
  176. addBanner(obj)
  177. .then(res => {
  178. this.completeOperation(message);
  179. })
  180. .catch(err => err);
  181. }
  182. if (this.type === 'edit') {
  183. message = '修改成功';
  184. let editObj = {
  185. id: obj.id,
  186. imgName: obj.imgName,
  187. url: obj.url,
  188. jumpType: obj.jumpType,
  189. type: obj.type,
  190. serialNo: obj.serialNo,
  191. image: obj.image,
  192. };
  193. editBanner(editObj)
  194. .then(() => {
  195. this.completeOperation(message);
  196. })
  197. .catch(err => err);
  198. }
  199. }
  200. });
  201. },
  202. /**
  203. * 手动验证upload、更新选择完成的图片数据
  204. * @param {Object} data: fileList是上传的图片数据 validateValues为true时说明有上传图片 为null时无上传图片
  205. */
  206. validateUpload(data) {
  207. this.changeUploadFile(data.fileList);
  208. // console.log(data,'data');
  209. this.formData.file = data.validateValues;
  210. this.$refs['formData'].validateField('file');
  211. },
  212. changeUploadFile(fileList) {
  213. this.fileList = fileList;
  214. console.log(this.fileList, 'this.fileList');
  215. },
  216. /**
  217. * 将新增、编辑操作完成之后共同需要调用的方法放在此函数里面
  218. * @param {String} message 提示信息
  219. */
  220. completeOperation(message) {
  221. this.$message({
  222. type: 'success',
  223. message,
  224. });
  225. this.close();
  226. this.submitBtndisabled = false;
  227. this.$emit('queryListData');
  228. },
  229. },
  230. watch: {
  231. fileList(val) {
  232. this.$nextTick(() => {
  233. this.$refs.upload.changeHideUpload();
  234. });
  235. },
  236. },
  237. };
  238. </script>
  239. <style lang="stylus" scoped>
  240. .add-edit-dialog-container {
  241. ::v-deep .el-input {
  242. width: auto;
  243. }
  244. ::v-deep .el-input__inner {
  245. width: 200px;
  246. }
  247. .upload {
  248. display: inline-block;
  249. }
  250. }
  251. </style>