diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index 28a8f74e4..bd7ba8751 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -412,7 +412,7 @@ module ApiHelper #课程动态的更新 def update_course_activity_api type, id - course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", type.to_s, id).first + course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", type.to_s, id.to_i).first if course_activity course_activity.updated_at = Time.now course_activity.save @@ -420,7 +420,7 @@ module ApiHelper end #首页动态更新 def update_user_activity_api type, id - user_activity = UserActivity.where("act_type=? and act_id =?", type.to_s, id).first + user_activity = UserActivity.where("act_type=? and act_id =?", type.to_s, id.to_i).first if user_activity user_activity.updated_at = Time.now user_activity.save @@ -428,7 +428,7 @@ module ApiHelper end #项目动态更新 def update_forge_activity_api type, id - forge_activity = ForgeActivity.where("forge_act_type=? and forge_act_id=?", type.to_s, id).first + forge_activity = ForgeActivity.where("forge_act_type=? and forge_act_id=?", type.to_s, id.to_i).first if forge_activity forge_activity.updated_at = Time.now forge_activity.save @@ -436,7 +436,7 @@ module ApiHelper end #组织动态更新 def update_org_activity_api type , id - org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", type.to_s, id).first + org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", type.to_s, id.to_i).first if org_activity org_activity.updated_at = Time.now org_activity.save @@ -444,7 +444,7 @@ module ApiHelper end #个人动态更新 def update_principal_activity_api type, id - principal_activity = PrincipalActivity.where("principal_act_type=? and principal_act_id =?", type.to_s, id).first + principal_activity = PrincipalActivity.where("principal_act_type=? and principal_act_id =?", type.to_s, id.to_i).first if principal_activity principal_activity.updated_at = Time.now principal_activity.save diff --git a/public/javascripts/wechat/app.js b/public/javascripts/wechat/app.js index 05fc16c0a..c69b119c1 100644 --- a/public/javascripts/wechat/app.js +++ b/public/javascripts/wechat/app.js @@ -1,41 +1,45 @@ var app = angular.module('wechat', ['ngRoute','ngCookies']); -var apiUrl = 'http://localhost:3000/api/v1/'; -var debug = true; //调试标志,如果在本地请置为true +var apiUrl = 'http://wechat.trustie.net/api/v1/'; +var debug = false; //调试标志,如果在本地请置为true -app.factory('auth', function($http,$routeParams, $cookies){ +if(debug===true){ + apiUrl = 'http://localhost:3000/api/v1/'; +} + +app.factory('auth', function($http,$routeParams, $cookies, $q){ var _openid = ''; if(debug===true){ _openid = "1"; } - var getOpenId = function(cb) { + var getOpenId = function() { + var deferred = $q.defer(); 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'); + 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'); + } } - } - - cb(_openid); - }, function errorCallback(response) { - cb(null); - }); - + deferred.resolve(_openid); + }, function errorCallback(response) { + deferred.reject(response); + }); + } + return deferred.promise; }; var openid = function(){ return _openid; @@ -43,14 +47,28 @@ app.factory('auth', function($http,$routeParams, $cookies){ return {getOpenId: getOpenId, openid: openid}; }); -app.controller('ActivityController',function($scope, $http, auth){ - $scope.replaceUrl = function(url){ + +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.repaceUrl = function(url){ return "http://www.trustie.net/" + url; } console.log("ActivityController load"); - $scope.activities = []; + $scope.activities = rms.get("activities") || []; $scope.page = 1; var loadActData = function(page){ @@ -61,17 +79,18 @@ app.controller('ActivityController',function($scope, $http, auth){ 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(function(oid){ - if(!oid){ - alert("获取openid出错"); - } else { + auth.getOpenId().then( + function successCallback(response){ loadActData($scope.page); + }, function errorCallback(response) { + alert("获取openid出错:"+response); } - }); + ); + $scope.loadActData = loadActData; });