Commit 753237a9 by 张立顺

storage方法更新

1 parent 5bbd3f62
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/store-assistant.iml" filepath="$PROJECT_DIR$/.idea/store-assistant.iml" />
</modules>
</component>
</project>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="bd1fb5f0-1648-410c-bbf9-a5683a23085f" name="变更" comment="">
<change beforePath="$PROJECT_DIR$/pages/index/index.wxml" beforeDir="false" afterPath="$PROJECT_DIR$/pages/index/index.wxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pages/tab/index/index.json" beforeDir="false" afterPath="$PROJECT_DIR$/pages/tab/index/index.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pages/tab/index/index.wxml" beforeDir="false" afterPath="$PROJECT_DIR$/pages/tab/index/index.wxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/project.private.config.json" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="2GF5TNpchj3Dij8T8FN6d5otL3G" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="bd1fb5f0-1648-410c-bbf9-a5683a23085f" name="变更" comment="" />
<created>1665971500524</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1665971500524</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file \ No newline at end of file
<!--pages/index/index.wxml--> <!--pages/index/index.wxml-->
<text>pages/index/index.wxml</text> <text>pages/index/index.wxml</text>
{ {
"usingComponents": { "usingComponents": {
"van-button": "@vant/weapp/button/index"
}, },
"navigationStyle": "custom", "navigationStyle": "custom",
"onReachBottomDistance": 10, "onReachBottomDistance": 10,
......
<view class="navigation-bar"> <view class="navigation-bar">
<navigation-bar navStyle="{{navStyle}}" bindnavbarReady="navbarReady" page-title="活动">门店助手</navigation-bar> <navigation-bar navStyle="{{navStyle}}" bindnavbarReady="navbarReady" page-title="活动">门店助手</navigation-bar>
</view> </view>
<van-button type="warning">警告按钮</van-button>
{
"setting": {
"compileHotReLoad": true,
"urlCheck": false,
"lazyloadPlaceholderEnable": true
},
"condition": {
"miniprogram": {
"list": [
{
"name": "加油",
"pathName": "packageEquity/pages/oil/oil",
"query": "",
"launchMode": "default",
"scene": null
},
{
"name": "车牌键盘",
"pathName": "packageEquity/pages/keyboard/keyboard",
"query": "",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "pages/tab/index/index",
"query": "merchantId=44205858",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "packageMine/pages/coupon/recommend/recommend",
"query": "",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "pages/serviceCode/serviceCode",
"query": "id=100&sendDate=1656496796863139&merchantId=123",
"launchMode": "default",
"scene": 1194
},
{
"name": "",
"pathName": "pages/serviceCode/serviceCode",
"query": "id=100&sendDate=1656496796863139",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "pages/index/index",
"query": "",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "pages/tab/discovery/discovery",
"query": "",
"launchMode": "default",
"scene": null
}
]
}
},
"projectname": "store-assistant",
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"libVersion": "2.24.4"
}
\ No newline at end of file \ No newline at end of file
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
* 微信小程序标准版:统一封装的 storage 操作类 * 微信小程序标准版:统一封装的 storage 操作类
*/ */
import { DEFAULT_GET_STORAGEKEY, GET_STORAGE_OBJECTkEY } from '~/utils/constantData' import { DEFAULT_GET_STORAGEKEY, STORAGE_OBJECTkEY } from '~/utils/constantData'
class Storage { class Storage {
types = { types = {
1: 'get', // get 1: 'get', // get
...@@ -15,7 +14,7 @@ class Storage { ...@@ -15,7 +14,7 @@ class Storage {
trial: '体验版', trial: '体验版',
release: '正式版' release: '正式版'
} }
// defaultGetStorageKey = DEFAULT_GET_STORAGEKEY
/** 构造函数 */ /** 构造函数 */
constructor() { constructor() {
/** 获取当前小程序环境变量 */ /** 获取当前小程序环境变量 */
...@@ -40,6 +39,7 @@ class Storage { ...@@ -40,6 +39,7 @@ class Storage {
throw new Error(`Storage ${this.types[type]} func: key can not be empty`) throw new Error(`Storage ${this.types[type]} func: key can not be empty`)
return false return false
} }
// if STORAGE_OBJECTkEY[key]
return tmpKey return tmpKey
} }
return key return key
...@@ -51,31 +51,32 @@ class Storage { ...@@ -51,31 +51,32 @@ class Storage {
* @param {*} syncFlag | 同步状态,默认 false (异步),true 为同步 * @param {*} syncFlag | 同步状态,默认 false (异步),true 为同步
*/ */
get = (key, syncFlag = true) => { get = (key, syncFlag = true) => {
// 有key值 // 有值默认取key值
if (key) { if (key) {
// 判断值是否是指定的对象值
let tmpKey = this.checkKeyType(key, 1) let tmpKey = this.checkKeyType(key, 1)
let isObjectKey = STORAGE_OBJECTkEY[tmpKey]
// 判断值是否是指定的对象值 是取定义的好的映射值 // console.log(key)
let isObjectKey = GET_STORAGE_OBJECTkEY[key] // console.log(isObjectKey)
if (isObjectKey) { if (isObjectKey) {
return this.getObjectKeyValue(isObjectKey) return this.getObjectKeyValue(isObjectKey)
} else { } else {
return this.getKeyValue(tmpKey, syncFlag) throw new Error(`Storage ${this.types[1]} ${key}: key must be register`)
} }
} else { } else {
// 为空取默认定义好的数组值
return this.getObjectKeyValue(DEFAULT_GET_STORAGEKEY) return this.getObjectKeyValue(DEFAULT_GET_STORAGEKEY)
} }
} }
/** /**
* *
* @param {*} key | 要操作的键值 * @param {*} 空key返回默认定义数组的值
* @param {*} syncFlag | 同步状态,默认 false (异步),true 为同步
*/ */
getKeyValue(tmpKey, syncFlag) { getObjectKeyValue(key) {
if (syncFlag) { // ObjectList
let res = wx.getStorageSync(tmpKey) if (typeof key == 'string') {
key = this.addEvnToKey(key)
let res = wx.getStorageSync(key)
if (res) { if (res) {
return JSON.parse(res) return JSON.parse(res)
} else { } else {
...@@ -83,35 +84,15 @@ class Storage { ...@@ -83,35 +84,15 @@ class Storage {
return '' return ''
} }
} else { } else {
return new Promise((resolve, reject) => { let Obj = {}
wx.getStorage({ key.forEach(key => {
key: tmpKey, let defaultKey = this.addEvnToKey(key)
success: res => { let res = wx.getStorageSync(defaultKey)
resolve(JSON.parse(res.data)) Obj[key] = res ? JSON.parse(res) : ''
},
fail: err => {
// console.log(err)
// reject(errMsg)
resolve('')
}
})
}) })
return Obj
} }
} }
/**
*
* @param {*} 空key返回默认定义数组的值
*/
getObjectKeyValue(ObjectList) {
let Obj = {}
ObjectList.forEach(key => {
let defaultKey = this.addEvnToKey(key)
let res = wx.getStorageSync(defaultKey)
Obj[key] = res ? JSON.parse(res) : ''
})
return Obj
}
/** /**
* *
* @param {*} key | 要操作的键值 * @param {*} key | 要操作的键值
......
...@@ -4,6 +4,11 @@ export const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/sto ...@@ -4,6 +4,11 @@ export const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/sto
export const DEFAULT_GET_STORAGEKEY = ['sessionId', 'merchantId', 'userInfo'] export const DEFAULT_GET_STORAGEKEY = ['sessionId', 'merchantId', 'userInfo']
// 缓存get方法key的对应才映射值 // 缓存get方法key的对应才映射值
export const GET_STORAGE_OBJECTkEY = { export const STORAGE_OBJECTkEY = {
super: ['sessionId', 'merchantId', 'userInfo'] DEFAULT_GET_STORAGEKEY,
sessionId: 'sessionId',
userInfo: 'userInfo',
system_info: 'systemInfo',
app_title: 'appTitle',
auto_login_object: 'auto_login_object'
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!