Commit 6be1016751fa8b2e174be994adcdea03ab572838

Authored by 王强
1 parent aadd2d82

定位准备测试~

src/common/helper.class.js
@@ -196,4 +196,24 @@ export default class Helper { @@ -196,4 +196,24 @@ export default class Helper {
196 196
197 /* 数据格式处理 结束 */ 197 /* 数据格式处理 结束 */
198 198
  199 + /* 定位 开始 */
  200 +
  201 + /**
  202 + * 获取用户地理位置坐标
  203 + * @param {Function} success [成功回调函数,获取定位坐标]
  204 + * @param {Function} error [错误回调函数,错误代码和信息]
  205 + * @param {Function} notSupport [浏览器不支持定位方法]
  206 + */
  207 + getLocation(success, error, notSupport) {
  208 + if (navigator.geolocation) { // 支持定位
  209 + navigator.geolocation.getCurrentPosition(success, error);
  210 + } else { // 不支持定位
  211 + if (typeof notSupport === "function") {
  212 + notSupport();
  213 + }
  214 + }
  215 + }
  216 +
  217 + /* 定位 结束 */
  218 +
199 } 219 }
200 \ No newline at end of file 220 \ No newline at end of file
src/pages/search/search.vue
@@ -96,7 +96,8 @@ @@ -96,7 +96,8 @@
96 }, 96 },
97 coordinate: { // 经纬度定位参数 97 coordinate: { // 经纬度定位参数
98 longitude: "0", 98 longitude: "0",
99 - latitude: "0" 99 + latitude: "0",
  100 + location: false
100 }, 101 },
101 titles: { // 板块标题 102 titles: { // 板块标题
102 goods: { 103 goods: {
@@ -190,6 +191,71 @@ @@ -190,6 +191,71 @@
190 }, 191 },
191 192
192 /** 193 /**
  194 + * 初始化页面
  195 + */
  196 + initPage() {
  197 +
  198 + // 定位执行完成,设置定位成功标识参数
  199 + this.$set(this.coordinate, "location", true);
  200 +
  201 + // 设置搜索历史和会话搜索关键字
  202 + this.getSearchHistoryArr();
  203 +
  204 + // 判断如果有搜索关键字,进行搜索
  205 + if (this.$refs.searchCom.getKeyword()) {
  206 +
  207 + // 搜索
  208 + this.doConfirm(this.$refs.searchCom.getKeyword());
  209 + }
  210 + },
  211 +
  212 + /**
  213 + * 获取用户定位坐标回调
  214 + * @param {Object} position [坐标数据对象]
  215 + */
  216 + positionCallback(position) {
  217 +
  218 + // 设置经纬度定位参数
  219 + this.$set(this.coordinate, "longitude", position.coords.longitude);
  220 + this.$set(this.coordinate, "latitude", position.coords.latitude);
  221 +
  222 + // 定位执行完成,设置定位成功标识参数
  223 + this.$set(this.coordinate, "location", true);
  224 +
  225 + // 根据实际状况,调用获取数据接口,
  226 + this.initPage();
  227 + },
  228 +
  229 + /**
  230 + * 获取用户定位坐标(error)回调
  231 + * @param {Object} positionError [错误信息对象]
  232 + */
  233 + positionErrorCallback(positionError) {
  234 +
  235 + // 定位出错提示
  236 + switch(positionError.code) {
  237 + case positionError.PERMISSION_DENIED:
  238 + Toast("用户拒绝定位");
  239 + break;
  240 + case positionError.POSITION_UNAVAILABLE:
  241 + Toast("无权获取当前位置");
  242 + break;
  243 + case positionError.TIMEOUT:
  244 + Toast("定位请求超时");
  245 + break;
  246 + case positionError.UNKNOWN_ERROR:
  247 + Toast("未知定位错误");
  248 + break;
  249 + }
  250 +
  251 + // 定位执行完成,设置定位成功标识参数
  252 + this.$set(this.coordinate, "location", true);
  253 +
  254 + // 根据实际状况,调用获取数据接口,
  255 + this.initPage();
  256 + },
  257 +
  258 + /**
193 * 全局搜索 259 * 全局搜索
194 * @param {String} keyword [搜索关键字] 260 * @param {String} keyword [搜索关键字]
195 */ 261 */
@@ -799,14 +865,11 @@ @@ -799,14 +865,11 @@
799 }, 865 },
800 mounted() { 866 mounted() {
801 867
802 - // 设置搜索历史和会话搜索关键字  
803 - this.getSearchHistoryArr();  
804 -  
805 - // 判断如果有搜索关键字,进行搜索  
806 - if (this.$refs.searchCom.getKeyword()) { 868 + // 获取用户定位坐标
  869 + if (typeof this.$parent.helper === "object" && typeof this.$parent.helper.getLocation === "function") {
807 870
808 - // 搜索  
809 - this.doConfirm(this.$refs.searchCom.getKeyword()); 871 + // 获取用户定位坐标
  872 + this.$parent.helper.getLocation(this.positionCallback, this.positionErrorCallback, this.initPage);
810 } 873 }
811 } 874 }
812 } 875 }