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.

429 lines
12 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-button
  5. class="filter-item el-button el-button--primary el-button--mini"
  6. type="primary"
  7. icon="el-icon-edit"
  8. @click="handleCreate"
  9. >添加资讯</el-button
  10. >
  11. </div>
  12. <el-table
  13. :key="tableKey"
  14. v-loading="listLoading"
  15. :data="list"
  16. border
  17. fit
  18. highlight-current-row
  19. style="width: 100%"
  20. >
  21. <el-table-column label="ID" prop="id" align="center">
  22. <template slot-scope="{ row }">
  23. <span>{{ row.news_id }}</span>
  24. </template>
  25. </el-table-column>
  26. <el-table-column label="分类" align="center">
  27. <template slot-scope="{ row }">
  28. <span>{{ row.static | getStaticName(statics) }}</span>
  29. </template>
  30. </el-table-column>
  31. <el-table-column label="标题" align="center">
  32. <template slot-scope="{ row }">
  33. <span>{{ row.title }}</span>
  34. </template>
  35. </el-table-column>
  36. <el-table-column label="封面图" align="center">
  37. <template slot-scope="{ row }">
  38. <img :src="row.image" height="100" />
  39. </template>
  40. </el-table-column>
  41. <el-table-column label="外链" align="center">
  42. <template slot-scope="{ row }">
  43. <span>{{ row.url }}</span>
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="创建时间" align="center">
  47. <template slot-scope="{ row }">
  48. <span>{{ row.create_time }}</span>
  49. </template>
  50. </el-table-column>
  51. <el-table-column
  52. label="操作"
  53. align="center"
  54. class-name="small-padding fixed-width"
  55. >
  56. <template slot-scope="scope">
  57. <el-button
  58. size="mini"
  59. type="text"
  60. icon="el-icon-edit"
  61. @click="handleUpdate(scope.row)"
  62. >修改</el-button
  63. >
  64. <el-button
  65. size="mini"
  66. type="text"
  67. icon="el-icon-delete"
  68. @click="handleDelete(scope.row)"
  69. >删除</el-button
  70. >
  71. </template>
  72. </el-table-column>
  73. </el-table>
  74. <pagination
  75. v-show="total > 0"
  76. :total="total"
  77. :page.sync="listQuery.page"
  78. :limit.sync="listQuery.limit"
  79. @pagination="getList"
  80. />
  81. <!-- 添加或修改分类对话框 -->
  82. <el-drawer
  83. ref="drawer"
  84. :title="textMap[dialogStatus]"
  85. :visible.sync="dialogFormVisible"
  86. direction="rtl"
  87. custom-class="drawer"
  88. size="950px"
  89. >
  90. <div class="drawer__content">
  91. <el-form
  92. ref="dataForm"
  93. :model="temp"
  94. label-position="left"
  95. label-width="70px"
  96. style
  97. >
  98. <el-form-item label="类型">
  99. <el-select v-model="temp.static" placeholder="选择类型">
  100. <el-option
  101. v-for="(item, index) in statics"
  102. :key="index"
  103. :label="item.name"
  104. :value="item.value"
  105. ></el-option>
  106. </el-select>
  107. </el-form-item>
  108. <el-form-item label="关联文章">
  109. <el-select
  110. v-model="temp.article_id"
  111. remote
  112. :loading="searchArticleLoading"
  113. :remote-method="remoteMethod"
  114. reserve-keyword
  115. placeholder="请输入关键字搜索"
  116. filterable
  117. :clearable="true"
  118. @change="handleSelectBranch"
  119. >
  120. <el-option
  121. v-for="(item, index) in articles"
  122. :key="index"
  123. :label="item.title"
  124. :value="item.article_id"
  125. ></el-option>
  126. </el-select>
  127. </el-form-item>
  128. <el-form-item label="标题" prop="title">
  129. <el-input v-model="temp.title" />
  130. </el-form-item>
  131. <el-form-item label="外链" prop="url">
  132. <el-input v-model="temp.url" />
  133. </el-form-item>
  134. <el-form-item label="封面" prop="image" required>
  135. <img
  136. v-if="temp.image"
  137. :src="temp.image"
  138. class="el-upload el-upload--picture-card"
  139. style="float: left; width: auto"
  140. />
  141. <el-upload
  142. ref="sys_app_logo"
  143. :on-success="uploadSuccess"
  144. :on-error="uploadError"
  145. :action="uploadAction"
  146. style="float: left"
  147. list-type="picture-card"
  148. :show-file-list="false"
  149. :before-upload="validationImages"
  150. >
  151. <i class="el-icon-plus" />
  152. </el-upload>
  153. </el-form-item>
  154. </el-form>
  155. <div class="drawer__footer">
  156. <el-button type="primary" @click="submitForm"> </el-button>
  157. <el-button @click="dialogFormVisible = false"> </el-button>
  158. </div>
  159. </div>
  160. </el-drawer>
  161. </div>
  162. </template>
  163. <script>
  164. import {
  165. fetchAdList,
  166. createAd,
  167. updateAd,
  168. delAd,
  169. searchArticle,
  170. } from "@/api/ad";
  171. import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  172. export default {
  173. name: "adIndex",
  174. components: { Pagination },
  175. data() {
  176. return {
  177. tableKey: 0,
  178. list: null,
  179. total: 0,
  180. listLoading: true,
  181. listQuery: {
  182. page: 1,
  183. limit: 20,
  184. },
  185. uploadAction: process.env.VUE_APP_BASE_API + "/files/uploadFile",
  186. temp: {
  187. id: undefined,
  188. static: 1,
  189. title: "",
  190. url: "",
  191. article_id: undefined,
  192. image: "",
  193. is_show: 1,
  194. },
  195. statics: [
  196. { name: "大轮播图", value: 1 },
  197. { name: "小轮播图", value: 2 },
  198. { name: "资讯列表", value: 3 },
  199. ],
  200. dialogFormVisible: false,
  201. dialogStatus: "",
  202. textMap: {
  203. update: "Edit",
  204. create: "Create",
  205. },
  206. rules: {
  207. title: [
  208. { required: true, message: "title is required", trigger: "blur" },
  209. ],
  210. },
  211. downloadLoading: false,
  212. searchArticleLoading: false,
  213. articles: [],
  214. validation: true,
  215. };
  216. },
  217. created() {
  218. this.getList();
  219. },
  220. filters: {
  221. getStaticName(id, statics) {
  222. for (var i = 0; i < statics.length; i++) {
  223. if (id == statics[i].value) {
  224. return statics[i].name;
  225. }
  226. }
  227. return "";
  228. },
  229. },
  230. methods: {
  231. // 验证图片宽高
  232. async validationImages(file) {
  233. let desc = this.temp.static === 1 ? "bigSwiper" : "smallSwiper";
  234. let res = await this.$getImgWidth(file, desc);
  235. this.validation = res;
  236. return res;
  237. },
  238. getList() {
  239. this.listLoading = true;
  240. fetchAdList(this.listQuery).then((response) => {
  241. this.list = response.data.list;
  242. this.total = response.data.count;
  243. // Just to simulate the time of the request
  244. setTimeout(() => {
  245. this.listLoading = false;
  246. }, 1.5 * 1000);
  247. });
  248. },
  249. handleFilter() {
  250. this.listQuery.page = 1;
  251. this.getList();
  252. },
  253. handleModifyStatus(row, status) {
  254. this.$message({
  255. message: "操作Success",
  256. type: "success",
  257. });
  258. row.status = status;
  259. },
  260. resetTemp() {
  261. this.temp = {
  262. id: undefined,
  263. static: 1,
  264. title: "",
  265. url: "",
  266. article_id: undefined,
  267. image: "",
  268. is_show: 1,
  269. };
  270. },
  271. submitForm() {
  272. if (this.dialogStatus == "create") {
  273. this.createData();
  274. } else if (this.dialogStatus == "update") {
  275. this.updateData();
  276. }
  277. },
  278. handleCreate() {
  279. this.resetTemp();
  280. this.dialogStatus = "create";
  281. this.dialogFormVisible = true;
  282. // this.$nextTick(() => {
  283. // this.$refs['dataForm'].clearValidate()
  284. // })
  285. },
  286. createData() {
  287. // this.$refs['dataForm'].validate((valid) => {
  288. // if (valid) {
  289. createAd(this.temp).then(() => {
  290. this.dialogFormVisible = false;
  291. this.$message({
  292. message: "添加成功",
  293. type: "success",
  294. duration: 2 * 1000,
  295. });
  296. this.getList();
  297. });
  298. // }
  299. // })
  300. },
  301. handleUpdate(row) {
  302. // let news_id = row.news_id
  303. // fetchAdDetails({news_id}).then(res => {
  304. var that = this;
  305. searchArticle(row.article_id)
  306. .then((res) => {
  307. that.articles = res.data;
  308. that.temp = row;
  309. this.dialogStatus = "update";
  310. this.dialogFormVisible = true;
  311. })
  312. .catch(function (err) {
  313. console.log(err);
  314. });
  315. },
  316. updateData() {
  317. // this.$refs['dataForm'].validate((valid) => {
  318. // if (valid) {
  319. updateAd(this.temp).then(() => {
  320. this.dialogFormVisible = false;
  321. this.$message({
  322. message: "更新成功",
  323. type: "success",
  324. duration: 2 * 1000,
  325. });
  326. this.getList();
  327. });
  328. // }
  329. // })
  330. },
  331. handleDelete({ title, news_id }) {
  332. this.$confirm('是否确认删除名称为"' + title + '"的数据项?', "警告", {
  333. confirmButtonText: "确定",
  334. cancelButtonText: "取消",
  335. type: "warning",
  336. })
  337. .then(function () {
  338. console.log("del");
  339. return delAd({ news_id });
  340. })
  341. .then((response) => {
  342. if (response.code === 200) {
  343. this.$message({
  344. message: "删除成功",
  345. type: "success",
  346. duration: 2 * 1000,
  347. });
  348. this.open = false;
  349. this.getList();
  350. } else {
  351. this.$message({
  352. message: "删除失败",
  353. type: "error",
  354. duration: 2 * 1000,
  355. });
  356. }
  357. })
  358. .catch(function () {});
  359. },
  360. uploadSuccess(response, file) {
  361. if (this.validation && response.state == "SUCCESS") {
  362. this.temp.image = response.url;
  363. }
  364. },
  365. uploadFileSuccess(response, file) {
  366. let obj = {};
  367. obj.name = response.original;
  368. obj.url = response.url;
  369. this.temp.script_url = JSON.stringify(obj);
  370. },
  371. uploadRemove(file) {
  372. const uid = file.uid;
  373. const objKeyArr = Object.keys(this.listObj);
  374. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  375. if (this.listObj[objKeyArr[i]].uid === uid) {
  376. delete this.listObj[objKeyArr[i]];
  377. return;
  378. }
  379. }
  380. },
  381. uploadError(err) {
  382. this.$alert(err, "发生错误", {
  383. confirmButtonText: "确定",
  384. callback: (action) => {},
  385. });
  386. },
  387. handleSelectBranch(item) {
  388. console.log("远程搜索选中后返回的item:::::即value的值");
  389. console.log(item);
  390. //如果要获取选择的 id或者名字 从item中取值
  391. this.$set(this.temp, "article_id", item);
  392. },
  393. // 搜索模糊查询数据下拉框
  394. remoteMethod(query) {
  395. var that = this;
  396. searchArticle(query)
  397. .then(function (res) {
  398. //将取到的值进行遍历
  399. that.articles = res.data;
  400. that.list = that.states.map((item) => {
  401. // item.uid item.name根据接口里的关键字
  402. return { value: item.uid, label: item.name };
  403. });
  404. })
  405. .catch(function (err) {
  406. console.log(err);
  407. });
  408. return;
  409. },
  410. },
  411. };
  412. </script>
  413. <style lang="scss" scoped>
  414. .app-container {
  415. .filter-container {
  416. margin-left: 5px;
  417. margin-bottom: 6px;
  418. }
  419. ::v-deep .el-drawer__body {
  420. padding: 20px;
  421. }
  422. }
  423. </style>