app.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. shareUserID:'',//分享人ID
  22. isH5:false, //是否是h5商城进来的
  23. isSkinPlan:false, //测肤推荐方案标识
  24. isTestSkin:false,//肤质下单标识
  25. isCardBag:false, //卡包更多次卡跳转
  26. },
  27. invite_id:"null",//邀请人id
  28. //记录登录状态相关的配置
  29. loginConfig: {
  30. code: '',
  31. codeTime: ''
  32. },
  33. address:{},
  34. onLaunch(options) {
  35. // 肤质 测肤方案埋点
  36. let RECORD_WHITE = ['pages/projectDetail/projectDetail','pages/reserveCar/reserveCar','pages/orderBy/orderBy']
  37. wx.onAppRoute((route) => {
  38. if(this.globalData.isSkinPlan||this.globalData.isTestSkin){
  39. if(!RECORD_WHITE.includes(route.path)){
  40. this.globalData.isSkinPlan = false
  41. this.globalData.isTestSkin = false
  42. }
  43. }
  44. });
  45. let WHITE_LIST=['pages/login/login','pages/home/home']
  46. if(!WHITE_LIST.includes(options.path)){
  47. if(wx.getStorageSync('userInfo')==''){
  48. wx.navigateTo({
  49. url: '/pages/login/login',
  50. })
  51. }
  52. }
  53. let that = this
  54. this.getPermission(that); //获取当前定位
  55. that.setNavBarInfo()
  56. // 查询是否首次购买
  57. if (wx.getStorageSync('token')) {
  58. get('api/order/check_first', {}, (res) => {
  59. if (res.data.order_status == 1) {
  60. wx.setStorageSync('agree', true)
  61. }
  62. })
  63. }
  64. that.initData()
  65. // this.getPermission()
  66. },
  67. onShow(opts) {
  68. this.globalData.sceneData = {
  69. path: opts.path,
  70. query: opts.query
  71. }
  72. if (!this.globalData.userInfo) {
  73. this.initData()
  74. }
  75. if (opts.path == 'pages/deviceLogin/deviceLogin') {
  76. return
  77. }
  78. // if (!wx.getStorageSync('token') || !wx.getStorageSync('userInfo')) {
  79. // login(opts)
  80. // }
  81. },
  82. /**
  83. * 初始化数据
  84. */
  85. initData() {
  86. let that = this
  87. // 获取用户信息
  88. if (wx.getStorageSync('token')) {
  89. get('api/user', {}, (res) => {
  90. if (res.data) {
  91. that.globalData.userInfo = res.data
  92. wx.setStorageSync('userInfo', res.data)
  93. }
  94. })
  95. }
  96. return
  97. // 获取店铺信息
  98. let storeId = wx.getStorageSync('store_id')
  99. if (storeId) {
  100. get(
  101. 'api/store/info',
  102. {
  103. store_id: storeId
  104. },
  105. (res) => {
  106. this.globalData.storeData = res.data
  107. }
  108. )
  109. }
  110. },
  111. /**
  112. * @description 设置导航栏信息
  113. */
  114. setNavBarInfo() {
  115. // 获取系统信息
  116. const systemInfo = wx.getSystemInfoSync()
  117. this.globalData.systemInfo = systemInfo
  118. // 胶囊按钮位置信息
  119. const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
  120. this.systemInfo = systemInfo
  121. // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
  122. let navBarData = {
  123. navBarHeight:
  124. (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
  125. menuButtonInfo.height +
  126. systemInfo.statusBarHeight,
  127. menuBotton: menuButtonInfo.top - systemInfo.statusBarHeight,
  128. menuRight: systemInfo.screenWidth - menuButtonInfo.right,
  129. menuHeight: menuButtonInfo.height
  130. }
  131. this.globalData.navBarData = navBarData
  132. },
  133. /**
  134. * 获取当前定位
  135. * @param {*} obj
  136. */
  137. getPermission: function (obj) {
  138. let that = this
  139. wx.getLocation({
  140. success: function (res) {
  141. that.getStore()
  142. that.globalData.address = res
  143. // console.log(that.address, 'that.addressthat.address', res);
  144. },
  145. fail: function () {
  146. wx.getSetting({
  147. success: function (res) {
  148. var statu = res.authSetting;
  149. if (!statu['scope.userLocation']) {
  150. wx.showModal({
  151. title: '是否授权当前位置',
  152. content: '需要获取您的地理位置,请确认授权,否则无法定位最近店铺',
  153. success: function (tip) {
  154. if (tip.confirm) {
  155. wx.openSetting({
  156. success: function (data) {
  157. if (data.authSetting["scope.userLocation"] === true) {
  158. wx.showToast({
  159. title: '授权成功',
  160. icon: 'success',
  161. duration: 1000
  162. })
  163. //授权成功之后,再调用chooseLocation选择地方
  164. wx.getLocation({
  165. success: function (res) {
  166. that.getStore()
  167. app.address = res
  168. },
  169. })
  170. } else {
  171. wx.showToast({
  172. title: '授权失败',
  173. icon: 'success',
  174. duration: 1000
  175. })
  176. }
  177. }
  178. })
  179. }
  180. }
  181. })
  182. }
  183. },
  184. fail: function (res) {
  185. wx.showToast({
  186. title: '调用授权窗口失败',
  187. icon: 'success',
  188. duration: 1000
  189. })
  190. }
  191. })
  192. }
  193. })
  194. },
  195. /**
  196. * 获取店铺接口
  197. * /api/store
  198. */
  199. getStore() {
  200. get(
  201. 'api/store', {
  202. page: 1,
  203. limit: 100,
  204. keyword: ""
  205. },
  206. (res) => {
  207. let lat1 = this.globalData.address.latitude,
  208. lng1 = this.globalData.address.longitude,
  209. storeList = res.data.list,
  210. distanceList = []
  211. //循环遍历计算获取当前定位到各门店的距离
  212. storeList.forEach(item => {
  213. let distanceInfo = {}
  214. distanceInfo["id"] = item.id
  215. distanceInfo["dist"] = this.getDistance(lat1, lng1, item.latitude, item.longitude)
  216. distanceList.push(distanceInfo)
  217. })
  218. //近到远排序
  219. let _distanceList = distanceList.sort(function (a, b) {
  220. return a.dist - b.dist
  221. })
  222. // //切换门店
  223. // this.getStoreInfo(String(_distanceList[0].id))
  224. wx.setStorageSync('store_id', String(_distanceList[0].id))
  225. }
  226. )
  227. },
  228. /**
  229. * 两点之间距离计算
  230. * @param {*} lat1
  231. * @param {*} lng1
  232. * @param {*} lat2
  233. * @param {*} lng2
  234. */
  235. getDistance(lat1, lng1, lat2, lng2) {
  236. lat1 = lat1 || 0;
  237. lng1 = lng1 || 0;
  238. lat2 = lat2 || 0;
  239. lng2 = lng2 || 0;
  240. var rad1 = lat1 * Math.PI / 180.0;
  241. var rad2 = lat2 * Math.PI / 180.0;
  242. var a = rad1 - rad2;
  243. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  244. var r = 6378137;
  245. 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)));
  246. return distance;
  247. },
  248. })