|
| 1 | +<template> |
| 2 | + <div class="notice-container"> |
| 3 | + <div style="margin-bottom:10px;display: flex;justify-content: flex-end;"> |
| 4 | + <el-button @click="editForm" v-if="disabledInput">编辑</el-button> |
| 5 | + <el-button @click="editFormCancel" v-if="!disabledInput">取消</el-button> |
| 6 | + <el-button @click="editFormSave" v-if="!disabledInput">保存</el-button> |
| 7 | + </div> |
| 8 | + <div v-for="(value,index) in timeList" :key="index" style="display:flex;margin-bottom:20px"> |
| 9 | + <el-select v-model="value.hour" style="width:90px" placeholder="请选择" |
| 10 | + :disabled="disabledInput"> |
| 11 | + <el-option |
| 12 | + v-for="item in options" |
| 13 | + :key="item.value" |
| 14 | + :label="item.label" |
| 15 | + :value="item.value"> |
| 16 | + </el-option> |
| 17 | + </el-select> |
| 18 | + <el-input style="width:90px" :disabled="disabledInput" v-model="value.minute" |
| 19 | + @input="changeMinute(value.minute, index)" type="number"></el-input> |
| 20 | + <div class="center-item">分,将提醒您:</div> |
| 21 | + <el-input style="width:400px" :disabled="disabledInput" |
| 22 | + v-model="value.remark" placeholder="请输入备注,为空不保存哦!"></el-input> |
| 23 | + <i class="el-icon-delete" v-if="!disabledInput" @click="deleteThis(index)"></i> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | +</template> |
| 27 | +<script> |
| 28 | +export default { |
| 29 | + data() { |
| 30 | + return { |
| 31 | + key: 'noticeList', |
| 32 | + timeList: [], |
| 33 | + tempList: [], |
| 34 | + disabledInput: false, |
| 35 | + options: [ |
| 36 | + { label: '每小时', value: -1 }, { label: '0点', value: 0 }, { label: '1点', value: 1 }, |
| 37 | + { label: '2点', value: 2 }, { label: '3点', value: 3 }, { label: '4点', value: 4 }, |
| 38 | + { label: '5点', value: 5 }, { label: '6点', value: 6 }, { label: '7点', value: 7 }, |
| 39 | + { label: '8点', value: 8 }, { label: '9点', value: 9 }, { label: '10点', value: 10 }, |
| 40 | + { label: '11点', value: 11 }, { label: '12点', value: 12 }, |
| 41 | + { label: '13点', value: 13 }, { label: '14点', value: 14 }, |
| 42 | + { label: '15点', value: 15 }, { label: '16点', value: 16 }, { label: '17点', value: 17 }, |
| 43 | + { label: '18点', value: 18 }, |
| 44 | + { label: '19点', value: 19 }, { label: '20点', value: 20 }, { label: '21点', value: 21 }, |
| 45 | + { label: '22点', value: 22 }, |
| 46 | + { label: '23点', value: 23 }, |
| 47 | + ], |
| 48 | + }; |
| 49 | + }, |
| 50 | + created() { |
| 51 | + this.disabledInput = true; |
| 52 | + const res = localStorage.getItem(this.key); |
| 53 | + if (res !== undefined && res !== null && res !== '') { |
| 54 | + try { |
| 55 | + this.timeList = JSON.parse(res); |
| 56 | + } catch (e) { |
| 57 | + console.log(e); |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + methods: { |
| 62 | + deleteThis(index) { |
| 63 | + this.timeList.splice(index, 1); |
| 64 | + }, |
| 65 | + editForm() { |
| 66 | + if (this.timeList.length > 0) { |
| 67 | + this.tempList = JSON.parse(JSON.stringify(this.timeList)); |
| 68 | + } else { |
| 69 | + this.tempList = []; |
| 70 | + } |
| 71 | + this.timeList.push({ |
| 72 | + hour: -1, |
| 73 | + minute: 0, |
| 74 | + remark: '', |
| 75 | + }); |
| 76 | + this.disabledInput = !this.disabledInput; |
| 77 | + }, |
| 78 | + editFormCancel() { |
| 79 | + this.timeList = this.tempList; |
| 80 | + this.disabledInput = !this.disabledInput; |
| 81 | + }, |
| 82 | + editFormSave() { |
| 83 | + const checkExistTime = {}; |
| 84 | + for (let i = 0; i < this.timeList.length; i += 1) { |
| 85 | + if (checkExistTime[`${this.timeList[i].hour}_${this.timeList[i].minute}`] === undefined) { |
| 86 | + checkExistTime[`${this.timeList[i].hour}_${this.timeList[i].minute}`] = 0; |
| 87 | + } |
| 88 | + checkExistTime[`${this.timeList[i].hour}_${this.timeList[i].minute}`] += 1; |
| 89 | + } |
| 90 | + let checkOk = true; |
| 91 | + for (const key in checkExistTime) { |
| 92 | + if (checkExistTime[key] > 1) { |
| 93 | + checkOk = false; |
| 94 | + this.$message.error('时间有重复,请检查'); |
| 95 | + } |
| 96 | + } |
| 97 | + if (!checkOk) { |
| 98 | + return; |
| 99 | + } |
| 100 | + const rows = []; |
| 101 | + for (let i = 0; i < this.timeList.length; i += 1) { |
| 102 | + if (this.timeList[i].remark !== '') { |
| 103 | + rows.push(this.timeList[i]); |
| 104 | + } |
| 105 | + } |
| 106 | + const jsonStr = JSON.stringify(rows); |
| 107 | + localStorage.setItem(this.key, jsonStr); |
| 108 | + this.timeList = rows; |
| 109 | + chrome.storage.local.set({ noticeList: jsonStr }, () => { |
| 110 | + console.log('ok'); |
| 111 | + }); |
| 112 | + this.disabledInput = !this.disabledInput; |
| 113 | + }, |
| 114 | + changeMinute(val, index) { |
| 115 | + if (val > 60) { |
| 116 | + this.timeList[index].minute = 0; |
| 117 | + } else if (val < 0) { |
| 118 | + this.timeList[index].minute = 0; |
| 119 | + } |
| 120 | + }, |
| 121 | + }, |
| 122 | +}; |
| 123 | +</script> |
| 124 | +<style lang="sass" scope> |
| 125 | +.notice-container |
| 126 | + font-size: 14px |
| 127 | +.center-item |
| 128 | + display: flex |
| 129 | + justify-content: center |
| 130 | + align-items: center |
| 131 | +</style> |
0 commit comments