Commit 55ddb3f8a694bb5d7e0b211e5a46b4f65a418a56
1 parent
09a4a9a0aa
Exists in
master
登陆逻辑修改
Showing
5 changed files
with
297 additions
and
20 deletions
Show diff stats
src/App.vue
1 | -<script lang="ts"> | |
1 | +<script> | |
2 | 2 | import Vue from 'vue'; |
3 | + import store from '@/store'; | |
4 | + | |
3 | 5 | export default Vue.extend({ |
4 | 6 | mpType: 'app', |
5 | - onLaunch() { | |
6 | - console.log('App Launch') | |
7 | + onLaunch(options) { | |
8 | + const option = options || {}; | |
9 | + // 获取用户来源 | |
10 | + console.log('软件启动,输出转来的参数:', option); | |
11 | + console.log('场景值------------------:', option.scene); | |
12 | + let loginQueryInfo = option ? option.query : {}; | |
13 | + loginQueryInfo = loginQueryInfo || {}; | |
14 | + console.log('loginQueryInfo onShow===>', loginQueryInfo); | |
15 | + const scene = decodeURIComponent(loginQueryInfo.scene); | |
16 | + console.log('decodeURIComponent scense====>', scene); | |
17 | + let fromInfo = {}; | |
18 | + | |
19 | + if(scene.length > 0 ) { | |
20 | + const getQueryString = { | |
21 | + uid: undefined, | |
22 | + sid: undefined, | |
23 | + pid: undefined | |
24 | + }; | |
25 | + const strB = scene.split('&'); | |
26 | + | |
27 | + for(let i = 0; i < strB.length; i += 1) { | |
28 | + getQueryString[strB[i].split('=')[0]] = unescape(strB[i].split('=')[1]); | |
29 | + } | |
30 | + fromInfo = { | |
31 | + loginQueryInfo, | |
32 | + fromuid: getQueryString.uid, | |
33 | + fromsid: getQueryString.sid, | |
34 | + frompid: getQueryString.pid, | |
35 | + scene: option.scene, | |
36 | + } | |
37 | + store.dispatch('user/setFrom', fromInfo); | |
38 | + console.log('loginQueryInfo.hasOwnProperty=====111===>', getQueryString); | |
39 | + } else { | |
40 | + console.log('loginQueryInfo.hasOwnProperty====22222====>', loginQueryInfo); | |
41 | + if (loginQueryInfo.hasOwnProperty('sid') == false) { | |
42 | + loginQueryInfo.sid = 0; | |
43 | + } | |
44 | + if (loginQueryInfo.hasOwnProperty('pid') == false) { | |
45 | + loginQueryInfo.pid = 0; | |
46 | + } | |
47 | + if (loginQueryInfo.hasOwnProperty('uid') == false) { | |
48 | + loginQueryInfo.uid = 0; | |
49 | + } | |
50 | + fromInfo = { | |
51 | + loginQueryInfo, | |
52 | + fromuid: loginQueryInfo.uid, | |
53 | + fromsid: loginQueryInfo.sid, | |
54 | + frompid: loginQueryInfo.pid, | |
55 | + scene: option.scene, | |
56 | + } | |
57 | + store.dispatch('user/setFrom', fromInfo) | |
58 | + } | |
59 | + | |
60 | + // 登陆 | |
61 | + const openId = uni.getStorageSync('openid') || ""; | |
62 | + if(openId.length <= 10) { | |
63 | + uni.clearStorageSync() | |
64 | + store.dispatch("user/login", fromInfo); | |
65 | + } else { | |
66 | + store.dispatch("user/getUserInfo", fromInfo); | |
67 | + } | |
7 | 68 | }, |
8 | 69 | onShow() { |
9 | 70 | |
... | ... | @@ -11,6 +72,8 @@ |
11 | 72 | }, |
12 | 73 | onHide() { |
13 | 74 | console.log('App Hide') |
75 | + }, | |
76 | + methods: { | |
14 | 77 | } |
15 | 78 | }); |
16 | 79 | </script> | ... | ... |
src/pages/index/index.vue
... | ... | @@ -138,8 +138,10 @@ export default { |
138 | 138 | }, |
139 | 139 | onLoad: function() { |
140 | 140 | store.dispatch('test/fetch'); |
141 | - store.dispatch('categoryOrder/fetch') | |
142 | - | |
141 | + store.dispatch('categoryOrder/fetch') | |
142 | + // 登陆 | |
143 | + // store.dispatch('user/login') | |
144 | + | |
143 | 145 | //定时器模拟ajax异步请求数据 |
144 | 146 | setTimeout(()=>{ |
145 | 147 | //传入defaultSelected的结构不能错,错了就报错运行异常。 不选中的项目传入null | ... | ... |
src/pages/user/user.vue
1 | 1 | <template> |
2 | 2 | <view class="wrap"> |
3 | - <view class="content"> | |
3 | + <view v-if="isAuth" class="content"> | |
4 | 4 | <view class="userInfo"> |
5 | 5 | <view class="info"> |
6 | - <image src="../../static/img/detail/d10.png" mode="aspectFill"></image> | |
6 | + <image :src="headerphoto" mode="aspectFill"></image> | |
7 | 7 | <view class="infoText"> |
8 | - <text class="userName">{{userName}}</text> | |
9 | - <text class="nickName">用户名:骆驼不死的祥子</text> | |
8 | + <text class="userName">{{nickName}}</text> | |
10 | 9 | </view> |
11 | 10 | </view> |
12 | 11 | <view class="service"> |
... | ... | @@ -57,19 +56,30 @@ |
57 | 56 | </view> |
58 | 57 | </view> |
59 | 58 | </view> |
60 | - | |
59 | + <view v-else class="auth"> | |
60 | + <view class="icon"></view> | |
61 | + <view class="divider"></view> | |
62 | + <view class="title">申请获取以下权限</view> | |
63 | + <view class="text">获得您的公开信息(昵称、头像等)</view> | |
64 | + <button | |
65 | + type="primary" | |
66 | + open-type="getUserInfo" | |
67 | + @getuserinfo="onGotUserInfo" | |
68 | + >授权登陆</button> | |
69 | + </view> | |
61 | 70 | </view> |
62 | 71 | </template> |
63 | 72 | |
64 | 73 | <script> |
65 | - import Card from "@/components/CommodityCard/CommodityCard.vue" | |
74 | + import Card from "@/components/CommodityCard/CommodityCard.vue"; | |
75 | + import store from '@/store'; | |
76 | + | |
66 | 77 | export default { |
67 | 78 | components: { |
68 | 79 | 'Card':Card |
69 | 80 | }, |
70 | 81 | data() { |
71 | 82 | return { |
72 | - | |
73 | 83 | //商品数据 |
74 | 84 | goodsList:[ |
75 | 85 | { goods_id: 0, img: "/static/img/goods/p1.jpg", name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' }, |
... | ... | @@ -83,14 +93,43 @@ |
83 | 93 | { goods_id: 8, img: '/static/img/goods/p9.jpg', name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' }, |
84 | 94 | { goods_id: 9, img: '/static/img/goods/p10.jpg', name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' } |
85 | 95 | ], |
86 | - headerphoto:'', | |
87 | - userName:'Adam' | |
96 | + isAuth: true, // 是否显示授权页面 | |
88 | 97 | } |
89 | 98 | }, |
90 | 99 | onLoad() { |
91 | - | |
100 | + // 判断是否授权 | |
101 | + uni.getSetting({ | |
102 | + success(res) { | |
103 | + console.log('authSetting',res.authSetting) | |
104 | + if(res.authSetting['scope.userInfo'] === true) { | |
105 | + this.isAuth = true | |
106 | + } else { | |
107 | + this.isAuth = false | |
108 | + } | |
109 | + } | |
110 | + }) | |
111 | + }, | |
112 | + computed: { | |
113 | + nickName() { | |
114 | + console.log('nickName', this.$store.state.user.userInfo) | |
115 | + return this.$store.state.user.userInfo.nickName; | |
116 | + }, | |
117 | + headerphoto() { | |
118 | + return this.$store.state.user.userInfo.headerphoto; | |
119 | + } | |
92 | 120 | }, |
93 | 121 | methods: { |
122 | + // 授权 | |
123 | + onGotUserInfo(e) { | |
124 | + console.log('anthInfo', e); | |
125 | + if(e.detail.errMsg == 'getUserInfo:ok') { | |
126 | + const { fromInfo } = this.$store.state.user; | |
127 | + console.log('=====la', fromInfo) | |
128 | + // 用户授权成功 | |
129 | + store.dispatch('user/getUserInfo', fromInfo); | |
130 | + this.isAuth = true | |
131 | + } | |
132 | + }, | |
94 | 133 | toMyOrder(){ |
95 | 134 | uni.navigateTo({ |
96 | 135 | url: '../myOrder/myOrder?current=0', |
... | ... | @@ -181,6 +220,7 @@ |
181 | 220 | } |
182 | 221 | } |
183 | 222 | .service{ |
223 | + margin-top: 20rpx; | |
184 | 224 | image{ |
185 | 225 | height: 36rpx; |
186 | 226 | width: 36rpx; |
... | ... | @@ -296,5 +336,42 @@ |
296 | 336 | } |
297 | 337 | } |
298 | 338 | } |
339 | + .auth { | |
340 | + height: 100vh; | |
341 | + display: flex; | |
342 | + flex-direction: column; | |
343 | + align-items: center; | |
344 | + .icon { | |
345 | + width: 140rpx; | |
346 | + height: 140rpx; | |
347 | + border-radius: 50%; | |
348 | + margin-top: 100rpx; | |
349 | + background-color: grey; | |
350 | + } | |
351 | + .divider { | |
352 | + height: 1rpx; | |
353 | + width: 600rpx; | |
354 | + margin-top: 80rpx; | |
355 | + background-color: #E6E3E3; | |
356 | + } | |
357 | + .title { | |
358 | + width: 600rpx; | |
359 | + margin-top: 50rpx; | |
360 | + text-align: left; | |
361 | + } | |
362 | + .text { | |
363 | + width: 600rpx; | |
364 | + margin-top: 30rpx; | |
365 | + text-align: left; | |
366 | + color: #E6E3E3; | |
367 | + } | |
368 | + button { | |
369 | + width: 450rpx; | |
370 | + height: 80rpx; | |
371 | + line-height: 80rpx; | |
372 | + margin-top: 80rpx; | |
373 | + border-radius: 30rpx; | |
374 | + } | |
375 | + } | |
299 | 376 | |
300 | 377 | </style> | ... | ... |
src/store/modules/user.js
... | ... | @@ -0,0 +1,132 @@ |
1 | +import urlAlias from '../url'; | |
2 | +import request from '../request'; | |
3 | + | |
4 | +const { | |
5 | + login, | |
6 | + getUserInfo, | |
7 | +} = urlAlias; | |
8 | + | |
9 | +const state = { | |
10 | + fromInfo: { | |
11 | + loginQueryInfo: {}, | |
12 | + fromuid: 0, | |
13 | + fromsid: 0, | |
14 | + frompid: 0, | |
15 | + scene: 1001, | |
16 | + }, | |
17 | + userInfo: { | |
18 | + uid: 1, | |
19 | + openid: '', | |
20 | + nickName: '', | |
21 | + headerphoto: '', | |
22 | + } | |
23 | +}; | |
24 | + | |
25 | +const mutations = { | |
26 | + FROM: (state, data) => { | |
27 | + state.formInfo = data; | |
28 | + }, | |
29 | + UPDATE: (state, data) => { | |
30 | + state.userInfo = data; | |
31 | + } | |
32 | +}; | |
33 | + | |
34 | +const actions = { | |
35 | + // 登陆 | |
36 | + login({ commit }, param) { | |
37 | + const { | |
38 | + frompid = undefined, | |
39 | + fromsid = undefined, | |
40 | + fromuid = undefined, | |
41 | + scene = 1001 , | |
42 | + } = param || {}; | |
43 | + uni.login({ | |
44 | + provider: 'weixin', | |
45 | + success: function (loginRes) { | |
46 | + console.log('login',loginRes); | |
47 | + request({ | |
48 | + url: login, | |
49 | + data: { | |
50 | + app_uid: 2020, | |
51 | + code: loginRes.code, | |
52 | + frompid, | |
53 | + fromsid, | |
54 | + fromuid, | |
55 | + | |
56 | + }, | |
57 | + success: (res) => { | |
58 | + const { data: { session_key, openid} } = res; | |
59 | + console.log('res', res) | |
60 | + // 写入缓存 | |
61 | + uni.setStorageSync('session_key', session_key); | |
62 | + uni.setStorageSync('openid', openid); | |
63 | + request({ | |
64 | + url: getUserInfo, | |
65 | + data: { | |
66 | + app_uid: 2020, | |
67 | + frompid, | |
68 | + fromsid, | |
69 | + fromuid, | |
70 | + openid, | |
71 | + scene, | |
72 | + }, | |
73 | + success: (res) => { | |
74 | + console.log('userInfo=====', res); | |
75 | + const { data: { data: { uid, username: nickName, openid, headerphoto } } } = res; | |
76 | + const userInfo = { | |
77 | + uid, | |
78 | + openid, | |
79 | + nickName, | |
80 | + headerphoto, | |
81 | + } | |
82 | + commit("UPDATE", userInfo) | |
83 | + } | |
84 | + }) | |
85 | + } | |
86 | + }) | |
87 | + } | |
88 | + }); | |
89 | + }, | |
90 | + setFrom({ commit }, param) { | |
91 | + commit("FROM", param); | |
92 | + }, | |
93 | + getUserInfo( { commit }, param) { | |
94 | + const { | |
95 | + frompid = undefined, | |
96 | + fromsid = undefined, | |
97 | + fromuid = undefined, | |
98 | + scene = 1001 , | |
99 | + } = param || {}; | |
100 | + // 写入缓存 | |
101 | + const openid = uni.getStorageSync('openid', openid); | |
102 | + request({ | |
103 | + url: getUserInfo, | |
104 | + data: { | |
105 | + app_uid: 2020, | |
106 | + frompid, | |
107 | + fromsid, | |
108 | + fromuid, | |
109 | + openid, | |
110 | + scene, | |
111 | + }, | |
112 | + success: (res) => { | |
113 | + console.log('userInfo=====', res); | |
114 | + const { data: { data: { uid, username: nickName, openid, headerphoto } } } = res; | |
115 | + const userInfo = { | |
116 | + uid, | |
117 | + openid, | |
118 | + nickName, | |
119 | + headerphoto, | |
120 | + } | |
121 | + commit("UPDATE", userInfo) | |
122 | + } | |
123 | + }) | |
124 | + }, | |
125 | +} | |
126 | + | |
127 | +export default { | |
128 | + namespaced: true, | |
129 | + state, | |
130 | + mutations, | |
131 | + actions, | |
132 | +} | |
0 | 133 | \ No newline at end of file | ... | ... |
src/store/url.js
1 | 1 | const urlAlias = { |
2 | - // 获取首页商品列表 | |
3 | - shopList: '/app/prod/list', | |
4 | - | |
5 | - // 获取首页商品列表 | |
6 | - category: '/app/prod/category', | |
2 | + // 获取首页商品列表 | |
3 | + shopList: '/app/prod/list', | |
4 | + | |
5 | + // 获取首页商品列表 | |
6 | + category: '/app/prod/category', | |
7 | + // 登陆 | |
8 | + login: '/app/glass/getOpenId', // 登陆 | |
9 | + getUserInfo: '/app/glass/userinfo', // 获取用户信息 | |
7 | 10 | } |
8 | 11 | |
9 | 12 | export default urlAlias; | ... | ... |