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 }); } }); }