socialforge/public/javascripts/wechat/app.js

422 lines
12 KiB
JavaScript

var app = angular.module('wechat', ['ngRoute','ngCookies']);
var apiUrl = 'http://wechat.trustie.net/api/v1/';
var debug = true; //调试标志,如果在本地请置为true
if(debug===true){
apiUrl = 'http://localhost:3000/api/v1/';
}
app.factory('auth', function($http,$routeParams, $cookies, $q){
var _openid = '';
if(debug===true){
_openid = "2";
}
var getOpenId = function() {
var deferred = $q.defer();
if (typeof _openid !== 'undefined' && _openid.length > 0) {
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);
});
}
return deferred.promise;
};
var openid = function(){
return _openid;
}
return {getOpenId: getOpenId, openid: openid};
});
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.replaceUrl = function(url){
return "http://www.trustie.net/" + url;
}
console.log("ActivityController load");
$scope.activities = rms.get("activities") || [];
$scope.page = 1;
var loadActData = function(page){
$scope.page = page;
$http({
method: 'POST',
url: apiUrl+ "activities",
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().then(
function successCallback(response){
loadActData($scope.page);
}, function errorCallback(response) {
alert("获取openid出错:"+response);
}
);
$scope.loadActData = loadActData;
var descToggle = function(){
$(".post-all-content").each(function(){
var postHeight = $(this).height();
if (postHeight > 90){
$(this).parent().next().css("display","block");
$(this).parent().next().toggle(function(){
$(this).text("点击隐藏");
$(this).prev().css("height",postHeight);
},function(){
$(this).text("点击展开");
$(this).prev().css("height",90);
});
}
});
}
$scope.descToggle = descToggle;
});
app.controller('IssueController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "issues/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.issue = response.data.data;
}, function errorCallback(response) {
});
}
loadData($routeParams.id);
$scope.addIssueReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "Issue",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.controller('HomeworkController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "whomeworks/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.homework = response.data.data;
}, function errorCallback(response) {
});
}
loadData($routeParams.id);
$scope.addHomeworkReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "HomeworkCommon",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.controller('CourseNoticeController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "newss/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.news = response.data.data;
}, function errorCallback(response) {
});
};
loadData($routeParams.id);
$scope.addNoticeReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "News",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.controller('CourseDiscussionController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "messages/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.discussion = response.data.data;
}, function errorCallback(response) {
});
};
loadData($routeParams.id);
$scope.addDiscussionReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "Message",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.controller('JournalsController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "journal_for_messages/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.message = response.data.data;
}, function errorCallback(response) {
});
};
loadData($routeParams.id);
$scope.addJournalReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "JournalsForMessage",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.controller('BlogController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
url: apiUrl+ "blog_comments/"+id,
}).then(function successCallback(response) {
console.log(response.data);
$scope.blog = response.data.data;
}, function errorCallback(response) {
});
};
loadData($routeParams.id);
$scope.addBlogReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
type: "BlogComment",
content: data.comment,
openid: auth.openid(),
};
$http({
method: 'POST',
url: apiUrl+ "new_comment/"+$routeParams.id,
data: userInfo,
}).then(function successCallback(response) {
alert("提交成功");
$scope.formData = {comment: ''};
loadData($routeParams.id);
}, function errorCallback(response) {
});
}
});
app.filter('safeHtml', function ($sce) {
return function (input) {
return $sce.trustAsHtml(input);
}
});
app.config(['$routeProvider',function ($routeProvider) {
$routeProvider
.when('/activities', {
templateUrl: 'activities.html',
controller: 'ActivityController'
})
.when('/issues/:id', {
templateUrl: 'issue_detail.html',
controller: 'IssueController'
})
.when('/project_discussion/:id', {
templateUrl: 'project_discussion.html',
controller: 'CourseDiscussionController'
})
.when('/homework/:id', {
templateUrl: 'homework_detail.html',
controller: 'HomeworkController'
})
.when('/course_notice/:id', {
templateUrl: 'course_notice.html',
controller: 'CourseNoticeController'
})
.when('/course_discussion/:id', {
templateUrl: 'course_discussion.html',
controller: 'CourseDiscussionController'
})
.when('/journal_for_message/:id', {
templateUrl: 'jour_message_detail.html',
controller: 'JournalsController'
})
.when('/blog_comment/:id', {
templateUrl: 'blog_detail.html',
controller: 'BlogController'
})
.otherwise({
redirectTo: '/activities'
});
}]);