1. APP如何使自己保持登錄狀態,需要token保持與后台服務程序的聯系。
后台使用java。首先生成一個Guid,后台檢查token值需要通過session即可。
import java.util.UUID; public class GuidUtil{ public static final String GenerateGUID(){ UUID uuid = UUID.randomUUID(); return uuid.toString(); } }
2. 使用用戶名和密碼登錄
AdSupportIOS.getAdvertisingId(function (deviceId) { var indata = 'username=' + user + '&password=' + password + "&deviceId=" + deviceId; Util.postAjax(path, indata, function (data) { if (!data.error) { that.props.setSet({ showLogin: { height:0, width:0, flex:0, }, showIndex:{ flex:1, opacity:1 }, isLoadingShow: false }); //加入數據到本地 AsyncStorage.multiSet([ ['user', user], ['password',password], ['token', data.token?data.token:''], ['realname', data.realname?data.realname:''], ['email', data.email?data.email:''], ['sex', data.sex?data.sex:''], ], function (err) { if (!err) { console.log("保存成功"); } }); } else { AlertIOS.alert('登錄', '用戶名或者密碼錯誤'); that.props.setSet({ showLogin: { flex:1, opacity:1 }, showIndex:{ height:0, width:0, flex:0, }, isLoadingShow: false }); } }); }, function () { AlertIOS.alert('設置', '無法獲取設備唯一標識'); });
3. 在componentDidMount方法讀取APP本地緩存AsyncStorage,再通過token與后台通信。
componentDidMount() { var that = this; AsyncStorage.getItem('token', function(err, token){ if(!err && token){ var path = Global.path +'sys/loginByTokenApp.do'; var indata = 'token=' + token; Util.postAjax(path, indata, function (data) { if(!data.error){ that.setState({ showLogin: { height:0, width:0, flex:0, }, showIndex:{ flex:1, opacity:1 }, isLoadingShow: false }); } }); }else{ that.setState({ showIndex: { height:0, opacity:0 }, showLogin:{ flex:1, opacity:1 }, isLoadingShow: false }); } }); }