首页

  1. <template>
  2. <view class="container">
  3. <scroll-view scroll-y="true" class="container-scroll">
  4. <view class="container-box">
  5. <view class="list-item">省份:{{obj.liveData.province || ''}}</view>
  6. <view class="list-item">位置:{{obj.liveData.city || ""}}</view>
  7. <view class="list-item">湿度:{{obj.liveData.humidity || 0}}%</view>
  8. <view class="list-item">温度:{{obj.liveData.temperature || 0}} 摄氏度</view>
  9. <view class="list-item">天气:{{obj.liveData.weather || ""}}</view>
  10. <view class="list-item">风向:{{obj.liveData.winddirection || ""}}</view>
  11. <view class="list-item">风力:{{obj.liveData.windpower || 0}}级</view>
  12. </view>
  13. <view class="container-wrap">
  14. <button type="default" class="container-button" @click="fnGetWeather()">获取当前城市天气</button>
  15. <button type="default" class="container-button" @click="fnGetCityCode()">获取其它城市天气</button>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </template>
  20. <script>
  21. const amap = require('@/libs/amap-wx.js')
  22. // 需要去高德开发平台注册小程序密钥 https://console.amap.com/dev/key/app
  23. // 接口文档:https://lbs.amap.com/api/webservice/guide/api/weatherinfo
  24. export default {
  25. data () {
  26. return {
  27. amapPlugin: null,
  28. key: "0ce6bce83bfd0f7a023849c4648747f8",
  29. obj: {
  30. liveData: {}
  31. }
  32. }
  33. },
  34. onShow() {
  35. },
  36. onLoad () {
  37. //监听城市列表选择城市code
  38. uni.$on('sendCode', res => {
  39. // console.log(res,'res');
  40. let item = JSON.parse(res);
  41. //city 城市编码表
  42. //key web服务类型的key值
  43. uni.showLoading({
  44. title: '请稍候...'
  45. })
  46. getApp().globalData.http.get('https://restapi.amap.com/v3/weather/weatherInfo', {
  47. params: {
  48. parameters: 'base',
  49. key: '9248c2a26dc611f751cec20a5cd23e48',
  50. city: item.adcode
  51. },
  52. header: {}
  53. }).then(res=> {
  54. // console.log({res:res})
  55. // console.log(res.data.lives, '1');
  56. uni.hideLoading()
  57. this.obj.liveData = res.data.lives;
  58. this.$set(this.obj, 'liveData', res.data.lives[0]);
  59. })
  60. })
  61. //sdk实例化
  62. this.amapPlugin = new amap.AMapWX({
  63. key: this.key
  64. })
  65. },
  66. methods: {
  67. fnGetWeather () {
  68. uni.showLoading({
  69. title: '请稍候...'
  70. })
  71. this.amapPlugin.getWeather({
  72. success: (data) => {
  73. console.log({data: data})
  74. uni.hideLoading()
  75. this.obj.liveData = data.liveData;
  76. },
  77. fail: function(info) {
  78. console.log(info)
  79. }
  80. })
  81. },
  82. fnGetCityCode () {
  83. uni.navigateTo({
  84. url: '/pages/index/al-city'
  85. })
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. page{
  92. width: 100%;
  93. height: 100%;
  94. background-color: #FFFFFF;
  95. }
  96. .container {
  97. height: 100%;
  98. display: flex;
  99. flex-direction: column;
  100. .container-scroll {
  101. flex: 1;
  102. .container-box {
  103. padding: 30rpx;
  104. box-sizing: border-box;
  105. border-radius: 20rpx;
  106. .list-item {
  107. background: #fff;
  108. font-size: 28rpx;
  109. padding-left: 20rpx;
  110. padding-bottom: 20rpx;
  111. }
  112. }
  113. .container-wrap {
  114. width: 750rpx;
  115. font-size: 28rpx;
  116. .container-button {
  117. width: 620rpx;
  118. margin-top: 30rpx;
  119. }
  120. }
  121. }
  122. }
  123. </style>

城市列表页

  1. <template>
  2. <view class="container">
  3. <scroll-view scroll-y="true" class="container-scroll">
  4. <view class="container-wrap" v-if="cityList.length > 0">
  5. <view
  6. class="container-list"
  7. v-for="(item,index) in cityList"
  8. :key="index"
  9. @click="fnGetCode(item)"
  10. >
  11. {{ item['中文名']}}
  12. </view>
  13. </view>
  14. </scroll-view>
  15. </view>
  16. </template>
  17. <script>
  18. const city = require('@/common/js/city.json');
  19. export default {
  20. data () {
  21. return {
  22. cityList: []
  23. }
  24. },
  25. onLoad() {
  26. this.cityList = city ? city : [];
  27. // this.init();
  28. },
  29. methods: {
  30. fnGetCode (item) {
  31. uni.$emit('sendCode', JSON.stringify(item));
  32. // console.log(item,'item')
  33. uni.navigateBack();
  34. },
  35. init () {
  36. let list = [];
  37. if(city.length > 0) {
  38. city.map(item=>{
  39. list.push({
  40. city: item['中文名'],
  41. adcode: item.adcode
  42. })
  43. })
  44. this.cityList = list;
  45. }
  46. }
  47. }
  48. }
  49. </script>
  50. <style lang="scss" scoped>
  51. page{
  52. width: 100%;
  53. height: 100%;
  54. background-color: #FFFFFF;
  55. }
  56. .container {
  57. height: 100%;
  58. display: flex;
  59. flex-direction: column;
  60. .container-scroll {
  61. flex: 1;
  62. .container-wrap {
  63. width: 750rpx;
  64. padding: 30rpx;
  65. box-sizing: border-box;
  66. .container-list {
  67. width: 690rpx;
  68. height: 90rpx;
  69. line-height: 90rpx;
  70. background-color: #F4F4F4;
  71. margin-bottom: 40rpx;
  72. padding-left: 30rpx;
  73. }
  74. }
  75. }
  76. }
  77. </style>

城市编码表

https://www.yuque.com/along-n3gko/ezt5z9/vlogui