socialforge/public/javascripts/wechat/others/factory.js

303 lines
9.7 KiB
JavaScript
Raw Normal View History

2016-06-17 18:18:03 +08:00
app.factory('alertService', function(){
function Alert(){
this.title = null;
this.message = null;
this.visible = null;
this.cb = null;
this.invite = "";
2016-06-17 18:18:03 +08:00
}
Alert.prototype.showMessage = function(title, msg, cb){
2016-06-17 18:18:03 +08:00
this.message = msg;
this.title = title;
this.visible = true;
this.cb = cb;
this.invite = "";
2016-06-17 18:18:03 +08:00
}
Alert.prototype.dismiss = function(){
this.message = null;
this.title = null;
this.visible = false;
this.invite = "";
if(this.cb) {this.cb();}
2016-06-17 18:18:03 +08:00
}
return {
create: function(){
return new Alert();
}
}
});
app.factory('auth', ['$http','$routeParams', '$q', 'session', 'config',function($http,$routeParams, $q, session,config){
//是否已经绑定
var isBind = function(){
var defer = $q.defer();
var token = getToken();
if(token && token.length>10){
defer.resolve(token);
} else {
var code = window.g_code || $routeParams.code || session.get("code");
$http.post(
'/wechat/get_bind',
{} ///不用传code了,都由服务器来处理
).then(function(response){
if(response.data.status!=0){
defer.reject(response.data.message);
}else {
session.save("token", response.data.token);
defer.resolve(response.data.token);
}
}).catch(function(e){
defer.reject(e);
});
}
return defer.promise;
}
var getToken = function(){
return session.get("token");
}
return {get_bind: isBind, token: getToken};
}]);
2016-06-14 14:24:39 +08:00
2016-06-17 18:18:03 +08:00
app.factory("session", function(){
return {
save: function(key,value){
sessionStorage.setItem(key,value);
},
get: function(key){
return sessionStorage.getItem(key);
}
}
});
2016-06-14 14:24:39 +08:00
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.factory('common', ['$http', 'auth', '$routeParams','rms','config', function($http, auth, $routeParams,rms,config){
2016-06-14 14:24:39 +08:00
var addCommonReply = function(id, type, data, cb){
if(!data.comment || data.comment.length<=0){
return;
}
var temp = data.comment.replace(/\n/g,'<br/>');
var userInfo = {
type: type,
content: temp,
token: auth.token()
2016-06-14 14:24:39 +08:00
};
//回复按钮禁用
data.disabled = true;
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+id,
data: userInfo
}).then(function successCallback(response) {
//alert("提交成功");
//数据提交完成,回复按钮启用
data.disabled = false;
if(typeof cb === 'function'){
cb();
}
//保证内外回复数一致
activities = rms.get("activities") || [];
course_activities = rms.get("course_activities") || [];
project_activities = rms.get("project_activities") || [];
for(var i in activities){
if(activities[i].act_id == id){
activities[i].reply_count += 1;
break;
}
}
for(var i in course_activities){
if(course_activities[i].act_id == id){
course_activities[i].reply_count += 1;
break;
}
}
for(var i in project_activities){
if(project_activities[i].act_id == id){
project_activities[i].reply_count += 1;
break;
}
}
rms.save("activities",activities);
rms.save("course_activities",course_activities);
rms.save("project_activities",project_activities);
2016-06-14 14:24:39 +08:00
}, function errorCallback(response) {
});
};
2016-07-20 13:34:58 +08:00
// var loadCommonData = function(id, type,replytype,page){
// return $http({
// method: 'GET',
// url: apiUrl+ type + "/" + id+"?token="+auth.token(),
// })
// };
var loadCommonData = function(id, type,replytype,page){
2016-06-14 14:24:39 +08:00
return $http({
2016-07-20 13:34:58 +08:00
method: 'POST',
url: apiUrl+ type + "/" + id,
data:{token:auth.token(),type:replytype,page:page}
2016-06-14 14:24:39 +08:00
})
};
var addCommonPraise = function(act){
// act.praise_count += 1;
// act.has_praise = true;
2016-06-14 14:24:39 +08:00
$http({
method: 'POST',
url: apiUrl + "praise/" + act.act_id,
data:{token:auth.token(),type:act.act_type}
2016-06-14 14:24:39 +08:00
}).then(function successCallback(response) {
console.log(response.data);
}, function errorCallback(response) {
});
};
var decreaseCommonPraise = function(act){
// act.praise_count -= 1;
// act.has_praise = false;
2016-06-14 14:24:39 +08:00
$http({
method: 'POST',
url: apiUrl + "praise/" + act.act_id,
data:{token:auth.token(),type:act.act_type}
2016-06-14 14:24:39 +08:00
}).then(function successCallback(response) {
console.log(response.data);
}, function errorCallback(response) {
});
};
var init = function(args){
args.scope.formData = {comment: ''};
2016-07-20 10:09:27 +08:00
var loadData = function(id,replytype,page){
loadCommonData(id, args.type,replytype,page).then(function successCallback(response) {
2016-07-20 13:34:58 +08:00
args.loadCallback(response.data);
}, function errorCallback(response) {
});
};
2016-07-20 10:09:27 +08:00
loadData(args.id,0,0);
args.scope.addReply = function(data){
console.log(data.comment);
addCommonReply(args.id, args.replyType, data, function(){
args.scope.formData = {comment: ''};
2016-07-20 10:09:27 +08:00
loadData(args.id,0,0);
if(typeof args.replyCallback === 'function'){
args.replyCallback();
}
});
};
// 动态详情界面点赞与动态界面的数据要同步 保证进入详情点赞后出来显示一致
args.scope.addPraise = function(act){
activities = rms.get("activities") || [];
course_activities = rms.get("course_activities") || [];
project_activities = rms.get("project_activities") || [];
for(var i in activities){
if(activities[i].act_id == act.act_id){
activities[i].praise_count += 1;
activities[i].has_praise = true;
break;
}
}
for(var i in course_activities){
if(course_activities[i].act_id == act.act_id){
course_activities[i].praise_count += 1;
course_activities[i].has_praise = true;
break;
}
}
for(var i in project_activities){
if(project_activities[i].act_id == act.act_id){
project_activities[i].praise_count += 1;
project_activities[i].has_praise = true;
break;
}
}
rms.save("activities",activities);
rms.save("course_activities",course_activities);
rms.save("project_activities",project_activities);
act.praise_count += 1;
act.has_praise = true;
addCommonPraise(act);
};
args.scope.decreasePraise = function(act){
activities = rms.get("activities") || [];
course_activities = rms.get("course_activities") || [];
project_activities = rms.get("project_activities") || [];
for(var i in activities){
if(activities[i].act_id == act.act_id){
activities[i].praise_count -= 1;
activities[i].has_praise = false;
break;
}
}
for(var i in course_activities){
if(course_activities[i].act_id == act.act_id){
course_activities[i].praise_count -= 1;
course_activities[i].has_praise = false;
break;
}
}
for(var i in project_activities){
if(project_activities[i].act_id == act.act_id){
project_activities[i].praise_count -= 1;
project_activities[i].has_praise = false;
break;
}
}
rms.save("activities",activities);
rms.save("course_activities",course_activities);
rms.save("project_activities",project_activities);
act.praise_count -= 1;
act.has_praise = false;
decreaseCommonPraise(act);
};
2016-07-20 13:34:58 +08:00
args.scope.showMoreReply = function(replytype,data){
if(!data.page){
data.page = 1;
}
else{
data.page = data.page + 1;
}
loadCommonData(data.id, args.type,replytype,data.page).then(function successCallback(response) {
2016-07-20 13:34:58 +08:00
args.loadCallback(response.data);
}, function errorCallback(response) {
});
};
args.scope.commentreply_path = config.rootPath+ 'templates/comment_reply.html';
}
return {init: init, addCommonReply: addCommonReply, loadCommonData: loadCommonData, addCommonPraise: addCommonPraise, decreaseCommonPraise: decreaseCommonPraise};
}]);