home.js 16 KB

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