├── README.md ├── storageInit.js └── HttpUtil.js /README.md: -------------------------------------------------------------------------------- 1 | # ReactNativeUtil 2 | 记录工作中遇到的一些问题(issues) 3 | 收集一些工具类方便使用 在issure中通过label检索,欢迎大家丰富内容。 4 | 目标是让大家轻松的学习rn。 5 | 6 | 简书:http://www.jianshu.com/u/1386d6b454e5 7 | 8 | CSDN:http://blog.csdn.net/u014041033?viewmode=contents 9 | 10 | # 目前标签:(未完待续,欢迎补充) 11 | [ios](https://github.com/wuyunqiang/ReactNativeUtil/labels/ios)
12 | [android](https://github.com/wuyunqiang/ReactNativeUtil/labels/android)
13 | [image](https://github.com/wuyunqiang/ReactNativeUtil/labels/Image)
14 | [WebView](https://github.com/wuyunqiang/ReactNativeUtil/issues?q=is%3Aissue+is%3Aopen+label%3Awebview)
15 | [react navigation](https://github.com/wuyunqiang/ReactNativeUtil/labels/react%20navigation)
16 | [codepush](https://github.com/wuyunqiang/ReactNativeUtil/labels/codepush)
17 | [redux](https://github.com/wuyunqiang/ReactNativeUtil/labels/redux)
18 | [immutable](https://github.com/wuyunqiang/ReactNativeUtil/labels/immutable)
19 | [saga](https://github.com/wuyunqiang/ReactNativeUtil/labels/saga)
20 | [wechat](https://github.com/wuyunqiang/ReactNativeUtil/labels/WeChat)
21 | [react-native-elements](https://github.com/wuyunqiang/ReactNativeUtil/labels/react-native-elements)
22 | [Util](https://github.com/wuyunqiang/ReactNativeUtil/labels/Util)
23 | [轮播](https://github.com/wuyunqiang/ReactNativeUtil/labels/轮播)
24 | [build](https://github.com/wuyunqiang/ReactNativeUtil/labels/build)
25 | [PropTypes](https://github.com/wuyunqiang/ReactNativeUtil/labels/PropTypes)
26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /storageInit.js: -------------------------------------------------------------------------------- 1 | import {AsyncStorage} from 'react-native'; 2 | import Storage from 'react-native-storage'; 3 | const storage = new Storage({ 4 | // 最大容量,默认值1000条数据循环存储 5 | size: 1000, 6 | 7 | // 存储引擎:对于RN使用AsyncStorage,对于web使用window.localStorage 如果不指定则数据只会保存在内存中,重启后即丢失 8 | storageBackend: AsyncStorage, 9 | 10 | // 数据过期时间,默认一整天(1000 * 3600 * 24 毫秒),设为null则永不过期 11 | defaultExpires: 1000 * 3600 * 24, 12 | 13 | // 读写时在内存中缓存数据。默认启用。 14 | enableCache: true, 15 | 16 | // 如果storage中没有相应数据,或数据已过期, 则会调用相应的sync方法,无缝返回最新数据。 sync方法的具体说明会在后文提到 17 | // 你可以在构造函数这里就写好sync的方法 或是写到另一个文件里,这里require引入 或是在任何时候,直接对storage.sync进行赋值修改 18 | // sync: require('./sync') 19 | }); 20 | 21 | global.storage = storage; 22 | 23 | // 读取缓存 24 | let readCache = (key,res,error)=>{ 25 | storage.load({ 26 | key: key, 27 | autoSync: true, 28 | syncInBackground: true 29 | }).then(ret => { 30 | res(ret); 31 | }).catch(err => { 32 | error(err) 33 | }); 34 | }; 35 | 36 | global.READ_CACHE = readCache; 37 | 38 | // 写入缓存 39 | let writeCache = (key,data,expires)=>{ 40 | 41 | storage.save({ 42 | key: key, //注意:请不要在key中使用_下划线符号! 43 | rawData: data, 44 | 45 | // 如果不指定过期时间,则会使用defaultExpires参数 46 | // 如果设为null,则永不过期 1000 * 3600 47 | expires: expires 48 | }); 49 | }; 50 | 51 | global.WRITE_CACHE = writeCache; 52 | 53 | // 删除单个数据 54 | let remove=(key)=>{ 55 | storage.remove({ 56 | key: key, 57 | }); 58 | }; 59 | global.REMOVE_ITEM = remove; 60 | 61 | 62 | //清空所有缓存 63 | let clearAll = ()=>{ 64 | storage.clearMap(); 65 | }; 66 | 67 | global.CLEAR_All = clearAll; -------------------------------------------------------------------------------- /HttpUtil.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by wuyunqiang on 2017/6/12. 3 | */ 4 | 5 | import {DeviceEventEmitter} from 'react-native'; 6 | 7 | 8 | const HOST = 'http://172.28.111.198:887/api.php'; 9 | 10 | function toParameterString(obj) { 11 | return obj ? Object.keys(obj).sort().map(function (key) { 12 | var val = obj[key]; 13 | if (Array.isArray(val)) { 14 | return val.sort().map(function (val2) { 15 | if (!val2) val2 = ""; 16 | return encodeURIComponent(key) + '=' + encodeURIComponent(val2); 17 | }).join('&'); 18 | } 19 | if (!val) val = ""; 20 | return encodeURIComponent(key) + '=' + encodeURIComponent(val); 21 | }).join('&') : ''; 22 | } 23 | 24 | export default class HttpRequest { 25 | constructor() { 26 | this.handleResponse = this.handleResponse.bind(this); 27 | } 28 | 29 | handleResponse(response) { 30 | 31 | console.log('response',response); 32 | const resJson = JSON.parse(response.body); 33 | if(resJson.status !== '000000'&&resJson.status !== 200){ 34 | return false; 35 | } 36 | if (response.err) 37 | throw response.err; 38 | return resJson; 39 | } 40 | 41 | 42 | 43 | async GET(url) { 44 | let result = await fetch(HOST + url , { 45 | method: 'GET', 46 | credentials: 'include', 47 | headers: { 48 | 'Cookie':'PHPSESSID=2psj93stkrg339qnkdj1qgjok7', 49 | 'Accept': 'application/json', 50 | // 'Content-Type': 'application/json;charset=UTF-8' 51 | 'Content-Type': 'application/x-www-form-urlencoded' 52 | } 53 | }).then(this.handleResponse).catch((error) => { 54 | console.log(error) 55 | }); 56 | 57 | return result; 58 | } 59 | 60 | async POST(url, params,headers) { 61 | let result = await fetch(HOST + url, { 62 | method: 'POST', 63 | credentials: 'include', 64 | headers: { 65 | 'Cookie':'PHPSESSID=2psj93stkrg339qnkdj1qgjok7', 66 | 'Accept': 'application/json', 67 | // 'Content-Type': 'application/json;charset=UTF-8' 68 | 'Content-Type': 'application/x-www-form-urlencoded' 69 | }, 70 | body: params 71 | }).then(this.handleResponse).catch((error) => { 72 | Log(error); 73 | }); 74 | return result; 75 | } 76 | 77 | async POSTBODY(url, pars, headers, security) { 78 | 79 | let result = await fetch(HOST + url, { 80 | method: 'POST', 81 | headers: { 82 | 'Accept': 'application/json', 83 | 'Content-Type': 'application/json', 84 | 'X-Requested-With': 'XMLHttpRequest', 85 | // ...HEADERS, 86 | ...headers, 87 | }, 88 | body: JSON.stringify(pars) 89 | }).then(this.handleResponse).catch((error) => { 90 | Log(error); 91 | }); 92 | return result; 93 | } 94 | 95 | // type : 'image/png' 'image/jpeg' 96 | // async POSTIMAGE(imageSource, type, headers, security) { 97 | // 98 | // let formData = new FormData(); 99 | // let file = {uri: imageSource, type: type, name: 'jpg'}; 100 | // formData.append('file', file); 101 | // 102 | // let HEADERS = { 103 | // 'Content-Type': 'multipart/form-data' 104 | // }; 105 | // let result = await fetch(HOST + imageURL, { 106 | // method: 'POST', 107 | // headers: { 108 | // 'Content-Type': 'multipart/form-data', 109 | // ...HEADERS, 110 | // ...headers, 111 | // }, 112 | // body: formData 113 | // }).then((response) => response.json()).catch((error) => { 114 | // Log(error); 115 | // }); 116 | // 117 | // return result; 118 | // } 119 | } 120 | 121 | global.HttpUtil = new HttpRequest(); 122 | 123 | --------------------------------------------------------------------------------