// components/navbar/index.js const app = getApp(); const config = require('../../utils/config.js'); var util = require("../../utils/util.js") Component({ options: { addGlobalClass: true, }, /** * 组件的属性列表 */ properties: { pageName: String, showNav: { type: Boolean, value: true }, showHome: { type: Boolean, value: true }, backHome: { type: [Boolean, String], value: false }, none: { type: [Boolean, String], value: false } }, /** * 组件的初始数据 */ data: { branche_name: '上海' }, lifetimes: { attached: function() { this.setData({ navH: app.globalData.navHeight }) } }, ready: function() { const that = this let branche_code = wx.getStorageSync('branche_code'); let branche_name = wx.getStorageSync('branche_name'); if (!branche_code) { wx.getLocation({ type: 'wgs84', success: function(res) { let latitude = res.latitude let longitude = res.longitude wx.request({ url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`, success: res => { let branche_code = res.data.result.ad_info.city_code.substring(3, 6) + '000'; wx.setStorageSync( 'branche_name', res.data.result.ad_info.city.substring(0, 2) ) util.getData('/checkMkt', { branche_code: branche_code }).then(res => { that.setData({ branche_name: res[0].branche_name, branche_code: res[0].branche_code }); wx.setStorageSync( 'branche_code', res[0].branche_code ) if (that.data.branche_name != '上海' && that.data.branche_name != '北京' && that.data.branche_name != '苏州') { that.setData({ branche_name: '上海' }) } }) }, fail: (res) => { //获取城市失败 } }) }, fail: (res) => { // 获取位置失败 } }) } else { let cityname = wx.getStorageSync('branche_name') that.setData({ branche_name: cityname }) } }, /** * 组件的方法列表 */ methods: { //回退 navBack: function() { wx.navigateBack({ delta: 1 }) }, //回主页 toIndex: function() { wx.navigateTo({ url: '/pages/admin/home/index/index' }) }, tocity() { wx.navigateTo({ url: '/pages/city/city' }) } } })