banner.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <template>
  2. <div class="pb_content" style="background: unset">
  3. <div
  4. class="pb_content_body"
  5. style="
  6. background: #fff;
  7. padding: 0px 25px;
  8. box-sizing: border-box;
  9. border-radius: 5px;
  10. "
  11. >
  12. <div class="pb_head">
  13. <span>Banner列表</span>
  14. </div>
  15. <div class="student_head">
  16. <div class="head_right">
  17. <el-button @click="dialogVisible = true">添加Banner</el-button>
  18. </div>
  19. </div>
  20. </div>
  21. <div class="pb_content_body">
  22. <div class="student_table">
  23. <el-table
  24. ref="table"
  25. :data="tableData"
  26. border
  27. :height="tableHeight"
  28. :fit="true"
  29. v-loading="isLoading"
  30. style="width: 100%"
  31. :header-cell-style="{ background: '#f1f1f1', fontSize: '17px' }"
  32. :row-class-name="tableRowClassName"
  33. >
  34. <el-table-column label="图片" min-width="60" align="center">
  35. <template slot-scope="scope">
  36. <el-image
  37. style="width: 200px; height: 100px; cursor: pointer"
  38. :src="scope.row.poster"
  39. fit="cover"
  40. @click="handlePictureCardPreviewA(scope.row.poster)"
  41. ></el-image>
  42. </template>
  43. </el-table-column>
  44. <el-table-column prop="type" label="分类" min-width="20">
  45. <template slot-scope="scope">
  46. {{
  47. scope.row.type == 1
  48. ? "绘画"
  49. : scope.row.type == 2
  50. ? "科技"
  51. : scope.row.type == 3
  52. ? "人文"
  53. : "艺术"
  54. }}
  55. </template>
  56. </el-table-column>
  57. <el-table-column label="操作" min-width="20">
  58. <template slot-scope="scope">
  59. <el-button
  60. type="primary"
  61. size="small"
  62. @click="deleteBList(scope.row.id)"
  63. >删除</el-button
  64. >
  65. </template>
  66. </el-table-column>
  67. </el-table>
  68. </div>
  69. <div class="student_page">
  70. <el-pagination
  71. background
  72. layout="prev, pager, next"
  73. :page-size="10"
  74. :total="total"
  75. v-if="page"
  76. @current-change="handleCurrentChange"
  77. ></el-pagination>
  78. </div>
  79. </div>
  80. <el-dialog
  81. title="添加Banner"
  82. :visible.sync="dialogVisible"
  83. width="500px"
  84. :before-close="handleClose"
  85. class="dialog_diy"
  86. center
  87. >
  88. <div>
  89. <div class="dialogBox">
  90. <span>添加Banner</span>
  91. <el-upload
  92. :class="{ disUoloadSty: noneBtnImg }"
  93. action="#"
  94. list-type="picture-card"
  95. :on-preview="handlePictureCardPreview"
  96. :on-remove="handleRemove"
  97. v-loading="uploadLoading"
  98. :http-request="beforeUpload"
  99. ref="upload"
  100. :show-file-list="true"
  101. :limit="1"
  102. accept="image/*"
  103. >
  104. <i class="el-icon-plus"></i>
  105. </el-upload>
  106. </div>
  107. <div class="dialogBox" style="color: #bebebe; margin: 10px 0 0 0">
  108. 注:适宜上传banner尺寸为1218×300
  109. </div>
  110. <div class="cTypeCss">
  111. <div>分类:</div>
  112. <el-select v-model="cType" placeholder="请选择分类">
  113. <el-option
  114. v-for="(t, tIndex) in typeList"
  115. :key="tIndex"
  116. :label="t.label"
  117. :value="t.value"
  118. ></el-option>
  119. </el-select>
  120. </div>
  121. </div>
  122. <span slot="footer" class="dialog-footer">
  123. <el-button @click="dialogVisible = false">取 消</el-button>
  124. <el-button type="primary" @click="addBanner">确 定</el-button>
  125. </span>
  126. </el-dialog>
  127. <el-dialog :visible.sync="imgVisible">
  128. <img width="100%" :src="dialogImageUrl" alt="" />
  129. </el-dialog>
  130. <el-dialog :visible.sync="imgVisible1">
  131. <img width="100%" :src="dialogImageUrl1" alt="" />
  132. </el-dialog>
  133. </div>
  134. </template>
  135. <script>
  136. export default {
  137. data() {
  138. return {
  139. tableHeight: "500px",
  140. isLoading: false,
  141. formLabelWidth: "100px",
  142. tableData: [],
  143. fileList: [],
  144. typeList: [
  145. {
  146. value: 1,
  147. label: "绘画",
  148. },
  149. {
  150. value: 2,
  151. label: "科技",
  152. },
  153. {
  154. value: 3,
  155. label: "人文",
  156. },
  157. {
  158. value: 4,
  159. label: "艺术",
  160. },
  161. ],
  162. cType: "",
  163. dialogVisible: false,
  164. noneBtnImg: false,
  165. dialogImageUrl: "",
  166. imgVisible: false,
  167. dialogImageUrl1: "",
  168. imgVisible1: false,
  169. page: 1,
  170. total: 0,
  171. userid: this.$route.query.userid,
  172. oid: this.$route.query.oid,
  173. cid: this.$route.query.cid,
  174. uploadLoading: false,
  175. tx: require("../../assets/avatar.png"),
  176. };
  177. },
  178. mounted() {
  179. this.$nextTick(function () {
  180. this.tableHeight =
  181. window.innerHeight - this.$refs.table.$el.offsetTop - 200;
  182. if (this.tableHeight <= 530) {
  183. this.tableHeight = 530;
  184. }
  185. // 监听窗口大小变化
  186. let self = this;
  187. window.onresize = function () {
  188. self.tableHeight =
  189. window.innerHeight - self.$refs.table.$el.offsetTop - 200;
  190. if (self.tableHeight <= 530) {
  191. self.tableHeight = 530;
  192. }
  193. };
  194. });
  195. },
  196. methods: {
  197. tableRowClassName({ row, rowIndex }) {
  198. if ((rowIndex + 1) % 2 === 0) {
  199. return "even_row";
  200. } else {
  201. return "";
  202. }
  203. },
  204. handleClose(done) {
  205. done();
  206. },
  207. handleCurrentChange(val) {
  208. this.page = val;
  209. this.getBannerList();
  210. },
  211. handlePictureCardPreviewA(url) {
  212. this.dialogImageUrl1 = url;
  213. this.imgVisible1 = true;
  214. },
  215. imgChange(file, fileList) {
  216. var _tmp = this.fileList;
  217. this.noneBtnImg = _tmp.length >= 1;
  218. },
  219. handleRemove(file) {
  220. var _tmp = this.fileList;
  221. for (var i = 0, len = _tmp.length; i < len; i++) {
  222. if (_tmp[i].uid == file.uid) {
  223. _tmp.splice(i, 1);
  224. break;
  225. }
  226. this.fileList = _tmp;
  227. }
  228. this.noneBtnImg = _tmp.length >= 1;
  229. },
  230. handlePictureCardPreview(file) {
  231. this.dialogImageUrl = this.fileList[0].url;
  232. this.imgVisible = true;
  233. },
  234. time() {
  235. if (!this.now) {
  236. this.now = new Date().getTime();
  237. return true;
  238. } else {
  239. let time = new Date().getTime();
  240. if (time - this.now > 3000) {
  241. this.now = time;
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. },
  248. beforeUpload(data) {
  249. this.$refs.upload.uploadFiles;
  250. this.uploadLoading = true;
  251. var file = data.file;
  252. var credentials = {
  253. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  254. secretAccessKey: "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",
  255. }; //秘钥形式的登录上传
  256. window.AWS.config.update(credentials);
  257. window.AWS.config.region = "cn-northwest-1"; //设置区域
  258. var bucket = new window.AWS.S3({ params: { Bucket: "ccrb" } }); //选择桶
  259. var _this = this;
  260. if (file) {
  261. var params = {
  262. Key:
  263. file.name.split(".")[0] +
  264. new Date().getTime() +
  265. "." +
  266. file.name.split(".")[1],
  267. ContentType: file.type,
  268. Body: file,
  269. "Access-Control-Allow-Credentials": "*",
  270. ACL: "public-read",
  271. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  272. var options = {
  273. partSize: 2048 * 1024 * 1024,
  274. queueSize: 2,
  275. leavePartsOnError: true,
  276. };
  277. bucket
  278. .upload(params, options)
  279. .on("httpUploadProgress", function (evt) {
  280. //这里可以写进度条
  281. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  282. })
  283. .send(function (err, data) {
  284. _this.uploadLoading = false;
  285. if (err) {
  286. var a = _this.$refs.upload.uploadFiles;
  287. a.splice(a.length - 1, a.length);
  288. _this.$message.error("上传失败");
  289. } else {
  290. //上传成功处理
  291. _this.fileList.push({
  292. name: file.name,
  293. url: data.Location,
  294. uid: file.uid,
  295. });
  296. console.log(data.Location);
  297. _this.imgChange();
  298. }
  299. });
  300. }
  301. },
  302. getBannerList() {
  303. this.isLoading = true;
  304. let params = {
  305. page: this.page,
  306. };
  307. this.ajax
  308. .get(this.$store.state.api + "selectBannerList", params)
  309. .then((res) => {
  310. this.isLoading = false;
  311. this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
  312. this.tableData = res.data[0];
  313. })
  314. .catch((err) => {
  315. this.isLoading = false;
  316. console.error(err);
  317. });
  318. },
  319. addBanner() {
  320. var list = this.fileList;
  321. if (list.length == 0) {
  322. this.$message.error("请上传需要添加的banner");
  323. return;
  324. }
  325. if (this.time()) {
  326. let params = [
  327. {
  328. url: list[0].url,
  329. t: this.cType,
  330. },
  331. ];
  332. this.ajax
  333. .post(this.$store.state.api + "insertBannerList", params)
  334. .then((res) => {
  335. this.$message({
  336. message: "添加成功",
  337. type: "success",
  338. });
  339. this.dialogVisible = false;
  340. this.cType = "";
  341. this.getBannerList();
  342. this.init();
  343. })
  344. .catch((err) => {
  345. this.$message.error("添加失败");
  346. console.error(err);
  347. });
  348. }
  349. },
  350. deleteBList(id) {
  351. let params = {
  352. id: id,
  353. };
  354. this.ajax
  355. .get(this.$store.state.api + "deleteBannerList", params)
  356. .then((res) => {
  357. this.$message({
  358. message: "删除成功",
  359. type: "success",
  360. });
  361. this.getBannerList();
  362. })
  363. .catch((err) => {
  364. this.$message.error("删除失败");
  365. console.error(err);
  366. });
  367. },
  368. init() {
  369. this.fileList = [];
  370. this.$refs.upload ? (this.$refs.upload.uploadFiles.length = 0) : "";
  371. this.noneBtnImg = false;
  372. this.dialogImageUrl = "";
  373. },
  374. },
  375. created() {
  376. this.page = 1;
  377. this.getBannerList();
  378. },
  379. };
  380. </script>
  381. <style scoped>
  382. .pb_head > span:nth-child(2) {
  383. font-size: 20px;
  384. margin-left: 5px;
  385. color: #828282;
  386. }
  387. .pb_head {
  388. margin: 0 !important;
  389. width: 100% !important;
  390. }
  391. .student_page {
  392. margin-top: 10px;
  393. }
  394. .student_head {
  395. margin-top: 10px;
  396. padding-bottom: 10px;
  397. display: flex;
  398. justify-content: flex-end;
  399. flex-direction: row;
  400. flex-wrap: nowrap;
  401. align-items: center;
  402. }
  403. .head_left {
  404. display: flex;
  405. align-items: center;
  406. }
  407. .head_right {
  408. display: flex;
  409. flex-direction: row;
  410. flex-wrap: nowrap;
  411. align-items: baseline;
  412. }
  413. .student_input >>> .el-input__inner {
  414. height: 30px;
  415. width: 190px;
  416. font-size: 13px;
  417. padding: 0 10px;
  418. }
  419. .student_button {
  420. color: #fff;
  421. background: #2268bc;
  422. width: 60px;
  423. height: 30px;
  424. padding: 0 !important;
  425. font-size: 12px;
  426. line-height: 30px;
  427. }
  428. .head_right > button:nth-child(1) {
  429. color: #fff;
  430. background: #2268bc;
  431. width: 120px;
  432. height: 35px;
  433. padding: 0 !important;
  434. font-size: 12px;
  435. line-height: 35px;
  436. }
  437. .head_right > button:nth-child(2) {
  438. color: #fff;
  439. background: #2268bc;
  440. width: 70px;
  441. height: 30px;
  442. padding: 0 !important;
  443. font-size: 12px;
  444. line-height: 30px;
  445. }
  446. .head_right > div {
  447. font-size: 12px;
  448. line-height: 40px;
  449. margin-left: 10px;
  450. color: #2a6dbe;
  451. text-decoration: underline;
  452. cursor: pointer;
  453. }
  454. .student_table >>> .el-table--border td {
  455. border-right: 0px !important;
  456. }
  457. .student_table >>> .el-table,
  458. .student_table >>> .el-table__body-wrapper {
  459. height: auto !important;
  460. }
  461. .el-table >>> .even_row {
  462. background-color: #f1f1f1 !important;
  463. }
  464. .de_button {
  465. color: #fff;
  466. background: #5190fd;
  467. width: 50px;
  468. height: 25px;
  469. padding: 0 !important;
  470. font-size: 12px;
  471. line-height: 25px;
  472. }
  473. .add_student >>> .el-dialog__header {
  474. padding: 20px 20px 10px;
  475. text-align: center;
  476. background: #32455b;
  477. }
  478. .add_student >>> .el-dialog__title {
  479. font-size: 14px !important;
  480. color: #fff !important;
  481. }
  482. .add_student >>> .el-dialog__headerbtn {
  483. font-size: 20px !important;
  484. }
  485. .add_student >>> .el-form-item__label {
  486. margin-left: 65px;
  487. }
  488. .add_student >>> .el-form-item {
  489. display: flex;
  490. }
  491. .add_student >>> .el-form-item__content {
  492. margin: 0 !important;
  493. }
  494. .add_input {
  495. width: 365px;
  496. }
  497. .add_student >>> .el-dialog__footer {
  498. text-align: center !important;
  499. }
  500. .right {
  501. width: 250px;
  502. color: #fff;
  503. background: #0e72e6;
  504. margin-bottom: 20px;
  505. }
  506. .header-title {
  507. display: flex;
  508. }
  509. .logoImg {
  510. width: 30px;
  511. }
  512. .logoImg > img {
  513. width: 100%;
  514. height: 100%;
  515. }
  516. .title_add_student {
  517. margin: 0 auto;
  518. color: #fff;
  519. }
  520. .upload-demo {
  521. line-height: 0px !important;
  522. }
  523. .upload-demo >>> .el-button {
  524. color: #fff;
  525. background: #2268bc;
  526. width: 70px;
  527. height: 30px;
  528. padding: 0 !important;
  529. font-size: 12px;
  530. line-height: 0 !important;
  531. }
  532. .userImg {
  533. display: flex;
  534. flex-direction: row;
  535. justify-content: center;
  536. align-items: center;
  537. }
  538. .tx {
  539. width: 40px;
  540. margin-right: 10px;
  541. }
  542. .delete {
  543. width: 25px;
  544. cursor: pointer;
  545. }
  546. .tx > img,
  547. .delete > img {
  548. width: 100%;
  549. height: 100%;
  550. }
  551. .newCss {
  552. display: flex;
  553. flex-direction: row;
  554. flex-wrap: nowrap;
  555. align-items: baseline;
  556. }
  557. .disUoloadSty >>> .el-upload--picture-card {
  558. display: none; /* 上传按钮隐藏 */
  559. }
  560. .dialogBox {
  561. display: flex;
  562. align-items: center;
  563. justify-content: center;
  564. }
  565. .dialogBox span {
  566. margin: 0 20px;
  567. }
  568. .cTypeCss {
  569. display: flex;
  570. flex-direction: row;
  571. flex-wrap: nowrap;
  572. justify-content: center;
  573. align-items: center;
  574. padding-top: 20px;
  575. }
  576. </style>