index.js 13 KB

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