socialforge/public/javascripts/wechat/app.js

239 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-04-05 17:19:00 +08:00
var app = angular.module('wechat', ['ngRoute','ngCookies']);
2016-04-06 11:29:50 +08:00
var apiUrl = 'http://localhost:3000/api/v1/';
var debug = true; //调试标志,如果在本地请置为true
2016-04-05 15:12:05 +08:00
2016-04-05 17:19:00 +08:00
app.factory('auth', function($http,$routeParams, $cookies){
var _openid = '';
2016-04-04 22:24:13 +08:00
2016-04-05 17:19:00 +08:00
if(debug===true){
2016-04-06 11:29:50 +08:00
_openid = "2";
2016-04-05 17:19:00 +08:00
}
2016-04-04 22:24:13 +08:00
var getOpenId = function(cb) {
2016-04-05 17:23:36 +08:00
if (typeof _openid !== 'undefined' && _openid.length > 0) {
2016-04-04 22:24:13 +08:00
cb(_openid);
2016-04-05 15:12:05 +08:00
return;
2016-04-04 22:24:13 +08:00
}
var code = $routeParams.code;
$http({
url: '/wechat/get_open_id',
data: {code: code},
method: 'POST'
}).then(function successCallback(response) {
2016-04-05 16:32:42 +08:00
_openid = response.data.openid;
2016-04-05 17:35:27 +08:00
if(typeof _openid !== 'undefined' && _openid.length>0){
if(debug !== true){ //如果是生产环境,就存到cookies中
$cookies.put("openid", _openid);
}
} else {
if(debug!==true){//考虑从cookies中取出
_openid = $cookies.get('openid');
}
2016-04-05 17:19:00 +08:00
}
2016-04-05 17:35:27 +08:00
2016-04-04 22:24:13 +08:00
cb(_openid);
}, function errorCallback(response) {
cb(null);
});
};
2016-04-05 17:19:00 +08:00
var openid = function(){
return _openid;
2016-04-04 22:24:13 +08:00
}
2016-04-05 17:19:00 +08:00
return {getOpenId: getOpenId, openid: openid};
2016-04-04 22:24:13 +08:00
});
app.controller('ActivityController',function($scope, $http, auth){
2016-04-06 09:38:46 +08:00
$scope.replaceUrl = function(url){
2016-04-04 22:24:13 +08:00
return "http://www.trustie.net/" + url;
}
2016-04-05 17:19:00 +08:00
console.log("ActivityController load");
2016-04-04 22:24:13 +08:00
$scope.activities = [];
$scope.page = 1;
2016-04-05 15:12:05 +08:00
var loadActData = function(page){
2016-04-04 22:24:13 +08:00
$scope.page = page;
$http({
method: 'POST',
url: apiUrl+ "activities",
2016-04-05 17:19:00 +08:00
data: {openid: auth.openid(), page: page},
2016-04-04 22:24:13 +08:00
}).then(function successCallback(response) {
$scope.activities = $scope.activities.concat(response.data.data);
}, function errorCallback(response) {
});
}
2016-04-05 16:32:42 +08:00
auth.getOpenId(function(oid){
if(!oid){
2016-04-05 15:12:05 +08:00
alert("获取openid出错");
} else {
loadActData($scope.page);
}
});
$scope.loadActData = loadActData;
2016-04-06 11:29:50 +08:00
});
app.controller('IssueController', function($scope, $http, $routeParams, auth){
$scope.formData = {comment: ''};
2016-04-05 15:12:05 +08:00
2016-04-06 11:29:50 +08:00
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) {
});
}
2016-04-04 22:24:13 +08:00
});
2016-04-06 14:44:35 +08:00
app.controller('HomeworkController', function($scope, $http, $routeParams, auth){
2016-04-04 22:24:13 +08:00
$scope.formData = {comment: ''};
var loadData = function(id){
$http({
method: 'GET',
2016-04-06 14:44:35 +08:00
url: apiUrl+ "whomeworks/"+id,
2016-04-04 22:24:13 +08:00
}).then(function successCallback(response) {
console.log(response.data);
2016-04-06 14:44:35 +08:00
$scope.homework = response.data.data;
2016-04-04 22:24:13 +08:00
}, function errorCallback(response) {
});
}
loadData($routeParams.id);
$scope.addIssueReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
2016-04-06 14:44:35 +08:00
type: "HomeworkCommon",
2016-04-04 22:24:13 +08:00
content: data.comment,
2016-04-05 17:19:00 +08:00
openid: auth.openid(),
2016-04-04 22:24:13 +08:00
};
$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) {
});
}
2016-04-06 11:29:50 +08:00
});
2016-04-04 22:24:13 +08:00
2016-04-06 14:44:35 +08:00
app.controller('CourseNoticeController', function($scope, $http, $routeParams, auth){
2016-04-06 11:29:50 +08:00
$scope.formData = {comment: ''};
2016-04-04 22:24:13 +08:00
2016-04-06 11:29:50 +08:00
var loadData = function(id){
$http({
method: 'GET',
2016-04-06 14:44:35 +08:00
url: apiUrl+ "newss/"+id,
2016-04-06 11:29:50 +08:00
}).then(function successCallback(response) {
console.log(response.data);
2016-04-06 14:44:35 +08:00
$scope.news = response.data.data;
2016-04-06 11:29:50 +08:00
}, function errorCallback(response) {
});
}
loadData($routeParams.id);
$scope.addIssueReply = function(data){
console.log(data.comment);
if(!data.comment || data.comment.length<=0){
return;
}
var userInfo = {
2016-04-06 14:44:35 +08:00
type: "News",
2016-04-06 11:29:50 +08:00
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) {
});
}
2016-04-04 22:24:13 +08:00
});
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'
})
2016-04-06 11:29:50 +08:00
.when('/homework/:id', {
templateUrl: 'homework_detail.html',
controller: 'HomeworkController'
})
2016-04-06 14:44:35 +08:00
.when('/course_notice/:id', {
templateUrl: 'course_notice.html',
controller: 'CourseNoticeController'
})
2016-04-04 22:24:13 +08:00
.otherwise({
redirectTo: '/activities'
});
2016-04-05 16:32:42 +08:00
}]);