home.js 9.5 KB

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