2016-06-17 18:18:03 +08:00
|
|
|
app.factory('alertService', function(){
|
|
|
|
function Alert(){
|
|
|
|
this.title = null;
|
|
|
|
this.message = null;
|
|
|
|
this.visible = null;
|
2016-06-20 14:39:20 +08:00
|
|
|
this.cb = null;
|
2016-06-17 18:18:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-20 14:39:20 +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;
|
2016-06-20 14:39:20 +08:00
|
|
|
this.cb = cb;
|
2016-06-17 18:18:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Alert.prototype.dismiss = function(){
|
|
|
|
this.message = null;
|
|
|
|
this.title = null;
|
|
|
|
this.visible = false;
|
2016-06-20 14:39:20 +08:00
|
|
|
if(this.cb) {this.cb();}
|
2016-06-17 18:18:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
create: function(){
|
|
|
|
return new Alert();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-06-20 14:39:20 +08:00
|
|
|
app.factory('auth', ['$http','$routeParams', '$q', 'session', 'config',function($http,$routeParams, $q, session,config){
|
2016-06-14 14:24:39 +08:00
|
|
|
var _openid = '';
|
|
|
|
|
|
|
|
if(typeof g_openid !== 'undefined'){
|
|
|
|
_openid = g_openid;
|
|
|
|
}
|
|
|
|
|
2016-06-17 18:18:03 +08:00
|
|
|
if(!_openid){
|
|
|
|
_openid = session.get("openid");
|
|
|
|
}
|
|
|
|
|
2016-06-20 14:39:20 +08:00
|
|
|
//是否已经绑定
|
|
|
|
var isBind = function(){
|
|
|
|
var defer = $q.defer();
|
|
|
|
|
|
|
|
var token = getToken();
|
|
|
|
if(token && token.length>10){
|
|
|
|
defer.resolve(token);
|
|
|
|
} else {
|
|
|
|
getOpenId().then(function(openid){
|
|
|
|
return $http.post(
|
|
|
|
config.apiUrl+ 'users/isbind',
|
|
|
|
{openid: openid}
|
|
|
|
)
|
|
|
|
}).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;
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:24:39 +08:00
|
|
|
var getOpenId = function() {
|
|
|
|
var deferred = $q.defer();
|
2016-06-17 18:18:03 +08:00
|
|
|
console.log(_openid);
|
|
|
|
if (typeof _openid !== 'undefined' && _openid && _openid.length > 0){
|
2016-06-14 14:24:39 +08:00
|
|
|
deferred.resolve(_openid);
|
|
|
|
} else {
|
2016-06-20 15:21:13 +08:00
|
|
|
var code = window.g_code || $routeParams.code || session.get("code");
|
2016-06-14 14:24:39 +08:00
|
|
|
$http({
|
|
|
|
url: '/wechat/get_open_id',
|
|
|
|
data: {code: code},
|
|
|
|
method: 'POST'
|
|
|
|
}).then(function successCallback(response) {
|
2016-06-17 18:18:03 +08:00
|
|
|
if(response.data.status != 0){
|
2016-06-17 18:48:27 +08:00
|
|
|
deferred.reject(response.data.message);
|
2016-06-17 18:18:03 +08:00
|
|
|
} else{
|
|
|
|
_openid = response.data.openid;
|
2016-06-20 14:39:20 +08:00
|
|
|
//session.save("openid", _openid);
|
2016-06-17 18:18:03 +08:00
|
|
|
deferred.resolve(_openid);
|
|
|
|
}
|
2016-06-14 14:24:39 +08:00
|
|
|
}, function errorCallback(response) {
|
|
|
|
deferred.reject(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
var openid = function(){
|
|
|
|
return _openid;
|
|
|
|
};
|
2016-06-20 14:39:20 +08:00
|
|
|
var getToken = function(){
|
|
|
|
return session.get("token");
|
|
|
|
}
|
|
|
|
return {isBind: isBind, token: getToken, openid: getOpenId};
|
2016-06-14 16:03:10 +08:00
|
|
|
}]);
|
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};
|
|
|
|
});
|
|
|
|
|
2016-06-14 16:03:10 +08:00
|
|
|
app.factory('common', ['$http', 'auth', '$routeParams', function($http, auth, $routeParams){
|
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,
|
2016-06-20 14:39:20 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}, function errorCallback(response) {
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadCommonData = function(id, type){
|
|
|
|
return $http({
|
|
|
|
method: 'GET',
|
2016-06-20 14:39:20 +08:00
|
|
|
url: apiUrl+ type + "/" + id+"?token="+auth.token()
|
2016-06-14 14:24:39 +08:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
var addCommonPraise = function(act){
|
|
|
|
act.praise_count += 1;
|
|
|
|
act.has_praise = true;
|
|
|
|
|
|
|
|
$http({
|
|
|
|
method: 'POST',
|
|
|
|
url: apiUrl + "praise/" + act.act_id,
|
2016-06-20 14:39:20 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
$http({
|
|
|
|
method: 'POST',
|
|
|
|
url: apiUrl + "praise/" + act.act_id,
|
2016-06-20 14:39:20 +08:00
|
|
|
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) {
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-20 14:39:20 +08:00
|
|
|
var init = function(args){
|
|
|
|
args.scope.formData = {comment: ''};
|
|
|
|
var loadData = function(id){
|
|
|
|
loadCommonData(id, args.type).then(function successCallback(response) {
|
|
|
|
args.loadCallback(response.data);
|
|
|
|
}, function errorCallback(response) {
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
loadData(args.id);
|
|
|
|
args.scope.addReply = function(data){
|
|
|
|
console.log(data.comment);
|
|
|
|
addCommonReply(args.id, args.replyType, data, function(){
|
|
|
|
args.scope.formData = {comment: ''};
|
|
|
|
loadData(args.id);
|
|
|
|
if(typeof args.replyCallback === 'function'){
|
|
|
|
args.replyCallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
args.scope.addPraise = addCommonPraise;
|
|
|
|
args.scope.decreasePraise = decreaseCommonPraise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {init: init, addCommonReply: addCommonReply, loadCommonData: loadCommonData, addCommonPraise: addCommonPraise, decreaseCommonPraise: decreaseCommonPraise};
|
2016-06-14 16:03:10 +08:00
|
|
|
}]);
|