home.js 19 KB

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