1919 <button @tap =" clear" >清空</button >
2020 </view >
2121 </view >
22+ <uni-popup :show =" type === 'showpopup'" mode =" fixed" @hidePopup =" togglePopup('')" >
23+ <view class =" popup-view" >
24+ <text class =" popup-title" >需要用户授权位置权限</text >
25+ <view class =" uni-flex popup-buttons" >
26+ <button class =" uni-flex-item" type =" primary" open-type =" openSetting" @tap =" openSetting" >设置</button >
27+ <button class =" uni-flex-item" @tap =" togglePopup('')" >取消</button >
28+ </view >
29+ </view >
30+ </uni-popup >
2231 </view >
2332</template >
2433<script >
34+ import uniPopup from ' @/components/uni-popup/uni-popup.vue'
2535 var util = require (' ../../../common/util.js' );
2636 var formatLocation = util .formatLocation ;
2737
2838 export default {
39+ components: {
40+ uniPopup
41+ },
2942 data () {
3043 return {
3144 title: ' getLocation' ,
3245 hasLocation: false ,
33- location: {}
46+ location: {},
47+ type: ' '
3448 }
3549 },
3650 methods: {
37- getLocation : function () {
51+ togglePopup (type ) {
52+ this .type = type;
53+ },
54+ showConfirm () {
55+ this .type = ' showpopup' ;
56+ },
57+ hideConfirm () {
58+ this .type = ' ' ;
59+ },
60+ async getLocation () {
61+ // #ifdef MP-WEIXIN || MP-TOUTIAO
62+ let status = await this .getSetting ();
63+ if (status === 2 ) {
64+ this .showConfirm ();
65+ return ;
66+ }
67+ // #endif
68+
69+ this .doGetLocation ();
70+ },
71+ doGetLocation () {
3872 uni .getLocation ({
3973 success : (res ) => {
40- this .hasLocation = true ,
41- this .location = formatLocation (res .longitude , res .latitude )
74+ this .hasLocation = true ;
75+ this .location = formatLocation (res .longitude , res .latitude );
76+ },
77+ fail : (err ) => {
78+ // #ifdef MP-BAIDU
79+ if (err .errCode === 202 || err .errCode === 10003 ) { // 202模拟器 10003真机 user deny
80+ this .showConfirm ();
81+ }
82+ // #endif
83+ // #ifndef MP-BAIDU
84+ uni .showToast ({
85+ title: err .errMsg
86+ })
87+ // #endif
4288 }
4389 })
4490 },
45- clear : function () {
91+ getSetting : function () {
92+ return new Promise ((resolve , reject ) => {
93+ uni .getSetting ({
94+ success : (res ) => {
95+ console .log (res .authSetting );
96+ if (res .authSetting [' scope.userLocation' ] === undefined ) {
97+ resolve (0 );
98+ return ;
99+ }
100+ if (res .authSetting [' scope.userLocation' ]) {
101+ resolve (1 );
102+ } else {
103+ resolve (2 );
104+ }
105+ }
106+ });
107+ });
108+ },
109+ openSetting : function () {
110+ this .hideConfirm ();
111+ uni .openSetting ({
112+ success : (res ) => {
113+ if (res .authSetting && res .authSetting [' scope.userLocation' ]) {
114+ this .doGetLocation ();
115+ }
116+ },
117+ fail : (err ) => {}
118+ })
119+ },
120+ clear : function () {
46121 this .hasLocation = false
47122 }
48123 }
49124 }
50125 </script >
51126
52127<style >
53-
54- </style >
128+ .popup-view {
129+ width : 500 upx;
130+ }
131+
132+ .popup-title {
133+ display : block ;
134+ font-size : 16px ;
135+ line-height : 3 ;
136+ margin-bottom : 10px ;
137+ text-align : center ;
138+ }
139+
140+ .popup-buttons button {
141+ margin-left : 4px ;
142+ margin-right : 4px ;
143+ }
144+ </style >
0 commit comments