index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <el-form ref="form" class="article-form" :model="form" :rules="rules" label-width="85px">
  3. <el-form-item label="名称" prop="name">
  4. <el-input v-model="form.name" maxlength="80" show-word-limit />
  5. </el-form-item>
  6. <el-form-item label="所属菜系" prop="cuisineCode">
  7. <div style="display: flex;">
  8. <el-select v-model="form.cuisineCode" placeholder="请选择所属菜系">
  9. <el-option v-for="item in cuisineList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
  10. </el-select>
  11. </div>
  12. </el-form-item>
  13. <el-form-item label="所属地区" prop="regionId">
  14. <div style="display: flex;">
  15. <el-select v-model="form.regionId" placeholder="请选择所属区域">
  16. <el-option v-for="item in regionList" :key="item.key" :label="item.title" :value="item.key" />
  17. </el-select>
  18. </div>
  19. </el-form-item>
  20. <el-form-item label="副标题" prop="subTitle">
  21. <el-input v-model="form.subTitle" type="textarea" maxlength="90" show-word-limit />
  22. </el-form-item>
  23. <el-form-item label="内容" prop="content">
  24. <!--<content-item v-if="contentItemVisible" ref="contentComp" :html.sync="form.content" />-->
  25. <avue-ueditor v-model="form.content" v-bind="editorOptions"></avue-ueditor>
  26. </el-form-item>
  27. <!--<el-form-item label="附件" prop="attachment">
  28. <el-upload class="upload-demo" :http-request="uploadImg" :data="{type:'att'}" :file-list="attachmentList" :on-remove="handleRemoveImg" :on-preview="handlePreview" :accept='["image/jpeg", "image/png", "image/tga", "image/gif", "image/bmp"]'>
  29. <el-button size="small" type="primary">点击上传</el-button>
  30. </el-upload>
  31. </el-form-item>-->
  32. <el-form-item label="封面" prop="cover">
  33. <div style="display: flex;">
  34. <el-upload class="avatar-uploader cover-w16h9" style="margin-right: 30px !important;" :http-request="uploadImg" :data="{type:'img'}" :show-file-list="false" :before-upload="beforeAvatarUpload">
  35. <el-image fit="contain" v-if="form.cover" :src="form.cover" class="avatar"></el-image>
  36. <el-icon v-else class="el-icon-plus avatar-uploader-icon">
  37. <Plus />
  38. </el-icon>
  39. </el-upload>
  40. <!--1.建议长×宽比例为16:9,否则默认拉伸<br /> 2.像素最大不超过960*540<br />3.只能上传jpg/png文件,且不超过5MB-->
  41. </div>
  42. </el-form-item>
  43. <el-form-item label="视频" prop="videos">
  44. <div style="display: flex;">
  45. <el-upload class="avatar-uploader cover-w16h9" style="margin-right: 30px !important;" :http-request="uploadImg" :data="{type:'video'}" :show-file-list="false" :before-upload="beforeAvatarUpload">
  46. <video fit="contain" v-if="form.videos" :src="form.videos" class="avatar"></video>
  47. <el-icon v-else class="el-icon-plus avatar-uploader-icon">
  48. <Plus />
  49. </el-icon>
  50. </el-upload>
  51. </div>
  52. </el-form-item>
  53. <el-form-item label="详细地址" prop="address">
  54. <div style="display: flex;width: 100%;">
  55. <el-input v-model="form.address" @focus="showMap('input')" :readonly="!(form.latitude && form.longitude)" placeholder="请填写详细地址"></el-input>
  56. <el-button type="primary" @click="showMap()">{{(form.latitude && form.latitude)?'重新选择':'选择地址'}}</el-button>
  57. </div>
  58. </el-form-item>
  59. <el-form-item label="顺序" prop="sort">
  60. <el-input v-model="form.sort" type="number" show-word-limit />
  61. </el-form-item>
  62. <div style="height: 72px;"></div>
  63. <div style="position: fixed;bottom: 0;padding: 20px 0;z-index: 999;">
  64. <el-button type="primary" @click="toSave(1)">保存</el-button>
  65. <!--<el-button type="primary" @click="toSave(0)">存为草稿</el-button>-->
  66. <!--<el-button type="primary" @click="toPreview">预览</el-button>-->
  67. <el-button type="info" @click="toCancel">关闭</el-button>
  68. </div>
  69. </el-form>
  70. <avue-input-map v-show="false" ref='amap' :params="{zoom:10, zoomEnable: true, dragEnable: true}" placeholder="请选择地址" v-model="mapForm"></avue-input-map>
  71. </template>
  72. <script>
  73. // import contentItem from "@/components/richtext/tinymce.vue"
  74. import {
  75. getDetail,
  76. add,
  77. update
  78. } from "@/api/ycwh/famousSnacks";
  79. import {
  80. uploadFile
  81. } from "@/api/oss/oss";
  82. import { getLazyTree } from "@/api/base/region";
  83. import { getDictionary } from "@/api/system/dictbiz";
  84. //
  85. export default {
  86. props: ['id'],
  87. components: {
  88. // contentItem
  89. },
  90. data() {
  91. var checkListNull = (rule, value, cb) => {
  92. if(value && value.length > 0) {
  93. return cb();
  94. }
  95. cb(new Error("请选择标签"));
  96. };
  97. return {
  98. loading: false,
  99. //
  100. cuisineList: [],
  101. regionList: [],
  102. //
  103. attachmentList: [],
  104. // 提交内容
  105. form: {
  106. id: null,
  107. name: '',
  108. subTitle: '',
  109. content: '',
  110. // 标签
  111. videos: null,
  112. cuisineCode: '',
  113. regionId: '',
  114. address: '',
  115. latitude: '',
  116. longitude: '',
  117. top: '',
  118. sort: ''
  119. },
  120. //
  121. contentItemVisible: false,
  122. rules: {
  123. name: [{
  124. required: true,
  125. message: '请输入名称',
  126. trigger: 'blur'
  127. },
  128. {
  129. min: 1,
  130. max: 80,
  131. message: '长度不能超过80个字',
  132. trigger: 'blur'
  133. }
  134. ],
  135. subTitle: [{
  136. required: true,
  137. message: '请输入副标题',
  138. trigger: 'blur'
  139. },
  140. {
  141. min: 1,
  142. max: 90,
  143. message: '长度不能超过90个字',
  144. trigger: 'blur'
  145. }
  146. ],
  147. cover: [{
  148. required: true,
  149. message: '请上传封面',
  150. trigger: 'blur',
  151. }],
  152. content: [{
  153. required: true,
  154. message: '请输入内容',
  155. trigger: 'blur'
  156. }],
  157. regionId: [{
  158. required: true,
  159. message: '请选择所属区域',
  160. trigger: 'blur'
  161. }],
  162. // address: [{
  163. // required: true,
  164. // message: '请输入详细地址',
  165. // trigger: 'blur'
  166. // }],
  167. },
  168. // 富文本相关配置
  169. editorOptions: {
  170. //普通上传地址
  171. action: "https://api.avuejs.com/imgupload",
  172. customConfig: {}, //wangEditor编辑的配置
  173. //是否启用oss(qiniu/ali)
  174. oss: '',
  175. headers: {},
  176. data: {},
  177. propsHttp: {
  178. home: '',
  179. url: 'url',
  180. res: 'data'
  181. },
  182. //七牛云oss配置
  183. qiniu: {
  184. AK: "",
  185. SK: "",
  186. scope: "",
  187. url: "",
  188. deadline: 1
  189. },
  190. //阿里云oss配置
  191. ali: {
  192. region: "",
  193. endpoint: "",
  194. accessKeyId: "",
  195. accessKeySecret: "",
  196. bucket: ""
  197. }
  198. },
  199. mapForm: [],
  200. }
  201. },
  202. watch: {},
  203. created() {
  204. this.toReloadData()
  205. },
  206. mounted() {
  207. if(this.id && this.id > 0) {
  208. this.getDetail(this.id)
  209. }
  210. this.getCuisineList();
  211. this.getRegionList();
  212. },
  213. watch: {
  214. 'mapForm' () {
  215. if(this.mapForm && this.mapForm.length == 3) {
  216. this.form.longitude = this.mapForm[0];
  217. this.form.latitude = this.mapForm[1];
  218. this.form.address = this.mapForm[2];
  219. }
  220. },
  221. },
  222. methods: {
  223. getCuisineList() {
  224. getDictionary({
  225. code: "cuisine"
  226. }).then(res => {
  227. this.cuisineList = res.data.data || [];
  228. })
  229. },
  230. getRegionList() {
  231. getLazyTree("44").then(res => {
  232. this.regionList = res.data.data || [];
  233. })
  234. },
  235. showMap(formType) {
  236. if(formType === 'input') {
  237. if(!(this.form.longitude && this.form.latitude)) {
  238. this.$refs.amap.handleShow();
  239. }
  240. } else {
  241. this.$refs.amap.handleShow();
  242. }
  243. },
  244. getDetail(id) {
  245. if(!(id && id > 0)) {
  246. return;
  247. }
  248. /** 获取对应的文章详情 */
  249. getDetail(id).then(res => {
  250. if(res.data.success && res.data.data) {
  251. let data = res.data.data;
  252. if(data.regionId && data.regionId > 0) {
  253. data.regionId = data.regionId.toString()
  254. }
  255. if(data.content) {
  256. data.content = decodeURIComponent(data.content)
  257. }
  258. //
  259. this.form = data;
  260. }
  261. })
  262. },
  263. toReloadData(data) { //
  264. this.form = data || {}
  265. this.contentItemVisible = true
  266. },
  267. toSave(isPublish) {
  268. if(this.loading) {
  269. return;
  270. }
  271. this.loading = true;
  272. this.$refs.form.validate((valid) => {
  273. if(valid) { //校验通过
  274. let submitObj = JSON.parse(JSON.stringify(this.form)) //克隆一份数据,避免影响到富文本编辑器
  275. submitObj.content = encodeURIComponent(submitObj.content)
  276. /** id不为空则为编辑,为空则新增 */
  277. let saveFunc = submitObj.id ? update : add
  278. this.loading = true;
  279. // 发布或存为草稿
  280. submitObj.isPublish = isPublish || 0;
  281. // 处理附件列表
  282. if(this.attachmentList && this.attachmentList.length > 0) {
  283. submitObj.attachment = ""
  284. this.attachmentList.map(e => {
  285. if(submitObj.attachment) {
  286. submitObj.attachment += "," + e.name + "::" + e.url
  287. } else {
  288. submitObj.attachment += e.name + "::" + e.url
  289. }
  290. })
  291. } else {
  292. submitObj.attachment = ''
  293. }
  294. // content
  295. submitObj.content = submitObj.content || ''
  296. saveFunc(submitObj).then(() => {
  297. this.$message({
  298. type: "success",
  299. message: "保存成功!"
  300. })
  301. this.$emit("toCancel", true)
  302. }, error => {
  303. this.loading = false;
  304. window.console.log(error)
  305. })
  306. } else { //校验不通过
  307. this.$message.error('存在未完善的内容,请完善后再试!');
  308. this.loading = false;
  309. return false;
  310. }
  311. })
  312. },
  313. toView() {
  314. // 预览
  315. },
  316. toCancel() {
  317. this.$emit("toCancel", false)
  318. },
  319. // 图片上传
  320. uploadImg(params) { // 调用接口上传不使用upload上传
  321. if(!(params.data && params.data.type)) {
  322. // att img
  323. return;
  324. }
  325. let uploadType = params.data.type;
  326. //
  327. let fd = new FormData();
  328. let f = params.file;
  329. fd.append("file", f);
  330. uploadFile(fd).then(res => {
  331. if(res && res.data && res.data.success) {
  332. // 提交内容
  333. if(uploadType == 'img') {
  334. this.form.cover = res.data.data.link;
  335. this.$refs.form.validateField(["cover"], () => {
  336. // 只触发校验不做其他处理
  337. })
  338. } else if(uploadType == 'video') {
  339. this.form.videos = res.data.data.link;
  340. this.$refs.form.validateField(["videos"], () => {
  341. // 只触发校验不做其他处理
  342. })
  343. } else if(uploadType == 'att') {
  344. this.attachmentList.push({
  345. uid: f.uid,
  346. name: f.name,
  347. url: res.data.data.link
  348. })
  349. }
  350. this.$forceUpdate();
  351. }
  352. this.loading = false;
  353. }, e => {
  354. console.log(e)
  355. this.loading = false;
  356. });
  357. },
  358. beforeAvatarUpload(file) {
  359. // 封面限制5m 附件暂时不限制
  360. // const isJPG = file.type === 'image/jpeg';
  361. // if(!isJPG) {
  362. // this.$message.error('上传头像图片只能是 JPG 格式!');
  363. // }
  364. const isLt5M = (file.size / 1024 / 1024) < 5;
  365. if(!isLt5M) {
  366. this.$message.error('上传头像图片大小不能超过 5MB!');
  367. }
  368. return true;
  369. },
  370. handlePreview(file) {
  371. window.open(file.url);
  372. },
  373. handleRemoveImg(file, fileList) {
  374. this.attachmentList = fileList;
  375. },
  376. toPreview() {
  377. let submitObj = JSON.parse(JSON.stringify(this.form)) //克隆一份数据,避免影响到富文本编辑器
  378. /** id不为空则为编辑,为空则新增 */
  379. this.loading = true;
  380. // 标签预览需要处理
  381. // content
  382. submitObj.content = submitObj.content || ''
  383. // 新选择的标签、专栏需要单独赋值提供显示
  384. // goPreview(this, "article", submitObj, this.tagOptions)
  385. }
  386. },
  387. }
  388. </script>
  389. <style scoped lang="scss">
  390. @import "./index.scss"
  391. </style>