storage.uvue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-common-mt">
  8. <view class="uni-list">
  9. <view class="uni-list-cell uni-list-cell-line">
  10. <view class="uni-list-cell-left">
  11. <view class="uni-label">key</view>
  12. </view>
  13. <view class="uni-list-cell-db">
  14. <input class="uni-input" type="text" placeholder="请输入key" name="key" :value="key" maxlength="-1"
  15. @input="keyChange" />
  16. </view>
  17. </view>
  18. <view class="uni-list-cell">
  19. <view class="uni-list-cell-left">
  20. <view class="uni-label">value</view>
  21. </view>
  22. <view class="uni-list-cell-db">
  23. <input class="uni-input" type="text" placeholder="请输入value" name="data"
  24. :value="typeof data === 'string' ? data : JSON.stringify(data)" maxlength="-1" @input="dataChange" />
  25. </view>
  26. </view>
  27. </view>
  28. <view class="uni-padding-wrap">
  29. <view class="uni-btn-v">
  30. <button class="uni-btn btn-getStorageInfoASync" type="primary" @tap="getStorageInfo">
  31. 获取存储概述信息-异步
  32. </button>
  33. <button class="uni-btn btn-getStorageInfoSync" @tap="getStorageInfoSync">
  34. 获取存储概述信息-同步
  35. </button>
  36. </view>
  37. <text class="uni-list-cell-db-text">{{ storageInfo }}</text>
  38. <view class="uni-flex uni-row">
  39. <button type="default" style="width:50%" @tap="strMock">
  40. 填充字符串
  41. </button>
  42. <button type="default" style="width:50%" @tap="complexMock">
  43. 填充复杂对象
  44. </button>
  45. </view>
  46. <view class="uni-flex uni-row">
  47. <button type="default" style="width:50%" @tap="numberMock">
  48. 填充整型
  49. </button>
  50. <button type="default" style="width:50%" @tap="floatMock">
  51. 填充浮点型
  52. </button>
  53. </view>
  54. <view class="uni-flex uni-row">
  55. <button type="default" style="width:50%" @tap="jsonLikeMock">
  56. 填充json字符串
  57. </button>
  58. <button type="default" style="width:50%" @tap="longLikeMock">
  59. 填充整数字符串
  60. </button>
  61. </view>
  62. <view class="uni-flex uni-row">
  63. <button type="default" style="width:50%" @tap="floatLikeMock">
  64. 填充浮点字符串
  65. </button>
  66. <button type="default" style="width:50%" @tap="negativeLikeMock">
  67. 填充负数字符串
  68. </button>
  69. </view>
  70. <view class="uni-flex uni-row">
  71. <button type="default" class="uni-btn btn-complexStaticTest" style="width:100%" @tap="complexStaticTest">
  72. 字面量读写测试
  73. </button>
  74. </view>
  75. </view>
  76. <view class="uni-padding-wrap">
  77. <view class="uni-btn-v">
  78. <button type="primary" class="uni-btn btn-setstorageAsync" @tap="setStorage">
  79. 存储数据-异步
  80. </button>
  81. <button class="uni-btn btn-getstorageAsync" @tap="getStorage">读取数据-异步</button>
  82. <button class="uni-btn btn-removeStorageInfoASync" @tap="removeStorage">移除数据-异步</button>
  83. <button class="uni-btn btn-clearStorageInfoASync" @tap="clearStorage">清理数据-异步</button>
  84. </view>
  85. <view class="uni-btn-v">
  86. <button type="primary" class="uni-btn btn-setstorageSync" @tap="setStorageSync">
  87. 存储数据-同步
  88. </button>
  89. <button class="uni-btn btn-getstorageSync" @tap="getStorageSync">读取数据-同步</button>
  90. <button class="uni-btn btn-removeStorageInfoSync" @tap="removeStorageSync">
  91. 移除数据-同步
  92. </button>
  93. <button class="uni-btn btn-clearStorageInfoSync" @tap="clearStorageSync">
  94. 清理数据-同步
  95. </button>
  96. </view>
  97. </view>
  98. </view>
  99. <button type="primary" @click="goto">前往storage管理器</button>
  100. </view>
  101. <!-- #ifdef APP -->
  102. </scroll-view>
  103. <!-- #endif -->
  104. </template>
  105. <script>
  106. export default {
  107. data() {
  108. return {
  109. title: 'get/set/clearStorage',
  110. key: '',
  111. data: '' as any,
  112. apiGetData: '' as any | null,
  113. storageInfo: '',
  114. staticComplexRet: false,
  115. jest_saveUTSJSONObjectSyncResult: 0,
  116. jest_saveUTSJSONObjectAsyncResult: 0,
  117. jest_saveUTSJSONObjectArraySyncResult: 0
  118. }
  119. },
  120. methods: {
  121. getStorageInfo() {
  122. uni.getStorageInfo({
  123. success: (res) => {
  124. this.apiGetData = res
  125. this.storageInfo = JSON.stringify(res)
  126. },
  127. })
  128. },
  129. getStorageInfoSync() {
  130. try {
  131. const res = uni.getStorageInfoSync()
  132. this.apiGetData = res
  133. this.storageInfo = JSON.stringify(res)
  134. } catch (e) {
  135. // error
  136. console.log(e)
  137. }
  138. },
  139. jsonLikeMock() {
  140. this.key = 'key_' + Math.random()
  141. this.data = JSON.stringify({
  142. name: "james",
  143. age: 12,
  144. from: "american"
  145. });
  146. },
  147. longLikeMock() {
  148. this.key = 'key_' + Math.random()
  149. this.data = "1234567890"
  150. },
  151. floatLikeMock() {
  152. this.key = 'key_' + Math.random()
  153. this.data = "321456.1234567890"
  154. },
  155. negativeLikeMock() {
  156. this.key = 'key_' + Math.random()
  157. this.data = "-321456"
  158. },
  159. strMock() {
  160. this.key = 'key_' + Math.random()
  161. this.data = '测试字符串数据,长度为16个字符'
  162. },
  163. complexStaticTest() {
  164. uni.setStorageSync("key_complexStaticMock", {
  165. name: "张三",
  166. age: 12
  167. })
  168. let savedData = uni.getStorageSync("key_complexStaticMock")
  169. this.staticComplexRet = false
  170. if (savedData instanceof UTSJSONObject) {
  171. if ((savedData as UTSJSONObject).getNumber('age') == 12) {
  172. this.staticComplexRet = true
  173. uni.showToast({
  174. icon: 'success',
  175. title: '测试通过'
  176. })
  177. }
  178. }
  179. },
  180. complexMock() {
  181. this.key = 'key_' + Math.random()
  182. let jsonObj = {
  183. name: '张三',
  184. age: 12,
  185. classMate: [
  186. {
  187. id: 1001,
  188. name: '李四',
  189. },
  190. {
  191. id: 1002,
  192. name: 'jack ma',
  193. },
  194. ],
  195. }
  196. this.data = jsonObj
  197. },
  198. numberMock() {
  199. this.key = 'key_' + Math.random()
  200. this.data = 10011
  201. },
  202. floatMock() {
  203. this.key = 'key_' + Math.random()
  204. this.data = 3.1415926535893384626
  205. },
  206. keyChange: function (e : InputEvent) {
  207. this.key = e.detail.value
  208. },
  209. dataChange: function (e : InputEvent) {
  210. this.data = e.detail.value
  211. },
  212. getStorage: function () {
  213. var key = this.key
  214. if (key.length == 0) {
  215. uni.showModal({
  216. title: '读取数据失败',
  217. content: 'key 不能为空',
  218. showCancel: false,
  219. })
  220. } else {
  221. let that = this
  222. uni.getStorage({
  223. key: key,
  224. success: (res) => {
  225. that.apiGetData = res.data
  226. let desc : string = typeof this.apiGetData
  227. if ("object" == desc) {
  228. desc = desc + ": " + JSON.stringify(this.apiGetData)
  229. } else {
  230. desc = desc + ": " + this.apiGetData
  231. }
  232. uni.showModal({
  233. title: '读取数据成功',
  234. content: desc,
  235. showCancel: false,
  236. })
  237. },
  238. fail: () => {
  239. uni.showModal({
  240. title: '读取数据失败',
  241. content: '找不到 key 对应的数据',
  242. showCancel: false,
  243. })
  244. },
  245. })
  246. }
  247. },
  248. getStorageSync: function () {
  249. var key = this.key
  250. if (key.length == 0) {
  251. uni.showModal({
  252. title: '读取数据失败',
  253. content: 'key 不能为空',
  254. showCancel: false,
  255. })
  256. } else {
  257. this.apiGetData = uni.getStorageSync(key)
  258. let desc : string = typeof this.apiGetData
  259. if ("object" == desc) {
  260. desc = desc + ": " + JSON.stringify(this.apiGetData)
  261. } else {
  262. desc = desc + ": " + this.apiGetData
  263. }
  264. uni.showModal({
  265. title: '读取数据成功',
  266. content: desc,
  267. showCancel: false,
  268. })
  269. }
  270. },
  271. setStorage: function () {
  272. var key = this.key
  273. var data = this.data
  274. if (key.length == 0) {
  275. uni.showModal({
  276. title: '保存数据失败',
  277. content: 'key 不能为空',
  278. showCancel: false,
  279. })
  280. } else {
  281. uni.setStorage({
  282. key: key,
  283. data: data,
  284. success: () => {
  285. uni.showModal({
  286. title: '存储数据成功',
  287. showCancel: false,
  288. })
  289. },
  290. fail: () => {
  291. uni.showModal({
  292. title: '储存数据失败!',
  293. showCancel: false,
  294. })
  295. },
  296. })
  297. }
  298. },
  299. setStorageSync: function () {
  300. var key = this.key
  301. var data = this.data
  302. if (key.length == 0) {
  303. uni.showModal({
  304. title: '保存数据失败',
  305. content: 'key 不能为空',
  306. showCancel: false,
  307. })
  308. } else {
  309. uni.setStorageSync(key, data)
  310. uni.showModal({
  311. title: '存储数据成功',
  312. showCancel: false,
  313. })
  314. }
  315. },
  316. removeStorage: function () {
  317. uni.removeStorage({
  318. key: this.key,
  319. success: () => {
  320. uni.showModal({
  321. title: '移除数据成功',
  322. showCancel: false,
  323. })
  324. },
  325. fail: () => {
  326. uni.showModal({
  327. title: '移除数据失败',
  328. showCancel: false,
  329. })
  330. },
  331. })
  332. },
  333. removeStorageSync: function () {
  334. uni.removeStorageSync(this.key)
  335. uni.showModal({
  336. title: '移除数据成功',
  337. showCancel: false,
  338. })
  339. },
  340. clearStorage: function () {
  341. this.key = ''
  342. this.data = ''
  343. uni.clearStorage({
  344. success: function (_) {
  345. uni.showModal({
  346. title: '清除数据成功',
  347. showCancel: false,
  348. })
  349. },
  350. fail: function (_) {
  351. uni.showModal({
  352. title: '清除数据失败',
  353. showCancel: false,
  354. })
  355. },
  356. })
  357. },
  358. clearStorageSync: function () {
  359. this.key = ''
  360. this.data = ''
  361. uni.clearStorageSync()
  362. uni.showModal({
  363. title: '清除数据成功',
  364. content: ' ',
  365. showCancel: false,
  366. })
  367. },
  368. jest_saveUTSJSONObject: function () {
  369. const key = 'test_key_saveUTSJSONObject'
  370. uni.setStorageSync(key, {
  371. a: {
  372. b: 1
  373. }
  374. })
  375. const dataSync = uni.getStorageSync(key) as UTSJSONObject
  376. const dataSyncA = dataSync['a'] as UTSJSONObject
  377. this.jest_saveUTSJSONObjectSyncResult = dataSyncA.get('b') as number
  378. uni.getStorage({
  379. key,
  380. success: (res) => {
  381. const dataAsync = res.data as UTSJSONObject
  382. const dataAsyncA = dataAsync['a'] as UTSJSONObject
  383. this.jest_saveUTSJSONObjectAsyncResult = dataAsyncA.get('b') as number
  384. console.log('this.jest_saveUTSJSONObjectSyncResult: ' + this.jest_saveUTSJSONObjectSyncResult)
  385. console.log('this.jest_saveUTSJSONObjectAsyncResult: ' + this.jest_saveUTSJSONObjectAsyncResult)
  386. }
  387. })
  388. },
  389. jest_saveUTSJSONObjectArray() {
  390. const key = 'test_key_saveUTSJSONObjectArray'
  391. uni.setStorageSync(key, [{
  392. a: 1
  393. }] as UTSJSONObject[])
  394. const dataSync = uni.getStorageSync(key) as UTSJSONObject[]
  395. this.jest_saveUTSJSONObjectArraySyncResult = dataSync[0].get('a') as number
  396. },
  397. goto(){
  398. uni.navigateTo({
  399. url:"/pages/API/storage/storagemanage"
  400. })
  401. }
  402. }
  403. }
  404. </script>
  405. <style>
  406. </style>