diff --git a/src/App.vue b/src/App.vue index a789f9f..62f620c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,9 +1,70 @@ -<script lang="ts"> +<script> import Vue from 'vue'; + import store from '@/store'; + export default Vue.extend({ mpType: 'app', - onLaunch() { - console.log('App Launch') + onLaunch(options) { + const option = options || {}; + // 获取用户来源 + console.log('软件启动,输出转来的参数:', option); + console.log('场景值------------------:', option.scene); + let loginQueryInfo = option ? option.query : {}; + loginQueryInfo = loginQueryInfo || {}; + console.log('loginQueryInfo onShow===>', loginQueryInfo); + const scene = decodeURIComponent(loginQueryInfo.scene); + console.log('decodeURIComponent scense====>', scene); + let fromInfo = {}; + + if(scene.length > 0 ) { + const getQueryString = { + uid: undefined, + sid: undefined, + pid: undefined + }; + const strB = scene.split('&'); + + for(let i = 0; i < strB.length; i += 1) { + getQueryString[strB[i].split('=')[0]] = unescape(strB[i].split('=')[1]); + } + fromInfo = { + loginQueryInfo, + fromuid: getQueryString.uid, + fromsid: getQueryString.sid, + frompid: getQueryString.pid, + scene: option.scene, + } + store.dispatch('user/setFrom', fromInfo); + console.log('loginQueryInfo.hasOwnProperty=====111===>', getQueryString); + } else { + console.log('loginQueryInfo.hasOwnProperty====22222====>', loginQueryInfo); + if (loginQueryInfo.hasOwnProperty('sid') == false) { + loginQueryInfo.sid = 0; + } + if (loginQueryInfo.hasOwnProperty('pid') == false) { + loginQueryInfo.pid = 0; + } + if (loginQueryInfo.hasOwnProperty('uid') == false) { + loginQueryInfo.uid = 0; + } + fromInfo = { + loginQueryInfo, + fromuid: loginQueryInfo.uid, + fromsid: loginQueryInfo.sid, + frompid: loginQueryInfo.pid, + scene: option.scene, + } + store.dispatch('user/setFrom', fromInfo) + } + + // 登陆 + const openId = uni.getStorageSync('openid') || ""; + if(openId.length <= 10) { + uni.clearStorageSync() + store.dispatch("user/login", fromInfo); + } else { + store.dispatch("user/getUserInfo", fromInfo); + } }, onShow() { @@ -11,6 +72,8 @@ }, onHide() { console.log('App Hide') + }, + methods: { } }); </script> diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 0cfd791..35947fb 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -138,8 +138,10 @@ export default { }, onLoad: function() { store.dispatch('test/fetch'); - store.dispatch('categoryOrder/fetch') - + store.dispatch('categoryOrder/fetch') + // 登陆 + // store.dispatch('user/login') + //定时器模拟ajax异步请求数据 setTimeout(()=>{ //传入defaultSelected的结构不能错,错了就报错运行异常。 不选中的项目传入null diff --git a/src/pages/user/user.vue b/src/pages/user/user.vue index 3e90623..8aeec73 100644 --- a/src/pages/user/user.vue +++ b/src/pages/user/user.vue @@ -1,12 +1,11 @@ <template> <view class="wrap"> - <view class="content"> + <view v-if="isAuth" class="content"> <view class="userInfo"> <view class="info"> - <image src="../../static/img/detail/d10.png" mode="aspectFill"></image> + <image :src="headerphoto" mode="aspectFill"></image> <view class="infoText"> - <text class="userName">{{userName}}</text> - <text class="nickName">用户名:骆驼不死的祥子</text> + <text class="userName">{{nickName}}</text> </view> </view> <view class="service"> @@ -57,19 +56,30 @@ </view> </view> </view> - + <view v-else class="auth"> + <view class="icon"></view> + <view class="divider"></view> + <view class="title">申请获取以下权限</view> + <view class="text">获得您的公开信息(昵称、头像等)</view> + <button + type="primary" + open-type="getUserInfo" + @getuserinfo="onGotUserInfo" + >授权登陆</button> + </view> </view> </template> <script> - import Card from "@/components/CommodityCard/CommodityCard.vue" + import Card from "@/components/CommodityCard/CommodityCard.vue"; + import store from '@/store'; + export default { components: { 'Card':Card }, data() { return { - //商品数据 goodsList:[ { goods_id: 0, img: "/static/img/goods/p1.jpg", name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' }, @@ -83,14 +93,43 @@ { goods_id: 8, img: '/static/img/goods/p9.jpg', name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' }, { goods_id: 9, img: '/static/img/goods/p10.jpg', name: '商品名称',originCost:'¥198',price: '¥168', slogan:'1235人浏览' } ], - headerphoto:'', - userName:'Adam' + isAuth: true, // 是否显示授权页面 } }, onLoad() { - + // 判断是否授权 + uni.getSetting({ + success(res) { + console.log('authSetting',res.authSetting) + if(res.authSetting['scope.userInfo'] === true) { + this.isAuth = true + } else { + this.isAuth = false + } + } + }) + }, + computed: { + nickName() { + console.log('nickName', this.$store.state.user.userInfo) + return this.$store.state.user.userInfo.nickName; + }, + headerphoto() { + return this.$store.state.user.userInfo.headerphoto; + } }, methods: { + // 授权 + onGotUserInfo(e) { + console.log('anthInfo', e); + if(e.detail.errMsg == 'getUserInfo:ok') { + const { fromInfo } = this.$store.state.user; + console.log('=====la', fromInfo) + // 用户授权成功 + store.dispatch('user/getUserInfo', fromInfo); + this.isAuth = true + } + }, toMyOrder(){ uni.navigateTo({ url: '../myOrder/myOrder?current=0', @@ -181,6 +220,7 @@ } } .service{ + margin-top: 20rpx; image{ height: 36rpx; width: 36rpx; @@ -296,5 +336,42 @@ } } } + .auth { + height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + .icon { + width: 140rpx; + height: 140rpx; + border-radius: 50%; + margin-top: 100rpx; + background-color: grey; + } + .divider { + height: 1rpx; + width: 600rpx; + margin-top: 80rpx; + background-color: #E6E3E3; + } + .title { + width: 600rpx; + margin-top: 50rpx; + text-align: left; + } + .text { + width: 600rpx; + margin-top: 30rpx; + text-align: left; + color: #E6E3E3; + } + button { + width: 450rpx; + height: 80rpx; + line-height: 80rpx; + margin-top: 80rpx; + border-radius: 30rpx; + } + } </style> diff --git a/src/store/modules/user.js b/src/store/modules/user.js new file mode 100644 index 0000000..ae0128e --- /dev/null +++ b/src/store/modules/user.js @@ -0,0 +1,132 @@ +import urlAlias from '../url'; +import request from '../request'; + +const { + login, + getUserInfo, +} = urlAlias; + +const state = { + fromInfo: { + loginQueryInfo: {}, + fromuid: 0, + fromsid: 0, + frompid: 0, + scene: 1001, + }, + userInfo: { + uid: 1, + openid: '', + nickName: '', + headerphoto: '', + } +}; + +const mutations = { + FROM: (state, data) => { + state.formInfo = data; + }, + UPDATE: (state, data) => { + state.userInfo = data; + } +}; + +const actions = { + // 登陆 + login({ commit }, param) { + const { + frompid = undefined, + fromsid = undefined, + fromuid = undefined, + scene = 1001 , + } = param || {}; + uni.login({ + provider: 'weixin', + success: function (loginRes) { + console.log('login',loginRes); + request({ + url: login, + data: { + app_uid: 2020, + code: loginRes.code, + frompid, + fromsid, + fromuid, + + }, + success: (res) => { + const { data: { session_key, openid} } = res; + console.log('res', res) + // 写入缓存 + uni.setStorageSync('session_key', session_key); + uni.setStorageSync('openid', openid); + request({ + url: getUserInfo, + data: { + app_uid: 2020, + frompid, + fromsid, + fromuid, + openid, + scene, + }, + success: (res) => { + console.log('userInfo=====', res); + const { data: { data: { uid, username: nickName, openid, headerphoto } } } = res; + const userInfo = { + uid, + openid, + nickName, + headerphoto, + } + commit("UPDATE", userInfo) + } + }) + } + }) + } + }); + }, + setFrom({ commit }, param) { + commit("FROM", param); + }, + getUserInfo( { commit }, param) { + const { + frompid = undefined, + fromsid = undefined, + fromuid = undefined, + scene = 1001 , + } = param || {}; + // 写入缓存 + const openid = uni.getStorageSync('openid', openid); + request({ + url: getUserInfo, + data: { + app_uid: 2020, + frompid, + fromsid, + fromuid, + openid, + scene, + }, + success: (res) => { + console.log('userInfo=====', res); + const { data: { data: { uid, username: nickName, openid, headerphoto } } } = res; + const userInfo = { + uid, + openid, + nickName, + headerphoto, + } + commit("UPDATE", userInfo) + } + }) + }, +} + +export default { + namespaced: true, + state, + mutations, + actions, +} \ No newline at end of file diff --git a/src/store/url.js b/src/store/url.js index 0c19d35..e0cf05f 100644 --- a/src/store/url.js +++ b/src/store/url.js @@ -1,9 +1,12 @@ const urlAlias = { - // 获取首页商品列表 - shopList: '/app/prod/list', - - // 获取首页商品列表 - category: '/app/prod/category', + // 获取首页商品列表 + shopList: '/app/prod/list', + + // 获取首页商品列表 + category: '/app/prod/category', + // 登陆 + login: '/app/glass/getOpenId', // 登陆 + getUserInfo: '/app/glass/userinfo', // 获取用户信息 } export default urlAlias;