index.js 14 KB

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