storage.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. Page({
  2. data: {
  3. key: '',
  4. data: '',
  5. keySync: '',
  6. dataSync: '',
  7. },
  8. keyChange(e) {
  9. console.log(e);
  10. this.setData({
  11. key: e.detail.value,
  12. });
  13. },
  14. dataChange(e) {
  15. this.setData({
  16. data: e.detail.value,
  17. });
  18. },
  19. keyChangeSync(e) {
  20. console.log(e);
  21. this.setData({
  22. keySync: e.detail.value,
  23. });
  24. },
  25. dataChangeSync(e) {
  26. this.setData({
  27. dataSync: e.detail.value,
  28. });
  29. },
  30. getStorage() {
  31. var key = this.data.key;
  32. var data = this.data.data;
  33. console.log('data: ', this.data);
  34. if (key.length === 0) {
  35. this.setData({
  36. key: key,
  37. data: data,
  38. });
  39. my.alert({
  40. title: '读取数据失败',
  41. content: 'key 不能为空',
  42. });
  43. } else {
  44. let that = this;
  45. my.getStorage({
  46. key,
  47. success(res) {
  48. my.alert({
  49. title: '读取数据成功',
  50. content: "data: '" + JSON.stringify(res.data) + "'",
  51. });
  52. },
  53. });
  54. console.log('读取数据成功', my.getStorageSync({ key }).data);
  55. }
  56. },
  57. setStorage() {
  58. var key = this.data.key;
  59. var data = this.data.data;
  60. if (key.length === 0) {
  61. this.setData({
  62. key: key,
  63. data: data,
  64. });
  65. my.alert({
  66. title: '保存数据失败',
  67. content: 'key 不能为空',
  68. });
  69. } else {
  70. let that = this;
  71. my.setStorage({
  72. key: key,
  73. data: data,
  74. success() {
  75. that.setData({
  76. key: key,
  77. data: data,
  78. });
  79. my.alert({
  80. title: '存储数据成功',
  81. content: `${key}: ${data}`,
  82. });
  83. },
  84. });
  85. }
  86. },
  87. removeStorage() {
  88. let that = this;
  89. my.removeStorage({
  90. key: that.data.key,
  91. success() {
  92. that.setData({
  93. key: '',
  94. data: '',
  95. });
  96. my.alert({
  97. title: '删除数据成功',
  98. content: '',
  99. });
  100. },
  101. });
  102. },
  103. clearStorage() {
  104. let that = this;
  105. my.clearStorage({
  106. key: that.data.key,
  107. success() {
  108. that.setData({
  109. key: '',
  110. data: '',
  111. });
  112. my.alert({
  113. title: '清除数据成功',
  114. content: '',
  115. });
  116. },
  117. });
  118. },
  119. getStorageInfo() {
  120. let that = this;
  121. my.getStorageInfo({
  122. success(res) {
  123. my.alert({
  124. content: JSON.stringify({
  125. keys: res.keys,
  126. currentSize: res.currentSize,
  127. limitSize: res.limitSize,
  128. }),
  129. });
  130. },
  131. });
  132. },
  133. getStorageSync() {
  134. var key = this.data.keySync;
  135. var data = this.data.dataSync;
  136. console.log('data: ', this.data);
  137. if (key.length === 0) {
  138. this.setData({
  139. keySync: key,
  140. dataSync: data,
  141. });
  142. my.alert({
  143. title: '同步读取数据失败',
  144. content: 'key 不能为空',
  145. });
  146. } else {
  147. let res = my.getStorageSync({ key: key });
  148. if (!res.error) {
  149. my.alert({
  150. title: '同步读取数据成功',
  151. content: "data: '" + JSON.stringify(res.data) + "'",
  152. });
  153. }
  154. }
  155. },
  156. setStorageSync() {
  157. var key = this.data.keySync;
  158. var data = this.data.dataSync;
  159. if (key.length === 0) {
  160. this.setData({
  161. keySync: key,
  162. dataSync: data,
  163. });
  164. my.alert({
  165. title: '同步保存数据失败',
  166. content: 'key 不能为空',
  167. });
  168. } else {
  169. let res = my.setStorageSync({ key: key, data: data });
  170. if (!res.error) {
  171. this.setData({
  172. keySync: key,
  173. dataSync: data,
  174. });
  175. my.alert({
  176. title: '同步存储数据成功',
  177. content: `${key}: ${data}`,
  178. });
  179. }
  180. }
  181. },
  182. removeStorageSync() {
  183. let res = my.removeStorageSync({ key: this.data.keySync });
  184. if (!res.error) {
  185. this.setData({
  186. keySync: '',
  187. dataSync: '',
  188. });
  189. my.alert({
  190. title: '同步删除数据成功',
  191. content: '',
  192. });
  193. }
  194. },
  195. clearStorageSync() {
  196. let res = my.clearStorageSync();
  197. if (!res.error) {
  198. this.setData({
  199. keySync: '',
  200. dataSync: '',
  201. });
  202. my.alert({
  203. title: '同步清除数据成功',
  204. content: '',
  205. });
  206. }
  207. },
  208. getStorageInfoSync() {
  209. let res = my.getStorageInfoSync();
  210. if (!res.error) {
  211. my.alert({
  212. content: JSON.stringify({
  213. keys: res.keys,
  214. currentSize: res.currentSize,
  215. limitSize: res.limitSize,
  216. }),
  217. });
  218. }
  219. },
  220. });