addStorage.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view>
  3. <view
  4. class="editMask"
  5. v-if="isShow"
  6. :style="{
  7. height: height + 'px',
  8. }"
  9. @click.stop
  10. >
  11. <view
  12. class="editDialog"
  13. @click.stop
  14. >
  15. <text class="title">新增缓存</text>
  16. <input
  17. type="text"
  18. placeholder="请输入Key"
  19. class="input"
  20. v-model="key"
  21. />
  22. <input
  23. type="text"
  24. placeholder="请输入Value"
  25. class="input"
  26. v-model="value"
  27. />
  28. <view class="btnGroup">
  29. <view
  30. class="btnItem left"
  31. @click="hide"
  32. >
  33. <text class="btnText">取消</text>
  34. </view>
  35. <view
  36. class="btnItem right"
  37. @click="save"
  38. >
  39. <text class="btnText">提交</text>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. let success, error;
  48. export default {
  49. props: {
  50. /**
  51. * 默认缓存类型
  52. */
  53. storageType: {
  54. type: String,
  55. default: "localStorage",
  56. },
  57. },
  58. data() {
  59. return {
  60. /**
  61. * 是否展示
  62. */
  63. isShow: false,
  64. /**
  65. * 键名称
  66. */
  67. key: "",
  68. /**
  69. * 值
  70. */
  71. value: "",
  72. /**
  73. * 屏幕高度
  74. */
  75. height: uni.getSystemInfoSync().windowHeight,
  76. };
  77. },
  78. mounted() {
  79. let that = this;
  80. /**
  81. * 挂载弹窗打开事件
  82. */
  83. uni.$on("devTools_showAddStorageDialog", () => {
  84. that.key = "";
  85. that.value = "";
  86. that.isShow = true;
  87. });
  88. },
  89. unmounted() {
  90. uni.$off("devTools_showAddStorageDialog");
  91. },
  92. methods: {
  93. /**
  94. * 关闭弹窗
  95. */
  96. hide() {
  97. this.isShow = false;
  98. },
  99. /**
  100. * 保存
  101. */
  102. save() {
  103. let that = this;
  104. if (that.key == "") {
  105. return uni.showToast({
  106. title: "请输入key",
  107. icon: "none",
  108. });
  109. }
  110. if (that.storageType == "localStorage") {
  111. uni.setStorageSync(that.key, that.value);
  112. } else if (that.storageType == "sessionStorage") {
  113. sessionStorage.setItem(that.key, that.value);
  114. } else if (that.storageType == "cookie") {
  115. let key = encodeURIComponent(that.key);
  116. let val = encodeURIComponent(that.value);
  117. let cookie =
  118. `${key}=${val}; path=/; expires=` + new Date(new Date().getTime() + 86400 * 1000 * 365).toGMTString();
  119. document.cookie = cookie;
  120. }
  121. uni.showToast({
  122. title: "添加成功!",
  123. icon: "success",
  124. });
  125. that.hide();
  126. setTimeout(() => {
  127. that.$emit("getPage");
  128. }, 300);
  129. },
  130. },
  131. };
  132. </script>
  133. <style lang="scss" scoped>
  134. .editMask {
  135. display: flex;
  136. flex-direction: column;
  137. align-items: center;
  138. justify-content: center;
  139. background-color: rgba(0, 0, 0, 0.3);
  140. width: 750rpx;
  141. flex: 1;
  142. /* #ifndef APP-PLUS */
  143. height: 100vh;
  144. backdrop-filter: blur(1px);
  145. /* #endif */
  146. position: fixed;
  147. left: 0;
  148. top: 0;
  149. z-index: 999;
  150. .editDialog {
  151. display: flex;
  152. flex-direction: column;
  153. align-items: center;
  154. justify-content: center;
  155. width: 690rpx;
  156. background-color: #fff;
  157. border-radius: 20rpx;
  158. padding: 20rpx;
  159. margin-bottom: 300rpx;
  160. .title {
  161. font-size: 28rpx;
  162. color: #333;
  163. height: 50rpx;
  164. line-height: 50rpx;
  165. }
  166. .input {
  167. margin-top: 20rpx;
  168. margin-bottom: 20rpx;
  169. width: 640rpx;
  170. height: 70rpx;
  171. padding: 5rpx;
  172. border-radius: 8rpx;
  173. border: 1px solid rgba(0, 0, 0, 0.05);
  174. }
  175. .btnGroup {
  176. display: flex;
  177. flex-direction: row;
  178. width: 640rpx;
  179. justify-content: space-between;
  180. .btnItem {
  181. height: 64rpx;
  182. border-radius: 10rpx;
  183. display: flex;
  184. flex-direction: row;
  185. align-items: center;
  186. justify-content: center;
  187. .btnText {
  188. font-size: 24rpx;
  189. color: #fff;
  190. }
  191. &.left {
  192. width: 160rpx;
  193. background-color: #8799a3;
  194. }
  195. &.right {
  196. width: 450rpx;
  197. background-color: #3cbb45;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. </style>