home.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. (function (app) {
  2. app.controller('homeCtrl', ["$scope", "$http", 'config', "$state", "msg", "$ionicPopover", "homeService"
  3. , function ($scope, $http, config, $state, msg, $ionicPopover, homeService) {
  4. $scope.type = 'hot';//tab切换
  5. $scope.chosedIndex = 0;
  6. $scope.clickAvatar = function(index){
  7. $scope.chosedIndex = index;
  8. };
  9. $ionicPopover.fromTemplateUrl('my-popover.html', {
  10. scope: $scope
  11. }).then(function (popover) {
  12. $scope.popover = popover;
  13. });
  14. $scope.openPopover = function ($event) {
  15. $scope.popover.show($event);
  16. };
  17. $scope.closePopover = function () {
  18. $scope.popover.hide();
  19. };
  20. $scope.changetype = function (type) {
  21. $scope.type = type;
  22. $scope.load(true);
  23. };
  24. $scope.toDetail = function (id) {
  25. $state.go('app.home_dreamdetail',{id:id});
  26. };
  27. $scope.toUserDetail = function (id) {
  28. $state.go('app.userDetail',{id:id});
  29. };
  30. $scope.filter = {
  31. hasMore: false,
  32. pageIndex: 1,
  33. pageSize: 20
  34. }
  35. $scope.index = {
  36. banner: [],
  37. users: [],
  38. dreams:[]
  39. };
  40. $scope.next=function(type){
  41. debugger;
  42. //type:1 前一个,type:2 后一个
  43. if($scope.index.users.length>0){
  44. if(type==1)
  45. {
  46. var temp= $scope.index.users.splice(0,1);
  47. $scope.index.users.splice($scope.index.users.length-1,1,temp);
  48. }
  49. if (type==2){
  50. var temp= $scope.index.users.splice($scope.index.users.length-1,1);
  51. $scope.index.users.splice(0,1,temp);
  52. }
  53. }
  54. }
  55. $scope.load = function (init) {
  56. if (init) {
  57. $scope.filter.pageIndex = 1;
  58. $scope.index.dreams = [];
  59. }
  60. msg.loading();
  61. homeService.index($scope.type, $scope.filter.pageIndex).then(function (result) {
  62. msg.hide();
  63. $scope.index.banners = result.data.data.banners;
  64. $scope.index.users = result.data.data.users;
  65. $scope.filter.pageIndex++;
  66. var more = (result.data.data.dreams.data.length >= $scope.filter.pageSize);
  67. $scope.filter.hasMore = more;
  68. $scope.index.dreams = $scope.index.dreams.concat(result.data.data.dreams.data);
  69. if (init) {
  70. $scope.$broadcast('scroll.refreshComplete');
  71. } else {
  72. $scope.$broadcast('scroll.infiniteScrollComplete');
  73. }
  74. }, function (error) {
  75. msg.hide();
  76. });
  77. }
  78. $scope.qrscan = function () {
  79. cordova.plugins.barcodeScanner.scan(
  80. function (result) {
  81. //扫码成功后执行的回调函数
  82. alert("收到一个二维码\n" +
  83. "扫码文字结果: " + result.text + "\n" +
  84. "格式: " + result.format + "\n" +
  85. "是否在扫码页面取消扫码: " + result.cancelled);
  86. },
  87. function (error) {
  88. //扫码失败执行的回调函数
  89. alert("Scanning failed: " + error);
  90. }, {
  91. preferFrontCamera: false, // iOS and Android 设置前置摄像头
  92. showFlipCameraButton: true, // iOS and Android 显示旋转摄像头按钮
  93. showTorchButton: true, // iOS and Android 显示打开闪光灯按钮
  94. torchOn: false, // Android, launch with the torch switched on (if available)打开手电筒
  95. prompt: "在扫描区域内放置二维码", // Android提示语
  96. resultDisplayDuration: 500, // Android, display scanned text for X ms.
  97. //0 suppresses it entirely, default 1500 设置扫码时间的参数
  98. formats: "QR_CODE", // 二维码格式可设置多种类型
  99. orientation: "portrait", // Android only (portrait|landscape),
  100. //default unset so it rotates with the device在安卓上 landscape 是横屏状态
  101. disableAnimations: true, // iOS 是否禁止动画
  102. disableSuccessBeep: false // iOS 禁止成功后提示声音 “滴”
  103. }
  104. );
  105. }
  106. $scope.data = {};
  107. $scope.load(true);
  108. $scope.$on('$ionicView.beforeLeave', function () {
  109. $scope.popover.hide();
  110. });
  111. }]);
  112. app.controller('dreamDetailCtrl', ["$scope", "$state", "$ionicModal","storage","msg", "common", "config", "homeService", "$ionicTabsDelegate", "$stateParams", "$ionicModal", "$timeout", "$ionicScrollDelegate", "$interval"
  113. , function ($scope, $state,$ionicModal, storage , msg, common, config, homeService, $ionicTabsDelegate, $stateParams, $ionicModal, $timeout, $ionicScrollDelegate, $interval) {
  114. var id = $stateParams.id;
  115. var top3user = '';
  116. $scope.chosedIndex = 0;
  117. $scope.clickAvatar = function(index){
  118. $scope.chosedIndex = index;
  119. };
  120. // id=5;
  121. $scope.load = function (id) {
  122. msg.loading();
  123. homeService.dreamDetail(id).then(function (result) {
  124. msg.hide();
  125. console.log(result);
  126. $scope.dream = result.data.data;
  127. var end_time = new Date($scope.dream.end_time.replace(/-/g, "/"));
  128. var date = new Date();
  129. var inter = (end_time.getTime() - date.getTime()) / 1000;
  130. leftTimer(inter);
  131. $scope.multi.a = $scope.dream.a;
  132. $scope.multi.b = new Date($scope.dream.created_at.replace(/-/g, "/"));
  133. $scope.multi.c = $scope.dream.c;
  134. $scope.multi.promise = $scope.dream.a;
  135. debugger;
  136. var newtop3user = '' ;
  137. angular.forEach($scope.dream.top3user, function(user) {
  138. newtop3user= newtop3user +user.id+","
  139. });
  140. if(top3user!=''&&newtop3user!=top3user){
  141. //测试动画切换
  142. $timeout(function() {
  143. $scope.sort.slide1 = 'list-grow-animation';
  144. $scope.sort.slide2 = 'slide-in-both-ways';
  145. $scope.sort.slide3 = 'bounce-animation';
  146. }, 50);
  147. }
  148. top3user = newtop3user;
  149. if($scope.dream.top3user[0])$scope.sort.first = $scope.dream.top3user[0].avatar;
  150. if($scope.dream.top3user[1])$scope.sort.second = $scope.dream.top3user[1].avatar;
  151. if($scope.dream.top3user[2])$scope.sort.third = $scope.dream.top3user[2].avatar;
  152. $scope.calcmultiplier();
  153. }, function (error) {
  154. msg.hide();
  155. });
  156. };
  157. $ionicModal.fromTemplateUrl('supportExplain.html', {
  158. scope: $scope
  159. }).then(function(modal) {
  160. $scope.supportmodal = modal;
  161. });
  162. $scope.openSupportmodal = function() {
  163. $scope.supportmodal.show();
  164. };
  165. $scope.closeSupportmodal = function() {
  166. $scope.supportmodal.hide();
  167. };
  168. $scope.multi = {
  169. a: '',
  170. b: '',
  171. c: '',
  172. result: 1,
  173. promise:null
  174. };
  175. $scope.type = 1;//tab切换
  176. $scope.tosupport = false;
  177. $scope.support = function ($event) {
  178. $event.stopPropagation();
  179. $scope.tosupport = true;
  180. $scope.vm.coin = 0;
  181. $scope.index = 0;
  182. }
  183. $scope.cancelSupport = function () {
  184. $scope.tosupport = false;
  185. }
  186. //实时排行
  187. $scope.sort = {
  188. slide: ''
  189. };
  190. $scope.changeType = function (type) {
  191. $ionicScrollDelegate.scrollTop(true);
  192. $scope.type = type;
  193. };
  194. $scope.index = 0;
  195. $scope.$on('$ionicView.beforeEnter', function () {
  196. $scope.load(id);
  197. $ionicTabsDelegate.showBar(false);
  198. $scope.thisUser = storage.getObject('user');
  199. console.log($scope.user);
  200. });
  201. $scope.$on('$ionicView.leave', function () {
  202. $ionicTabsDelegate.showBar(true);
  203. if($scope.multi.promise)$interval.cancel($scope.multi.promise);
  204. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  205. });
  206. $scope.vm = {
  207. coin:"",
  208. title:""
  209. }
  210. $scope.timer = '';
  211. var leftTimer = function (countDown) {
  212. if (isNaN(countDown)) {
  213. $scope.timer = '结束';
  214. return;
  215. }
  216. var day=parseInt(countDown/(24*60*60));
  217. var h=parseInt(countDown/(60*60)%24);
  218. var m=parseInt(countDown/60%60);
  219. var s=parseInt(countDown%60);
  220. $scope.timer=(h<10?'0'+h:h)+'时'+(m<10?'0'+m:m)+'分'+(s<10?'0'+s:s)+'秒';
  221. if(day>0) $scope.timer = day+'天'+ $scope.timer;
  222. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  223. $scope.leftTimer = $interval(function () {
  224. if (countDown >= 1) leftTimer(countDown - 1);
  225. },1000);
  226. if(countDown<=0){
  227. $scope.timer='结束';
  228. }
  229. }
  230. $scope.changeIndex = function (index) {
  231. $scope.index = index;
  232. $scope.vm.coin = index*5;
  233. }
  234. $scope.vidEnded = function () {
  235. alert('播放完毕');
  236. }
  237. //实时计算支持乘数
  238. $scope.calcmultiplier = function () {
  239. $scope.multi.promise = $interval(function () {
  240. var date = new Date();
  241. var inter = date.getTime() - $scope.multi.b.getTime();
  242. var minutes = Math.floor(inter / (60 * 1000));
  243. var number = $scope.multi.a * minutes + $scope.multi.c;//js浮点运算会失真,根据muti.a的可能值范围,比如乘以100再除以100
  244. if (number<=1) {
  245. number = 1;
  246. }
  247. //$scope.multi.result = Math.round(number * 100) / 100;
  248. $scope.multi.result = Math.round(number * 100 / 100);
  249. },1000);
  250. // 点击支持取消 $interval.cancel($scope.multi.promise); 获取数据后重新开始执行
  251. }
  252. $scope.supportDream = function (data) {
  253. if(!$scope.vm.coin){
  254. msg.error("请选择梦想币数量!");
  255. return ;
  256. }
  257. var data = {
  258. id:id,
  259. coin:$scope.vm.coin
  260. };
  261. homeService.supportDream(data).then(function(result){
  262. $scope.tosupport = false;
  263. $scope.load(id);
  264. $interval.cancel($scope.multi.promise);
  265. })
  266. };
  267. $scope.collectionDream = function(is_collection){
  268. homeService.collectionDream(id,is_collection).then(function(result){
  269. $scope.dream.is_collection=result.data.data;
  270. })
  271. }
  272. $scope.add = function(is_collection){
  273. var data = {
  274. id:id,
  275. title:$scope.vm.title,
  276. pics:$scope.imgs,
  277. };
  278. homeService.add_interaction(data).then(function(){
  279. $scope.load(id);
  280. $scope.closeModal();
  281. })
  282. }
  283. $scope.showText = function(){
  284. $scope.aboutStyle = {
  285. "white-space" : "normal"
  286. };
  287. $scope.ionDownStyle = {
  288. "display" : "none"
  289. }
  290. };
  291. $scope.input = {
  292. placeholder: '评论',
  293. focus:false
  294. };
  295. $scope.replay = function ($event, name) {
  296. $event.stopPropagation();
  297. $scope.input.focus = true;
  298. $scope.input.placeholder = "回复" + name;
  299. $scope.vm.title='';
  300. }
  301. $scope.comment = function(){
  302. // $event.stopPropagation();
  303. //$scope.input.placeholder = "评论";
  304. $scope.input.focus = true;
  305. $scope.vm.title='';
  306. }
  307. $scope.submitComment = function(iid){
  308. if($scope.vm.comment&&$scope.vm.comment==''||!iid)return;
  309. var data = {
  310. id:iid,
  311. content:$scope.vm.comment,
  312. };
  313. console.log($scope.vm.comment+" "+iid)
  314. homeService.add_comment(data).then(function(){
  315. $scope.vm.comment = '' ;
  316. $scope.load(id);
  317. $scope.closeModal();
  318. })
  319. }
  320. $scope.addpict = function () {
  321. common.chooseImage().then(function (img) {
  322. common.uploadFiles(img,1).then(function (result) {
  323. var response = JSON.parse(result.response);
  324. var file = response.data.file;
  325. $scope.imgs.push(config.imgServer+file);
  326. console.log(JSON.stringify(config.imgServer+file));
  327. }, function (error) {
  328. msg.error('图片上传失败');
  329. });
  330. }, function (error) {
  331. console.log('图片选择失败');
  332. });
  333. };
  334. $ionicModal.fromTemplateUrl('recharge-modal.html', {
  335. scope: $scope,
  336. animation: 'slide-in-up'
  337. }).then(function(modal) {
  338. $scope.rechagemodal = modal;
  339. });
  340. $scope.openRechargeModal = function() {
  341. $scope.rechagemodal.show();
  342. $scope.vm.money = '';
  343. };
  344. $scope.closeRechargeModal = function() {
  345. $scope.rechagemodal.hide();
  346. };
  347. $ionicModal.fromTemplateUrl('interaction-modal.html', {
  348. scope: $scope,
  349. animation: 'slide-in-up'
  350. }).then(function(modal) {
  351. $scope.modal = modal;
  352. });
  353. $scope.openModal = function() {
  354. $scope.modal.show();
  355. $scope.vm.title = '';
  356. $scope.imgs = [];
  357. };
  358. $scope.closeModal = function() {
  359. $scope.modal.hide();
  360. };
  361. //当我们用到模型时,清除它!
  362. $scope.$on('$destroy', function() {
  363. $scope.modal.remove();
  364. });
  365. }]);
  366. app.controller('userDetailCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  367. , function ($scope,homeService,$stateParams, $state, msg) {
  368. var id = $stateParams.id;
  369. $scope.load = function (id) {
  370. msg.loading();
  371. homeService.dreamDetail(id).then(function (result) {
  372. msg.hide();
  373. $scope.user = result.data.data;
  374. }, function (error) {
  375. msg.hide();
  376. });
  377. };
  378. $scope.load(id);
  379. $scope.toDetail = function (id) {
  380. $state.go('app.home_dreamdetail',{id:id});
  381. };
  382. $scope.toOldDream = function(){
  383. $state.go("app.oldDream");
  384. }
  385. $scope.toSupportDream = function(){
  386. $state.go("app.supportDream");
  387. }
  388. }]);
  389. app.controller('oldDreamCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  390. , function ($scope,homeService,$stateParams, $state, msg) {
  391. }]);
  392. app.controller('supportDreamCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  393. , function ($scope,homeService,$stateParams, $state, msg) {
  394. }]);
  395. app.controller('searchCtrl', ["$scope","homeService", "$state", "msg", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  396. , function ($scope,homeService, $state, msg, $ionicTabsDelegate, $ionicNavBarDelegate) {
  397. $scope.showMore = function (keyword,index) {
  398. if(!keyword)return;
  399. $scope.index = index;
  400. // $scope.vm.keyword = keyword;
  401. homeService.search(keyword).then(function(result){
  402. $scope.isResult = true;
  403. $scope.dream_infos = result.data.data.dream_infos;
  404. $scope.user_infos = result.data.data.user_infos;
  405. });
  406. };
  407. $scope.search = function (keyword) {
  408. $scope.vm.keyword = keyword;
  409. homeService.search(keyword).then(function(result){
  410. $scope.isResult = true;
  411. $scope.dream_infos = result.data.data.dream_infos;
  412. $scope.user_infos = result.data.data.user_infos;
  413. });
  414. };
  415. $scope.toDetail = function (id) {
  416. $state.go('app.home_dreamdetail',{id:id});
  417. };
  418. $scope.toUser = function (id) {
  419. $state.go('app.home_dreamdetail',{id:id});
  420. };
  421. $scope.$on('$ionicView.beforeEnter', function () {
  422. $scope.isResult = false;
  423. $scope.index = 0;
  424. $scope.vm = {
  425. keyword : ""
  426. }
  427. $ionicTabsDelegate.showBar(false);
  428. // $ionicNavBarDelegate.showBackButton(false);
  429. homeService.search('').then(function(result){
  430. $scope.hot_searches = result.data.data.hot_searches;
  431. $scope.history_searches = result.data.data.history_searches;
  432. });
  433. });
  434. $scope.$on('$ionicView.leave', function () {
  435. $ionicTabsDelegate.showBar(true);
  436. });
  437. }]);
  438. app.controller('interactionAddCtrl', ["$scope","homeService", "$state", "msg","config","common", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  439. , function ($scope,homeService, $state, msg,config,common, $ionicTabsDelegate, $ionicNavBarDelegate) {
  440. $scope.$on('$ionicView.beforeEnter', function () {
  441. });
  442. }]);
  443. app.controller('homeDreamsCtrl', ["$scope", "$state", "myService", "msg"
  444. , function ($scope, $state, myService, msg) {
  445. $scope.$on('$ionicView.beforeEnter', function () {
  446. myService.myDream().then(function(result){
  447. console.log(result.data.data);
  448. $scope.dreams = result.data.data;
  449. });
  450. });
  451. $scope.toDetail = function (id) {
  452. $state.go('app.home_dreamdetail',{id:id});
  453. };
  454. }]);
  455. })(angular.module('app.controllers'));