把activities列表存起来,防止返回时页面变空

This commit is contained in:
guange 2016-04-06 15:19:18 +08:00
parent 606599047d
commit 70c2c9cc9f
1 changed files with 49 additions and 35 deletions

View File

@ -1,41 +1,41 @@
var app = angular.module('wechat', ['ngRoute','ngCookies']);
var apiUrl = 'http://wechat.trustie.net/api/v1/';
var debug = false; //调试标志,如果在本地请置为true
var debug = true; //调试标志,如果在本地请置为true
app.factory('auth', function($http,$routeParams, $cookies){
app.factory('auth', function($http,$routeParams, $cookies, $q){
var _openid = '';
if(debug===true){
_openid = "oCnvgvz8R7QheXE-R9Kkr39j8Ndg";
}
var getOpenId = function(cb) {
var getOpenId = function() {
var deferred = $q.defer();
if (typeof _openid !== 'undefined' && _openid.length > 0) {
cb(_openid);
return;
deferred.resolve(_openid);
} else {
var code = $routeParams.code;
$http({
url: '/wechat/get_open_id',
data: {code: code},
method: 'POST'
}).then(function successCallback(response) {
_openid = response.data.openid;
if(typeof _openid !== 'undefined' && _openid.length>0){
if(debug !== true){ //如果是生产环境,就存到cookies中
$cookies.put("openid", _openid);
}
} else {
if(debug!==true){//考虑从cookies中取出
_openid = $cookies.get('openid');
}
}
deferred.resolve(_openid);
}, function errorCallback(response) {
deferred.reject(response);
});
}
var code = $routeParams.code;
$http({
url: '/wechat/get_open_id',
data: {code: code},
method: 'POST'
}).then(function successCallback(response) {
_openid = response.data.openid;
if(typeof _openid !== 'undefined' && _openid.length>0){
if(debug !== true){ //如果是生产环境,就存到cookies中
$cookies.put("openid", _openid);
}
} else {
if(debug!==true){//考虑从cookies中取出
_openid = $cookies.get('openid');
}
}
cb(_openid);
}, function errorCallback(response) {
cb(null);
});
return deferred.promise;
};
var openid = function(){
return _openid;
@ -43,14 +43,27 @@ app.factory('auth', function($http,$routeParams, $cookies){
return {getOpenId: getOpenId, openid: openid};
});
app.controller('ActivityController',function($scope, $http, auth){
app.factory('rms', function(){
var _saveStorage = {};
var save = function(key, value){
_saveStorage[key] = value;
};
var get = function(key){
return _saveStorage[key];
}
return {save: save, get: get};
});
app.controller('ActivityController',function($scope, $http, auth, rms){
$scope.repaceUrl = function(url){
return "http://www.trustie.net/" + url;
}
console.log("ActivityController load");
$scope.activities = [];
$scope.activities = rms.get("activities") || [];
$scope.page = 1;
var loadActData = function(page){
@ -61,17 +74,18 @@ app.controller('ActivityController',function($scope, $http, auth){
data: {openid: auth.openid(), page: page},
}).then(function successCallback(response) {
$scope.activities = $scope.activities.concat(response.data.data);
rms.save('activities', $scope.activities);
}, function errorCallback(response) {
});
}
auth.getOpenId(function(oid){
if(!oid){
alert("获取openid出错");
} else {
auth.getOpenId().then(
function successCallback(response){
loadActData($scope.page);
}, function errorCallback(response) {
alert("获取openid出错:"+response);
}
});
);
$scope.loadActData = loadActData;