socialforge/public/javascripts/wechat/app.js

239 lines
6.3 KiB
JavaScript

var app = angular.module('wechat', ['ngRoute','ngCookies']);
var apiUrl = 'http://localhost:3000/api/v1/';
var debug = true; //调试标志,如果在本地请置为true
app.factory('auth', function($http,$routeParams, $cookies){
var _openid = '';
if(debug===true){
_openid = "2";
}
var getOpenId = function(cb) {
if (typeof _openid !== 'undefined' && _openid.length > 0) {
cb(_openid);
return;
}
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);
});
};
var openid = function(){
return _openid;
}
return {getOpenId: getOpenId, openid: openid};
});
app.controller('ActivityController',function($scope, $http, auth){
$scope.replaceUrl = function(url){
return "http://www.trustie.net/" + url;
}
console.log("ActivityController load");
$scope.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);
}, function errorCallback(response) {
});
}
auth.getOpenId(function(oid){
if(!oid){
alert("获取openid出错");
} else {
loadActData($scope.page);
}
});
$scope.loadActData = loadActData;
});
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.addIssueReply = 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.addIssueReply = 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.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('/homework/:id', {
templateUrl: 'homework_detail.html',
controller: 'HomeworkController'
})
.when('/course_notice/:id', {
templateUrl: 'course_notice.html',
controller: 'CourseNoticeController'
})
.otherwise({
redirectTo: '/activities'
});
}]);