addChampionSay.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <style lang="scss">
  2. .avatar-uploader .el-upload {
  3. border: 1px dashed #d9d9d9;
  4. border-radius: 6px;
  5. cursor: pointer;
  6. position: relative;
  7. overflow: hidden;
  8. }
  9. .avatar-uploader .el-upload:hover {
  10. border-color: #409EFF;
  11. }
  12. .avatar-uploader-icon {
  13. font-size: 28px;
  14. color: #8c939d;
  15. width: 178px;
  16. height: 178px;
  17. line-height: 178px;
  18. text-align: center;
  19. }
  20. .avatar {
  21. width: 178px;
  22. height: 178px;
  23. display: block;
  24. }
  25. </style>
  26. <template>
  27. <section class="content">
  28. <h4>新增冠军心得</h4>
  29. <el-divider></el-divider>
  30. <el-form label-width="80px">
  31. <el-form-item label="标题">
  32. <el-input placeholder="标题" v-model="form.name"></el-input>
  33. </el-form-item>
  34. <el-form-item label="封面图">
  35. <el-upload
  36. class="avatar-uploader"
  37. action="/api/admin/uploadfile"
  38. :show-file-list="false"
  39. :on-success="handleAvatarSuccess"
  40. :before-upload="beforeAvatarUpload"
  41. >
  42. <img v-if="form.img" :src="form.img" class="avatar" />
  43. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  44. </el-upload>
  45. </el-form-item>
  46. <el-form-item label="详情">
  47. <fuEditor
  48. v-model="form.content"
  49. :isClear="isClear"
  50. @change="change"
  51. ></fuEditor>
  52. </el-form-item>
  53. <el-form-item label="状态" prop="status">
  54. <el-select v-model="form.status" placeholder="请选择状态">
  55. <el-option key="1" label="编辑中" :value="1"></el-option>
  56. <el-option key="2" label="上线" :value="2"></el-option>
  57. <el-option key="-1" label="下线" :value="-1"></el-option>
  58. </el-select>
  59. </el-form-item>
  60. <el-form-item label="">
  61. <el-button type="primary" @click="save">保存</el-button>
  62. </el-form-item>
  63. </el-form>
  64. </section>
  65. </template>
  66. <script>
  67. import fuEditor from "@/components/fuEditor/index.vue";
  68. export default {
  69. components: {
  70. fuEditor,
  71. },
  72. data() {
  73. return {
  74. imageUrl:"",
  75. test: "",
  76. list: [],
  77. form: {
  78. title: "",
  79. category_id: "",
  80. content: "",
  81. imgs: [],
  82. address: "",
  83. point: "",
  84. status: 1,
  85. img:""
  86. },
  87. isClear: false,
  88. keyword: "",
  89. point: {
  90. lng: "",
  91. lat: "",
  92. },
  93. BMap: null,
  94. };
  95. },
  96. methods: {
  97. getData() {
  98. this.$api.getArticleById({ id: this.form.id }).then((res) => {
  99. this.form = res.data.data;
  100. });
  101. },
  102. getContent() {
  103. this.$api.getContentList({ page: this.page }).then((res) => {
  104. this.loading = false;
  105. if (res.status == 200) {
  106. this.list = res.data.data.list;
  107. } else {
  108. this.$message({
  109. message: res.message,
  110. type: "error",
  111. });
  112. }
  113. });
  114. },
  115. change(val) {
  116. this.form.content = val;
  117. },
  118. upload(type) {
  119. var file = document.getElementById(type).files;
  120. var data = new FormData();
  121. data.append("file", file[0]);
  122. this.$api.uploadFile(data).then((res) => {
  123. if (res.data.code == 0) {
  124. let imgs = this.form.imgs || [];
  125. imgs.push(res.data.data.url);
  126. this.form.imgs = imgs;
  127. // this.$set(form,type,res.data.data.url)
  128. this.$message({ message: "上传成功!", type: "success" });
  129. } else {
  130. this.$message.error(res.data.message);
  131. }
  132. });
  133. },
  134. save() {
  135. var parm = this.form;
  136. // parm.point=this.point.lng+','+this.point.lat
  137. parm.type = "champion";
  138. if (parm.id) {
  139. // debugger;
  140. this.$api.editArticle(parm).then((res) => {
  141. if (res.data.code == 0) {
  142. this.$message({ message: "修改成功!", type: "success" });
  143. this.$router.push({ path: "/championsay" });
  144. } else {
  145. this.$message.error(res.data.message);
  146. }
  147. });
  148. } else {
  149. this.$api.addArticle(parm).then((res) => {
  150. if (res.data.code == 0) {
  151. this.$message({ message: "添加成功!", type: "success" });
  152. this.$router.push({ path: "/championsay" });
  153. } else {
  154. this.$message.error(res.data.message);
  155. }
  156. });
  157. }
  158. },
  159. handleAvatarSuccess(res, file) {
  160. this.form.img = res.data.url;
  161. },
  162. beforeAvatarUpload(file) {
  163. return
  164. const isJPG = file.type === "image/jpeg";
  165. const isLt2M = file.size / 1024 / 1024 < 2;
  166. if (!isJPG) {
  167. this.$message.error("上传头像图片只能是 JPG 格式!");
  168. }
  169. if (!isLt2M) {
  170. this.$message.error("上传头像图片大小不能超过 2MB!");
  171. }
  172. return isJPG && isLt2M;
  173. },
  174. onBaiduMapReady(e) {
  175. // const that = this
  176. this.BMap = e.BMap;
  177. },
  178. getClickInfo(e) {
  179. // 调整地图中心位置
  180. this.point = e.point;
  181. // 此时已经可以获取到BMap类
  182. if (this.BMap) {
  183. const that = this;
  184. // Geocoder() 类进行地址解析
  185. // 创建地址解析器的实例
  186. const geoCoder = new this.BMap.Geocoder();
  187. // getLocation() 类--利用坐标获取地址的详细信息
  188. // getPoint() 类--获取位置对应的坐标
  189. geoCoder.getLocation(e.point, function (res) {
  190. const addrComponent = res.addressComponents;
  191. const surroundingPois = res.surroundingPois;
  192. const province = addrComponent.province;
  193. const city = addrComponent.city;
  194. const district = addrComponent.district;
  195. let addr = addrComponent.street;
  196. if (surroundingPois.length > 0 && surroundingPois[0].title) {
  197. if (addr) {
  198. addr += `-${surroundingPois[0].title}`;
  199. } else {
  200. addr += `${surroundingPois[0].title}`;
  201. }
  202. } else {
  203. addr += addrComponent.streetNumber;
  204. }
  205. that.form.address = province + city + district + addr;
  206. });
  207. }
  208. },
  209. },
  210. created() {
  211. // this.getContent()
  212. if (this.$route.query.id) {
  213. this.form.id = this.$route.query.id;
  214. this.id = this.$route.query.id;
  215. this.getData();
  216. }
  217. },
  218. };
  219. </script>