{"id":30031,"date":"2024-11-25T16:05:59","date_gmt":"2024-11-25T08:05:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30031\/"},"modified":"2024-11-25T16:05:59","modified_gmt":"2024-11-25T08:05:59","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e4%b9%8b%e5%a4%a9%e6%b0%94%e9%a2%84%e6%8a%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30031\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u529f\u80fd\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e00\u3001\u9879\u76ee\u6587\u4ef6\u5217\u8868<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/001\/505\/a830472a7ec277321c82d1008a30e894-0.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5\u63d2\u56fe\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001 <\/h2>\n<p>\u4f7f\u7528app.json\u6587\u4ef6\u6765\u5bf9\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8fdb\u884c\u5168\u5c40\u914d\u7f6e\uff0c\u51b3\u5b9a\u9875\u9762\u6587\u4ef6\u7684\u8def\u5f84\u3001\u7a97\u53e3\u8868\u73b0\u3001\u8bbe\u7f6e\u7f51\u7edc\u8d85\u65f6\u65f6\u95f4\u3001\u8bbe\u7f6e\u591a tab \u7b49\u3002<\/p>\n<pre>{  \"pages\":[    \"pages\/index\/index\"\r\n  ],  \"window\":{    \"backgroundTextStyle\":\"light\",    \"navigationBarBackgroundColor\": \"#fff\",    \"navigationBarTitleText\": \"\u5929\u6c14\u9884\u62a5\",    \"navigationBarTextStyle\":\"black\"\r\n  },  \"networkTimeout\": {    \"request\": 10000\r\n  },  \"debug\": true}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7531\u4e8e\u9879\u76ee\u53ea\u6709\u4e00\u4e2a\u9875\u9762\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u5e95\u90e8tab\u3002\u53e6\u5916\u8bbe\u7f6e\u7f51\u7edc\u8bf7\u6c42\u65f6\u95f4\u4e3a10\u79d2\uff0c\u5e76\u4e14\u542f\u7528\u3002<\/p>\n<h2>\u4e09\u3001\u5c0f\u7a0b\u5e8f\u903b\u8f91\u5c42<\/h2>\n<p>\u9996\u5148\u5728common.js\u4e2d\u4f7f\u7528\u83b7\u53d6\u7528\u6237\u5f53\u524d\u5730\u7406\u4f4d\u7f6e\u83b7\u53d6\u7528\u6237\u7684\u5750\u6807\u5730\u5740\uff0c\u5750\u6807\u7c7b\u578b\u9009\u62e9gcj02\u3002<br \/>\/\/\u5750\u6807<\/p>\n<pre>function getLocation(callback) {\r\n    wx.getLocation({\r\n        type: 'gcj02',\r\n        success: function(res) {\r\n            callback(true, res.latitude, res.longitude);\r\n        },\r\n        fail: function() {\r\n            callback(false);\r\n        }\r\n    })\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Wx.getlocation\u8c03\u7528\u6210\u529f\u4e4b\u540e\uff0c\u5c06\u5750\u6807\u4fe1\u606f\u8fd4\u56de\u7ed9callback\u3002\u5931\u8d25\u65f6\u5c06false\u4f20\u7ed9callback\u51fd\u6570\u3002<br \/>\u83b7\u53d6\u5230\u5750\u6807\u4e4b\u540e\uff0c\u518d\u4f7f\u7528\u767e\u5ea6\u63a5\u53e3\u5929\u6c14\u3002\u76f8\u5e94\u7684\u67e5\u8be2\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>function getWeather(latitude, longitude, callback) {    var ak = \"\";\/\/\u6362\u6210\u81ea\u5df1\u7684ak,\u4e0d\u8981\u7528\u65b9\u500d\u5de5\u4f5c\u5ba4\u7684\r\n    var url = \"https:\/\/api.map.baidu.com\/telematics\/v3\/weather?location=\" + longitude + \",\" + latitude + \"&amp;output=json&amp;ak=\" + ak;\r\n    wx.request({\r\n        url: url,\r\n        success: function(res){\r\n            console.log(res);\r\n            callback(res.data);            \r\n        }\r\n    });\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5148\u5b9a\u4e49\u767e\u5ea6\u63a5\u53e3\u7684ak\uff0c\u518d\u901a\u8fc7\u62fc\u63a5\u53c2\u6570\u6784\u9020url\u7684\u5176\u4ed6\u90e8\u5206\u3002\u7136\u540e\u8c03\u7528 wx.request \u8bf7\u6c42\u5929\u6c14\u9884\u62a5\u6570\u636e\u3002<br \/>\u63a5\u4e0b\u6765\u628a\u4e0a\u8ff0\u63a5\u53e3\u7ec4\u5408\u8d77\u6765\uff0c\u7ec4\u6210\u7ed9\u5e94\u7528\u5c42\u7684\u63a5\u53e3\uff0c\u76f8\u5e94\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>function loadWeatherData(callback) {\r\n    getLocation(function(success, latitude, longitude){  \r\n        getWeather(latitude, longitude, function(weatherData){\r\n            callback(weatherData);   \r\n        });\r\n    });\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u540e\u901a\u8fc7 module.exports\u5bf9\u5916\u66b4\u9732\u8be5\u63a5\u53e3\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>module.exports = {\r\n    loadWeatherData: loadWeatherData\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u56db\u3001\u5c0f\u7a0b\u5e8f\u9875\u9762\u4e0e <\/h2>\n<p>\u5728\u9875\u9762\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528  \u5c06\u516c\u5171\u4ee3\u7801\u5f15\u5165\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>\/\/index.jsvar common = require('common.js')\r\n\r\nPage({\r\n    data: {\r\n        weather: {}\r\n    },\r\n    onLoad: function () {        var that = this;\r\n        common.loadWeatherData(function(data){\r\n            that.setData({\r\n                weather: data\r\n            });\r\n        });    \r\n    }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u9875\u9762\u7684Page\u51fd\u6570\u4e2d\uff0c data\u5b9a\u4e49\u4e3a\u5929\u6c14\u7684\u521d\u59cb\u5316\u6570\u636e\uff0c\u8be5\u6570\u636e\u5c06\u4f1a\u4ee5 JSON \u7684\u5f62\u5f0f\u7531\u903b\u8f91\u5c42\u4f20\u81f3\u6e32\u67d3\u5c42\u3002\u5728 onLoad \u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528common\u4e2d\u7684 loadWeatherData \u65b9\u6cd5\u83b7\u53d6\u5929\u6c14\u6570\u636e\u5e76\u8bbe\u7f6e\u5230 UI \u4e0a\uff0c\u5e76\u5c06\u53d6\u5230\u7684\u6570\u636e\u4f7f\u7528 setData \u65b9\u6cd5\u5c06\u5b83\u8bbe\u7f6e\u5230\u6570\u636e\u5c42\u4e2d\u3002<br \/>\u5728\u9875\u9762\u7684\u754c\u9762\u5b9e\u73b0\u4e2d\uff0c\u76f8\u5e94\u7684\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>&lt;!--index.wxml--&gt;&lt;view class=\"container\"&gt;  \r\n  &lt;view class=\"header\"&gt;\r\n      &lt;view class=\"title\"&gt;{{weather.results[0].currentCity}}&lt;\/view&gt;\r\n      &lt;view class=\"desc\"&gt;{{weather.date}}&lt;\/view&gt;\r\n  &lt;\/view&gt;\r\n\r\n  &lt;view class=\"menu-list\"&gt;\r\n      &lt;view class=\"menu-item\" wx:for=\"{{weather.results[0].weather_data}}\" wx:key=\"*this\"&gt;\r\n        &lt;view class=\"menu-item-main\"&gt;\r\n          &lt;text class=\"menu-item-name\"&gt;{{item.date}} {{item.weather}} {{item.wind}} {{item.temperature}}&lt;\/text&gt;\r\n          &lt;image class=\"menu-item-arrow\" src=\"{{item.dayPictureUrl}}\"&gt;&lt;\/image&gt;\r\n        &lt;\/view&gt;\r\n      &lt;\/view&gt;\r\n  &lt;\/view&gt;&lt;\/view&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u5916\u5c42\u662f\u4e00\u4e2a class \u4e3a container \u7684 View\uff0c\u5b83\u7684\u91cc\u9762\u653e\u4e862\u4e2a\u5b50 View\uff0c\u5206\u522b\u7528\u4e8e\u5b58\u653e\u9875\u9762\u5934\u548c\u9875\u9762\u5217\u8868\u3002\u9875\u9762\u5934\u4e2d\u5b58\u653e\u57ce\u5e02\u540d\u79f0\u548c\u65f6\u95f4\u3002\u9875\u9762\u5217\u8868\u7528\u4e8e\u5b58\u653e\u6700\u8fd1\u51e0\u5929\u7684\u5929\u6c14\u60c5\u51b5\u3002<br \/>\u9875\u9762\u7684\u6837\u5f0f\u8868\u5b9e\u73b0\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>.header {\r\n  padding: 30rpx;\r\n  line-height: 1;\r\n}.title {\r\n  font-size: 52rpx;\r\n}.desc {\r\n  margin-top: 10rpx;\r\n  color: #888888;\r\n  font-size: 28rpx;\r\n}.menu-list {\r\n  display: flex;\r\n  flex-direction: column;\r\n  background-color: #fbf9fe;\r\n  margin-top: 10rpx;\r\n}.menu-item {\r\n  color: #000000;\r\n  display: flex;\r\n  background-color: #fff;\r\n  margin: 10rpx 30rpx;\r\n  flex-direction: column;\r\n}.menu-item-main {\r\n  display: flex;\r\n  padding: 40rpx;\r\n  border-radius: 10rpx;\r\n  align-items: center;\r\n  font-size: 32rpx;\r\n  justify-content: space-between;\r\n}.menu-item-arrow {\r\n  width: 96rpx;\r\n  height: 96rpx;\r\n  transition: 400ms;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u9875\u9762\u6587\u4ef6\u548c\u6837\u5f0f\u8868\uff0c\u90fd\u662f\u4ece\u5fae\u4fe1\u5b98\u65b9Demo\u4e2d\u79fb\u690d\u800c\u6765\u3002<br \/>\u6700\u7ec8\u5b9e\u73b0\u7684\u5929\u6c14\u9884\u62a5\u5c0f\u7a0b\u5e8f\u6548\u679c\u5982\u56fe\u6240\u793a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/001\/505\/a830472a7ec277321c82d1008a30e894-1.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5\u63d2\u56fe1\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5\u63d2\u56fe1\" \/><\/p>\n<p>&nbsp;\u3010\u76f8\u5173\u63a8\u8350\u3011<\/p>\n<p>1.<\/p>\n<p>2.<\/p>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5929\u6c14\u9884\u62a5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u529f\u80fd\u3002 &nbsp; \u4e00\u3001\u9879\u76ee\u6587\u4ef6\u5217\u8868 &nbsp; \u4e8c\u3001 \u4f7f\u7528app.json\u6587\u4ef6\u6765\u5bf9\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8fdb\u884c\u5168\u5c40\u914d\u7f6e\uff0c\u51b3\u5b9a\u9875\u9762\u6587\u4ef6\u7684\u8def\u5f84\u3001\u7a97\u53e3\u8868\u73b0\u3001\u8bbe\u7f6e\u7f51\u7edc\u8d85\u65f6\u65f6\u95f4\u3001\u8bbe\u7f6e\u591a tab \u7b49\u3002 { &#8220;pages&#8221;:[ &#8220;pages\/index\/index&#8221; ], &#8220;window&#8221;:{ &#8220;backgroundTextStyle&#8221;:&#8221;light&#8221;, &#8220;navigationBarBackgroundColor&#8221;: &#8220;#fff&#8221;, &#8220;navigationBarTitleText&#8221;: &#8220;\u5929\u6c14\u9884\u62a5&#8221;, &#8220;navigationBarTextStyle&#8221;:&#8221;black&#8221; }, &#8220;networkTimeout&#8221;: { &#8220;request&#8221;: 10000 }, &#8220;debug&#8221;: true} \u767b\u5f55\u540e\u590d\u5236 \u7531\u4e8e\u9879\u76ee\u53ea\u6709\u4e00\u4e2a\u9875\u9762\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u5e95\u90e8tab\u3002\u53e6\u5916\u8bbe\u7f6e\u7f51\u7edc\u8bf7\u6c42\u65f6\u95f4\u4e3a10\u79d2\uff0c\u5e76\u4e14\u542f\u7528\u3002 \u4e09\u3001\u5c0f\u7a0b\u5e8f\u903b\u8f91\u5c42 \u9996\u5148\u5728common.js\u4e2d\u4f7f\u7528\u83b7\u53d6\u7528\u6237\u5f53\u524d\u5730\u7406\u4f4d\u7f6e\u83b7\u53d6\u7528\u6237\u7684\u5750\u6807\u5730\u5740\uff0c\u5750\u6807\u7c7b\u578b\u9009\u62e9gcj02\u3002\/\/\u5750\u6807 function getLocation(callback) { wx.getLocation({ type: &#8216;gcj02&#8217;, success: function(res) { callback(true, res.latitude, res.longitude); }, fail: function() { callback(false); } }) } \u767b\u5f55\u540e\u590d\u5236 Wx.getlocation\u8c03\u7528\u6210\u529f\u4e4b\u540e\uff0c\u5c06\u5750\u6807\u4fe1\u606f\u8fd4\u56de\u7ed9callback\u3002\u5931\u8d25\u65f6\u5c06false\u4f20\u7ed9callback\u51fd\u6570\u3002\u83b7\u53d6\u5230\u5750\u6807\u4e4b\u540e\uff0c\u518d\u4f7f\u7528\u767e\u5ea6\u63a5\u53e3\u5929\u6c14\u3002\u76f8\u5e94\u7684\u67e5\u8be2\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002 function getWeather(latitude, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-30031","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=30031"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30031\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}