ReactNative網(wǎng)絡(luò)請求

1劫樟、登錄界面


登錄.png
import React, { Component } from 'react';
import {   
  AppRegistry,   
  StyleSheet,   
  TouchableOpacity,  
  Image,  
  TextInput,    
  Text,  
  View,   
  Navigator, 
  StatusBar
} from 'react-native';
import NetUtil from './NetUtil';
import Util from './Util';
import MainView from './MainView';
export default class LoginView extends Component {   
 constructor(props) {     
   super(props);       
   this.state =  {  
                          userNameText:"13918068798",             
                           passwordText:"1234554321"     
                  }; 
   }   
 loginClick(){      
    let data={'seller_name':this.state.userNameText,'password':this.state.passwordText};      
    let url='http://192.168.0.85/mobile/index.php?act=login&op=seller_login';        
    var common=NetUtil.commonParamsWithAct('login','seller_login');     
    var  _this=this;        
    NetUtil.post(url,data,common,function (set){  
          if(set.code==10000){       
              Util.saveItem('member_id',set.datas.member_info.member_id);     
              Util.saveItem('member_name',set.datas.member_info.member_name);         
              Util.saveItem('member_nickname',set.datas.member_info.member_nickname);    
              Util.saveItem('member_avatar',set.datas.member_info.member_avatar);       
              Util.saveItem('store_id',set.datas.store_info.store_id);     
              Util.saveItem('store_name',set.datas.store_info.store_name);     
              Util.saveItem('TOKEN',set.datas.token);                Util.saveItem('IsLogin','YES');           
     const { navigator } = _this.props;  
      if(navigator) {             
           navigator.push({           
             name: '主頁面',                 
             component: MainView,             
       }) }          
  }else{          
     Util.saveItem('member_id','');
     Util.saveItem('member_name','');
     Util.saveItem('member_nickname','');
     Util.saveItem('member_avatar','');
     Util.saveItem('store_id','');
     Util.saveItem('store_name','');
     Util.saveItem('TOKEN','');
     Util.saveItem('IsLogin','NO');         
  }  
 }); 
 }   

 render() {      
   var Dimensions = require('Dimensions');     
   var {width, height} = Dimensions.get('window');        
   var _this=this;    
    return (       
       <Image source={require('../images/loginBackImage.png')} style={{ flex:1, width:width,height:height}}>    
            <Image source={require('../images/slh.png')} style={styles.iconImage} />    
            <StatusBar  backgroundColor="white"  barStyle="light-content"/>      
            <Text  style={styles.titleStyle}>商 · 家 · 管 · 理 · 中 · 心</Text>            
            <TextInput style={[styles.inputUsreName ,{width:width-40} ]}                            placeholder="請輸入用戶名"                            onChangeText = {(text) => {                                _this.setState({                                    userNameText:text  })  }  }                            placeholderTextColor='white'/>            
            <View  style={ {width:width-40,height:1,backgroundColor:'#696969', alignSelf:'center',}}/>             
            <TextInput style={[styles.inputPassword,{width:width-40} ]}                            placeholder="請輸入密碼"                            onChangeText = {(text) => {                                _this.setState({                                    passwordText:text    })   } }                            placeholderTextColor='white'                            secureTextEntry={true}  />      
            <View  style={ {width:width-40,height:1,backgroundColor:'#696969', alignSelf:'center',}}/>            
            <TouchableOpacity onPress={this.loginClick.bind(this)}>      
                  <Text style={[styles.loginBtn ,{width:width-40}, ]}>登錄</Text>     
            </TouchableOpacity>          
       </Image>       
 );   
 };
}
var styles = StyleSheet.create({  
    iconImage:{     
        width:70,    
        height:70,     
        marginTop:50,     
        alignSelf:'center',  
    },   
    titleStyle:{     
        marginTop:20,     
        width:200,      
        color:'white',     
        fontSize:18,     
        alignSelf:'center',      
        textAlign:'center',      
        backgroundColor:'transparent' 
   },   
    inputUsreName:{      
       marginTop:120,     
       height:35,      
       color:'white',     
       alignSelf:'center',   
   },  
   inputPassword:{   
     marginTop:20,    
     height:35,    
     color:'white',     
     alignSelf:'center',  
  },  
  loginBtn:{    
     marginTop:50,      
     height:40,      
     alignSelf:'center',   
     textAlign:'center',    
     padding:10,    
     fontSize:16,   
     backgroundColor:'#696969',        
     color:'white',     
     borderWidth:2,     
     borderRadius:15,      
     borderColor:'#696969',   
   }
});

2桃焕、網(wǎng)絡(luò)請求

import React, { Component } from 'react';
import MD5 from './md5';
class NetUtil extends React.Component {  
  //post請求   
  /***url :請求地址     
       *data:參數(shù)     
       *callback:回調(diào)函數(shù)   
  */    
static  postFrom(url, data, callback) {      
    var fetchOptions = {         
        method: 'POST',       
        headers: {       
             'Accept': 'application/json',     
             'Content-Type': 'application/x-www-form-urlencoded'        
        },        
        body:'data='+data+''
    };      
   fetch(url, fetchOptions).then((response) => 
        response.text()).then((responseText) => 
             {       
                    callback(JSON.parse(responseText));    
            }).done();  
  } 
   /**    
     *url :請求地址    
     *data:參數(shù)(Json對象)   
     *callback:回調(diào)函數(shù)   
  */    
static postJson (url, data, callback) {   
     var fetchOptions = {       
         method: 'POST',         
         headers: {            
        'Accept': 'application/json',                
        'Content-Type': 'application/json'    
        },         
       body: JSON.stringify(data)      
  };    
    fetch(url, fetchOptions).then((response) =>  
         response.text()).then((responseText) => {  
                  callback(JSON.parse(responseText));  
             }).done();   
 }  
  /**   
     *url :請求地址  
     *data:參數(shù)(Json對象) 
     *callback:回調(diào)函數(shù)   
  */    
static post (url, data,common, callback) {  
      var sign=this.sign(data,common);     
      var timestamp = (new Date()).valueOf();   
      var arr=[];      
      for (var key in data) {        
         var obj=key+'='+data[key];             
         arr.push(obj);       
      }      
     var fetchOptions = {     
          method: 'POST',     
          headers: {            
         'Accept': 'application/json',         
         'Content-Type':'application/x-www-form-urlencoded',             
         'PROJECTID':'2',    
         'CLIENT':'51355579f87bb4bcc06ff980e26e10b473d7b72a',              
         'TIMESTAMP':timestamp,        
         'VERSION':'1.0.0',        
         'SIGN':sign,        
         'TOKEN':''           
      },          
       body: arr.join('&')    
    };     
   fetch(url, fetchOptions).then((response) => 
      response.text()).then((responseText) => {   
             callback(JSON.parse(responseText));  
          }).done();   
 }   
 static commonParamsWithAct(act,op){     
      var timestamp = (new Date()).valueOf();       
     return {       
          'PROJECTID':'2',      
          'CLIENT':'51355579f87bb4bcc06ff980e26e10b473d7b72a',      
          'TIMESTAMP':timestamp,       
          'VERSION':'1.0.0',        
          'TOKEN':'',        
          'act':act,        
          'op':op      
  };  
  }   
 static sign(data,common){    
    var arr=[];      
    for (var key in data) {    
        var obj=key+'='+data[key];      
        arr.push(obj);      
    }      
    for (var key in common) {  
          var obj=key+'='+common[key];    
          arr.push(obj);     
    }       
    arr.sort();    
    var sign= arr.join('&');   
    var result= MD5.hex_md5(sign);     
    return result;   
 }   
 //get請求   
 /**   
    *url :請求地址   
    *callback:回調(diào)函數(shù)   
  */   
 static  get(url, callback) {    
    fetch(url).then((response) => 
             response.text()).then((responseText) => {  
                    callback(JSON.parse(responseText));            
             }).done();   
       }
}
module.exports = NetUtil;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末缨叫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子渐尿,更是在濱河造成了極大的恐慌洋侨,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坦敌,死亡現(xiàn)場離奇詭異侣诵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)狱窘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門杜顺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蘸炸,你說我怎么就攤上這事躬络。” “怎么了搭儒?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵穷当,是天一觀的道長提茁。 經(jīng)常有香客問我,道長馁菜,這世上最難降的妖魔是什么茴扁? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮汪疮,結(jié)果婚禮上峭火,老公的妹妹穿的比我還像新娘。我一直安慰自己智嚷,他們只是感情好躲胳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著纤勒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪隆檀。 梳的紋絲不亂的頭發(fā)上摇天,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音恐仑,去河邊找鬼泉坐。 笑死,一個胖子當(dāng)著我的面吹牛裳仆,可吹牛的內(nèi)容都是我干的腕让。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼歧斟,長吁一口氣:“原來是場噩夢啊……” “哼纯丸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起静袖,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤觉鼻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后队橙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坠陈,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年捐康,在試婚紗的時候發(fā)現(xiàn)自己被綠了仇矾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡解总,死狀恐怖贮匕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情倾鲫,我是刑警寧澤粗合,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布萍嬉,位于F島的核電站,受9級特大地震影響隙疚,放射性物質(zhì)發(fā)生泄漏壤追。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一供屉、第九天 我趴在偏房一處隱蔽的房頂上張望行冰。 院中可真熱鬧,春花似錦伶丐、人聲如沸悼做。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽肛走。三九已至,卻和暖如春录别,著一層夾襖步出監(jiān)牢的瞬間朽色,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工组题, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留葫男,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓崔列,卻偏偏與公主長得像梢褐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子赵讯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容