From e48bea77a915a7d87f36f921c4c3987f38084aae Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Thu, 7 Nov 2024 17:12:22 +0800 Subject: [PATCH] fix --- src/components/getPhoneModal/index.tsx | 206 ++++++++++++-------- src/components/gift/index.tsx | 2 +- src/pages/Subscribe/index.tsx | 7 +- src/pages/UserHome/UserSubscribe/index.less | 4 +- src/pages/UserHome/index.tsx | 25 ++- 5 files changed, 156 insertions(+), 88 deletions(-) diff --git a/src/components/getPhoneModal/index.tsx b/src/components/getPhoneModal/index.tsx index 94b86f2..e3eacfe 100644 --- a/src/components/getPhoneModal/index.tsx +++ b/src/components/getPhoneModal/index.tsx @@ -31,79 +31,118 @@ export default function GetPhoneNumber({ onClose, scene = null, modalVisible, on const handleGetUserPhone = (e: any) => { console.log(e.detail) - Taro.request({ - url: 'https://interapi.jm-kid.com/api/register', - method: 'POST', - data: { - MCODE: e.detail.code, - channel: 'wusehua_mini', - open_id: Taro.getStorageSync('LoginKey'), - invite_code: inviteCode - }, - success: (res: any) => { - console.log('用户注册信息=>',res) - Taro.setStorageSync('UserAvatar', res.data.user.Avatar) - Taro.setStorageSync('UserNickName', res.data.user.NickName) - Taro.setStorageSync('UserPhoneNumber', res.data.user.Mobile) - Taro.setStorageSync('UserInviteCode', res.data.user.InviteCode) - Taro.setStorageSync('UserToken', res.data.token) - - if(scene){ + + const open_id = Taro.getStorageSync('LoginKey') + if (open_id) { + Taro.request({ + url: 'https://interapi.jm-kid.com/api/register', + method: 'POST', + data: { + MCODE: e.detail.code, + channel: 'wusehua_mini', + open_id, + invite_code: inviteCode + }, + success: (res: any) => { + console.log('用户注册信息=>',res) + Taro.setStorageSync('UserAvatar', res.data.user.Avatar) + Taro.setStorageSync('UserNickName', res.data.user.NickName) + Taro.setStorageSync('UserPhoneNumber', res.data.user.Mobile) + Taro.setStorageSync('UserInviteCode', res.data.user.InviteCode) + Taro.setStorageSync('UserToken', res.data.token) + + if(scene){ + Taro.request({ + url: 'https://interapi.jm-kid.com/api/product/exchange', + method: 'POST', + header: { + authorization: `bearer ${Taro.getStorageSync('UserToken')}` + }, + data: { + timestamp: Date.now(), + change_code: scene + }, + success: (s_res: any) => { + console.log('兑换成功=>', s_res.data) + if(s_res.data.state === '200'){ + let modalContent = '' + s_res.data.data.Subscribed.forEach(element => { + modalContent += `兑换成功: ${element.PrdtTitle}\n` + }) + + Taro.showModal({ + title: '兑换成功', + content: modalContent, + showCancel: true, + success: (modal_res) => { + if (modal_res.confirm) { + console.log('用户点击确定') + Taro.navigateTo({ + url: '/pages/CollectionDetail/index?id=' + s_res.data.data.Subscribed[0].PrdtId + }) + } else if (modal_res.cancel) { + console.log('用户点击取消') + } + } + }) + }else if(s_res.data.state === '207'){ + Taro.showToast({ + title: '兑换码已使用', + icon: 'error' + }) + } + }, + fail: (err: any) => { + console.log('兑换失败=>', err) + } + }) + } + + //注册成功 + onClose(true) + //关闭弹窗 + onModalClose() + }, + fail: (err: any) => { + console.log('用户注册失败=>',err) + } + }) + } else { + // 如果没有openId,则先调登录 + Taro.login({ + success:function(res) { + console.log('微信登录成功', res.code) + Taro.request({ - url: 'https://interapi.jm-kid.com/api/product/exchange', method: 'POST', - header: { - authorization: `bearer ${Taro.getStorageSync('UserToken')}` - }, + url: 'https://interapi.jm-kid.com/api/login', data: { - timestamp: Date.now(), - change_code: scene + CODE: res.code, + channel: 'wusehua_mini', }, - success: (s_res: any) => { - console.log('兑换成功=>', s_res.data) - if(s_res.data.state === '200'){ - let modalContent = '' - s_res.data.data.Subscribed.forEach(element => { - modalContent += `兑换成功: ${element.PrdtTitle}\n` - }) - - Taro.showModal({ - title: '兑换成功', - content: modalContent, - showCancel: true, - success: (modal_res) => { - if (modal_res.confirm) { - console.log('用户点击确定') - Taro.navigateTo({ - url: '/pages/CollectionDetail/index?id=' + s_res.data.data.Subscribed[0].PrdtId - }) - } else if (modal_res.cancel) { - console.log('用户点击取消') - } - } - }) - }else if(s_res.data.state === '207'){ - Taro.showToast({ - title: '兑换码已使用', - icon: 'error' - }) + success: function(sres) { + console.log('静默授权登录成功', sres.data) + + if(sres.data.state === 200){ + Taro.setStorageSync('UserAvatar', sres.data.data.user.Avatar) + Taro.setStorageSync('UserNickName', sres.data.data.user.NickName) + Taro.setStorageSync('UserPhoneNumber', sres.data.data.user.Mobile) + Taro.setStorageSync('UserInviteCode', sres.data.data.user.InviteCode) + Taro.setStorageSync('UserToken', sres.data.token) + + //注册成功 + onClose(true) + //关闭弹窗 + onModalClose() + }else{ + //注册key + Taro.setStorageSync('LoginKey', sres.data.open_id) } - }, - fail: (err: any) => { - console.log('兑换失败=>', err) } }) } - - //注册成功 - onClose(true) - //关闭弹窗 - onModalClose() - }, - fail: (err: any) => { - console.log('用户注册失败=>',err) - } - }) + }) + } } return ( @@ -121,18 +160,31 @@ export default function GetPhoneNumber({ onClose, scene = null, modalVisible, on - + { + Taro.getStorageSync('LoginKey') ? + : + + } + + ) } diff --git a/src/pages/Subscribe/index.tsx b/src/pages/Subscribe/index.tsx index f0a528e..58863b2 100644 --- a/src/pages/Subscribe/index.tsx +++ b/src/pages/Subscribe/index.tsx @@ -1,5 +1,5 @@ import { useEffect, useRef, useState } from 'react' -import Taro, { useDidShow, useLoad, useShareAppMessage, useShareTimeline } from '@tarojs/taro' +import Taro, { useDidShow, useDidHide, useShareAppMessage, useShareTimeline } from '@tarojs/taro' import { Image, Input, ScrollView, View, Text } from '@tarojs/components' import PopupHalfScreen from '../../components/Popup' @@ -319,13 +319,16 @@ export default function Subscribe() { } useDidShow(() => { - console.log('componentDidShow') if(Taro.getStorageSync('UserToken')){ setisGetUsePhone(true) } requestUserSubscribe() }) + useDidHide(() => { + setisGetUsePhone(false) + }) + useEffect(() => { if(isGetUsePhone){ requestUserSubscribe() diff --git a/src/pages/UserHome/UserSubscribe/index.less b/src/pages/UserHome/UserSubscribe/index.less index 1c13a32..a250c6a 100644 --- a/src/pages/UserHome/UserSubscribe/index.less +++ b/src/pages/UserHome/UserSubscribe/index.less @@ -1,6 +1,8 @@ +page { + background: #fafafa; +} .subscribe_bg { height: 100vh; - background: #fafafa; padding: 1rem; .subscribe_header { diff --git a/src/pages/UserHome/index.tsx b/src/pages/UserHome/index.tsx index baac395..e8a57f2 100644 --- a/src/pages/UserHome/index.tsx +++ b/src/pages/UserHome/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react' +import React, { useEffect, useState, useCallback } from 'react' import Taro, { useDidHide, useDidShow, useLoad, useShareAppMessage, useShareTimeline } from '@tarojs/taro' import { Image, View, Text } from '@tarojs/components' import UserExchangeModal from '../../components/UserExchangeModal' @@ -80,6 +80,8 @@ export default function UserHome() { //管理登录弹窗 const [isLoginOpen, setisLoginOpen] = useState(false) + const [initialized, setInitialized] = useState(true); + const handleGetUserProfile = () => { if(!Taro.getStorageSync('UserToken')){ setisLoginOpen(true) @@ -101,13 +103,12 @@ export default function UserHome() { // }) } - //更新用户信息 - useDidShow(() => { - console.log('更新用户信息', Taro.getStorageSync('UserNickName')) + const handleUserInfo = useCallback(() => { + setInitialized(false) + console.log('更新用户信息', isGetUsePhone,Taro.getStorageSync('UserNickName')) + setuserAvator(Taro.getStorageSync('UserAvatar') || '') + setuserNickName(Taro.getStorageSync('UserNickName') || '') if(Taro.getStorageSync('UserToken')){ - setuserAvator(Taro.getStorageSync('UserAvatar')) - setuserNickName(Taro.getStorageSync('UserNickName')) - Taro.request({ url: 'https://interapi.jm-kid.com/api/info/num', method: 'POST', @@ -138,6 +139,15 @@ export default function UserHome() { setOperationList([...list]) setisLoginOpen(true) } + }, []) + + useEffect(() => { + initialized || handleUserInfo() + }, [isGetUsePhone]) + + //更新用户信息 + useDidShow(() => { + handleUserInfo() }) useDidHide(() => { @@ -145,6 +155,7 @@ export default function UserHome() { setuserAvator(Taro.getStorageSync('UserAvatar')) setuserNickName(Taro.getStorageSync('UserNickName')) } + setisGetUsePhone(false) }) // useLoad((query) => {