Commit 6d7235bc by zhangcheng

授权登录 首页数据

parent c38bcaf5
<template> <template>
<!-- Tab --> <!-- Tab -->
<u-tabbar v-model="selectedTabbar" :border="false" inactive-color="#000000" active-color="#006ECF" :fixed="true" :safeAreaInsetBottom="true" @change="beforeSwitch"> <u-tabbar zIndex="10075" v-model="selectedTabbar" :border="false" inactive-color="#000000" active-color="#006ECF" :fixed="true" :safeAreaInsetBottom="true" @change="beforeSwitch">
<u-tabbar-item v-for="(item,index) in list" :key="item.text" :text="item.text" :icon="selectedTabbar==index?item.activeIcon:item.icon"></u-tabbar-item> <u-tabbar-item v-for="(item,index) in list" :key="item.text" :text="item.text" :icon="selectedTabbar==index?item.activeIcon:item.icon"></u-tabbar-item>
</u-tabbar> </u-tabbar>
</template> </template>
......
import App from './App' import App from './App'
import uView from 'uview-ui';
Vue.use(uView);
//引入vuex //引入vuex
import store from './store' import store from './store'
import utils from './utils/utils' import utils from './utils/utils'
Vue.prototype.$utils = utils
// console.log(Vue.$u,22) // console.log(Vue.$u,22)
// 需要在Vue.use(uView)之后执行 // 需要在Vue.use(uView)之后执行
// #ifndef VUE3 // #ifndef VUE3
import Vue from 'vue' import Vue from 'vue'
import uView from 'uview-ui';
Vue.use(uView);
Vue.prototype.$utils = utils
Vue.config.productionTip = false Vue.config.productionTip = false
App.mpType = 'app' App.mpType = 'app'
const app = new Vue({ const app = new Vue({
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
<script> <script>
import config from '../../static/config/index.js' import config from '../../static/config/index.js'
import Order from '../../request/order/index' import Order from '../../request/order/index'
import User from '@/request/user'
export default { export default {
data() { data() {
return { return {
...@@ -26,88 +27,24 @@ ...@@ -26,88 +27,24 @@
// let curRoute = routes[routes.length - 1].route //获取当前页面路由 // let curRoute = routes[routes.length - 1].route //获取当前页面路由
//   let curParam = routes[routes.length - 1].options; //   let curParam = routes[routes.length - 1].options;
//  console.log(curParam) //  console.log(curParam)
let Authorization=uni.getStorageSync(`Authorization`);
let userInfo=uni.getStorageSync(`userInfo`);
let user=uni.getStorageSync(`currentUser`); if(Authorization){ //
if(user){ // if(userInfo){
user=await JSON.parse(user); this.$store.commit('setUserInfo',userInfo);
if(user.realName){ //有说明授权登陆过 }
this.checkSession(); uni.reLaunch({
}else{ url:'/pages/menu/menu'
uni.reLaunch({ })
url:'/pages/menu/menu'
})
}
}else{ }else{
uni.clearStorageSync(); uni.removeStorageSync('Authorization');
uni.reLaunch({ uni.removeStorageSync('userInfo');
url:'/pages/menu/menu' uni.reLaunch({
}) url:'/pages/menu/menu'
})
} }
}, },
methods: { methods: {
// 登录
login(){
this.$utils.uniLogin().then(async user => {
this.$store.commit('setUserInfo',user);
if(!user.realName){
uni.reLaunch({
url:'/pages/menu/menu'
})
}else{
if(this.id!=''){//二维码参数
this.valiFrequency();
}else{
uni.reLaunch({
url:'/pages/menu/menu'
})
}
}
}).catch((err)=>{
uni.showToast({
icon:'none',
title:'登录失败'
})
});
},
// 校验
async valiFrequency() {//去订单查看是否有此订单
let order=await Order.valiFrequency(this.id);
if(!order){
//无此订单
// uni.navigateTo({
// url:`/pages/detail/detail?id=${this.id}`
// })
}else{
// uni.navigateTo({
// url:`/pages/detail/detail?id=${this.id}`
// });
//是订单跳转到详情页或者其他相关页面
}
},
// sessionkey校验登陆状态
checkSession(){
uni.checkSession({
success:(res)=>{
let user=uni.getStorageSync(`currentUser`);
this.$store.commit('setUserInfo',user);
if(this.id!=''){//二维码参数
this.valiFrequency();
}else{
uni.reLaunch({
url:'/pages/menu/menu'
})
}
},
fail:(res)=>{
uni.clearStorageSync();
this.login();
}
});
}
} }
} }
</script> </script>
......
...@@ -188,7 +188,7 @@ ...@@ -188,7 +188,7 @@
"name": "今日特惠2", "name": "今日特惠2",
"goods": [ "goods": [
{ {
"goodId": 100, "goodId": 101,
"name": "冰茶咖啡2", "name": "冰茶咖啡2",
"price": 29.9, "price": 29.9,
"discount": 19.9, "discount": 19.9,
...@@ -345,7 +345,7 @@ ...@@ -345,7 +345,7 @@
"name": "今日特惠3", "name": "今日特惠3",
"goods": [ "goods": [
{ {
"goodId": 100, "goodId": 102,
"name": "冰茶咖啡3", "name": "冰茶咖啡3",
"price": 29.9, "price": 29.9,
"discount": 19.9, "discount": 19.9,
...@@ -502,7 +502,7 @@ ...@@ -502,7 +502,7 @@
"name": "今日特惠4", "name": "今日特惠4",
"goods": [ "goods": [
{ {
"goodId": 100, "goodId": 103,
"name": "冰茶咖啡4", "name": "冰茶咖啡4",
"price": 29.9, "price": 29.9,
"discount": 19.9, "discount": 19.9,
......
...@@ -29,15 +29,12 @@ uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做 ...@@ -29,15 +29,12 @@ uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做
title: '加载中', title: '加载中',
mask: true, mask: true,
}); });
let user = uni.getStorageSync(`currentUser`); let Authorization = uni.getStorageSync(`Authorization`);
let token = undefined; if (Authorization) {
if(user){ config.header.Authorization = Authorization;
token = JSON.parse(user).sessionToken;
if (token){
config.header.token = token;
}
} }
if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求 console.log(config,4444)
if (!Authorization) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
// return Promise.reject(config); // return Promise.reject(config);
} }
return config return config
......
export default { export default {
// 获取菜单列表 // 获取菜单列表
getMenuList(id) { getMenuList(id) {
uni.$u.http.get('/erer/erer',{ return uni.$u.http.get('/weixin/infoByShop', {
params:{ params:{
shopId:"4"
} }
}).then(res => { }).then(res => {
return res;
}).catch(err => { }).catch(err => {
}) })
......
import Store from '@/store'
export default{
getLocation() {
uni.getLocation({
type: 'gcj02', //返回可以用于uni.openLocation的经纬度
success: function (res) {
console.log(res, 'res')
var params = {
lat: res.latitude,
lng: res.longitude
}
// getAddress(params).then(res => {
// console.log(res, 'aaa')
// })
}
})
},
uniLogin(res) {
uni.login({
provider: 'weixin',
success: function (data) {
// 传给后台接口所需登录数据
uni.$u.http.post('/weixin/login', {
code: data.code,
source: 3, //默认是3,扫码进来是2
iv: res.iv,
encryptedData: res.encryptedData
}).then((response) => {
if (response.statusCode == 200 && response.data && response.data.code==200) {
console.log(response.data.token,54443)
uni.setStorage({key:'Authorization',data:response.data.token});
Store.commit('setAuthorization', response.data.token);
uni.showToast({
title:'登录成功'
});
}
}).catch((err) => {
uni.showToast({
title: err.msg
});
})
}
})
},
async uniGetUserInfo(e) {
uni.getUserProfile({ //授权后可以通过uni.getUserProfile得到用户信息
lang: "zh_CN",
desc: '用于完善用户信息',
success: res => {
uni.setStorage({key:'userInfo', data:res})
Store.commit("setUserInfo",res)
this.uniLogin(res);
}
})
},
getLocation() {
uni.getLocation({
type: 'gcj02', //返回可以用于uni.openLocation的经纬度
success: function (res) {
console.log(res, 'res')
var params = {
lat: res.latitude,
lng: res.longitude
}
getAddress(params).then(res => {
console.log(res, 'aaa')
})
}
})
},
}
\ No newline at end of file
const getters = {
userInfo: state => state.user.userInfo.userInfo,
nickName: state => state.user.userInfo.userInfo.nickName,
Authorization: state => state.user.Authorization,
menuAllInfo: state => state.menu.menuAllInfo,
}
export default getters
\ No newline at end of file
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import user from './modules/user'
import menu from './modules/menu'
import getters from './getters'
Vue.use(Vuex) Vue.use(Vuex)
const store = new Vuex.Store({ const store = new Vuex.Store({
modules: {
user,
menu
},
state: { state: {
list: [ list: [
{ {
...@@ -25,24 +32,17 @@ const store = new Vuex.Store({ ...@@ -25,24 +32,17 @@ const store = new Vuex.Store({
text: '我的' text: '我的'
} }
], ],
userInfo:{},//授权信息
authorization:false,//是否授权
orderId:''//外部扫码进入的商品id orderId:''//外部扫码进入的商品id
}, },
mutations: { mutations: {
// 设置是否授权
setAuthorization(state,data){
state.authorization=data;
},
// 设置用户信息
setUserInfo(state,userInfo){
state.userInfo=userInfo;
},
// 设置扫码进入的商品id // 设置扫码进入的商品id
setOrderId(state, orderId) { setOrderId(state, orderId) {
state.orderId = orderId; state.orderId = orderId;
} }
}, },
getters,
actions: {} actions: {}
}) })
export default store export default store
\ No newline at end of file
const menu = {
state: {
menuAllInfo: null, //菜单信息
},
mutations: {
// 设置用户菜单信息
setMenuAllInfo(state, menuAllInfo) {
state.menuAllInfo = menuAllInfo;
},
},
actions: {
}
}
export default menu
const user = {
state: {
userInfo: null, //授权信息
Authorization: null, //是否授权
},
mutations: {
// 设置是否授权
setAuthorization(state, data) {
console.log(data,665)
state.Authorization = data;
},
// 设置用户信息
setUserInfo(state, userInfo) {
state.userInfo = userInfo;
},
},
actions: {
}
}
export default user
import Vue from 'vue'
export const $EventBus = new Vue();
\ No newline at end of file
import Config from '../static/config/index.js'; import Config from '../static/config/index.js';
import WXBizDataCrypt from "./WXBizDataCrypt.js"; import WXBizDataCrypt from "./WXBizDataCrypt.js";
uni.getLocation({ import {$EventBus} from './EventBus'
type: 'gcj02', //返回可以用于uni.openLocation的经纬度
success: function (res) {
console.log(res, 'res')
var params = {
lat: res.latitude,
lng: res.longitude
}
// getAddress(params).then(res => {
// console.log(res, 'aaa')
// })
}
})
export default{ export default{
uniLogin() { getallNum(arrA) {
uni.login({ let arrA1 = [{
provider: 'weixin', "goodsId": 100,
success: function (loginRes) { "name": "冰茶咖啡",
console.log(loginRes) "price": 29.9,
"discount": 19.9,
}}) flag: true,
}, thumbnail: 'https://img2.baidu.com/it/u=1001625387,3275765924&fm=26&fmt=auto&gp=0.jpg',
async uniGetUserInfo(e) { num: 1,
uni.getUserProfile({ //授权后可以通过uni.getUserProfile得到用户信息 rules: [{
lang: "zh_CN", "specId": 1,
desc: '用于完善用户信息', "specName": "温度",
success: res => { "ruleId": 1,
let avatarUrl = res.userInfo.avatarUrl; "ruleName": "常温"
let nickName = res.userInfo.nickName; },
console.log(res) {
this.uniLogin(); "specId": 2,
"specName": "糖度",
"ruleId": 3,
"ruleName": "无糖"
}
]
},
{
"goodsId": 101,
"name": "冰茶咖啡",
"price": 29.9,
"discount": 19.9,
flag: true,
thumbnail: 'https://img2.baidu.com/it/u=1001625387,3275765924&fm=26&fmt=auto&gp=0.jpg',
num: 3,
rules: [{
"specId": 1,
"specName": "温度",
"ruleId": 1,
"ruleName": "常温"
},
{
"specId": 2,
"specName": "糖度",
"ruleId": 3,
"ruleName": "无糖"
}
]
} }
}) ]
}, // arrA = []
getLocation() { let shopCarInfo = uni.getStorageSync('shopCarInfo');
uni.getLocation({ let shopCarInfoAdd = []
type: 'gcj02', //返回可以用于uni.openLocation的经纬度 console.log(shopCarInfo, 55553333)
success: function (res) { if (shopCarInfo) {
console.log(res, 'res') let ids = shopCarInfo.map(v => v.goodsId);
var params = { console.log(ids, 444)
lat: res.latitude, shopCarInfo.forEach((item) => {
lng: res.longitude arrA.forEach((preItem, index) => {
} console.log(ids.includes(preItem.goodsId), 1234567)
getAddress(params).then(res => { let ind = ids.indexOf(preItem.goodsId);
console.log(res, 'aaa') if (ind > -1) {
}) let num = 0;
} for (let i = 0; i < item.rules.length; i++) {
}) let it = item.rules[i];
for (let r = 0; r < preItem.rules.length; r++) {
let ry = preItem.rules[r];
if (ry.specId == it.specId && ry.ruleId == it.ruleId) {
num += 1;
}
}
}
if (num == item.rules.length) {
item.num += 1
} else {
shopCarInfoAdd.push(preItem)
}
} else {
shopCarInfoAdd.push(preItem);
arrA.splice(index, 1)
}
});
})
} else {
shopCarInfo = arrA
}
shopCarInfo = [...shopCarInfo, ...shopCarInfoAdd];
uni.setStorageSync('shopCarInfo', shopCarInfo);
$EventBus.$emit('updateCar');
// this.goods = uni.getStorageSync('shopCarInfo');
}, },
// 解密手机 // 解密手机
async onGetPhoneNumber(appid,session_key,encryptedData,iv) { async onGetPhoneNumber(appid,session_key,encryptedData,iv) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment