index.js 12 KB

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