app.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // app.js
  2. const ald = require('./utils/ald-stat.js')
  3. import { post, get, login } from './utils/http'
  4. App({
  5. globalData: {
  6. navBarData: {
  7. navBarHeight: 0, // 导航栏高度
  8. menuBotton: 0, // 胶囊距底部间距(保持底部间距一致)
  9. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  10. menuHeight: 0 // 胶囊高度(自定义内容可与胶囊高度保证一致)
  11. },
  12. userInfo: null,
  13. couponPopup: false,
  14. coupon: null,
  15. systemInfo: {},
  16. sceneData: {},
  17. memberRules: '', // 会员守则
  18. storeData: {},
  19. showGoods:false, //点击商品或者按钮跳转到下单页面是否直接显示美妆产品
  20. toViewIndex:'', //记录首页跳转到项目页面的滚动下标
  21. },
  22. invite_id:"null",//邀请人id
  23. //记录登录状态相关的配置
  24. loginConfig: {
  25. code: '',
  26. codeTime: ''
  27. },
  28. address:{},
  29. onLaunch(options) {
  30. let WHITE_LIST=['pages/login/login','pages/home/home']
  31. if(!WHITE_LIST.includes(options.path)){
  32. if(wx.getStorageSync('userInfo')==''){
  33. console.log('登录');
  34. // wx.navigateTo({
  35. // url: '/pages/login/login',
  36. // })
  37. }
  38. }
  39. let that = this
  40. // this.getPermission(that); //获取当前定位
  41. that.setNavBarInfo()
  42. // 查询是否首次购买
  43. if (wx.getStorageSync('token')) {
  44. get('api/order/check_first', {}, (res) => {
  45. if (res.data.order_status == 1) {
  46. wx.setStorageSync('agree', true)
  47. }
  48. })
  49. }
  50. that.initData()
  51. // this.getPermission()
  52. },
  53. onShow(opts) {
  54. this.globalData.sceneData = {
  55. path: opts.path,
  56. query: opts.query
  57. }
  58. if (!this.globalData.userInfo) {
  59. this.initData()
  60. }
  61. if (opts.path == 'pages/deviceLogin/deviceLogin') {
  62. return
  63. }
  64. // if (!wx.getStorageSync('token') || !wx.getStorageSync('userInfo')) {
  65. // login(opts)
  66. // }
  67. },
  68. /**
  69. * 初始化数据
  70. */
  71. initData() {
  72. let that = this
  73. // 获取用户信息
  74. if (wx.getStorageSync('token')) {
  75. get('api/user', {}, (res) => {
  76. if (res.data) {
  77. that.globalData.userInfo = res.data
  78. wx.setStorageSync('userInfo', res.data)
  79. }
  80. })
  81. }
  82. return
  83. // 获取店铺信息
  84. let storeId = wx.getStorageSync('store_id')
  85. if (storeId) {
  86. get(
  87. 'api/store/info',
  88. {
  89. store_id: storeId
  90. },
  91. (res) => {
  92. this.globalData.storeData = res.data
  93. }
  94. )
  95. }
  96. },
  97. /**
  98. * @description 设置导航栏信息
  99. */
  100. setNavBarInfo() {
  101. // 获取系统信息
  102. const systemInfo = wx.getSystemInfoSync()
  103. this.globalData.systemInfo = systemInfo
  104. // 胶囊按钮位置信息
  105. const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
  106. this.systemInfo = systemInfo
  107. // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
  108. let navBarData = {
  109. navBarHeight:
  110. (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
  111. menuButtonInfo.height +
  112. systemInfo.statusBarHeight,
  113. menuBotton: menuButtonInfo.top - systemInfo.statusBarHeight,
  114. menuRight: systemInfo.screenWidth - menuButtonInfo.right,
  115. menuHeight: menuButtonInfo.height
  116. }
  117. this.globalData.navBarData = navBarData
  118. },
  119. /**
  120. * 获取当前定位
  121. * @param {*} obj
  122. */
  123. getPermission: function (obj) {
  124. let that = this
  125. // wx.getLocation({
  126. // success: function (res) {
  127. // that.getStore()
  128. // that.globalData.address = res
  129. // // console.log(that.address, 'that.addressthat.address', res);
  130. // },
  131. // fail: function () {
  132. // wx.getSetting({
  133. // success: function (res) {
  134. // var statu = res.authSetting;
  135. // if (!statu['scope.userLocation']) {
  136. // wx.showModal({
  137. // title: '是否授权当前位置',
  138. // content: '需要获取您的地理位置,请确认授权,否则无法定位最近店铺',
  139. // success: function (tip) {
  140. // if (tip.confirm) {
  141. // wx.openSetting({
  142. // success: function (data) {
  143. // if (data.authSetting["scope.userLocation"] === true) {
  144. // wx.showToast({
  145. // title: '授权成功',
  146. // icon: 'success',
  147. // duration: 1000
  148. // })
  149. // //授权成功之后,再调用chooseLocation选择地方
  150. // wx.getLocation({
  151. // success: function (res) {
  152. // that.getStore()
  153. // app.address = res
  154. // },
  155. // })
  156. // } else {
  157. // wx.showToast({
  158. // title: '授权失败',
  159. // icon: 'success',
  160. // duration: 1000
  161. // })
  162. // }
  163. // }
  164. // })
  165. // }
  166. // }
  167. // })
  168. // }
  169. // },
  170. // fail: function (res) {
  171. // wx.showToast({
  172. // title: '调用授权窗口失败',
  173. // icon: 'success',
  174. // duration: 1000
  175. // })
  176. // }
  177. // })
  178. // }
  179. // })
  180. },
  181. /**
  182. * 获取店铺接口
  183. * /api/store
  184. */
  185. getStore() {
  186. get(
  187. 'api/store', {
  188. page: 1,
  189. limit: 100,
  190. keyword: ""
  191. },
  192. (res) => {
  193. let lat1 = this.globalData.address.latitude,
  194. lng1 = this.globalData.address.longitude,
  195. storeList = res.data.list,
  196. distanceList = []
  197. //循环遍历计算获取当前定位到各门店的距离
  198. storeList.forEach(item => {
  199. let distanceInfo = {}
  200. distanceInfo["id"] = item.id
  201. distanceInfo["dist"] = this.getDistance(lat1, lng1, item.latitude, item.longitude)
  202. distanceList.push(distanceInfo)
  203. })
  204. //近到远排序
  205. let _distanceList = distanceList.sort(function (a, b) {
  206. return a.dist - b.dist
  207. })
  208. // //切换门店
  209. // this.getStoreInfo(String(_distanceList[0].id))
  210. wx.setStorageSync('store_id', String(_distanceList[0].id))
  211. }
  212. )
  213. },
  214. /**
  215. * 两点之间距离计算
  216. * @param {*} lat1
  217. * @param {*} lng1
  218. * @param {*} lat2
  219. * @param {*} lng2
  220. */
  221. getDistance(lat1, lng1, lat2, lng2) {
  222. lat1 = lat1 || 0;
  223. lng1 = lng1 || 0;
  224. lat2 = lat2 || 0;
  225. lng2 = lng2 || 0;
  226. var rad1 = lat1 * Math.PI / 180.0;
  227. var rad2 = lat2 * Math.PI / 180.0;
  228. var a = rad1 - rad2;
  229. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  230. var r = 6378137;
  231. var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
  232. return distance;
  233. },
  234. })