mainCtrl.js 14 KB

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