Bläddra i källkod

多租户登录

hmp 4 år sedan
förälder
incheckning
bff97f27e5
8 ändrade filer med 132 tillägg och 54 borttagningar
  1. 11 1
      assets/http/api.js
  2. 50 13
      assets/http/service.js
  3. 24 0
      main.js
  4. 1 0
      pages.json
  5. 3 12
      pages/index/index.vue
  6. 41 26
      pages/login/account-login.vue
  7. 1 1
      pages/stores/stores.vue
  8. 1 1
      utils/mixin.js

+ 11 - 1
assets/http/api.js

@@ -1,6 +1,11 @@
 import {http} from './service.js'
 
 
+const header={
+	'Authorization': "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
+	'Content-Type': 'application/x-www-form-urlencoded',
+}
+
 //文件上传
 const uploadFile =(p)=> http.upload('blade-resource/oss/endpoint/put-file', {name:'file',filePath:p})
 
@@ -9,13 +14,17 @@ const sms={
 	send:p => http.post('/sms/sendSms'+p)
 }
 
+const system={
+	login:p => http.post('blade-auth/oauth/token',p,{header}),
+}
+
 
 //商场管理
 const mall={
 	login:p => http.post('/shop/account/login',p),
 	detail:p => http.get("ldt_mall/mall/detail", {params:p}),
 	submit:p => http.post("ldt_mall/mall/submit", p),
-	getListByPhone:p => http.get("/ldt_mall/mall/list", {params:p}),
+	list:p => http.get("/ldt_mall/mall/list", {params:p}),
 	memberInfo:p => http.get('mall/memberInfo',{params:p}),
 	getShopList:p => http.get("shop/list",{params:p}),
 	statistic:p => http.get("mall/statistic?mallId="+p),
@@ -93,6 +102,7 @@ const flow={
 export const api={
 	uploadFile,
 	sms,
+	system,
 	mall,
 	appaccount,
 	member,

+ 50 - 13
assets/http/service.js

@@ -1,12 +1,9 @@
 import Request from 'luch-request'
 
-let baseURL = 'http://192.168.1.48:2366/'
-// let baseURL = 'http://192.168.1.244:1888/api/'
 // let baseURL = 'https://ldt.guosen-fumao.cn/api/'
+let baseURL = 'http://192.168.1.168:2366/'
 
-
-let tokenUrl = baseURL +
-	"blade-auth/oauth/token?tenantId=000000&password=21232f297a57a5a743894a0e4a801fc3&username=admin"
+let refreshTokenUrl = baseURL + "blade-auth/oauth/token"
 
 //获取token
 const getTokenStorage = () => {
@@ -16,6 +13,20 @@ const getTokenStorage = () => {
 	} catch (e) {}
 	return token
 }
+//缓存token信息
+const cacheToken = (data) => {
+	let tokenInfo = {
+		accessToken: data.access_token,
+		//提前200秒过期
+		expiresIn: new Date().getTime() + (data.expires_in - 200) * 1000,
+		refreshToken: data.refresh_token,
+		tenantId: data.tenant_id,
+		nickName: data.nick_name
+	}
+	uni.setStorageSync('token', data.access_token)
+	uni.setStorageSync('tokenInfo', tokenInfo)
+}
+
 const http = new Request()
 http.setConfig((config) => {
 	/* 设置全局配置 */
@@ -25,10 +36,12 @@ http.setConfig((config) => {
 	}
 	return config
 })
+
 http.interceptors.request.use((config) => {
 	/* 请求之前拦截器。可以使用async await 做异步操作 */
 	config.header = {
 		...config.header,
+		Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
 		"Blade-Auth": getTokenStorage()
 	}
 	return config
@@ -42,9 +55,8 @@ let requests = []
 http.interceptors.response.use(async (response) => {
 	/* 请求之后拦截器。可以使用async await 做异步操作  */
 	//toekn过期处理 
-
 	//200 返回数据成功 0
-	if (response.data.code != 200 && response.data.code !=400) {
+	if (response.data.code != 200 && response.data.code != 400 && response.data.access_token==null) {
 		return Promise.reject(response)
 	}
 	return response.data
@@ -55,17 +67,27 @@ http.interceptors.response.use(async (response) => {
 		} = err
 		if (!isRefreshing) {
 			isRefreshing = true
-			let token = uni.getStorageSync('token')
+
+			let tokenInfo = uni.getStorageSync('tokenInfo')
+
 			let [, res] = await uni.request({
-				url: tokenUrl,
+				url: refreshTokenUrl,
 				method: 'POST',
 				header: {
-					Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0"
+					'Authorization': "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
+					'Content-Type': 'application/x-www-form-urlencoded',
+					'Tenant-Id': tokenInfo.tenantId
 				},
+				data: {
+					'tenantId': tokenInfo.tenantId,
+					'refresh_token': tokenInfo.refreshToken,
+					'grant_type': "refresh_token",
+					'scope': "all",
+				}
 			})
 			//否则保存新的token
-			token = res.data.token_type + " " + res.data.access_token
-			uni.setStorageSync('token', token)
+			cacheToken(res.data)
+
 			requests.forEach(cb => cb())
 			// 重试完了清空这个队列
 			requests = []
@@ -80,7 +102,22 @@ http.interceptors.response.use(async (response) => {
 			})
 		}
 	}
-	return Promise.reject(err)
+	if (err.data.code == 500) {
+		uni.showToast({
+			title: err.data.msg,
+			icon: "none"
+		})
+		return Promise.reject(err)
+	}
+	if (!err.data.success) {
+		uni.showToast({
+			title: err.data.msg,
+			icon: "none"
+		})
+		return err.data
+	} else {
+		return err.data
+	}
 })
 export {
 	http

+ 24 - 0
main.js

@@ -93,6 +93,30 @@ Vue.prototype.$isEmpty=function(value){
 	return false;
 }
 
+Vue.prototype.$isNotEmpty=function(value){
+	switch (typeof value) {
+		case 'undefined':
+			return false;
+		case 'string':
+			if(value=='undefined') return false
+			if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return false;
+			break;
+		case 'boolean':
+			if (!value) return false;
+			break;
+		case 'number':
+			if (0 === value || isNaN(value)) return false;
+			break;
+		case 'object':
+			if (null === value || value.length === 0) return false;
+			for (var i in value) {
+				return true;
+			}
+			return false;
+	}
+	return true;
+}
+
 
 Vue.config.productionTip = false
 

+ 1 - 0
pages.json

@@ -19,6 +19,7 @@
 		}, {
 			"path": "pages/member-center/member-center",
 			"style": {
+				"navigationStyle":"custom",
 				"navigationBarTitleText": "会员中心",
 				"enablePullDownRefresh": false
 			}

+ 3 - 12
pages/index/index.vue

@@ -4,10 +4,8 @@
 			<image src="../../static/icon/bg.png"  style="width: 100vw;height: 400rpx"></image>
 			<view class="card-top" >
 				<view class="top">
-					<view @click="mallShow=true">
+					<view >
 						<text class="name" >{{mallLable}}</text>
-						<u-icon v-if="mallShow" name="arrow-up-fill" style="margin-left: 10rpx;"></u-icon>
-						<u-icon v-else name="arrow-down-fill" style="margin-left: 10rpx;"></u-icon>
 					</view>
 					<view class="btn">
 						<view class="image">
@@ -150,11 +148,6 @@
 						name:'商家管理',
 						path:'/pages/stores/stores'
 					},
-					{
-						icon:'/static/icon/password.png',
-						name:'修改密码',
-						path:'/pages/login/updatePassword'
-					},
 				]
 			}
 		},
@@ -170,8 +163,7 @@
 				})
 			},
 			async init(){
-				let phone=this.$cache.get('phone')
-				let res=await this.$api.mall.getListByPhone({personTel:phone})
+				let res=await this.$api.mall.list()
 				this.mallList=res.data.records
 				this.mallId=this.mallList[0].id
 				this.mallLable=this.mallList[0].mallName
@@ -195,13 +187,12 @@
 				this.cacheSelectedMall(item[0])
 				this.getStatistic()
 			},
-			
-			
 			cacheSelectedMall(item){
 				let selectedMall={
 					id:this.mallId,
 					label:this.mallLable
 				}
+				this.$cache.put('phone',item.personTel)
 				this.$cache.put("selectedMall", Object.assign(item, selectedMall))
 				this.$u.vuex('vuex_mallId',this.mallId)
 			}

+ 41 - 26
pages/login/account-login.vue

@@ -11,24 +11,22 @@
 			
 			<view  style="width: 76%;margin-top: 30rpx;">
 				<u-form :model="form" :error-type="['message']"  ref="uForm">
-					<u-form-item  label-position="top" label="账号" prop="phone" label-width="150">
-						<u-input :border="false" placeholder="请输入账号" v-model="form.phone" type="number"></u-input>
+					<u-form-item  label="商编"  prop="tenantId" label-width="150">
+						<u-input :border="false" placeholder="请输入商编" v-model="form.tenantId" ></u-input>
 					</u-form-item>
-					<u-form-item   label-position="top" label="密码" prop="password" label-width="150">
-						<u-input :border="false" placeholder="请输入登录密码" type="password" v-model="form.password"></u-input>
+					<u-form-item  label="账号" prop="username" label-width="150">
+						<u-input :border="false" placeholder="请输入账号" v-model="form.username" ></u-input>
+					</u-form-item>
+					<u-form-item   label="密码" prop="password" label-width="150">
+						<u-input :border="false" placeholder="请输入密码" type="password" v-model="form.password"></u-input>
 					</u-form-item>
 				</u-form>
-				<view  class="text-right margin-top-10 text-sm">
-					<text @click="$jump('/pages/login/forget')" style="text-decoration: underline;">忘记密码</text>
-					<text @click="$jump('/pages/login/login')" style="margin-left: 20rpx;text-decoration: underline;">手机号快捷登录</text>
-				</view>
 			</view>
 			
 			<view  style="width: 86%;margin-top: 120rpx;">
 				<view @click="login" class="btn cu-btn round" style="width:100%;height: 90rpx;font-size: 36rpx;">
 					立即登录
 				</view>
-			
 			</view>
 		</view>
 	</view>
@@ -44,11 +42,19 @@
 					'color':'#ffffff'
 				},
 				form:{
-					phone:'',
+					tenantId:'',
+					username:'',
 					password:''
 				},
 				rules: {
-					phone: [
+					tenantId: [
+						{ 
+							required: true, 
+							message: '请输入商编', 
+							trigger: ['change','blur'],
+						}
+					],
+					username: [
 						{ 
 							required: true, 
 							message: '请输入账号', 
@@ -69,7 +75,7 @@
 			this.$refs.uForm.setRules(this.rules);
 		},
 		onLoad() {
-			 if (this.$cache.get('phone')) {
+			 if (this.$cache.get('token')) {
 			 	uni.reLaunch({
 			 		url:"/pages/index/index"
 			 	})
@@ -88,22 +94,31 @@
 			},
 			async doLogin(){
 				let params={
-					type: "MALL",
-					phone:this.form.phone,
-					secret: md5Libs.md5(this.form.password)
+					tenantId:this.form.tenantId,
+					username:this.form.username,
+					password:md5Libs.md5(this.form.password)
 				}
-				try{
-					let res=await this.$api.appaccount.login(params)
-					this.$cache.put('phone',this.form.phone)
-					uni.reLaunch({
-						url:"/pages/index/index"
-					})
-				}catch(e){
-					this.$u.toast('账号或者密码错误!')
-				}finally{
-					uni.hideLoading()
+				this.$api.system.login(params).then(res=>{
+					if (this.$isNotEmpty(res.access_token)) {
+						this.cacheToken(res)
+						uni.redirectTo({
+							url:"../index/index"
+						})
+					}
+				})
+			},
+			cacheToken(data){
+				let tokenInfo={
+					accessToken:data.access_token,
+					//提前200秒过期
+					expiresIn:new Date().getTime() + (data.expires_in - 200) * 1000,
+					refreshToken:data.refresh_token,
+					tenantId:data.tenant_id,
+					nickName:data.nick_name
 				}
-			}
+				this.$cache.put('token',data.access_token)
+				this.$cache.put("tokenInfo",tokenInfo)
+			},
 		}
 	}
 </script>

+ 1 - 1
pages/stores/stores.vue

@@ -8,7 +8,7 @@
 			</view>
 		</u-navbar>
 
-		<view class="bg-white ">
+		<view class="bg-white " v-if="tagsList.length!=1">
 			<scroll-view scroll-x class="bg-white nav">
 				<view class="flex text-center">
 					<view class="cu-item flex-sub" :class="index==current?'text-base cur text-xl text-bold ':'text-lg'"

+ 1 - 1
utils/mixin.js

@@ -33,7 +33,7 @@ let page = {
 			'pages/test/test',
 		]
 		if (!WHiTE_LIST.includes(currentPage)) {
-			if (!this.$cache.get('phone')) {
+			if (!this.$cache.get('token')) {
 				uni.reLaunch({
 					url: "/pages/login/login"
 				})