index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../utils/user.js');
  4. const map = require('../../utils/map.js');
  5. const area = require('../../utils/area.js');
  6. //获取应用实例
  7. const app = getApp();
  8. Page({
  9. data: {
  10. newGoods: [],
  11. hotGoods: [],
  12. topics: [],
  13. brands: [],
  14. groupons: [],
  15. floorGoods: [],
  16. banner: [],
  17. channel: [],
  18. coupon: [],
  19. goodsCount: 0,
  20. hasLogin: true,
  21. city: '', //当前业务城市
  22. citys: [],
  23. cityIndex: 0,
  24. addressInfo: {
  25. city: '上海市',
  26. shopId: 0,
  27. lng: 121.4737,
  28. lat: 31.23037,
  29. address: "上海市浦东新区祖冲之路1505弄100号3栋",
  30. shopName: '',
  31. },
  32. showModalStatus: false,
  33. couponsData: [],
  34. randomNum: (Math.random() * 100000000).toFixed(0)
  35. },
  36. onShareAppMessage: function () {
  37. return {
  38. title: '保洁擦窗 家电清洗 专区收纳',
  39. desc: '鲸致生活,为美好生活',
  40. path: '/pages/index/index'
  41. }
  42. },
  43. onPullDownRefresh() {
  44. wx.showNavigationBarLoading() //在标题栏中显示加载
  45. this.getIndexData();
  46. wx.hideNavigationBarLoading() //完成停止加载
  47. wx.stopPullDownRefresh() //停止下拉刷新
  48. },
  49. getIndexData: function () {
  50. //新人登陆优惠券发放判断 !app.globalData.hasLogin ||,需要获取地址后再拿券
  51. if (wx.getStorageSync('userInfo').id == null) {
  52. console.log('未登陆进入' + app.globalData.hasLogin + "----" + wx.getStorageSync('userInfo').id);
  53. this.youhuiquan();
  54. }
  55. wx.showLoading({
  56. title: '加载中',
  57. });
  58. let that = this;
  59. console.info(that.data.addressInfo.shopId)
  60. util.request(api.IndexUrl, {
  61. city: that.data.addressInfo.city,
  62. //shopId: that.data.addressInfo.shopId
  63. }).then(function (res) {
  64. if (res.errno === 0) {
  65. res.data.hotGoodsList.forEach(h => {
  66. if (h.gallery != null) {
  67. h.gallery = h.gallery.split(",");
  68. }
  69. })
  70. that.setData({
  71. // newGoods: res.data.newGoodsList,
  72. hotGoods: res.data.hotGoodsList,
  73. // topics: res.data.topicList,
  74. //brands: res.data.brandList,
  75. //floorGoods: res.data.floorGoodsList,
  76. banner: res.data.banner,
  77. //groupons: res.data.grouponList,
  78. channel: res.data.channel,
  79. //coupon: res.data.couponList
  80. });
  81. //console.info(res.data.banner);
  82. }
  83. wx.hideLoading();
  84. });
  85. // util.request(api.GoodsCount).then(function (res) {
  86. // that.setData({
  87. // goodsCount: res.data
  88. // });
  89. // });
  90. },
  91. youhuiquan() {
  92. //进入小程序优惠券展示
  93. let that = this;
  94. util.request(api.CouponRegister, {
  95. city: that.data.addressInfo.city
  96. }).then(function (res) {
  97. //console.info(res.data);
  98. if (res.errno === 0 && res.data.length > 0 && wx.getStorageSync('userInfo').nickName == null) {
  99. that.setData({
  100. showModalStatus: true,
  101. couponsData: res.data
  102. })
  103. }
  104. })
  105. },
  106. onLoad: function (options) {
  107. let that = this;
  108. // 页面初始化 options为页面跳转所带来的参数
  109. if (options.rId) {
  110. // wx.setStorageSync('r_userid', options.rId);
  111. app.globalData.rId = options.rId;
  112. }
  113. if (options.ch) {
  114. wx.setStorageSync('channelId', options.ch);
  115. }
  116. if (options.goodsId) {
  117. console.info(options.goodsId)
  118. wx.navigateTo({
  119. url: '../goods/goods?id=' + options.goodsId
  120. });
  121. return;
  122. }
  123. var shareId;
  124. if (options.scene) {
  125. //这个scene的值存在则证明首页的开启来源于朋友圈分享的图,同时可以通过获取到的goodId的值跳转导航到对应的详情页
  126. var scene = decodeURIComponent(options.scene);
  127. console.log(scene);
  128. //console.info(scene); sId:分享id
  129. //goods,1181009;rId,10;sId:0
  130. //g,1181009;rId,10;sId:0 因为超长 缩写了goods
  131. //三种格式(1) rId,2 (2)g,3;rId,2 (3)g,3;rId,3;sId:10(阿姨开单分享)
  132. // 第四种 新增fid,2 或 fid,2;gid,3(标识加盟商的id) 商品id
  133. // 第五种 新增rId,2(修改加盟商的阿姨开单)
  134. //第六种 ch,2(渠道二维码 下单order_channel归属)
  135. //shopId=1031;
  136. if (scene.indexOf("rId") >= 0) { //阿姨开单
  137. let sceneStr = scene.split(',');
  138. wx.setStorageSync('rId', sceneStr[1]);
  139. } else if (scene.indexOf("ch") >= 0) { //下单渠道
  140. let sceneStr = scene.split(',');
  141. wx.setStorageSync('channelId', sceneStr[1]);
  142. } else if (scene.indexOf("fid") >= 0) { //加盟商id
  143. if (scene.indexOf("gid") < 0) { //没有商品id
  144. let sceneStr = scene.split(',');
  145. wx.setStorageSync('fraId', sceneStr[1]);
  146. } else {
  147. let fraId = scene.split(';')[0].split(",")[1];
  148. wx.setStorageSync('fraId', fraId);
  149. console.info(fraId);
  150. let goods = scene.split(';')[1].split(",");
  151. wx.navigateTo({
  152. url: '../goods/goods?id=' + goods[1]
  153. });
  154. }
  155. } else if (scene.indexOf("shopId") >= 0) {
  156. let sceneStr = scene.split('=');
  157. let shopId = sceneStr[1];
  158. console.log('scene:shopId=' + shopId);
  159. // 通过shopId获取地址信息
  160. app.globalData.shopId = shopId;
  161. util.request(api.OrderBookGetShop, {
  162. shopId: shopId
  163. }).then(res => {
  164. if (res.errno == 0) {
  165. console.log(res);
  166. var addressInfo = {
  167. address: res.data.address,
  168. lng: res.data.longitude,
  169. lat: res.data.latitude,
  170. shopId: res.data.shopId,
  171. city: res.data.city,
  172. shopName: res.data.shopName
  173. }
  174. wx.setStorageSync('addressInfo', addressInfo);
  175. console.log(addressInfo);
  176. //that.getLocation();
  177. }
  178. }).catch(res => {
  179. });
  180. } else {
  181. if (scene.indexOf("rId") >= 0 || scene.indexOf("r_userid") >= 0) { //兼容之前的r_userid
  182. let value = null;
  183. if (scene.indexOf(";") > 0) { //包含分号 表示是good和user两种
  184. value = scene.split(";")[1];
  185. //console.info(value);
  186. wx.setStorageSync('r_userid', value.split(",")[1]); //获取r_userid
  187. app.globalData.rId = value.split(",")[1];
  188. if (scene.indexOf("sId") >= 0 || scene.indexOf("shareId") >= 0) { //阿姨开单分享id ||兼容之前的shareId
  189. shareId = scene.split(";")[2].split(",")[1];
  190. }
  191. scene = scene.split(";")[0];
  192. //console.info(scene);
  193. }
  194. }
  195. let info_arr = [];
  196. info_arr = scene.split(',');
  197. let _type = info_arr[0];
  198. let id = info_arr[1];
  199. console.info(_type);
  200. if (_type == 'goods' || _type == 'g') {
  201. if (shareId) {
  202. wx.navigateTo({
  203. url: '../goods/goods?id=' + id + '&shareId=' + shareId
  204. });
  205. } else {
  206. let rId = wx.getStorageSync('r_userid');
  207. if (!rId) rId = 0;
  208. wx.navigateTo({
  209. url: '../goods/goods?id=' + id + '&rId=' + rId
  210. });
  211. }
  212. } else if (_type == 'groupon') {
  213. wx.navigateTo({
  214. url: '../goods/goods?grouponId=' + id
  215. });
  216. } else {
  217. wx.navigateTo({
  218. url: '../index/index'
  219. });
  220. }
  221. }
  222. }
  223. // 页面初始化 options为页面跳转所带来的参数
  224. if (options.grouponId) {
  225. //这个pageId的值存在则证明首页的开启来源于用户点击来首页,同时可以通过获取到的pageId的值跳转导航到对应的详情页
  226. wx.navigateTo({
  227. url: '../goods/goods?grouponId=' + options.grouponId
  228. });
  229. }
  230. // 页面初始化 options为页面跳转所带来的参数
  231. if (options.goodId) {
  232. //这个goodId的值存在则证明首页的开启来源于分享,同时可以通过获取到的goodId的值跳转导航到对应的详情页
  233. if (options.share_id)
  234. wx.navigateTo({
  235. url: '../goods/goods?id=' + options.goodId + '&share_id=' + options.share_id
  236. });
  237. else
  238. wx.navigateTo({
  239. url: '../goods/goods?id=' + options.goodId
  240. });
  241. }
  242. // 页面初始化 options为页面跳转所带来的参数
  243. if (options.orderId) {
  244. //这个orderId的值存在则证明首页的开启来源于订单模版通知,同时可以通过获取到的pageId的值跳转导航到对应的详情页
  245. wx.navigateTo({
  246. url: '../ucenter/orderDetail/orderDetail?id=' + options.orderId
  247. });
  248. }
  249. },
  250. getLocation() {
  251. let that = this
  252. //获取地理位置 缓存中是否有地址
  253. /**
  254. * 分2重判断
  255. * 1:缓存中是否有地址
  256. * 2:未登录 取当前地理位置判断
  257. */
  258. var addressInfo = wx.getStorageSync("addressInfo");
  259. if (addressInfo) {
  260. console.info(1);
  261. console.info(addressInfo.shopId == null);
  262. that.setData({
  263. addressInfo: addressInfo
  264. })
  265. this.getIndexData();
  266. } else {
  267. if (!app.globalData.hasLogin) { //未登陆
  268. map.getCity().then(res => {
  269. console.log("getcity啊啊啊啊啊啊啊啊啊啊" + res);
  270. that.setData({
  271. addressInfo: res
  272. })
  273. console.info(res)
  274. this.getIndexData();
  275. }).catch(res => { //reject内容 1:未授权 2:不在服务范围 3:异常解析
  276. console.log(res);
  277. wx.showModal({
  278. title: '提醒',
  279. showCancel: false,
  280. content: '您未授权或非业务城市,已切换成默认【上海市】'
  281. })
  282. // if (res == 1 || res == 2) {
  283. this.getdefaultCityShop();
  284. // }
  285. //路由到上海市 latitude: 31.23037 longitude: 121.4737
  286. });
  287. }
  288. }
  289. },
  290. getdefaultCityShop() {
  291. let that = this;
  292. util.request(api.getDefaultCityShop, {}, 'get').then(res => {
  293. if (res.errno === 0) {
  294. that.setData({
  295. addressInfo: res.data
  296. })
  297. wx.setStorageSync('addressInfo', res.data);
  298. that.getIndexData();
  299. } else {
  300. util.showErrorToast(res.errmsg);
  301. }
  302. })
  303. },
  304. goCheckAddress() {
  305. user.checkLogin().then(res => {
  306. wx.navigateTo({
  307. url: '/pages/ucenter/address/address',
  308. })
  309. }).catch(res => {
  310. wx.navigateTo({
  311. url: '/pages/auth/login/login',
  312. })
  313. })
  314. },
  315. onReady: function () {
  316. // 页面渲染完成
  317. },
  318. onShow: function () {
  319. //this.naviToMiniProgram();
  320. // if (!app.globalData.shopId) {
  321. // this.getLocation();
  322. // }
  323. let addressInfo = {
  324. city: '上海市',
  325. shopId: 0,
  326. lng: 121.4737,
  327. lat: 31.23037,
  328. address: "上海市浦东新区祖冲之路1505弄100号3栋",
  329. shopName: '',
  330. };
  331. wx.setStorageSync('addressInfo', addressInfo);
  332. this.getIndexData();
  333. },
  334. naviToMiniProgram() {
  335. let that = this;
  336. wx.showModal({
  337. title: '温馨提示',
  338. content: '尊敬的客户,鲸致生活小程序已迁移到【鲸致生活lifeplus】,您的相关权益也全部迁移保留,请点击确认 使用新小程序。',
  339. showCancel: false, //是否显示取消按钮
  340. cancelText: "取消", //默认是“取消”
  341. cancelColor: '#000000', //取消文字的颜色
  342. confirmText: "确认跳转", //默认是“确定”
  343. confirmColor: '#3cc51f', //确定文字的颜色
  344. success: function (res) {
  345. if (res.cancel) {
  346. //点击取消,wx.navigateBack
  347. } else {
  348. // 使用wx.navigateToMiniProgram跳转到小程序
  349. wx.navigateToMiniProgram({
  350. appId: 'wxf0a77f9993d8b60d',
  351. path: 'pages/index/index',
  352. envVersion: 'release', // 打开正式版
  353. success(res) {
  354. // 打开成功
  355. },
  356. fail: function (err) {
  357. console.log(err);
  358. that.naviToMiniProgram();
  359. }
  360. })
  361. }
  362. },
  363. fail: function (res) {
  364. //接口调用失败的回调函数,wx.navigateBack
  365. },
  366. complete: function (res) {
  367. console.info(res)
  368. //接口调用结束的回调函数(调用成功、失败都会执行)
  369. },
  370. })
  371. },
  372. goLogin: function (e) {
  373. if (e.detail.userInfo == null) {
  374. app.globalData.hasLogin = false;
  375. util.showErrorToast('微信授权失败');
  376. return;
  377. }
  378. app.globalData.hasLogin = false;
  379. wx.setStorageSync('userInfo', null);
  380. user.loginByWeixin(e.detail.userInfo).then(res => {
  381. //跳转注册页
  382. if (user.checkRegisted() == false) {
  383. wx.navigateTo({
  384. url: "/pages/auth/register/register"
  385. });
  386. } else {
  387. this.setData({
  388. hasLogin: true
  389. })
  390. }
  391. }).catch((err) => {
  392. app.globalData.hasLogin = false;
  393. });
  394. },
  395. onHide: function () {
  396. // 页面隐藏
  397. },
  398. onUnload: function () {
  399. // 页面关闭
  400. },
  401. getCoupon(e) {
  402. if (!app.globalData.hasLogin) {
  403. wx.navigateTo({
  404. url: "/pages/auth/login/login"
  405. });
  406. }
  407. let couponId = e.currentTarget.dataset.index
  408. util.request(api.CouponReceive, {
  409. couponId: couponId
  410. }, 'POST').then(res => {
  411. if (res.errno === 0) {
  412. wx.showToast({
  413. title: "领取成功"
  414. })
  415. } else {
  416. util.showErrorToast(res.errmsg);
  417. }
  418. })
  419. },
  420. goinviteFriends(e) {
  421. console.log(app.globalData.hasLogin);
  422. //跳转到邀请页面
  423. if (app.globalData.hasLogin) {
  424. wx.navigateTo({
  425. url: '/pages/activity/invitefriends/inviteFriends'
  426. })
  427. } else {
  428. wx.navigateTo({
  429. url: "/pages/auth/login/login"
  430. });
  431. };
  432. },
  433. guanbicoupon() {
  434. this.setData({
  435. showModalStatus: false
  436. })
  437. },
  438. gocoupon(e) {
  439. this.setData({
  440. showModalStatus: false
  441. })
  442. if (!app.globalData.hasLogin) {
  443. wx.navigateTo({
  444. url: "/pages/auth/login/login"
  445. });
  446. };
  447. },
  448. myshowtost: function (v) {
  449. wx.showToast({
  450. title: v,
  451. icon: 'none',
  452. duration: 3000
  453. })
  454. },
  455. goCatalog() {
  456. wx.navigateTo({
  457. url: '/pages/tabBar/catalog/catalog',
  458. })
  459. },
  460. goHotGoods() {
  461. wx.navigateTo({
  462. url: '/pages/goods/hotGoods/hotGoods',
  463. })
  464. },
  465. callPhone: function () {
  466. wx.makePhoneCall({
  467. phoneNumber: '4009218987',
  468. })
  469. },
  470. goJoin() {
  471. wx.navigateTo({
  472. url: "/pages/ucenter/wx/join"
  473. });
  474. // if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  475. // console.log(2222);
  476. // util.request(api.AuthInfo, {}, "GET")
  477. // .then(res => {
  478. // if (res.errno == 0) {
  479. // let mobile=res.data.mobile;
  480. // let randomBoolean = Math.random() < 0.5;
  481. // let cp=randomBoolean==true?"13127931250" : "13122656821";
  482. // wx.navigateTo({
  483. // url: '/pages/ucenter/wx/join?mobile='+mobile+'&cp='+cp
  484. // })
  485. // // url: '/pages/ucenter/wx/join?mobile='+mobile
  486. // // url: '/pages/ucenter/wx/join?mobile='+mobile+'&cp='+cp,
  487. // }
  488. // })
  489. // }else{
  490. // console.log(1111);
  491. // wx.navigateTo({
  492. // url: "/pages/ucenter/wx/join?cp=13122656821"
  493. // });
  494. // }
  495. },
  496. tbb(e) {
  497. let nm = e.target.dataset.name;
  498. if (nm.indexOf('花王') != -1) {
  499. wx.navigateToMiniProgram({
  500. appId: 'wxe185311df7c1c235',
  501. path: 'pages/index/index',
  502. extraData: {
  503. // 需要传递的数据
  504. },
  505. envVersion: 'develop', // 开发版、体验版和正式版,默认正式版
  506. success(res) {
  507. // 打开成功
  508. }
  509. })
  510. return;
  511. }
  512. wx.navigateTo({
  513. url: e.target.dataset.url,
  514. })
  515. }
  516. })