home.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. //充值卡
  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. //优惠券
  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-appliances', function (event) {
  135. event.preventDefault();
  136. // var type = 4;
  137. var type = parseInt($(this).attr('data-product-type'));
  138. /*
  139. * 这个写法是SPA路由框架规定的。
  140. * $(selector).trigger(event, params)是jQuery的:触发被选元素上的指定事件
  141. */
  142. $doc.trigger('spa:navigate', {
  143. hash: 'placeOrder',
  144. pushData: {
  145. product: product.allList[type - 1],
  146. needReset: true
  147. }
  148. });
  149. }, $view);
  150. /* 点击标签
  151. $.newTouch('.btn-product-group', function (event) {
  152. event.preventDefault();
  153. if ($(this).hasClass('global-msg-active')) return;
  154. $(this).addClass('global-msg-active').siblings().removeClass('global-msg-active');
  155. var index = $(this).attr('data-group-index');
  156. switch (index){
  157. case '0':
  158. $('#list0').show().siblings().hide();
  159. break;
  160. case '1':
  161. $('#list1').show().siblings().hide();
  162. break;
  163. case '2':
  164. $('#list2').show().siblings().hide();
  165. break;
  166. case '3':
  167. $('#list3').show().siblings().hide();
  168. break;
  169. }
  170. }, $view); */
  171. // 双十一活动
  172. $.newTouch('.btn-home-day', function (event) {
  173. event.preventDefault();
  174. $doc.trigger('spa:navigate', {
  175. hash: 'activity-day',
  176. pushData: {
  177. needReset: true
  178. }
  179. });
  180. }, $view);
  181. // 民宿保洁
  182. $.newTouch('.btn-homestay', function (event) {
  183. event.preventDefault();
  184. $doc.trigger('spa:navigate', {
  185. hash: 'homestay',
  186. pushData: {
  187. needReset: true
  188. }
  189. });
  190. }, $view);
  191. // 企业服务
  192. $.newTouch('.btn-homeenterprise', function (event) {
  193. event.preventDefault();
  194. $doc.trigger('spa:navigate', {
  195. hash: 'homeenterprise',
  196. pushData: {
  197. needReset: true
  198. }
  199. });
  200. }, $view);
  201. // im
  202. $.newTouch('.btn-action-logo', function (event) {
  203. event.preventDefault();
  204. $doc.trigger('spa:navigate', {
  205. hash: 'im',
  206. pushData: {
  207. needReset: true
  208. }
  209. });
  210. }, $view);
  211. // 个人中心
  212. $.newTouch('.btn-action-coupon', function (event) {
  213. event.preventDefault();
  214. user.checkLogin(function () {
  215. $doc.trigger('spa:navigate', {
  216. hash: 'mine',
  217. pushData: {
  218. needReset: true
  219. }
  220. });
  221. });
  222. }, $view);
  223. /* 招聘
  224. $.newTouch('.btn-action-hire', function () {
  225. event.preventDefault();
  226. $doc.trigger('spa:navigate', {
  227. hash: 'hire'
  228. });
  229. }, $view); */
  230. /* 不知道什么意思。*/
  231. dtd.resolve();
  232. /* 跳转到填写订单页面
  233. function goProductPage(type) {
  234. 如果没登陆会跳转到登录。这个function参数是登陆成功的回调
  235. user.checkLogin(function () {
  236. 预期字段不足 先清除本地数据
  237. if (!user['productVisit'][13]) {
  238. localStorage.removeItem('user_productVisit');
  239. localStorage.removeItem('user_productVisit-time');
  240. user['productVisit'][13] = {
  241. visited: false
  242. }
  243. }
  244. 判断是否是第一次看这个类型。如果不是的话跳转到placeOrder页面
  245. if (user['productVisit'][type]['visited']) {
  246. } else {
  247. 如果是的话跳转到product页面
  248. $doc.trigger('spa:navigate', {
  249. hash: 'product',
  250. pushData: {
  251. type: type,
  252. needReload: true,
  253. fromHome: true
  254. }
  255. });
  256. 并把这个类别的visit属性设置为true
  257. user.productVisit[type].visited = true;
  258. user.setCache('productVisit');
  259. }
  260. // });
  261. } */
  262. /* 跳转到coming页面。
  263. function goComing(type) {
  264. user.checkLogin(function () {
  265. $doc.trigger('spa:navigate', {
  266. hash: 'coming',
  267. pushData: {
  268. productType: type
  269. }
  270. });
  271. });
  272. }*/
  273. /* 不知道这是干什么。离线显示一张图片?
  274. if (!user.activities['visited_offservice0126']) {
  275. // showActivity();
  276. }
  277. function showActivity() {
  278. var imgUrl = 'http://wozhua-others.qiniudn.com/20160126-offservice.png';
  279. var d = new Image();
  280. d.src = imgUrl;
  281. d.onload = function () {
  282. $doc.trigger('spa:openpanel', ['activity', {
  283. content: imgUrl
  284. }]);
  285. }
  286. } */
  287. },
  288. beforeopen: function (pageData, dtd) {
  289. localStorage.removeItem('cart_products');
  290. dtd.resolve();
  291. native.switchPopGesture(true);
  292. },
  293. afteropen: function (pageData, dtd) {
  294. var $view = this;
  295. var $doc = $(document);
  296. /* 如果需要重新加载,就请求服务列表。 */
  297. if (!pageData.pushData.dontNeedReload) {
  298. $doc.trigger('spa:openloader');
  299. /*
  300. * tmodjs:
  301. * 使用模版语法把数据填到html文件,生成一个js文件。
  302. * var tpl = template(html文件路径,数据)
  303. */
  304. var tpl = template('home/index', {
  305. showNavBar: !config.isWX && !config.isChubao
  306. });
  307. /*
  308. * jQuery:
  309. * 提供一个html元素到js变量的映射,和findViewById一个作用。
  310. * $(selector).???(xxx)
  311. * 其中selector是选择器,对应html的元素、类、id等
  312. * ???是jQuery定义的事件
  313. * xxx是参数,具体是什么取决于事件
  314. *
  315. * 这句的意思是,选取class=".spa-page-body"的元素,把tpl插入其中
  316. */
  317. $('.spa-page-body', $view).html(tpl);
  318. /* 建立IScroll */
  319. setTimeout(function () {
  320. pageData.scroll = new IScroll('#homeScroll');
  321. var swiper = new Swiper('.swiper-container', {
  322. /* paginationClickable: true,
  323. pagination: '.pagination',
  324. spaceBetween: 30,
  325. centeredSlides: true,
  326. autoplay: 1500,
  327. autoplayDisableOnInteraction: false*/
  328. pagination: '.pagination',
  329. loop: true,
  330. paginationClickable: true,
  331. centeredSlides: true,
  332. autoplay: 5000,
  333. autoplayDisableOnInteraction: false
  334. });
  335. // $('.swiper-container').height($('.swiper-container img').height());
  336. }, 20);
  337. // 目前的作用只是供后端判断,该用户是否为新用户,所以没有回调。
  338. product.getListAll(user.id);
  339. address.getList({
  340. request_from: 'weixin',
  341. user_id: user.id
  342. });
  343. $doc.trigger('spa:closeloader');
  344. }
  345. /* 又是这个。不知道什么玩意…… */
  346. dtd.resolve();
  347. //强制设置首页的上一页是空的,后面再调整框架
  348. pageData.prevPage = $('.spa-page-empty');
  349. },
  350. beforeclose: function (pageData, dtd) {
  351. dtd.resolve();
  352. native.switchPopGesture(false);
  353. }
  354. }
  355. })