Browse Source

微信一键登录

shish 4 years ago
parent
commit
1ca558ed27
3 changed files with 173 additions and 145 deletions
  1. 168 9
      ghsApp/src/admin/home/login.vue
  2. 5 0
      ghsApp/src/api/auth/index.js
  3. 0 136
      ghsApp/src/components/login-model.vue

+ 168 - 9
ghsApp/src/admin/home/login.vue

@@ -1,30 +1,105 @@
 <template>
 	<view class="app-main app-content">
-		<loginModel @comfirm="loginFun" @mobileLogin="mobileLogin"></loginModel>
+		<view class="model">
+			<view class="experFrame">
+			<view>
+				<view style="text-align:center;margin-bottom:20upx;">
+				<image :src="`${constant.imgUrl}/logo3.png`" class="logo" mode="widthFix"></image> 
+				</view>
+				<view style="color:#666666;text-align:center;margin-bottom:50upx;font-size:35upx;">销花宝</view>
+				<view>
+				<input @focus="mobile=''" v-model="mobile" class="experInput" placeholder="手机号" type="number" />
+				</view>
+				<view>
+				<input @focus="password=''" v-model="password" class="experInput" placeholder="密码" type="password" />
+				</view>
+				<view style="margin-top:50upx;">
+				<button class="admin-button-com small default buttonText" @click="loginFun">登陆</button>
+				</view>
+				<!-- #ifdef MP-WEIXIN -->
+				<view style="margin-top:35upx;">
+				<button class="admin-button-com small buttonText" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin"
+				style="background-color:#2BA245;border-color:#2BA245;color:white;">微信手机号一键登录</button>
+				</view>
+				<!-- #endif -->
+				<!-- #ifdef APP-PLUS -->
+				<view style="margin-top:35upx;">
+				<button class="admin-button-com small blue buttonText" @click="mobileLogin()">本机号码一键登录</button>
+				</view>
+				<!-- #endif -->
+				<view style="margin-top:60upx;color:#999999;width:88%;font-size:30upx;">
+				<text style="float:right;" @click="pageTo({url: '/pagesClient/official/apply',type:2})">注册帐号</text>
+				</view>
+			</view>
+			</view>
+		</view>
 	</view>
 </template>
 <script>
 import { mapActions } from "vuex";
 import { getUser} from "@/utils/auth";
-import loginModel from "@/components/login-model";
 import {appLogin} from '@/api/common'
 import { getDictionaries } from '@/api/mini'
 import { replaceClientId } from '@/api/admin'
-import { phoneLogin } from "@/api/auth";
+import { phoneLogin,wxMobileLogin } from "@/api/auth";
 export default {
 	name: "login",
-	components: {
-		loginModel
-	},
+	components: {},
 	mixins: [],
 	data() {
 		return {
-		};
+			mobile:'',
+			password: '',
+		}
 	},
 	computed: {
 	},
 	methods: {
 		...mapActions(['setUserShopAll']),
+		wxPhoneLogin(e){
+			let that = this
+			let currentMiniOpenId = uni.getStorageSync('currentMiniOpenId')
+			if (e.detail.errMsg === "getPhoneNumber:ok") {
+				wxMobileLogin({ iv: e.detail.iv, encryptedData: e.detail.encryptedData,miniOpenId:currentMiniOpenId }).then(subRes => {
+					let token = subRes.data.token || ''
+					uni.hideLoading()
+					if (that.$util.isEmpty(token)) {
+						that.$msg('请先注册')
+						return false
+					}
+					//有几个地方涉及登陆,需要统一方法,请搜索关键词 uni_login
+					if(subRes.code == 1){
+						if (that.$util.isEmpty(subRes)) {
+							return false
+						}
+						uni.setStorageSync('token', subRes.data.token)
+						uni.setStorageSync('shopId', subRes.data.admin.currentShopId)
+						getDictionaries({ token: subRes.data.token }).then(res => {
+							if (that.$util.isEmpty(res)) {
+								return false
+							}
+							that.$store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data })
+							that.$store.commit('setMyShopInfo', { ...res.data.shop, ...res.data })
+						})
+						that.$store.commit('setLoginInfo', { ...subRes.data.admin, ...subRes.data })
+						getUser(true).then(res => { uni.setNavigationBarTitle({ title: res.name }) })
+						that.setUserShopAll()
+						// #ifdef APP-PLUS
+						//保存安卓的clientId
+						let hasClientId = !that.$util.isEmpty(subRes.data.admin.clientId) ? subRes.data.admin.clientId : ''
+						const clientInfo = plus.push.getClientInfo()
+						let currentClientId = !that.$util.isEmpty(clientInfo.clientid) ? clientInfo.clientid : ''
+						if(hasClientId == '' || hasClientId != currentClientId){
+							replaceClientId({id:currentClientId})
+						}
+						// #endif
+						uni.navigateBack()
+					}
+				});
+			} else {
+				this.$msg("登录失败");
+			}
+		},
 		mobileLogin(){
 			let that = this
 			uni.showLoading({mask:true})
@@ -35,7 +110,8 @@ export default {
 						provider: 'univerify',
 						// 自定义登录框样式
 						univerifyStyle: {"fullScreen": true},
-						success:(res)=>{ // 登录成功
+						// 登录成功
+						success:(res)=>{
 							let openid = res.authResult.openid
 							let access_token = res.authResult.access_token
 							phoneLogin({openid,access_token}).then(subRes=>{
@@ -96,8 +172,22 @@ export default {
 				}
 			})
 		},
-		loginFun(mobile,password){
+		loginFun(){
 			let that = this
+			let mobile = this.mobile
+			let password = this.password
+			if (that.$util.isEmpty(mobile)) {
+				this.$msg('请输入手机号')
+				return false
+			}
+			if(this.$util.checkMobile(mobile) == false){
+				this.$msg('请输入正确手机号')
+				return false
+			}
+			if (that.$util.isEmpty(password)) {
+				this.$msg('请输入密码')
+				return false
+			}
 			uni.showLoading({mask:true})
 			//有几个地方涉及登陆,需要统一方法,请搜索关键词 uni_login
 			appLogin({mobile,password}).then(subRes=>{
@@ -136,4 +226,73 @@ export default {
 };
 </script>
 <style lang="scss" scoped>
+.model {
+  position: fixed;
+  z-index: 9999;
+  top: 0;
+  left: 0;
+  width: 100vw;
+  height: 100vh;
+  color: #fff;
+  overflow: hidden;
+  text-align: center;
+  & > .noData_img {
+    width: 318upx;
+    height: 318upx;
+    margin: 332upx auto 0;
+  }
+  & > .no_data_text {
+    font-size: 32upx;
+    color: #ffffff;
+    text-align: center;
+    margin: 50upx 0;
+  }
+  & > .freeApply {
+    width: 376upx;
+    height: 88upx;
+    line-height: 88upx;
+    background: linear-gradient(90deg, #3385ff, #33b0ff);
+    border-radius: 44upx;
+    margin: 0 auto;
+    font-size: 32upx;
+    color: #ffffff;
+    text-align: center;
+  }
+  &>.experience{
+    margin-top: 40upx;
+    color: #fff;
+    font-size: 30upx;
+  }
+  &>.experFrame{
+      margin-top: 90upx;
+      height: 50upx;
+      .experInput{
+        height: 80upx;
+        width: 80%;
+        margin-bottom:38upx;
+        display: inline-block;
+        vertical-align: middle;
+        background-color: #f5f5f5;
+        border:none;
+        border-bottom: 1px solid #CCCCCC;
+        color: #666;
+        padding-left: 20upx;
+        box-sizing: border-box;
+        text-align: left;
+      }
+      .buttonText{
+        display: inline-block;
+        line-height:92upx;
+        padding: 0 20upx;
+        vertical-align: middle;
+        font-size: 32upx;
+        width: 80%;
+        height:90upx;
+      }
+      .logo{
+        width:180upx;
+        margin:0 auto;
+      }
+  }
+}
 </style>

+ 5 - 0
ghsApp/src/api/auth/index.js

@@ -4,3 +4,8 @@ import https from '@/plugins/luch-request_0.0.7/request'
 export const phoneLogin = data => {
 	return https.get('/auth/phone-login', data)
 }
+
+//微信手机号一键登录 shish 20210121
+export const wxMobileLogin = data => { 
+	return	https.post("/auth/wx-mobile-login", data) 
+}

+ 0 - 136
ghsApp/src/components/login-model.vue

@@ -1,136 +0,0 @@
-<template>
-  <view class="model" v-if="show">
-    <view class="experFrame">
-      <view>
-        <view style="text-align:center;margin-bottom:20upx;">
-          <image :src="`${constant.imgUrl}/logo3.png`" class="logo" mode="widthFix"></image> 
-        </view>
-        <view style="color:#666666;text-align:center;margin-bottom:50upx;font-size:35upx;">销花宝</view>
-        <view>
-          <input @focus="mobile=''" v-model="mobile" class="experInput" placeholder="手机号" type="number" />
-        </view>
-        <view>
-          <input @focus="password=''" v-model="password" class="experInput" placeholder="密码" type="password" />
-        </view>
-        <view style="margin-top:50upx;">
-        <button class="admin-button-com small default buttonText" @click="loginClick">登陆</button>
-        </view>
-        <!-- #ifdef MP-WEIXIN -->
-        <view style="margin-top:35upx;">
-          <button class="admin-button-com small buttonText" style="background-color:#2BA245;border-color:#2BA245;color:white;" @click="wxMobileLogin()">微信手机号一键登录</button>
-        </view>
-        <!-- #endif -->
-        <!-- #ifdef APP-PLUS -->
-        <view style="margin-top:35upx;">
-          <button class="admin-button-com small blue buttonText" @click="mobileLogin">本机号码一键登录</button>
-        </view>
-        <!-- #endif -->
-        <view style="margin-top:60upx;color:#999999;width:88%;font-size:30upx;">
-          <text style="float:right;" @click="pageTo({url: '/pagesClient/official/apply',type:2})">注册帐号</text>
-        </view>
-
-      </view>
-    </view>
-  </view>
-</template>
-<script>
-export default {
-  props: {
-    show: {
-      type: Boolean,
-      default: true,
-    }
-  },
-  watch: {
-  },
-  data() {
-    return {
-      password: '',
-      mobile:''
-    };
-  },
-  methods: {
-    loginClick() {
-      this.$emit('comfirm', this.mobile,this.password)
-    },
-    mobileLogin(){
-      this.$emit('mobileLogin')
-    },
-    wxMobileLogin(){
-      
-    }
-  },
-};
-</script>
-
-<style lang="scss" scoped>
-.model {
-  position: fixed;
-  z-index: 9999;
-  top: 0;
-  left: 0;
-  width: 100vw;
-  height: 100vh;
-  color: #fff;
-  overflow: hidden;
-  text-align: center;
-  & > .noData_img {
-    width: 318upx;
-    height: 318upx;
-    margin: 332upx auto 0;
-  }
-  & > .no_data_text {
-    font-size: 32upx;
-    color: #ffffff;
-    text-align: center;
-    margin: 50upx 0;
-  }
-  & > .freeApply {
-    width: 376upx;
-    height: 88upx;
-    line-height: 88upx;
-    background: linear-gradient(90deg, #3385ff, #33b0ff);
-    border-radius: 44upx;
-    margin: 0 auto;
-    font-size: 32upx;
-    color: #ffffff;
-    text-align: center;
-  }
-  &>.experience{
-    margin-top: 40upx;
-    color: #fff;
-    font-size: 30upx;
-  }
-  &>.experFrame{
-      margin-top: 90upx;
-      height: 50upx;
-      .experInput{
-        height: 80upx;
-        width: 80%;
-        margin-bottom:38upx;
-        display: inline-block;
-        vertical-align: middle;
-        background-color: #f5f5f5;
-        border:none;
-        border-bottom: 1px solid #CCCCCC;
-        color: #666;
-        padding-left: 20upx;
-        box-sizing: border-box;
-        text-align: left;
-      }
-      .buttonText{
-        display: inline-block;
-        line-height:92upx;
-        padding: 0 20upx;
-        vertical-align: middle;
-        font-size: 32upx;
-        width: 80%;
-        height:90upx;
-      }
-      .logo{
-        width:180upx;
-        margin:0 auto;
-      }
-  }
-}
-</style>