index.js 14 KB

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