<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 許天惠 on Medium]]></title>
        <description><![CDATA[Stories by 許天惠 on Medium]]></description>
        <link>https://medium.com/@grace550213?source=rss-542ea7c3535e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5Ytwc-pKRx1jXNdP</url>
            <title>Stories by 許天惠 on Medium</title>
            <link>https://medium.com/@grace550213?source=rss-542ea7c3535e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:03:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@grace550213/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[websocket 實作於 Vue.js]]></title>
            <link>https://medium.com/@grace550213/websocket-%E5%AF%A6%E4%BD%9C%E6%96%BC-vue-js-688dcf94a8be?source=rss-542ea7c3535e------2</link>
            <guid isPermaLink="false">https://medium.com/p/688dcf94a8be</guid>
            <dc:creator><![CDATA[許天惠]]></dc:creator>
            <pubDate>Sun, 06 Feb 2022 08:15:01 GMT</pubDate>
            <atom:updated>2022-02-06T08:18:20.134Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZWJXXd2QmNltOWbILH32mg.jpeg" /></figure><h3>首先，要先有 websocket 連線的底層</h3><p>建立一個檔案叫做socket.js</p><pre>utils/socket.js  // 或是直接放在最外層 (/socket.js)</pre><pre>import Vue from &quot;vue&quot;;<br>const wsUrl = process.env.VUE_APP_WEBSOCKET_API_URL;<br>const emitter = new Vue({<br>  data() {<br>    return {<br>      socket: null<br>    };<br>  },<br>  methods: {<br>    send(message) {<br>      if (1 === this.socket.readyState) {<br>        this.socket.send(JSON.stringify(message));<br>      }<br>    },<br>    close() {<br>      this.socket.close();<br>    },<br>    connect() {<br>      // 每連線一次，則建立一個新的 socket channel<br>      this.socket = new WebSocket(wsUrl); <br>      this.socket.onmessage = function(msg) {<br>        emitter.$emit(&#39;message&#39;, msg.data);<br>      };<br>      this.socket.onerror = function(err) {<br>        emitter.$emit(&#39;error&#39;, err);<br>        // 若錯誤重新連線(先斷線，然後偵測到斷線後再自動重先連線)<br>        emitter.close();<br>      };<br>      this.socket.onclose = function() {<br>        // 重連<br>        emitter.connect();<br>      };<br>      this.socket.onopen = function(msg) {<br>        emitter.$emit(&#39;open&#39;, msg);<br>      };<br>    }<br>  }<br>});</pre><pre>emitter.connect();<br>export default emitter;</pre><p>emitter這個東西是方便之後我們在vue裡面傳送資料給 server 的寫法。</p><p><strong>此處機制為，當 websocket 偵測到錯誤時，會直接去斷線原有的 socket，然後偵測到 close 後，會再重新進行 connect</strong></p><h3>在 vuex 中準備一個 SocketModule 去接 websocket 的資料</h3><p>新增一個檔案 store/modules/Socket/index.js，記得要在store/index.js裡面引用進去。</p><pre>store/modules/Socket/index.js</pre><pre>export default {<br>  namespaced: true,<br> state: {<br>  // 放入收到特定 websocket 的 handler 會用到的 state,<br> },<br> getters:{},<br> mutations:{<br>  // 放入收到特定 websocket 的 handler<br> },<br> actions:{<br>  // 放入收到特定 websocket 的 handler <br> },<br>};</pre><pre>store/index.js</pre><pre>import Vue from &quot;vue&quot;;<br>import Vuex from &quot;vuex&quot;;<br>import SocketModule from &#39;./modules/Socket/index&#39;;</pre><pre>Vue.use(Vuex);</pre><pre>export default new Vuex.Store({<br>  state: {},<br>  mutations: {},<br>  actions: {},<br>  modules: { SocketModule }<br>});</pre><h3>在 App.vue 中全局監聽 websocket，並且收到資料後馬上傳入 vuex</h3><pre>App.vue</pre><pre>&lt;template&gt;<br>  &lt;div id=&quot;app&quot;&gt;<br>    &lt;router-view /&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br>&lt;script&gt;<br>import Socket from &#39;./utils/socket&#39;;<br>import { mapMutations, mapActions } from &#39;vuex&#39;;<br>export default {<br>  name: &quot;App&quot;, <br>  data() {<br>    return {<br>      // 接受到的 socket message(依序處理的資料)<br>      socketData: [],<br>      // 正在處理 socketData 的 flag<br>      startHandleSocketData: false,<br>    };<br>  },<br>  created() {<br>    Socket.$on(&#39;message&#39;, this.handleGetMessage);<br>    Socket.$on(&#39;error&#39;, this.handleErrorMessage);<br>    Socket.$on(&#39;open&#39;, this.handleOpenMessage);<br>  },<br>  beforeDestroy() {<br>    Socket.$off(&#39;message&#39;, this.handleGetMessage);<br>    Socket.$off(&#39;error&#39;, this.handleErrorMessage);<br>    Socket.$off(&#39;open&#39;, this.handleOpenMessage);<br>    Socket.close();<br>  },<br>  watch: {<br>    // 放 socket 依序處理的資料<br>    socketData: function() {<br>      if (this.socketData.length === 0 || this.startHandleSocketData) {<br>        return;<br>      }<br>      this.startHandleSocketData = true;<br>      while (this.socketData.length &gt; 0) {<br>        this.handleSocketData(this.socketData[0]);<br>        this.socketData.splice(0, 1);<br>      }<br>      this.startHandleSocketData = false;<br>    },<br>  },<br>  methods: {<br>    ...mapActions(&#39;SocketModule&#39;, []),    <br>    // websoket 收到的 open message<br>    handleOpenMessage(msg) {<br>      if (msg.type === &#39;open&#39;) {<br>        Socket.send({<br>          ...要 init 的格式<br>        });<br>      }<br>    },<br>    // websoket 收到的 message<br>    handleGetMessage(msg) {<br>      // 若 msg 不是物件，就直接印出來就好<br>      if (msg.indexOf(&#39;:&#39;) === -1) {<br>        console.log(msg);<br>        return;<br>      }<br>      // 若為物件，才須要執行相對 function 去更新新的 data，並渲染頁面<br>      console.log(JSON.parse(msg));<br>      this.socketData.push(JSON.parse(msg));<br>    },<br>    // websoket 收到的 error<br>    handleErrorMessage(msg) {<br>      console.log(msg);<br>    },<br>   // 處理 socket message，不同 type 會有不同的動作<br>    handleSocketData(data) {<br>   // 判斷 message 中不同的 type，以分別使用 vuex SocketModule 中不同的 function 進行處理<br>    }<br>  }<br>};<br>&lt;/script&gt;</pre><p>此專案的 websocket 後端因有需要進行 init 的步驟，所以在收到 open 的 message 時會多傳一個 init 的訊息給後端。</p><p>目前機制為，不平行處理 websocket 收到的資料，為依序處理每筆資料。</p><h3>重頭戲：在頁面中使用</h3><p>如果是要傳送資料的話，要使用send方法：</p><pre>Home.vue</pre><pre>methods: {<br>  handleSendSocketMessage() {<br>    Socket.send(<br>      //...一些後端要求要傳的資料request，通常會是一包物件{}。<br>    );<br>  }<br>}</pre><p>若要顯示從 websocket 接收到的資料，則可以直接引用 vuex SocketModule紀錄的 state 來做顯示就可以了</p><p>參考資料:</p><blockquote><a href="https://penueling.com/%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98/vue-%E5%85%A8%E7%AB%99%E4%BD%BF%E7%94%A8-websocket-%E5%82%B3%E8%BC%B8%E8%B3%87%E6%96%99/">https://penueling.com/%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98/vue-%E5%85%A8%E7%AB%99%E4%BD%BF%E7%94%A8-websocket-%E5%82%B3%E8%BC%B8%E8%B3%87%E6%96%99/</a></blockquote><blockquote><a href="https://www.jianshu.com/p/9d8b2e42328c">https://www.jianshu.com/p/9d8b2e42328c</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=688dcf94a8be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[筆記] Vue 串接 google map api(2)]]></title>
            <link>https://medium.com/@grace550213/%E7%AD%86%E8%A8%98-vue-%E4%B8%B2%E6%8E%A5-google-map-api-2-f320160b9cba?source=rss-542ea7c3535e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f320160b9cba</guid>
            <dc:creator><![CDATA[許天惠]]></dc:creator>
            <pubDate>Thu, 03 Feb 2022 09:58:54 GMT</pubDate>
            <atom:updated>2022-02-03T09:58:54.626Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s3mZaLTXBJuzeb2ne1XgoQ.jpeg" /></figure><p><a href="https://medium.com/@grace550213/%E7%AD%86%E8%A8%98-vue-%E4%B8%B2%E6%8E%A5-google-map-api-1-ac11098dddde">繼上一篇將 google 地圖引入完畢</a>，若點擊 marker 的訊息視窗需要 &quot;在地圖上查看&quot;的按鈕，需要自行將 google map 的網址拼出。</p><p>其中需要該地點的 place_id，可另外寫一頁畫面，搜尋地圖上的位置，來產生對應的 place_id，並將其 place_id 紀錄於資料庫中。</p><pre><strong>// component 用到的 function</strong><br>// 依關鍵字搜尋地圖內容<br> async serchKeyword() {<br>   await this.googleFindPlaceFromText({<br>     input: this.searchText,<br>     inputtype: ‘textquery’,<br>     key: process.env.VUE_APP_GOOGLE_KEY,<br>     fields: ‘formatted_address,name,geometry,place_id,photos’<br>   });<br>   this.serchKeywordFinished = true;<br> }<br></pre><pre><strong>// .env.production</strong><br>VUE_APP_GOOGLE_PATH=<a href="https://maps.googleapis.com/">https://maps.googleapis.com</a><br></pre><pre><strong>// api 資料夾<br></strong>// google map place 相關的 api<br>const googleapisPlaceRequest = axios.create({<br>  baseURL: `${process.env.VUE_APP_GOOGLE_PATH}/maps/api/place/`<br>});<br>// 依關鍵字搜尋地圖 api<br>export const googleFindplacefromtext = ({ input, inputtype, key, fields }) =&gt; googleapisPlaceRequest.get(`/findplacefromtext/json?input=${input}&amp;inputtype=${inputtype}&amp;key=${key}&amp;fields=${fields}`);<br></pre><pre><strong>// module 中的 actions</strong><br>// google 依關鍵字搜尋地圖<br>async googleFindPlaceFromText(context, data) {<br>  // loading icon 顯示<br>  context.commit(&#39;SET_SHOW_LOADING&#39;, true, { root: true });<br>  let result = null;<br>  await googleFindplacefromtext(data)<br>    .then(async (response) =&gt; {<br>      if (process.env.NODE_ENV === &#39;development&#39;) {<br>        console.log(response);<br>      }<br>      result = &#39;success&#39;;<br>      // 將每筆資料的經緯度拿出來<br>      const newData = response.data.candidates.map((item) =&gt; {<br>        return { ...item, lat: item.geometry.location.lat, lng: item.geometry.location.lng };<br>      });<br>      // 將資料存起來<br>      context.commit(&#39;SET_GOOGLE_FIND_PLACE_RESULT&#39;, newData);<br>      // loading icon 消失<br>      context.commit(&#39;SET_SHOW_LOADING&#39;, false, { root: true });<br>    })<br>    .catch((error) =&gt; {<br>      result = &#39;failed&#39;;<br>      // loading icon 消失<br>      context.commit(&#39;SET_SHOW_LOADING&#39;, false, { root: true });<br>      // 錯誤提示框出現<br>      context.commit(&#39;SET_SHOW_DIALOG&#39;, true, { root: true });<br>      // 設定提示框的字<br>      let errorText = error.response.data.error_code === &#39;131073&#39; ? &#39;網站待機過久，請重新登入，謝謝。&#39; : &#39;資料拿取時發生錯誤，請確認網路連線或嘗試重新整理網頁，謝謝。&#39;;<br>      context.commit(&#39;SET_DIALOG_TEXT&#39;, [&#39;喔不...&#39;, errorText], { root: true });<br>      console.error(error);<br>        });<br>   return result;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f320160b9cba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[筆記] Vue 串接 google map api(1)]]></title>
            <link>https://medium.com/@grace550213/%E7%AD%86%E8%A8%98-vue-%E4%B8%B2%E6%8E%A5-google-map-api-1-ac11098dddde?source=rss-542ea7c3535e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac11098dddde</guid>
            <dc:creator><![CDATA[許天惠]]></dc:creator>
            <pubDate>Thu, 03 Feb 2022 09:41:11 GMT</pubDate>
            <atom:updated>2022-02-06T08:17:23.478Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kZLmFz4bsTiBWSJ8lghHwA.jpeg" /></figure><h3>首先，建立 google 專案</h3><ol><li>進入 <a href="https://cloud.google.com/maps-platform/">Google Map Platform</a> 點選「Get started」開始建立新專案</li><li>選擇&quot;Maps JavaScript API&quot;服務</li><li>取得專案 API Key：在 index.html 載入</li></ol><pre>&lt;script src=”https://maps.googleapis.com/maps/api/js?key=&lt;%= process.env.VUE_APP_GOOGLE_KEY %&gt;&amp;v=weekly” async &gt;&lt;/script&gt;</pre><h3>建立一個 google map 的 component，之後有需要引入地圖的地方都可以使用</h3><pre>&lt;template&gt;<br>  &lt;div id=”map”&gt;&lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import { mapState } from ‘vuex’;<br>export default {<br> name: ‘GoogleMap’,<br> components: {},<br> props: {<br>   // 預設經緯度<br><strong>   lat: Number,<br>   lng: Number,</strong><br>   // 一進入畫面縮放大小<br>   <strong>zoom: Number,<br></strong>   data: Array<br> },<br> data() {<br>   return {<br>      map: null,<br>     // 存放目前開啟的訊息視窗<br>     infowindow: null,<br>     // 存放已建立的地標<br>     markers: [],<br>     // 頁面使用的類型<br>     type: null<br>  };<br> },<br> mounted() {<br>   // 紀錄頁面的類型<br>   this.type = this.$route.params.type;<br>   // 初始化地圖<br><strong>   this.initMap();</strong><br>   if (this.data.length &gt; 0) {<br>     this.data.forEach(item =&gt; {<br>       // 建立地標<br>       <strong>this.setMarker(item)</strong>;<br>     });<br>   }<br> },<br> watch: {<br>   data: function() {<br>     // 清除所有地標<br>     this.deleteMarkers();<br>     this.data.forEach(item =&gt; {<br>       // 建立地標<br>       this.setMarker(item);<br>    });<br>   }<br> },<br> methods: {<br>   <strong>// 建立地圖<br>   initMap() {<br>     // 透過 Map 物件建構子建立新地圖 map 物件實例，並將地圖呈現在 id 為 map 的元素中<br>     this.map = new google.maps.Map(document.getElementById(‘map’),{<br>       // 設定地圖的中心點經緯度位置<br>       center: { lat: this.lat, lng: this.lng },<br>       // 設定地圖縮放比例 0–20<br>       zoom: this.zoom,<br>       // 限制使用者能縮放地圖的最大比例<br>       maxZoom: 20,<br>       // 限制使用者能縮放地圖的最小比例<br>       minZoom: 3,<br>       // 設定是否呈現右下角街景小人<br>       streetViewControl: false,<br>       // 設定是否讓使用者可以切換地圖樣式：一般、衛星圖等<br>       mapTypeControl: false<br>     });<br>   },</strong><br>   <strong>// 建立地標<br>   setMarker(data) {<br>     // 建立一個新地標<br>     const marker = new google.maps.Marker({<br>       // 設定地標的座標<br>       position: { lat: data.lat, lng: data.lng },<br>       // 設定地標要放在哪一個地圖<br>       map: this.map<br>     });<br>     // 將新建立的地標存起來<br>     this.markers.push(marker);<br>     // 透過 InfoWindow 物件建構子建立新訊息視窗<br>     const infowindow = this.createInfowindow(data);<br>     // 在地標上監聽點擊事件<br>     marker.addListener(‘click’, () =&gt; {<br>       // 如果目前有開啟中的訊息視窗，先將其關閉<br>       if (this.infowindow) this.infowindow.close();<br>       // 顯示被點擊地標的訊息視窗<br>       infowindow.open(this.map, marker);<br>       // 存入目前開啟的訊息視窗<br>       this.infowindow = infowindow;<br>     });<br>   },</strong><br>   // 重設地圖中心點<br>   resetCenter(data) {<br>     // set center<br>     this.map.panTo({ lat: data.lat, lng: data.lng });<br>     this.map.setZoom(15);<br>     const myMarker = this.markers.find(item =&gt; {<br>       return item.position.lat() === data.lat &amp;&amp; item.position.lng() === data.lng;<br>     });<br>     // 如果目前有開啟中的訊息視窗，先將其關閉<br>     if (this.infowindow) this.infowindow.close();<br>     // 透過 InfoWindow 物件建構子建立新訊息視窗<br>     const infowindow = this.createInfowindow(data);<br>     // 顯示被點擊地標的訊息視窗<br>     infowindow.open(this.map, myMarker);<br>     // 存入目前開啟的訊息視窗<br>     this.infowindow = infowindow;<br>   },<br>   // 透過 InfoWindow 物件建構子建立新訊息視窗<br>   createInfowindow(data) {<br>     const self = this;<br>     // 透過 InfoWindow 物件建構子建立新訊息視窗<br>     const result = new google.maps.InfoWindow({<br>       // 設定想要顯示的內容<br>       content: this.getInfowindowOfType(data, self),<br>       // 設定訊息視窗最大寬度<br>       minWidth: 150<br>     });<br>     return result;<br>   },<br>   // 清除所有地標<br>   deleteMarkers() {<br>     this.markers.forEach(marker =&gt; marker.setMap(null));<br>     this.markers = [];<br>   },<br>  // 拿取不同頁面類型要顯示的 Infowindow 畫面<br>  getInfowindowOfType(data, self) {<br>   ...<br>   根據不同的 type，return 不同的 Infowindow 畫面 html<br>   ...<br>  }<br> }<br>};<br>&lt;/script&gt;<br>&lt;style scoped lang=”scss”&gt;<br>#map {<br> height: 100%;<br>}<br>&lt;/style&gt;</pre><pre>Map Options 建立地圖時的設定<br>1. <a href="https://developers.google.com/maps/documentation/javascript/tutorial#zoom-levels">center</a>: 設定地圖的中心點經緯度位置 <br>2. <a href="https://developers.google.com/maps/documentation/javascript/tutorial#zoom-levels">zoom</a>: 設定地圖縮放比例--0 能看到個地球的比例、20 能看到大樓的比例<br>3. <a href="https://developers.google.com/maps/documentation/javascript/controls#ControlModification">streetViewControl</a>: 設定是否呈現右下角街景小人，讓使用者能使用街景功能<br>4. <a href="https://developers.google.com/maps/documentation/javascript/controls#ControlOptions">mapTypeControl</a>: 設定是否讓使用者可以切換地圖<a href="https://developers.google.com/maps/documentation/javascript/controls#ControlsOverview">樣式</a>：公路圖、衛星圖等<br>5. mapTypeControlOptions: 針對「切換地圖樣式」能有更多客製化設定，如樣式等<br>6. fullscreenControl: 設定是否讓使用者可以點擊開啟全螢幕地圖功能<br>7. <a href="https://developers.google.com/maps/documentation/javascript/controls#Adding_Controls_to_the_Map">zoomControl</a>: 設定顯示縮放大小的控制鈕（地圖尺寸小於200x200px時預設消失）<br>8. ZoomControlOptions: 針對縮放大小按鈕有更多設定，如更改在地圖上的擺放位置<br></pre><pre>Marker 地標特效與客製化<br>1. <a href="https://developers.google.com/maps/documentation/javascript/markers#animate">animation</a> 屬性: 設定地標特效--DROP 為從地圖上方墜落 / BOUNCE 會持續彈跳<br>2. <a href="https://developers.google.com/maps/documentation/javascript/markers#simple_icons">icon</a> 屬性: 設定使用客製化的地標圖示<br>3. <a href="https://developers.google.com/maps/documentation/javascript/markers#draggable">draggable</a> 屬性: 設定地標可以被使用者拖拉至他處</pre><h3>於其他 page 或 component中使用</h3><pre>&lt;GoogleMap ref=&quot;GoogleMap&quot; :lat=&quot;lat&quot; :lng=&quot;lng&quot; :zoom=&quot;8&quot; :data=&quot;handleGymMapData&quot; /&gt;</pre><pre>import GoogleMap from &#39;../../components/Map/GoogleMap.vue&#39;;</pre><pre>components: { GoogleMap },</pre><p>備註: 拼出 google map 連結</p><pre>&lt;!--地圖上查看按鈕--&gt;<br>&lt;a target=”_blank” jstcache=”6&quot; href=”https://www.google.com/maps/search/?api=1&amp;query=${data.lat},${data.lng}${data.place_id ? ‘&amp;query_place_id=’ + data.place_id : ‘’}” tabindex=”0&quot;&gt; <br>   &lt;span&gt;在 Google 地圖上查看&lt;/span&gt; <br>&lt;/a&gt;</pre><pre>// google 的 place_id 可用搜尋地圖位置 api 找到對應的 place_id<br>   (將於<a href="https://medium.com/@grace550213/%E7%AD%86%E8%A8%98-vue-%E4%B8%B2%E6%8E%A5-google-map-api-2-f320160b9cba">&quot;[筆記] Vue 串接 google map api(2)&quot;</a>提到)</pre><p>參考資料:</p><blockquote><a href="https://blog.happycoding.today/note-vue-js-googlemap-api/">https://blog.happycoding.today/note-vue-js-googlemap-api/</a></blockquote><p><a href="https://medium.com/@peicheng_88746/%E9%80%8F%E9%81%8E%E7%B6%B2%E5%9D%80%E9%96%8B%E5%95%9F%E5%9C%B0%E5%9C%96-app-%E4%B8%A6%E9%A1%AF%E7%A4%BA%E7%89%B9%E5%AE%9A%E5%9C%B0%E9%BB%9E%E8%B3%87%E8%A8%8A-6cc73ee2a871">透過網址開啟地圖 APP 並顯示特定地點資訊</a></p><blockquote><a href="https://medium.com/%E9%BA%A5%E5%85%8B%E7%9A%84%E5%8D%8A%E8%B7%AF%E5%87%BA%E5%AE%B6%E7%AD%86%E8%A8%98/%E7%AD%86%E8%A8%98-%E5%BE%9E%E9%9B%B6%E6%8E%A5%E8%A7%B8-google-map-api-2-%E5%9C%A8-vue-js-%E4%B8%AD%E4%BD%BF%E7%94%A8-geocoding-%E5%B0%87%E5%9C%B0%E5%9D%80%E8%BD%89%E6%8F%9B%E6%88%90%E5%9C%B0%E6%A8%99-6bd839d2ec68">https://medium.com/%E9%BA%A5%E5%85%8B%E7%9A%84%E5%8D%8A%E8%B7%AF%E5%87%BA%E5%AE%B6%E7%AD%86%E8%A8%98/%E7%AD%86%E8%A8%98-%E5%BE%9E%E9%9B%B6%E6%8E%A5%E8%A7%B8-google-map-api-2-%E5%9C%A8-vue-js-%E4%B8%AD%E4%BD%BF%E7%94%A8-geocoding-%E5%B0%87%E5%9C%B0%E5%9D%80%E8%BD%89%E6%8F%9B%E6%88%90%E5%9C%B0%E6%A8%99-6bd839d2ec68</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac11098dddde" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[筆記]Vue 中使用 QR Code Scanner]]></title>
            <link>https://medium.com/@grace550213/%E7%AD%86%E8%A8%98-vue-%E4%B8%AD%E4%BD%BF%E7%94%A8-qr-code-scanner-af378a180c8a?source=rss-542ea7c3535e------2</link>
            <guid isPermaLink="false">https://medium.com/p/af378a180c8a</guid>
            <dc:creator><![CDATA[許天惠]]></dc:creator>
            <pubDate>Thu, 27 Jan 2022 14:40:41 GMT</pubDate>
            <atom:updated>2022-01-27T14:44:13.941Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A-ma1SrIjjLJsOKgfsh0fA.jpeg" /></figure><h4>目標:</h4><ol><li>使用 vue 做出 QR Code 掃描器</li><li>需可在 line 中使用</li></ol><h4>實作:</h4><ol><li>Installation</li></ol><pre>npm install vue-qrcode-reader</pre><p>2. 建立 QR code scanner component，並引入套件中需用到的 component</p><p>2.1. 加入掃描的 component(qrcode-steam)</p><p>加入相機開關設定，可以達到多次掃描同個 QR code 的功能，因為 QR code scanner 有 cached 的功能，會記住之前掃過的內容</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p class=&quot;decode-result&quot;&gt;Last result: &lt;b&gt;{{ result }}&lt;/b&gt;&lt;/p&gt;<br>    &lt;qrcode-stream :camera=&quot;camera&quot; @decode=&quot;onDecode&quot; @init=&quot;onInit&quot;&gt;<br>    &lt;/qrcode-stream&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>import { QrcodeStream } from &#39;vue-qrcode-reader&#39;;</pre><pre>export default {<br><br>  components: { QrcodeStream },<br><br>  data () {<br>    return {<br>      camera: &#39;auto&#39;,  //相機是否開啟<br>      result: null,  // 掃描結果<br>    }<br>  },<br><br>  methods: {<br><br>    async onInit (promise) {<br>      try {<br>        await promise<br>      } catch (e) {<br>        console.error(e)<br>      }<br>    },<br><br>    async onDecode (content) {<br>      this.result = content<br><br>      this.pause()<br>      await this.timeout(500)<br>      this.unpause()<br>    },<br><br>    unpause () {<br>      this.camera = &#39;auto&#39;<br>    },<br><br>    pause () {<br>      this.camera = &#39;off&#39;<br>    },<br><br>    timeout (ms) {<br>      return new Promise(resolve =&gt; {<br>        window.setTimeout(resolve, ms)<br>      })<br>    }<br>  }<br>}<br>&lt;/script&gt;<br><br>&lt;style scoped&gt;<br>.scan-confirmation {<br>  position: absolute;<br>  width: 100%;<br>  height: 100%;<br><br>  background-color: rgba(255, 255, 255, .8);<br><br>  display: flex;<br>  flex-flow: row nowrap;<br>  justify-content: center;<br>}<br>&lt;/style&gt;</pre><p>2.2. 加入 visual tracking</p><p>官方有三種 visual tracking: “outline”、”centered text”、”bounding box”。</p><p>這次專案使用 “outline”</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p class=&quot;decode-result&quot;&gt;Last result: &lt;b&gt;{{ result }}&lt;/b&gt;&lt;/p&gt;<br>    &lt;qrcode-stream :camera=&quot;camera&quot; @decode=&quot;onDecode&quot; @init=&quot;onInit&quot; <strong>:track=&quot;paintOutline&quot;</strong>&gt;<br>    &lt;/qrcode-stream&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>// ...同上</pre><pre> methods: {<br>  <strong>  paintOutline (detectedCodes, ctx) {<br>      for (const detectedCode of detectedCodes) {<br>        const [ firstPoint, ...otherPoints ] = detectedCode.cornerPoints</strong></pre><pre><strong>        ctx.lineWidth = 5;  // 設定外框寬度</strong><strong><br>        ctx.strokeStyle = &quot;red&quot;; // 設定顏色<br><br>        ctx.beginPath();<br>        ctx.moveTo(firstPoint.x, firstPoint.y);<br>        for (const { x, y } of otherPoints) {<br>          ctx.lineTo(x, y);<br>        }<br>        ctx.lineTo(firstPoint.x, firstPoint.y);<br>        ctx.closePath();<br>        ctx.stroke();<br>      }<br>    },</strong><br>}</pre><p>2.3. 新增上傳 QR code 圖片的方式讀取內容，避免裝置沒有相機或無法使用相機的情況發生</p><p>qrcode-capture component 中 props.capture value 預設為 &#39;environment&#39;，其他還有兩種形式，如下:</p><p>{ text: &quot;rear camera (default)&quot;, value: &quot;environment&quot; },<br> { text: &quot;front camera&quot;, value: &quot;user&quot; },<br> { text: &quot;force file dialog&quot;, value: false },</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p class=&quot;decode-result&quot;&gt;Last result: &lt;b&gt;{{ result }}&lt;/b&gt;&lt;/p&gt;<br>    &lt;qrcode-stream :camera=&quot;camera&quot; @decode=&quot;onDecode&quot; @init=&quot;onInit&quot; :track=&quot;paintOutline&quot;&gt;<br>    &lt;/qrcode-stream&gt;    <br><strong>    &lt;qrcode-capture @decode=&quot;onDecode&quot; :capture=&quot;&#39;environment&#39;&quot; /&gt;  </strong>&lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br><strong>import { QrcodeStream, </strong><strong>QrcodeCapture} from &#39;vue-qrcode-reader&#39;;</strong><br>export default {<br>  <strong>components: { QrcodeStream, QrcodeCapture },</strong><br><br>  data () {<br>    return {<br>      camera: &#39;auto&#39;,  //相機是否開啟<br>      result: null,  // 掃描結果<br>    }<br>  },<br><br>  methods: {</pre><pre>// ...同上</pre><pre>  }<br>}<br>&lt;/script&gt;</pre><p>3. 完成!</p><h4>遇到困難:</h4><ol><li>在 line 中開啟網頁，android 手機無法正確詢問是否允許開啟相機，造成無法打開相機，進行掃描</li></ol><p>解決辦法: 使用 line 提供的 LIFF App 將網頁包起來，即可正確詢問相機權限，進行掃描</p><p>2. ios version 15 無法正確 render 相機畫面</p><p>解決辦法: 更新 <a href="https://github.com/gruhn/vue-qrcode-reader">vue-qrcode-reader</a> 套件版本，作者已於 v3.1.0 進行修正</p><p>參考資料:</p><blockquote><a href="https://github.com/gruhn/vue-qrcode-reader">https://github.com/gruhn/vue-qrcode-reader</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af378a180c8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>