Ver Fonte

微信登录

zhanghui há 1 ano atrás
pai
commit
9f4f04613a
2 ficheiros alterados com 82 adições e 9 exclusões
  1. 2 2
      main.js
  2. 80 7
      pages/users/wechat_login/index.vue

+ 2 - 2
main.js

@@ -18,7 +18,7 @@ import pageLoading from './components/pageLoading.vue'
 import skeleton from './components/skeleton/index.vue'
 
 Vue.component('skeleton', skeleton)
-Vue.component('pageLoading',pageLoading) 
+Vue.component('pageLoading',pageLoading)
 
 /* uni.scss */
 import uView from '@/uni_modules/uview-ui'
@@ -64,4 +64,4 @@ const app = new Vue({
 	Cache,
 	i18n,
 })
-app.$mount();
+app.$mount();

+ 80 - 7
pages/users/wechat_login/index.vue

@@ -18,20 +18,24 @@
 			</view>
 			<view class="btn-wrapper">
 				<!-- #ifdef H5 -->
-				<button hover-class="none" @click="wechatLogin" class="bg-green btn1">{{ $t(`微信登录`) }}</button>
+<!--				<button hover-class="none" @click="wechatLogin" class="bg-green btn1">{{ $t(`微信登录`) }}</button>-->
 				<!-- #endif -->
 				<!-- #ifdef MP -->
 				<!-- <button v-if="canUseGetUserProfile && code" hover-class="none" @tap="getUserProfile"
 					class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button> -->
-				<!-- <button v-if="canUseGetUserProfile && code" hover-class="none" 
+				<!-- <button v-if="canUseGetUserProfile && code" hover-class="none"
 				open-type="getPhoneNumber" @getphonenumber="getphonenumber"
 					class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button> -->
-				<button v-if="canUseGetUserProfile && code" open-type="getPhoneNumber" hover-class="none"
-					@getphonenumber="getPhoneCode" class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button>
-				<button v-else hover-class="none" open-type="getUserInfo" @getuserinfo="setUserInfo"
-					class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button>
+<!--				<button v-if="canUseGetUserProfile && code" open-type="getPhoneNumber" hover-class="none"-->
+<!--					@getphonenumber="getPhoneCode" class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button>-->
+
+				<button  open-type="getPhoneNumber" hover-class="none"
+						@getphonenumber="getPhoneNumber" class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button>
+
+<!--				<button v-else hover-class="none" open-type="getUserInfo" @getuserinfo="setUserInfo"-->
+<!--					class="bg-green btn1">{{ $t(`微信用户一键登录`) }}</button>-->
 				<!-- #endif -->
-				<button hover-class="none" @click="isUp = true" class="btn2">{{ $t(`账号密码登录`) }}</button>
+<!--				<button hover-class="none" @click="isUp = true" class="btn2">{{ $t(`账号密码登录`) }}</button>-->
 			</view>
 		</view>
 		<block v-if="isUp">
@@ -185,6 +189,74 @@ export default {
 				this.isPhoneBox = false;
 			}
 		},
+
+		getPhoneNumber(e) {
+			let _this =this;
+			console.log(e)
+			if (e.detail.errMsg === 'getPhoneNumber:fail user deny' || !e.detail.code){
+				console.log("用户拒绝获取手机号");
+				return
+			}
+			uni.showLoading({
+				title: this.$t(`正在登录中`)
+			});
+			let data = {
+				code: '',
+				getPhoneNumberCode: ''
+			}
+			data.getPhoneNumberCode = e.detail.code
+			uni.login({
+				"provider": "weixin",
+				"onlyAuthorize": true, // 微信登录仅请求授权认证
+				success: function(event) {
+					console.log(event.code)
+					data.code = event.code
+					console.log(data)
+					//客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
+					postLoginAuth(data).then(res => {
+						if (res.code === 200) {
+							_this.$store.commit('LOGIN', {
+								token: res.data.access_token,
+								time: res.data.expires_in
+							});
+							_this.$store.commit('OPENID', res.data.openId);
+							_this.getUserInfo();
+							// 登录成功,跳转首页
+							uni.showToast({
+								title: "登录成功,即将跳转首页",
+								icon: 'none',
+								duration: 2000
+							});
+							setTimeout(() => {
+								// 自动登录失败时,跳转用户注册
+								uni.switchTab({
+									url: '/pages/index/index'
+								})
+							}, 2000);
+
+						} else {
+							uni.hideLoading();
+							uni.showToast({
+								title: "登录失败",
+								icon: 'none',
+								duration: 2000
+							});
+
+						}
+					}).catch(res => {
+								uni.hideLoading();
+							});
+
+				},
+				fail: function(err) {
+					// 登录授权失败
+					uni.$u.toast('登录失败,错误代码:' + err.code);
+				}
+			})
+
+
+		},
+
 		getPhoneCode(e) {
 			console.log(e)
 			if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || !e.detail.code){
@@ -279,6 +351,7 @@ export default {
 		 * 获取个人用户信息
 		 */
 		getUserInfo: function () {
+			console.log("获取个人信息+++++++++++++++++++++++++++++++++")
 			let that = this;
 			getUserInfo().then(res => {
 				uni.hideLoading();