home.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $('.swiper-container').height($('.swiper-container img').height());
  45. $('.pagination').css('bottom', '0.15rem');
  46. }, 200);
  47. /*setTimeout(function () {
  48. $doc.trigger('spa:openpanel', ['simpleAlert', {
  49. message: '您已获得双十一大礼包',
  50. // title: '系统通知',
  51. // html: '您已获得双十一大礼包',
  52. buttons: [{text: '查看', class: 'btn-o2o-home btn-o2o-home-2'}, {text: '关闭', class: 'btn-o2o-home'}]
  53. }]);
  54. }, 1000);*/
  55. $.newTouch('.btn-o2o-home', function (event) {
  56. event.preventDefault();
  57. $doc.trigger('spa:navigate', {
  58. hash: 'myCoupon',
  59. pushData: {
  60. needReload: true
  61. }
  62. });
  63. console.log('测试')
  64. }, $view);
  65. //点击banner-1进入充值卡页
  66. $.newTouch('.btn-banner-recharge', function (event) {
  67. event.preventDefault();
  68. $doc.trigger('spa:navigate', {
  69. hash: 'recharge',
  70. pushData: {
  71. needReload: true
  72. }
  73. });
  74. }, $view);
  75. //点击banner-2进入优惠券
  76. $.newTouch('.btn-banner-coupon', function (event) {
  77. event.preventDefault();
  78. $doc.trigger('spa:navigate', {
  79. hash: 'myCoupon',
  80. pushData: {
  81. needReload: true
  82. }
  83. });
  84. }, $view);
  85. /* 点击产品列表的某一项 */
  86. $.newTouch('.btn-appliances', function (event) {
  87. event.preventDefault();
  88. // var type = 4;
  89. var type = parseInt($(this).attr('data-product-type'));
  90. /*
  91. * 这个写法是SPA路由框架规定的。
  92. * $(selector).trigger(event, params)是jQuery的:触发被选元素上的指定事件
  93. */
  94. $doc.trigger('spa:navigate', {
  95. hash: 'placeOrder',
  96. pushData: {
  97. product: product.allList[type - 1],
  98. needReset: true
  99. }
  100. });
  101. }, $view);
  102. /* 点击标签
  103. $.newTouch('.btn-product-group', function (event) {
  104. event.preventDefault();
  105. if ($(this).hasClass('global-msg-active')) return;
  106. $(this).addClass('global-msg-active').siblings().removeClass('global-msg-active');
  107. var index = $(this).attr('data-group-index');
  108. switch (index){
  109. case '0':
  110. $('#list0').show().siblings().hide();
  111. break;
  112. case '1':
  113. $('#list1').show().siblings().hide();
  114. break;
  115. case '2':
  116. $('#list2').show().siblings().hide();
  117. break;
  118. case '3':
  119. $('#list3').show().siblings().hide();
  120. break;
  121. }
  122. }, $view); */
  123. // 家庭保洁
  124. $.newTouch('.btn-secondary', function (event) {
  125. event.preventDefault();
  126. $doc.trigger('spa:navigate', {
  127. hash: 'secondary',
  128. pushData: {
  129. needReset: true
  130. }
  131. });
  132. }, $view);
  133. // 民宿保洁
  134. $.newTouch('.btn-homestay', function (event) {
  135. event.preventDefault();
  136. $doc.trigger('spa:navigate', {
  137. hash: 'homestay',
  138. pushData: {
  139. needReset: true
  140. }
  141. });
  142. }, $view);
  143. // 企业服务
  144. $.newTouch('.btn-homeenterprise', function (event) {
  145. event.preventDefault();
  146. $doc.trigger('spa:navigate', {
  147. hash: 'homeenterprise',
  148. pushData: {
  149. needReset: true
  150. }
  151. });
  152. }, $view);
  153. // 个人中心
  154. $.newTouch('.btn-action-coupon', function (event) {
  155. event.preventDefault();
  156. user.checkLogin(function () {
  157. $doc.trigger('spa:navigate', {
  158. hash: 'mine',
  159. pushData: {
  160. needReset: true
  161. }
  162. });
  163. });
  164. }, $view);
  165. /* 招聘
  166. $.newTouch('.btn-action-hire', function () {
  167. event.preventDefault();
  168. $doc.trigger('spa:navigate', {
  169. hash: 'hire'
  170. });
  171. }, $view); */
  172. /* 不知道什么意思。*/
  173. dtd.resolve();
  174. /* 跳转到填写订单页面
  175. function goProductPage(type) {
  176. 如果没登陆会跳转到登录。这个function参数是登陆成功的回调
  177. user.checkLogin(function () {
  178. 预期字段不足 先清除本地数据
  179. if (!user['productVisit'][13]) {
  180. localStorage.removeItem('user_productVisit');
  181. localStorage.removeItem('user_productVisit-time');
  182. user['productVisit'][13] = {
  183. visited: false
  184. }
  185. }
  186. 判断是否是第一次看这个类型。如果不是的话跳转到placeOrder页面
  187. if (user['productVisit'][type]['visited']) {
  188. } else {
  189. 如果是的话跳转到product页面
  190. $doc.trigger('spa:navigate', {
  191. hash: 'product',
  192. pushData: {
  193. type: type,
  194. needReload: true,
  195. fromHome: true
  196. }
  197. });
  198. 并把这个类别的visit属性设置为true
  199. user.productVisit[type].visited = true;
  200. user.setCache('productVisit');
  201. }
  202. // });
  203. } */
  204. /* 跳转到coming页面。
  205. function goComing(type) {
  206. user.checkLogin(function () {
  207. $doc.trigger('spa:navigate', {
  208. hash: 'coming',
  209. pushData: {
  210. productType: type
  211. }
  212. });
  213. });
  214. }*/
  215. /* 不知道这是干什么。离线显示一张图片?
  216. if (!user.activities['visited_offservice0126']) {
  217. // showActivity();
  218. }
  219. function showActivity() {
  220. var imgUrl = 'http://wozhua-others.qiniudn.com/20160126-offservice.png';
  221. var d = new Image();
  222. d.src = imgUrl;
  223. d.onload = function () {
  224. $doc.trigger('spa:openpanel', ['activity', {
  225. content: imgUrl
  226. }]);
  227. }
  228. } */
  229. },
  230. beforeopen: function (pageData, dtd) {
  231. localStorage.removeItem('cart_products');
  232. dtd.resolve();
  233. native.switchPopGesture(true);
  234. },
  235. afteropen: function (pageData, dtd) {
  236. var $view = this;
  237. var $doc = $(document);
  238. /* 如果需要重新加载,就请求服务列表。 */
  239. if (!pageData.pushData.dontNeedReload) {
  240. $doc.trigger('spa:openloader');
  241. /*
  242. * tmodjs:
  243. * 使用模版语法把数据填到html文件,生成一个js文件。
  244. * var tpl = template(html文件路径,数据)
  245. */
  246. var tpl = template('home/index', {
  247. showNavBar: !config.isWX && !config.isChubao
  248. });
  249. /*
  250. * jQuery:
  251. * 提供一个html元素到js变量的映射,和findViewById一个作用。
  252. * $(selector).???(xxx)
  253. * 其中selector是选择器,对应html的元素、类、id等
  254. * ???是jQuery定义的事件
  255. * xxx是参数,具体是什么取决于事件
  256. *
  257. * 这句的意思是,选取class=".spa-page-body"的元素,把tpl插入其中
  258. */
  259. $('.spa-page-body', $view).html(tpl);
  260. /* 建立IScroll */
  261. setTimeout(function () {
  262. pageData.scroll = new IScroll('#homeScroll');
  263. var swiper = new Swiper('.swiper-container', {
  264. /* paginationClickable: true,
  265. pagination: '.pagination',
  266. spaceBetween: 30,
  267. centeredSlides: true,
  268. autoplay: 1500,
  269. autoplayDisableOnInteraction: false*/
  270. pagination: '.pagination',
  271. loop: true,
  272. paginationClickable: true,
  273. centeredSlides: true,
  274. autoplay: 5000,
  275. autoplayDisableOnInteraction: false
  276. });
  277. // $('.swiper-container').height($('.swiper-container img').height());
  278. }, 20);
  279. // 目前的作用只是供后端判断,该用户是否为新用户,所以没有回调。
  280. product.getListAll(user.id);
  281. address.getList({
  282. request_from: 'weixin',
  283. user_id: user.id
  284. });
  285. $doc.trigger('spa:closeloader');
  286. }
  287. /* 又是这个。不知道什么玩意…… */
  288. dtd.resolve();
  289. //强制设置首页的上一页是空的,后面再调整框架
  290. pageData.prevPage = $('.spa-page-empty');
  291. },
  292. beforeclose: function (pageData, dtd) {
  293. dtd.resolve();
  294. native.switchPopGesture(false);
  295. }
  296. }
  297. })