From 6b1ac948fd993613f238ad706286801099163d5a Mon Sep 17 00:00:00 2001 From: "jqtmviyu@gmail.com" Date: Sat, 6 Mar 2021 13:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/http.api.js | 22 ++++++++++++++++------ src/pages/my/my.vue | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/common/http.api.js b/src/common/http.api.js index eff1dc1..eff85a2 100644 --- a/src/common/http.api.js +++ b/src/common/http.api.js @@ -2,14 +2,14 @@ // 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' @@ -17,9 +17,11 @@ const GOODS_SEARCH_URL = '/goods/search' // 商品详情搜索 const GOODS_DETAIOL_URL = '/goods/detail' +// 获取用户token +const USERS_WXLOGIN_URL = '/users/wxlogin' + // 此处第二个参数vm,就是我们在页面使用的this,你可以通过vm获取vuex等操作 const install = (Vue, vm) => { - // 首页 const getSwiperdata = (params = {}) => vm.$u.get(SWIPERDATA_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) // 将各个定义的接口名称,统一放进对象挂载到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 { diff --git a/src/pages/my/my.vue b/src/pages/my/my.vue index bb69f30..de414c2 100644 --- a/src/pages/my/my.vue +++ b/src/pages/my/my.vue @@ -98,7 +98,26 @@ export default { this.userInfo = uni.getStorageSync("userInfo") || {} }, methods: { - getUserInfo({detail: {userInfo}}){ + async getUserInfo({detail: {userInfo, encryptedData, rawData, iv, signature}}){ + // 通过uni.login拿到code(验证码) + 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 uni.setStorageSync("userInfo",userInfo) },