home.js 17 KB

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