|
| 1 | +//shujuqignqiu |
| 2 | +//url successCallback failCallback |
| 3 | + |
| 4 | +var key = "UaZnKe1JAotWiL7yI2zhvlW3KnTihwc1xVwzt2TZ"; |
| 5 | +var access = "ny7MGZ6kjXhKH80okBB3GCXxPXELNBA8kXCcU2nV"; |
| 6 | + |
| 7 | +import { |
| 8 | + Alert, |
| 9 | + StyleSheet, |
| 10 | + Text, |
| 11 | + View, |
| 12 | + TouchableWithoutFeedback, |
| 13 | + TouchableHighlight |
| 14 | +} from 'react-native'; |
| 15 | + |
| 16 | + let HttpUtil={ |
| 17 | + //get |
| 18 | + fetchGet:(url,params,APTSIN,successCallback,failCallback)=>{ |
| 19 | + var Gkey=key; |
| 20 | + var GSecret = access; |
| 21 | + let nonce = require('nonce')(); |
| 22 | + let tonces=parseInt(nonce()/100); |
| 23 | + params.tonce = tonces; |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + var paramString = Object.keys(params).sort().map(function(param){ |
| 28 | + return encodeURIComponent(param) + '=' + encodeURIComponent(params[param]); |
| 29 | + }).join('&'); |
| 30 | + var crypto = require("crypto-js"); |
| 31 | + // GET|/api/v2/members/me.json|access_key=UaZnKe1JAotWiL7yI2zhvlW3KnTihwc1xVwzt2TZ&tonce=1520911488466 |
| 32 | + |
| 33 | + |
| 34 | + var payload='GET|/'+APTSIN+'|access_key='+key+''+'&tonce='+tonces; |
| 35 | + var signature = crypto.HmacSHA256(payload,GSecret).toString(); |
| 36 | + |
| 37 | + |
| 38 | + // Alert.alert("payload= ", JSON.stringify(signature)); |
| 39 | + |
| 40 | + params.signature=signature |
| 41 | + var paramStrings = paramString +"&"+encodeURIComponent('signature') + "=" + encodeURIComponent(signature) |
| 42 | + |
| 43 | + if(params){ |
| 44 | + // var paramString=Object.keys(params) |
| 45 | + // .reduce((a,k)=>{ |
| 46 | + // a.push(k+"="+encodeURIComponent(params[k])); |
| 47 | + // return a; |
| 48 | + // },[]) |
| 49 | + // .join('&'); |
| 50 | + url +="?access_key="+key+""+"&"+paramStrings; |
| 51 | + Alert.alert("responseURL= ",JSON.stringify(url)); |
| 52 | + } |
| 53 | + fetch(url) |
| 54 | + .then((response)=>{ |
| 55 | + Alert.alert("responseSSSSS ",JSON.stringify(response)); |
| 56 | + |
| 57 | + return response.json(); |
| 58 | + }) |
| 59 | + .then((responseObj |
| 60 | + Alert.alert("responseSccuees ",JSON.stringify(responseObj)); |
| 61 | + return successCallback.json(); |
| 62 | + }) |
| 63 | + .catch((error)=>{ |
| 64 | + Alert.alert("responseError ",JSON.stringify(error)); |
| 65 | + |
| 66 | + failCallback(error); |
| 67 | + }).done(); |
| 68 | + }, |
| 69 | + fetchGetTWO: (url, params, successCallback, failCallback) => { |
| 70 | + |
| 71 | + if (params) { |
| 72 | + var paramsBody = Object.keys(params) |
| 73 | + .reduce((a, k) => { |
| 74 | + a.push(k + "=" + encodeURIComponent(params[k])); |
| 75 | + return a; |
| 76 | + }, []) |
| 77 | + .join('&'); |
| 78 | + url += "" + paramsBody; |
| 79 | + } |
| 80 | + // Alert.alert("1+1",url); |
| 81 | + fetch(url) |
| 82 | + .then((response) => { |
| 83 | + // Alert.alert("response ",response.json()); |
| 84 | + // response.text().then(r => Alert.alert(r)) |
| 85 | + |
| 86 | + return response.json(); |
| 87 | + }) |
| 88 | + .then((responseObj) => { |
| 89 | + // Alert.alert("response json",JSON.stringify(response)); |
| 90 | + // responseObj.text().then(r => Alert.alert(r)) |
| 91 | + successCallback(responseObj) |
| 92 | + }) |
| 93 | + .catch((error) => { |
| 94 | + console.log(error); |
| 95 | + Alert.alert("error ",error); |
| 96 | + // failCallback(error) |
| 97 | + }).done(); |
| 98 | + }, |
| 99 | +//post |
| 100 | +fetchPost: (url, params, successCallback, failCallback) => { |
| 101 | + |
| 102 | + |
| 103 | + var paramsBody = Object.keys(params) |
| 104 | + .reduce((a, k) => { |
| 105 | + a.push(k + "=" + encodeURIComponent(obj[k])); |
| 106 | + return a; |
| 107 | + }, []) |
| 108 | + .join('&'); |
| 109 | + fetch(url, { |
| 110 | + method: 'POST', |
| 111 | + headers: { |
| 112 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 113 | + }, |
| 114 | + body: paramsBody + "&key=664b9884a4c2199b15ccfcfbdd7dbf5c" |
| 115 | + }) |
| 116 | + .then((response) => response.json()) |
| 117 | + .then((responseObj) => successCallback(responseObj)) |
| 118 | + .catch((error) => failCallback(error)).done(); |
| 119 | + } |
| 120 | +} |
| 121 | +export default HttpUtil; |
0 commit comments