获取token

This commit is contained in:
jqtmviyu@gmail.com 2021-03-06 13:03:33 +08:00
parent aa8b768eb6
commit 6b1ac948fd
2 changed files with 36 additions and 7 deletions

View File

@ -2,14 +2,14 @@
// url不用写完整的, 已经在http封装配置好了 // url不用写完整的, 已经在http封装配置好了
// 首页轮播图 // 首页轮播图
const SWIPERDATA_URL = "/home/swiperdata" const SWIPERDATA_URL = '/home/swiperdata'
// 首页导航 // 首页导航
const CATITEMS_URL = "/home/catitems" const CATITEMS_URL = '/home/catitems'
// 首页楼层 // 首页楼层
const FLOORDATA_URL = "/home/floordata" const FLOORDATA_URL = '/home/floordata'
// 分类 // 分类
const CATEGORIES_URL = "/categories" const CATEGORIES_URL = '/categories'
// 商品页列表搜索 // 商品页列表搜索
const GOODS_SEARCH_URL = '/goods/search' const GOODS_SEARCH_URL = '/goods/search'
@ -17,9 +17,11 @@ const GOODS_SEARCH_URL = '/goods/search'
// 商品详情搜索 // 商品详情搜索
const GOODS_DETAIOL_URL = '/goods/detail' const GOODS_DETAIOL_URL = '/goods/detail'
// 获取用户token
const USERS_WXLOGIN_URL = '/users/wxlogin'
// 此处第二个参数vm就是我们在页面使用的this你可以通过vm获取vuex等操作 // 此处第二个参数vm就是我们在页面使用的this你可以通过vm获取vuex等操作
const install = (Vue, vm) => { const install = (Vue, vm) => {
// 首页 // 首页
const getSwiperdata = (params = {}) => vm.$u.get(SWIPERDATA_URL, params) const getSwiperdata = (params = {}) => vm.$u.get(SWIPERDATA_URL, params)
const getCatitems = (params = {}) => vm.$u.get(CATITEMS_URL, params) const getCatitems = (params = {}) => vm.$u.get(CATITEMS_URL, params)
@ -35,7 +37,15 @@ const install = (Vue, vm) => {
const getGoodsDetail = (params = {}) => vm.$u.get(GOODS_DETAIOL_URL, params) const getGoodsDetail = (params = {}) => vm.$u.get(GOODS_DETAIOL_URL, params)
// 将各个定义的接口名称统一放进对象挂载到vm.$u.api(因为vm就是this也即this.$u.api)下 // 将各个定义的接口名称统一放进对象挂载到vm.$u.api(因为vm就是this也即this.$u.api)下
vm.$u.api = { getSwiperdata, getCatitems, getFloordata, getCategories, getGoods, getGoodsDetail } vm.$u.api = {
getSwiperdata,
getCatitems,
getFloordata,
getCategories,
getGoods,
getGoodsDetail,
getUserToken: (params = {}) => vm.$u.post(USERS_WXLOGIN_URL, params)
}
} }
export default { export default {

View File

@ -98,7 +98,26 @@ export default {
this.userInfo = uni.getStorageSync("userInfo") || {} this.userInfo = uni.getStorageSync("userInfo") || {}
}, },
methods: { methods: {
getUserInfo({detail: {userInfo}}){ async getUserInfo({detail: {userInfo, encryptedData, rawData, iv, signature}}){
// uni.logincode()
const [,{code}] = await uni.login()
const res = await this.$u.api.getUserToken({
encryptedData,
rawData,
iv,
signature,
code
})
// message,
if(res.message){
uni.setStorageSync("token", res.message.token)
}else{
uni.showToast({
title: "登录失败, 请重试",
icon: "none"
})
return
}
this.userInfo = userInfo this.userInfo = userInfo
uni.setStorageSync("userInfo",userInfo) uni.setStorageSync("userInfo",userInfo)
}, },