Dictionary.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class="cases">
  3. <el-button type="primary" @click="openDialog()">新增用户</el-button>
  4. <el-table border :data="tableData" v-loading="loading" style="width: 100%">
  5. <el-table-column prop="Id" label="序号" width="180"></el-table-column>
  6. <el-table-column prop="Key" label="键" width="180"></el-table-column>
  7. <el-table-column prop="Content" label="值"></el-table-column>
  8. <el-table-column label="操作">
  9. <template slot-scope="scope">
  10. <el-button
  11. type="primary"
  12. icon="el-icon-edit"
  13. @click="handleEdit(scope.$index, scope.row)"
  14. ></el-button>
  15. <el-button
  16. type="danger"
  17. icon="el-icon-delete"
  18. @click="handleDelete(scope.$index, scope.row)"
  19. ></el-button>
  20. </template>
  21. </el-table-column>
  22. </el-table>
  23. <el-dialog title="案例编辑" :visible.sync="dialogFormVisible">
  24. <el-form :model="formData">
  25. <el-form-item label="数据键" :label-width="formLabelWidth">
  26. <el-input v-model="formData.Key" autocomplete="off"></el-input>
  27. </el-form-item>
  28. <el-form-item label="数据键" :label-width="formLabelWidth">
  29. <el-input v-model="formData.Content" autocomplete="off"></el-input>
  30. </el-form-item>
  31. </el-form>
  32. <div slot="footer" class="dialog-footer">
  33. <el-button @click="dialogFormVisible = false">取 消</el-button>
  34. <el-button type="primary" @click="handleCreateOrModify()">确 定</el-button>
  35. </div>
  36. </el-dialog>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. loading: true,
  44. dialogFormVisible: false,
  45. formLabelWidth: "120px",
  46. tableData: [],
  47. formData: {
  48. Id: 0,
  49. Key: "",
  50. Content: "",
  51. CreateTime: new Date()
  52. },
  53. options: {}
  54. };
  55. },
  56. mounted() {
  57. let token = "Browser " + sessionStorage.getItem("token");
  58. //window.console.log(token);
  59. this.options = {
  60. headers: {
  61. Authorization: token
  62. }
  63. };
  64. this.loadData();
  65. },
  66. methods: {
  67. loadData() {
  68. this.loading = true;
  69. this.$http
  70. .get(`DataDictionary/GetDataDictionaryAll?key=`)
  71. .then(response => {
  72. window.console.log(response);
  73. this.tableData = response.data;
  74. this.loading = false;
  75. })
  76. .catch(e => {
  77. this.$message({
  78. message: "网络或程序异常!" + e,
  79. type: "error"
  80. });
  81. });
  82. },
  83. openDialog() {
  84. // 清除数据
  85. this.formData.Id = 0;
  86. this.formData.Key = "";
  87. this.formData.Content = "";
  88. this.formData.CreateTime = new Date();
  89. this.dialogFormVisible = true;
  90. },
  91. // 新增
  92. handleCreateOrModify() {
  93. window.console.log(this.formData);
  94. //window.console.log(JSON.stringify(this.formData));
  95. if (!this.formData.Id) {
  96. // ID 无效时 视为新增
  97. this.loading = true;
  98. this.$http
  99. .post(
  100. "DataDictionary/CreateDataDictionary",
  101. this.formData,
  102. this.options
  103. )
  104. .then(response => {
  105. this.loading = false;
  106. window.console.log(response);
  107. this.$message({
  108. message: "创建成功!",
  109. type: "success"
  110. });
  111. this.dialogFormVisible = false;
  112. this.loadData();
  113. })
  114. .catch(e => {
  115. this.$message({
  116. message: "网络或程序异常!" + e,
  117. type: "error"
  118. });
  119. });
  120. } else {
  121. this.loading = true;
  122. this.$http
  123. .post(
  124. "DataDictionary/ModifiedDataDictionary",
  125. this.formData,
  126. this.options
  127. )
  128. .then(response => {
  129. this.loading = false;
  130. window.console.log(response);
  131. this.$message({
  132. message: "修改成功!",
  133. type: "success"
  134. });
  135. this.dialogFormVisible = false;
  136. this.loadData();
  137. })
  138. .catch(e => {
  139. this.$message({
  140. message: "网络或程序异常!" + e,
  141. type: "error"
  142. });
  143. });
  144. }
  145. },
  146. handleEdit(index, row) {
  147. window.console.log(index, row);
  148. this.formData = row;
  149. this.dialogFormVisible = true;
  150. },
  151. handleDelete(index, row) {
  152. window.console.log(index, row);
  153. this.$confirm("此操作将永久此条数据, 是否继续?", "提示", {
  154. confirmButtonText: "确定",
  155. cancelButtonText: "取消",
  156. type: "warning"
  157. })
  158. .then(() => {
  159. // 已确认删除
  160. // 调接口删除
  161. this.loading = true;
  162. this.$http
  163. .post(
  164. `DataDictionary/DeleteDataDictionary?id=${row.Id}`,
  165. null,
  166. this.options
  167. )
  168. .then(response => {
  169. this.loading = false;
  170. window.console.log(response);
  171. this.$message({
  172. message: "删除成功!",
  173. type: "success"
  174. });
  175. this.loadData();
  176. })
  177. .catch(e => {
  178. this.$message({
  179. message: "网络或程序异常!" + e,
  180. type: "error"
  181. });
  182. });
  183. })
  184. .catch(() => {
  185. this.$message({
  186. type: "info",
  187. message: "已取消删除"
  188. });
  189. });
  190. },
  191. //时间格式化
  192. dateFormat: function(row) {
  193. //row 表示一行数据, CreateTime 表示要格式化的字段名称
  194. let t = new Date(row.CreateTime);
  195. return t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate();
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. .el-table {
  202. margin-top: 20px;
  203. }
  204. </style>