index.js 14 KB

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