home.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. define(['$', 'template', 'IScroll', 'product', 'user', 'native', 'config', 'address'], function ($, template, IScroll, product, user, native, config, address) {
  2. /*
  3. * 先定义一些模块内用到的function
  4. */
  5. function back(event) {
  6. event.preventDefault();
  7. native.back();
  8. }
  9. /*
  10. * 模块结构:5部分。
  11. * title:题目。
  12. * content:不知道是什么,似乎从没用过。
  13. * 以下可以当作生命周期回调,虽然并不清楚是什么时候回调的……
  14. * init:根据上一个页面传来的pageData初始化本页数据,生成页面,绑定事件。
  15. * beforeopen:接收上一个页面传来的数据、有需要的话重置数据
  16. * afteropen:做一些网络请求。
  17. * beforeclose:
  18. */
  19. return {
  20. title: '壹管家上门服务',
  21. body: '',
  22. init: function (pageData, dtd) {
  23. /* this是什么? */
  24. var $view = this;
  25. var $doc = $(document);
  26. /*提示信息*/
  27. /* 定义按钮事件。
  28. * 为什么不是$(selector).onclick(function)呢?
  29. */
  30. //返回
  31. $.newTouch('.back-button', function (event) {
  32. back.call(this, event);
  33. }, $view);
  34. $view.on('tapBackButton', function (event) {
  35. back.call(this, event);
  36. });
  37. //客服电话
  38. $.newTouch('.service-telephone', function (event) {
  39. event.preventDefault();
  40. native.call('4000328170');
  41. }, $view);
  42. //加载弹框
  43. // setTimeout(function () {
  44. // $doc.trigger('spa:openpanel', ['simpleAlert', {
  45. // title: '通知',
  46. // message: '2017年1月20日至2月11日放假调休,共23天。2017年2月12日正常服务给您造成的不便敬请谅解,感谢大家长期对我们工作的大力支持与信任。祝您节日愉快!'
  47. // }]);
  48. // return;
  49. // },500);
  50. /*自动关闭活动窗口*/
  51. $.newTouch('.btn-o2o-home', function (event) {
  52. event.preventDefault();
  53. $doc.trigger('spa:navigate', {
  54. hash: 'myCoupon',
  55. pushData: {
  56. needReload: true
  57. }
  58. });
  59. }, $view);
  60. //充值卡
  61. $.newTouch('.btn-banner-recharge', function (event) {
  62. event.preventDefault();
  63. $doc.trigger('spa:navigate', {
  64. hash: 'recharge',
  65. pushData: {
  66. needReload: true,
  67. backHash: ''
  68. }
  69. });
  70. }, $view);
  71. //优惠券
  72. $.newTouch('.btn-banner-coupon', function (event) {
  73. event.preventDefault();
  74. $doc.trigger('spa:navigate', {
  75. hash: 'myCoupon',
  76. pushData: {
  77. needReload: true
  78. }
  79. });
  80. },$view);
  81. /* 点击标签
  82. $.newTouch('.btn-product-group', function (event) {
  83. event.preventDefault();
  84. if ($(this).hasClass('global-msg-active')) return;
  85. $(this).addClass('global-msg-active').siblings().removeClass('global-msg-active');
  86. var index = $(this).attr('data-group-index');
  87. switch (index){
  88. case '0':
  89. $('#list0').show().siblings().hide();
  90. break;
  91. case '1':
  92. $('#list1').show().siblings().hide();
  93. break;
  94. case '2':
  95. $('#list2').show().siblings().hide();
  96. break;
  97. case '3':
  98. $('#list3').show().siblings().hide();
  99. break;
  100. }
  101. }, $view); */
  102. // 家庭保洁
  103. $.newTouch('.btn-secondary', function (event) {
  104. event.preventDefault();
  105. $doc.trigger('spa:navigate', {
  106. hash: 'secondary',
  107. pushData: {
  108. needReset: true,
  109. backHash: ''
  110. }
  111. });
  112. },$view);
  113. /* 点击产品列表的某一项 */
  114. $.newTouch('.btn-appliances', function (event) {
  115. event.preventDefault();
  116. // var type = 4;
  117. var type = parseInt($(this).attr('data-product-type'));
  118. /*
  119. * 这个写法是SPA路由框架规定的。
  120. * $(selector).trigger(event, params)是jQuery的:触发被选元素上的指定事件
  121. */
  122. $doc.trigger('spa:navigate', {
  123. hash: 'placeOrder',
  124. pushData: {
  125. product: product.allList[type - 1],
  126. needReset: true
  127. }
  128. });
  129. },$view );
  130. /* 点击标签
  131. $.newTouch('.btn-product-group', function (event) {
  132. event.preventDefault();
  133. if ($(this).hasClass('global-msg-active')) return;
  134. $(this).addClass('global-msg-active').siblings().removeClass('global-msg-active');
  135. var index = $(this).attr('data-group-index');
  136. switch (index){
  137. case '0':
  138. $('#list0').show().siblings().hide();
  139. break;
  140. case '1':
  141. $('#list1').show().siblings().hide();
  142. break;
  143. case '2':
  144. $('#list2').show().siblings().hide();
  145. break;
  146. case '3':
  147. $('#list3').show().siblings().hide();
  148. break;
  149. }
  150. }, $view); */
  151. // 双十一活动
  152. $.newTouch('.btn-home-day', function (event) {
  153. event.preventDefault();
  154. $doc.trigger('spa:navigate', {
  155. hash: 'activity-day',
  156. pushData: {
  157. needReset: true
  158. }
  159. });
  160. });
  161. //民宿保洁
  162. $.newTouch('.btn-homestay', function (event) {
  163. event.preventDefault();
  164. $doc.trigger('spa:navigate', {
  165. hash: 'homestay',
  166. pushData: {
  167. needReset: true
  168. }
  169. });
  170. }, $view);
  171. // 企业服务
  172. $.newTouch('.btn-homeenterprise', function (event) {
  173. event.preventDefault();
  174. $doc.trigger('spa:navigate', {
  175. hash: 'homeenterprise',
  176. pushData: {
  177. needReset: true
  178. }
  179. });
  180. }, $view);
  181. // im
  182. $.newTouch('.btn-action-logo', function (event) {
  183. event.preventDefault();
  184. window.location.href = '/index.php?r=o2o/advisory/im';
  185. }, $view);
  186. // 个人中心
  187. $.newTouch('.btn-action-coupon', function (event) {
  188. event.preventDefault();
  189. user.checkLogin(function () {
  190. $doc.trigger('spa:navigate', {
  191. hash: 'mine',
  192. pushData: {
  193. needReset: true
  194. }
  195. });
  196. });
  197. }, $view);
  198. /* 招聘
  199. $.newTouch('.btn-action-hire', function () {
  200. event.preventDefault();
  201. $doc.trigger('spa:navigate', {
  202. hash: 'hire'
  203. });
  204. }, $view); */
  205. /* 不知道什么意思。*/
  206. dtd.resolve();
  207. /* 跳转到填写订单页面
  208. function goProductPage(type) {
  209. 如果没登陆会跳转到登录。这个function参数是登陆成功的回调
  210. user.checkLogin(function () {
  211. 预期字段不足 先清除本地数据
  212. if (!user['productVisit'][13]) {
  213. localStorage.removeItem('user_productVisit');
  214. localStorage.removeItem('user_productVisit-time');
  215. user['productVisit'][13] = {
  216. visited: false
  217. }
  218. }
  219. 判断是否是第一次看这个类型。如果不是的话跳转到placeOrder页面
  220. if (user['productVisit'][type]['visited']) {
  221. } else {
  222. 如果是的话跳转到product页面
  223. $doc.trigger('spa:navigate', {
  224. hash: 'product',
  225. pushData: {
  226. type: type,
  227. needReload: true,
  228. fromHome: true
  229. }
  230. });
  231. 并把这个类别的visit属性设置为true
  232. user.productVisit[type].visited = true;
  233. user.setCache('productVisit');
  234. }
  235. // });
  236. } */
  237. /* 跳转到coming页面。
  238. function goComing(type) {
  239. user.checkLogin(function () {
  240. $doc.trigger('spa:navigate', {
  241. hash: 'coming',
  242. pushData: {
  243. productType: type
  244. }
  245. });
  246. });
  247. }*/
  248. /* 不知道这是干什么。离线显示一张图片?
  249. if (!user.activities['visited_offservice0126']) {
  250. // showActivity();
  251. }
  252. function showActivity() {
  253. var imgUrl = 'http://wozhua-others.qiniudn.com/20160126-offservice.png';
  254. var d = new Image();
  255. d.src = imgUrl;
  256. d.onload = function () {
  257. $doc.trigger('spa:openpanel', ['activity', {
  258. content: imgUrl
  259. }]);
  260. }
  261. } */
  262. },
  263. beforeopen: function (pageData, dtd) {
  264. localStorage.removeItem('cart_products');
  265. dtd.resolve();
  266. native.switchPopGesture(true);
  267. },
  268. afteropen: function (pageData, dtd) {
  269. var $view = this;
  270. var $doc = $(document);
  271. /* 如果需要重新加载,就请求服务列表。 */
  272. if (!pageData.pushData.dontNeedReload) {
  273. $doc.trigger('spa:openloader');
  274. /*
  275. * tmodjs:
  276. * 使用模版语法把数据填到html文件,生成一个js文件。
  277. * var tpl = template(html文件路径,数据)
  278. */
  279. var tpl = template('home/index', {
  280. showNavBar: !config.isWX && !config.isChubao
  281. });
  282. /*
  283. * jQuery:
  284. * 提供一个html元素到js变量的映射,和findViewById一个作用。
  285. * $(selector).???(xxx)
  286. * 其中selector是选择器,对应html的元素、类、id等
  287. * ???是jQuery定义的事件
  288. * xxx是参数,具体是什么取决于事件
  289. *
  290. * 这句的意思是,选取class=".spa-page-body"的元素,把tpl插入其中
  291. */
  292. $('.spa-page-body', $view).html(tpl);
  293. /* 建立IScroll */
  294. setTimeout(function () {
  295. pageData.scroll = new IScroll('#homeScroll');
  296. /*设置页面高度*/
  297. var homeBox = $('.home-box');
  298. var boxAlignCenter = $('.box-align-center').height();
  299. var preventScroll = $('.prevent-scroll').height();
  300. homeBox.height(boxAlignCenter * 4 + preventScroll);
  301. }, 750);
  302. setTimeout(function () {
  303. var swiper = new Swiper('.swiper-container', {
  304. pagination: '.pagination',
  305. loop: true,
  306. paginationClickable: true,
  307. centeredSlides: true,
  308. autoplay: 5000,
  309. autoplayDisableOnInteraction: false
  310. });
  311. }, 700);
  312. setInterval(function () {
  313. if($('.swiper-wrapper').height()<=30){
  314. $('.swiper-wrapper').height($('.swiper-slide img').height());
  315. }
  316. },1000);
  317. // 目前的作用只是供后端判断,该用户是否为新用户,所以没有回调。
  318. // product.getListAll(user.id);
  319. // address.getList({
  320. // request_from: 'weixin',
  321. // user_id: user.id
  322. // });
  323. $doc.trigger('spa:closeloader');
  324. }
  325. /* 又是这个。不知道什么玩意…… */
  326. dtd.resolve();
  327. //强制设置首页的上一页是空的,后面再调整框架
  328. pageData.prevPage = $('.spa-page-empty');
  329. },
  330. beforeclose: function (pageData, dtd) {
  331. dtd.resolve();
  332. native.switchPopGesture(false);
  333. }
  334. }
  335. })