index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const App = getApp();
  2. import Toptips from '../../../components/toptips/toptips';
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. selectedId: -1,
  9. isAuthor: true,
  10. isLoading: true, // 是否正在加载中
  11. shopList: [] // 门店列表
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad(options) {
  17. let _this = this;
  18. // 记录已选中的id
  19. _this.setData({
  20. selectedId: options.selected_id
  21. });
  22. // 获取默认门店列表
  23. _this.getShopList();
  24. // 获取用户坐标
  25. _this.getLocation((res) => {
  26. _this.getShopList(res.longitude, res.latitude);
  27. });
  28. },
  29. /**
  30. * 获取门店列表
  31. */
  32. getShopList(longitude, latitude) {
  33. let _this = this;
  34. _this.setData({
  35. isLoading: true
  36. });
  37. App._get('shop/lists', {
  38. longitude: longitude || '',
  39. latitude: latitude || ''
  40. }, (result) => {
  41. _this.setData({
  42. shopList: result.data.list,
  43. isLoading: false
  44. });
  45. });
  46. },
  47. /**
  48. * 获取用户坐标
  49. */
  50. getLocation(callback) {
  51. let _this = this;
  52. wx.getLocation({
  53. type: 'wgs84',
  54. success(res) {
  55. // console.log(res);
  56. callback && callback(res);
  57. },
  58. fail() {
  59. Toptips({
  60. duration: 3000,
  61. content: '获取定位失败,请点击右下角按钮打开定位权限'
  62. });
  63. _this.setData({
  64. isAuthor: false
  65. });
  66. },
  67. })
  68. },
  69. /**
  70. * 授权启用定位权限
  71. */
  72. onAuthorize() {
  73. let _this = this;
  74. wx.openSetting({
  75. success(res) {
  76. if (res.authSetting["scope.userLocation"]) {
  77. console.log('授权成功');
  78. _this.setData({
  79. isAuthor: true
  80. });
  81. setTimeout(() => {
  82. // 获取用户坐标
  83. _this.getLocation((res) => {
  84. console.log('获取用户坐标');
  85. _this.getShopList(res.longitude, res.latitude);
  86. });
  87. }, 1000);
  88. }
  89. }
  90. })
  91. },
  92. /**
  93. * 选择门店
  94. */
  95. onSelectedShop(e) {
  96. let _this = this,
  97. selectedId = e.currentTarget.dataset.id;
  98. // 设置选中的id
  99. _this.setData({
  100. selectedId
  101. });
  102. // 设置上级页面的门店id
  103. let pages = getCurrentPages();
  104. if (pages.length < 2) {
  105. return false;
  106. }
  107. let prevPage = pages[pages.length - 2];
  108. prevPage.setData({
  109. selectedShopId: selectedId
  110. });
  111. // 返回上级页面
  112. wx.navigateBack({
  113. delta: 1
  114. });
  115. },
  116. })