获取token
This commit is contained in:
		
							parent
							
								
									aa8b768eb6
								
							
						
					
					
						commit
						6b1ac948fd
					
				| @ -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 { | ||||
|  | ||||
| @ -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) | ||||
|     }, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user