123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- 'use strict';
- /**
- * Clip-Two Main Controller
- */
- app.controller('mainCtrl', ['$rootScope', '$scope', '$state', '$stateParams', '$window', '$timeout', '$filter', '$ionicLoading', 'Restangular', 'api_login', 'api_text', 'api_user_data', 'getWXJsConfig',
- function($rootScope, $scope, $state, $stateParams, $window, $timeout, $filter, $ionicLoading, Restangular, api_login, api_text, api_user_data, getWXJsConfig) {
- console.log('mainCtrl loading');
- var width = screen.width;
- var size = 24 * width / 750 + 'px';
- $('html').css({ "font-size": size });
- //获取login中的数据
- $scope.$on('login-to-main', function(data) {
- console.log(data);
- var ceshi=data;
- var sessionLogin = JSON.parse(sessionStorage.sessionLogin);
- var loginUser = sessionLogin.user;
- $scope.loginUser = sessionLogin.tokenType;
- $ionicLoading.hide();
- })
- // console.log(sessionLogin);
- // if (!sessionStorage.sessionLogin) {
- // var promise = getLoginUser.query();
- // promise.then(function(sessionLogin) { //返回成功
- // $scope.tokenType = 'requester';
- // }, function(data) { //返回失败
- // });
- // } else {
- // var sessionLogin = JSON.parse(sessionStorage.sessionLogin);
- // var loginUser = sessionLogin.user;
- // $scope.tokenType = sessionLogin.tokenType;
- // }
- $scope.addColor = function(id) {
- $scope.iconfontColor = id;
- }
- $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
- // if (isEmpty($auth.retrieveData('auth_headers'))) {
- // if (toState.name != "login.signin") {
- // event.preventDefault();
- // $rootScope.app.isLoginFixed = true;
- // return $state.go("login.signin");
- // }
- // }
- // cfpLoadingBar.start();
- // $auth.validateUser();
- if (toState.name) {
- }
- });
- $rootScope.$on('openroom', function(event, data) {
- ddp.subscribe('stream-room-messages', [data.rid, true]).then(function() {}, function(reason) {
- console.log('subscribe stream-room-messages:%o', reason);
- });
- if ($rootScope.imUsrMap[data.from]['unread'].msgs) {
- $rootScope.imUsrMap[data.from]['unread'].msgs.clear;
- }
- $rootScope.filternotifyuser.push(data.from);
- });
- $rootScope.$on("clip-two.toggled", function(e, target, newState, sameGroup) {
- if (newState == false && target == 'off-sidebar') {
- $rootScope.$broadcast('chatcloseroom');
- //$rootScope.toggle("users");
- //$rootScope.filternotifyuser = [];
- //ddp.unsubscribe('stream-room-messages');
- }
- });
- $rootScope.$on("closeroom", function(event, data) {
- $rootScope.filternotifyuser = [];
- ddp.unsubscribe('stream-room-messages');
- });
- $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
- //$auth.validateUser();
- //stop loading bar on stateChangeSuccess
- // event.targetScope.$watch("$viewContentLoaded", function() {
- // cfpLoadingBar.complete();
- // });
- // scroll top the page on change state
- // $document.scrollTo(0, 0);
- // if (angular.element('.email-reader').length) {
- // angular.element('.email-reader').animate({
- // scrollTop: 0
- // }, 0);
- // }
- // Save the route title
- $rootScope.currTitle = $state.current.title;
- if (angular.isDefined($state.current.pdKey)) {
- $rootScope.pdKey = $state.current.pdKey;
- }
- // console.log($state.current.title);
- });
- // State not found
- $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams) {
- //$rootScope.loading = false;
- if (angular.isUndefined($rootScope.user.id)) {
- $rootScope.loading = false;
- }
- console.log(unfoundState.to);
- // "lazy.state"
- console.log(unfoundState.toParams);
- // {a:1, b:2}
- console.log(unfoundState.options);
- // {inherit:false} + default options
- });
- $rootScope.$on('auth:login-success', function(ev, data) {
- $state.go('login.signin');
- initImUser(data.email, data.msgUuid);
- getMenus();
- //console.log(data);
- //$rootScope.user = data.user;
- //$rootScope.menus = data.menu;
- });
- $rootScope.$on('auth:validation-success', function(ev, user) {
- console.log(user);
- // initImUser(user.email, user.msgUuid);
- // if ($rootScope.userMenus.length == 0) {
- // getMenus();
- // }
- //alert('Welcome ', user.email);
- if (user.user || user.requester) {
- if (user.user) {
- $rootScope.user = user.user;
- } else if (user.requester) {
- $rootScope.user = user.requester;
- }
- }
- $rootScope.user.menu = user.menu;
- getMenus();
- });
- $rootScope.$on('auth:invalid', function(ev) {
- $state.go('login.signin');
- });
- $rootScope.$on('auth:validation-error', function(ev) {
- // $state.go('login.signin');
- //alert('Welcome ', user.email);
- console.log("validation-error");
- });
- $rootScope.$on('auth:session-expired', function(ev) {
- //alert('Welcome ', user.email);
- $state.go('login.signin');
- });
- $rootScope.$on('auth:logout-success', function(ev) {
- //alert('goodbye');
- //delete $rootScope.user;
- //delete $rootScope.menus;
- $state.go('login.signin');
- });
- $rootScope.$on('auth:logout-error', function(ev) {
- $state.go('login.signin');
- });
- //测试微信公众号接口
- $scope.getWXJsConfig = function() {
- // console.log($state);
- // wechat / wechat / getJsConfig
- // var test = window.location.href;
- var test = window.location.href.split('#')[0];
- // var test = 'http://testncc.hust.edu.cn';
- // var test = 'http://testncc.hust.edu.cn/wechat';
- // var test = 'http://testncc.hust.edu.cn/wechat/tab/requesterDashboard';
- // alert(test);
- var requestUrl = {
- // requestUrl: 'http://115.156.150.70:8080/wechat/#/tab/requesterDashboard'
- requestUrl: test
- };
- getWXJsConfig.getConfig(requestUrl).then(function(response) {
- var myData = Restangular.stripRestangular(response);
- for (var msg in myData) {
- alert(msg + ':' + myData[msg]);
- }
- wx.config({
- beta: true,
- debug: true,
- appId: myData.appId,
- timestamp: myData.timestamp,
- nonceStr: myData.nonceStr,
- signature: myData.signature,
- jsApiList: myData.jsApiList
- });
- wx.ready(function() {
- alert("wx.config success.");
- // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
- });
- wx.error(function(res) {
- // alert("wx.config failed.");
- for (msg in res) {
- alert(msg + ':' + res[msg]);
- }
- // alert(res);
- // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,
- // 也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
- });
- }, function() {
- });
- };
- // $scope.getWXJsConfig();
- // 图片接口
- // 拍照、本地选图
- var images = {
- localId: [],
- serverId: []
- };
- // 拍照或者选择照片
- function take_a_photo() {
- wx.chooseImage({
- count: 1, // 默认9,这里每次只处理一张照片
- sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
- success: function(res) {
- images.localId = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
- var i = 0,
- length = images.localId.length;
- function upload() {
- wx.uploadImage({
- localId: images.localId[i],
- success: function(res) {
- i++;
- alert('已上传:' + i + '/' + length);
- images.serverId.push(res.serverId);
- // res.serverId 就是 media_id,根据它去微信服务器读取图片数据:
- var indata = {
- "media_id": res.serverId
- };
- $.post("/weixin/getPhoto.json", indata, function(data) {
- if (data.rs == 'f') {
- alert("系统错误");
- } else {
- alert(data.body); // 返回 图片在我们自己的服务器的url
- }
- }, 'json');
- if (i < length) {
- upload();
- }
- },
- fail: function(res) {
- alert(JSON.stringify(res));
- }
- });
- }
- upload();
- }
- });
- }
- // $scope.takePhoto = take_a_photo;
- }
- ]);
|