mainCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. 'use strict';
  2. /**
  3. * Clip-Two Main Controller
  4. */
  5. app.controller('mainCtrl', ['$rootScope', '$scope', '$state', '$stateParams', '$window', '$timeout', '$filter', '$ionicLoading', 'Restangular', 'api_login', 'api_text', 'api_user_data', 'getWXJsConfig',
  6. function($rootScope, $scope, $state, $stateParams, $window, $timeout, $filter, $ionicLoading, Restangular, api_login, api_text, api_user_data, getWXJsConfig) {
  7. console.log('mainCtrl loading');
  8. var width = screen.width;
  9. var size = 24 * width / 750 + 'px';
  10. $('html').css({ "font-size": size });
  11. //获取login中的数据
  12. $scope.$on('login-to-main', function(data) {
  13. console.log(data);
  14. var ceshi=data;
  15. var sessionLogin = JSON.parse(sessionStorage.sessionLogin);
  16. var loginUser = sessionLogin.user;
  17. $scope.loginUser = sessionLogin.tokenType;
  18. $ionicLoading.hide();
  19. })
  20. // console.log(sessionLogin);
  21. // if (!sessionStorage.sessionLogin) {
  22. // var promise = getLoginUser.query();
  23. // promise.then(function(sessionLogin) { //返回成功
  24. // $scope.tokenType = 'requester';
  25. // }, function(data) { //返回失败
  26. // });
  27. // } else {
  28. // var sessionLogin = JSON.parse(sessionStorage.sessionLogin);
  29. // var loginUser = sessionLogin.user;
  30. // $scope.tokenType = sessionLogin.tokenType;
  31. // }
  32. $scope.addColor = function(id) {
  33. $scope.iconfontColor = id;
  34. }
  35. $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
  36. // if (isEmpty($auth.retrieveData('auth_headers'))) {
  37. // if (toState.name != "login.signin") {
  38. // event.preventDefault();
  39. // $rootScope.app.isLoginFixed = true;
  40. // return $state.go("login.signin");
  41. // }
  42. // }
  43. // cfpLoadingBar.start();
  44. // $auth.validateUser();
  45. if (toState.name) {
  46. }
  47. });
  48. $rootScope.$on('openroom', function(event, data) {
  49. ddp.subscribe('stream-room-messages', [data.rid, true]).then(function() {}, function(reason) {
  50. console.log('subscribe stream-room-messages:%o', reason);
  51. });
  52. if ($rootScope.imUsrMap[data.from]['unread'].msgs) {
  53. $rootScope.imUsrMap[data.from]['unread'].msgs.clear;
  54. }
  55. $rootScope.filternotifyuser.push(data.from);
  56. });
  57. $rootScope.$on("clip-two.toggled", function(e, target, newState, sameGroup) {
  58. if (newState == false && target == 'off-sidebar') {
  59. $rootScope.$broadcast('chatcloseroom');
  60. //$rootScope.toggle("users");
  61. //$rootScope.filternotifyuser = [];
  62. //ddp.unsubscribe('stream-room-messages');
  63. }
  64. });
  65. $rootScope.$on("closeroom", function(event, data) {
  66. $rootScope.filternotifyuser = [];
  67. ddp.unsubscribe('stream-room-messages');
  68. });
  69. $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
  70. //$auth.validateUser();
  71. //stop loading bar on stateChangeSuccess
  72. // event.targetScope.$watch("$viewContentLoaded", function() {
  73. // cfpLoadingBar.complete();
  74. // });
  75. // scroll top the page on change state
  76. // $document.scrollTo(0, 0);
  77. // if (angular.element('.email-reader').length) {
  78. // angular.element('.email-reader').animate({
  79. // scrollTop: 0
  80. // }, 0);
  81. // }
  82. // Save the route title
  83. $rootScope.currTitle = $state.current.title;
  84. if (angular.isDefined($state.current.pdKey)) {
  85. $rootScope.pdKey = $state.current.pdKey;
  86. }
  87. // console.log($state.current.title);
  88. });
  89. // State not found
  90. $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams) {
  91. //$rootScope.loading = false;
  92. if (angular.isUndefined($rootScope.user.id)) {
  93. $rootScope.loading = false;
  94. }
  95. console.log(unfoundState.to);
  96. // "lazy.state"
  97. console.log(unfoundState.toParams);
  98. // {a:1, b:2}
  99. console.log(unfoundState.options);
  100. // {inherit:false} + default options
  101. });
  102. $rootScope.$on('auth:login-success', function(ev, data) {
  103. $state.go('login.signin');
  104. initImUser(data.email, data.msgUuid);
  105. getMenus();
  106. //console.log(data);
  107. //$rootScope.user = data.user;
  108. //$rootScope.menus = data.menu;
  109. });
  110. $rootScope.$on('auth:validation-success', function(ev, user) {
  111. console.log(user);
  112. // initImUser(user.email, user.msgUuid);
  113. // if ($rootScope.userMenus.length == 0) {
  114. // getMenus();
  115. // }
  116. //alert('Welcome ', user.email);
  117. if (user.user || user.requester) {
  118. if (user.user) {
  119. $rootScope.user = user.user;
  120. } else if (user.requester) {
  121. $rootScope.user = user.requester;
  122. }
  123. }
  124. $rootScope.user.menu = user.menu;
  125. getMenus();
  126. });
  127. $rootScope.$on('auth:invalid', function(ev) {
  128. $state.go('login.signin');
  129. });
  130. $rootScope.$on('auth:validation-error', function(ev) {
  131. // $state.go('login.signin');
  132. //alert('Welcome ', user.email);
  133. console.log("validation-error");
  134. });
  135. $rootScope.$on('auth:session-expired', function(ev) {
  136. //alert('Welcome ', user.email);
  137. $state.go('login.signin');
  138. });
  139. $rootScope.$on('auth:logout-success', function(ev) {
  140. //alert('goodbye');
  141. //delete $rootScope.user;
  142. //delete $rootScope.menus;
  143. $state.go('login.signin');
  144. });
  145. $rootScope.$on('auth:logout-error', function(ev) {
  146. $state.go('login.signin');
  147. });
  148. //测试微信公众号接口
  149. $scope.getWXJsConfig = function() {
  150. // console.log($state);
  151. // wechat / wechat / getJsConfig
  152. // var test = window.location.href;
  153. var test = window.location.href.split('#')[0];
  154. // var test = 'http://testncc.hust.edu.cn';
  155. // var test = 'http://testncc.hust.edu.cn/wechat';
  156. // var test = 'http://testncc.hust.edu.cn/wechat/tab/requesterDashboard';
  157. // alert(test);
  158. var requestUrl = {
  159. // requestUrl: 'http://115.156.150.70:8080/wechat/#/tab/requesterDashboard'
  160. requestUrl: test
  161. };
  162. getWXJsConfig.getConfig(requestUrl).then(function(response) {
  163. var myData = Restangular.stripRestangular(response);
  164. for (var msg in myData) {
  165. alert(msg + ':' + myData[msg]);
  166. }
  167. wx.config({
  168. beta: true,
  169. debug: true,
  170. appId: myData.appId,
  171. timestamp: myData.timestamp,
  172. nonceStr: myData.nonceStr,
  173. signature: myData.signature,
  174. jsApiList: myData.jsApiList
  175. });
  176. wx.ready(function() {
  177. alert("wx.config success.");
  178. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  179. });
  180. wx.error(function(res) {
  181. // alert("wx.config failed.");
  182. for (msg in res) {
  183. alert(msg + ':' + res[msg]);
  184. }
  185. // alert(res);
  186. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,
  187. // 也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  188. });
  189. }, function() {
  190. });
  191. };
  192. // $scope.getWXJsConfig();
  193. // 图片接口
  194. // 拍照、本地选图
  195. var images = {
  196. localId: [],
  197. serverId: []
  198. };
  199. // 拍照或者选择照片
  200. function take_a_photo() {
  201. wx.chooseImage({
  202. count: 1, // 默认9,这里每次只处理一张照片
  203. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  204. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  205. success: function(res) {
  206. images.localId = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  207. var i = 0,
  208. length = images.localId.length;
  209. function upload() {
  210. wx.uploadImage({
  211. localId: images.localId[i],
  212. success: function(res) {
  213. i++;
  214. alert('已上传:' + i + '/' + length);
  215. images.serverId.push(res.serverId);
  216. // res.serverId 就是 media_id,根据它去微信服务器读取图片数据:
  217. var indata = {
  218. "media_id": res.serverId
  219. };
  220. $.post("/weixin/getPhoto.json", indata, function(data) {
  221. if (data.rs == 'f') {
  222. alert("系统错误");
  223. } else {
  224. alert(data.body); // 返回 图片在我们自己的服务器的url
  225. }
  226. }, 'json');
  227. if (i < length) {
  228. upload();
  229. }
  230. },
  231. fail: function(res) {
  232. alert(JSON.stringify(res));
  233. }
  234. });
  235. }
  236. upload();
  237. }
  238. });
  239. }
  240. // $scope.takePhoto = take_a_photo;
  241. }
  242. ]);