uni-bindMobileByMpWeixin.vue 3.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<template>
	<uni-popup ref="popup" type="bottom">
		<view class="box">
			<text class="headBox">绑定资料</text>
			<text class="tip">将一键获取你的手机号码绑定你的个人资料</text>
			<view class="btnBox">
				<text @click="closeMe" class="close">关闭</text>
				<button class="agree" type="warn" @click="beforeGetphonenumber" open-type="getPhoneNumber"
					@getphonenumber="bindMobileByMpWeixin">获取</button>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex';
	const db = uniCloud.database();
	const usersTable = db.collection('uni-id-users')
	let userId = ''
	export default {
		emits: ['next'],
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
			})
		},
		data() {
			return {}
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
雪洛's avatar
雪洛 已提交
38 39 40
			beforeGetphonenumber() {
				uni.showLoading({
					mask:true
DCloud_JSON's avatar
DCloud_JSON 已提交
41 42 43
				})
				wx.checkSession({
					success() {
雪洛's avatar
雪洛 已提交
44
						console.log('session_key 未过期');
DCloud_JSON's avatar
DCloud_JSON 已提交
45 46 47
						uni.hideLoading()
					},
					fail() {
雪洛's avatar
雪洛 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
						console.log('session_key 已经失效,正在执行更新');
						wx.login({
							success({code}) {
								uniCloud.callFunction({
									name: "uni-id-cf",
									data: {
										"action": "refreshSessionKey",
										"params": {
											code
										}
									},
									complete: (e) => {
										console.log(e);
										uni.hideLoading()
									}
								})
							},
							fail: (err) => {
								console.error(err);
							}
DCloud_JSON's avatar
DCloud_JSON 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
						})
					}
				})
			},
			bindMobileByMpWeixin(e) {
				console.log(e.detail);
				uniCloud.callFunction({
					name: "uni-id-cf",
					data: {
						"action": "bindMobileByMpWeixin",
						"params": e.detail
					},
					complete: (e) => {
						console.log(e);
雪洛's avatar
雪洛 已提交
82 83 84 85 86 87 88 89 90 91 92 93
					},
					success: (e) => {
						uni.showToast({
							title: e.result.msg,
							icon: 'none'
						});
						if(e.result.code === 0){
							this.setUserInfo({
								"mobile": e.result.mobile
							})
						}
						this.closeMe()
DCloud_JSON's avatar
DCloud_JSON 已提交
94 95 96 97 98
					}
				})
			},
			async open(uid) {
				userId = uid
雪洛's avatar
雪洛 已提交
99
				this.$refs.popup.open()
DCloud_JSON's avatar
DCloud_JSON 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
				this.beforeGetphonenumber()
			},
			closeMe(e) {
				this.$refs.popup.close()
			},
		}
	}
</script>

<style lang="scss" scoped>
	view {
		display: flex;
	}

	.box {
		background-color: #FFFFFF;
		height: 200px;
		width: 750rpx;
		flex-direction: column;
		border-top-left-radius: 15px;
		border-top-right-radius: 15px;
	}

	.headBox {
		padding: 20rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: left;
		font-size: 32upx;
		color: #333333;
		margin-left: 15rpx;
	}

	.tip {
		color: #666666;
		text-align: left;
		justify-content: center;
		margin: 10rpx 30rpx;
		font-size: 36rpx;
	}

	.btnBox {
		margin-top: 45rpx;
		justify-content: center;
		flex-direction: row;
	}

	.close,
	.agree {
		text-align: center;
		width: 200rpx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 50px;
		margin: 0 20rpx;
		font-size: 36rpx;
	}

	.close {
		color: #999999;
		border-color: #EEEEEE;
		border-style: solid;
		border-width: 1px;
		background-color: #FFFFFF;
	}

	.close:active {
		color: #989898;
		background-color: #E2E2E2;
	}

	.agree {
		background-color: #DD524D;
		color: #FFFFFF;
	}

	/* #ifdef MP */
	.agree::after {
		border: none;
	}

	.agree {
		background-color: #DD524D;
	}

	/* #endif */
	.agree:active {
		background-color: #F5F5F6;
	}
雪洛's avatar
雪洛 已提交
189
</style>