socialforge/public/javascripts/wechat/app.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-04-04 22:24:13 +08:00
var app = angular.module('wechat', ['ngRoute']);
var apiUrl = 'http://wechat.trustie.net/api/v1/';
//var openid= "oCnvgvz8R7QheXE-R9Kkr39j8Ndg";
var openid = '';
2016-04-05 15:12:05 +08:00
var debug = true; //调试标志,如果在本地请置为true
if(debug){
openid = "oCnvgvz8R7QheXE-R9Kkr39j8Ndg";
}
2016-04-04 22:24:13 +08:00
app.factory('auth', function($http,$routeParams){
2016-04-05 15:12:05 +08:00
var _openid = openid;
2016-04-04 22:24:13 +08:00
var getOpenId = function(cb) {
if (_openid.length > 0) {
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) {
_openid = data.openid;
cb(_openid);
}, function errorCallback(response) {
cb(null);
});
};
var setOpenId = function(id){
_openid = id;
}
return {getOpenId: getOpenId, setOpenId: setOpenId};
});
app.controller('ActivityController',function($scope, $http, auth){
$scope.repaceUrl = function(url){
return "http://www.trustie.net/" + url;
}
$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",
data: {openid: openid, page: page},
}).then(function successCallback(response) {
$scope.activities = $scope.activities.concat(response.data.data);
}, function errorCallback(response) {
});
}
2016-04-05 15:12:05 +08:00
auth.getOpenId(function(openid){
if(!openid){
alert("获取openid出错");
} else {
openid = openid;
loadActData($scope.page);
}
});
$scope.loadActData = loadActData;
2016-04-04 22:24:13 +08:00
});
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: 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) {
});
}
console.log(auth.getOpenId());
});
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'
})
.otherwise({
redirectTo: '/activities'
});
}]);