uniapp 抖音小程序登录

<view class="container">
	<view class="dialog-view flex-col">
		<view class="flex-col-center" data-eventsync="true">
			<view style="color: #333333;font-size: 36rpx;">号码管家</view>
		</view>
		<view class="baoming-btn" data-eventsync="true" @click="getUserProfile">抖音授权登录</view>
	</view>
</view>
//data-eventsync="true" 需要再点击事件标签上配置,如有父级,父级也需要上
export default {
	data() {
		return {
			code: '',
			anonymous_code: '',
		}
	},
	methods: {
			async getUserProfile() {
				let that = this
				let obj = {}
				uni.login({ //uni,tt 都行
					force: true,
					provider: 'toutiao',
					success: function(loginRes) {
						console.log(loginRes)//获取code
						that.code = loginRes.code
						that.anonymous_code = loginRes.anonymousCode
					}
				})
				uni.getUserProfile({
					success: (res) => {
						if (that.code) {
							obj = { //组装后台登录接口所需数据
								code: that.code,
								nickname: res.userInfo.nickName,
								headimgurl: res.userInfo.avatarUrl
							}
						}
						uni.setStorageSync('authInfo', res.userInfo);
						if (res) {
							that.loginF(obj)//调用登录接口
						}
					},
					fail: (err) => {
						console.log('err', err);
						uni.showModal({
							title: '提示',
							content: JSON.stringify(err),
							success: function(res) {
								if (res.confirm) {

								} else if (res.cancel) {

								}
							}
						});

					}
				})
			},
			async loginF(obj) { //登录接口
				var res = await this.$post("接口地址", obj);
				if (res.errcode != 0) {
					this.$toast(res.errmsg);
					return;
				}
				this.$toast('登陆成功');
				this.$store.set('jwt', res.data.jwt); //将token存到缓存里
				setTimeout(() => {
					uni.switchTab({
						url: '登陆成功后跳转的页面'
					})
				}, 1000)
			}
		}
}

 

posted @ 2024-04-15 10:29  土小狗  阅读(24)  评论(0编辑  收藏  举报