Liuyi
2024-11-21 9857f8cdadf9bbda695a689f42e0e8cecfefab3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<script setup>
    import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
    import { wxLoginApi } from './api/index.js'
    
    //用户打开小程序默认授权
    function wxLogin(){
        // uni.removeStorageSync('openId');
        if(!uni.getStorageSync('openId')){
            wx.login({
                success:async (res1)=>{
                    //存储微信登录的code,调用后端接口传递code参数,返回openid(也就是userId)存储到本地,通过判断本地是否存在openId,存在表示已登录
                    uni.setStorageSync('code',res1.code)
                    await wxLoginApi({code:res1.code}).then((res2) =>{
                        if(res2.code == 200){
                            uni.setStorageSync('openId',res2.data.userId)
                            uni.setStorageSync('token',res2.data.token)
                        }else{
                            console.log('登录接口调用失败')
                        }
                  })
                },
                fail:(e) =>{
                    uni.showToast({
                        title: '微信登录失败,请退出重试!',
                        duration: 2000,
                        icon:'fail'
                    });
                    console.log(e)
                }
            })
        }
    }
    // //申请获取用户位置权限
    // let userLocation = {
    //     lat:'',
    //     lon:'',
    // }
    // function getUserLocation(){
    //     // 请求用户授权,第一次进入会有位置授权的提示
    //     uni.authorize({
    //       scope: 'scope.userLocation',
    //       success() {
    //          console.log("成功授权位置信息1")
    //          storageLocation()
    //       },
    //       fail() {
    //           console.log("用户拒绝授权位置信息,再次提示用户授权")
    //           showToast()
    //       }
    //     })
    //     //获取用户位置并存储
    //     function storageLocation(){
    //         uni.getLocation({
    //             type:'gcj02',
    //             isHighAccuracy:true,
    //             success:(res) =>{
    //                 userLocation.lat = res.latitude
    //                 userLocation.lon = res.longitude
    //                 uni.setStorageSync('userLocation',JSON.stringify(userLocation))
    //             },
    //             fail:() =>{
    //                 showToast()
    //             }
    //         })
    //     }
    //     //用户拒绝授权
    //     function showToast(){
    //         uni.showModal({
    //             title: "请求授权当前位置",
    //             content: "请求获取您的位置,加载您附近饮水设备信息!",
    //             confirmText: "前往设置",
    //             success: (res) => {
    //                 if (res.confirm) {
    //                     uni.openSetting({
    //                          success:(res1) =>{ //打开设置成功
    //                             if (res1.authSetting['scope.userLocation']){
    //                                 console.log('用户二次授权成功')
    //                                 setTimeout(() =>{
    //                                     storageLocation()
    //                                 },1000)
    //                             }else{
    //                                 showToast()
    //                                 console.log('用户拒绝授权')
    //                             }
    //                         },
    //                     })
    //                 }else{
    //                     uni.showToast({
    //                         title: '请先授权!',
    //                         duration: 2000,
    //                         icon:'none'
    //                     });
    //                     showToast()
    //                 }
    //             },
    //         });
    //     }
    // }
    onLaunch(async() => {
        await wxLogin()
        // await getUserLocation()
        console.log('进入项目启动页APP.vue了')
    });
    onShow(() => {
      console.log("App Show");
    });
    onHide(() => {
      console.log("App Hide");
    });
</script>
 
<style lang='scss'>
</style>