不要shouye

import java.awt.* ;
import javax.swing.* ;
import java.awt.event.MouseListener;
import java.util.EventListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;


public class Huatu extends JFrame
{ 
Huatu()
{
HtPanel p1 = new HtPanel() ;
this.setSize( 700 , 700 ) ;
Container cn = this.getContentPane() ;
cn.add(p1) ;
this.addMouseListener(p1) ;
this.addMouseMotionListener(p1) ;
}
public static void main (String args[])
{
Huatu w = new Huatu() ;
w.show() ;  
} 
}


class HtPanel extends JPanel implements MouseListener, MouseMotionListener
{
int x = 30;

int y = 30 ;

boolean b = true ;
public void paint(Graphics g)
{
super.paint(g) ;
this.setBackground(Color.LIGHT_GRAY) ;
// g.clearRect( 0 , 0 , 700 , 700 ) ;


//``````````````````````````````````````````````````````````````````````````
if (b)
{
g.setColor(new Color( 80 , 180 , 80 )) ;

g.fillOval( 140+x , 285+y , 150 , 150 ) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval( 155+x , 300+y , 140 , 140) ;
g.fillOval( 195+x , 260+y , 200 ,240 ) ;
g.setColor(new Color( 80 , 180 , 80 )) ;
g.fillOval( 145+x , 355+y , 40 , 40) ;

g.setColor(Color.LIGHT_GRAY) ;
g.fillOval( 157+x , 367+y , 15 , 15) ;
}
else
{
g.setColor(new Color( 80 , 180 , 80)) ;

g.fillOval(13+x , 260+y , 150 , 150) ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillOval(3+x , 265+y , 143 , 143) ;
g.setColor(new Color( 80 , 180 , 80)) ;
g.fillOval(100+x,353+y,40,40) ;

g.setColor(Color.LIGHT_GRAY) ;
g.fillOval(113+x , 365+y , 15 , 15) ;
}


//``````````````````````````````````````````````````````````````````````````

if (b)
{
g.setColor(new Color(80,180,80)) ;

g.fillOval( 120+x , 80+y , 60 , 100 ) ;
g.setColor(Color.WHITE) ;
g.fillOval( 130+x ,90+y , 10 ,10 ) ;

g.fillOval( 160+x ,90+y , 10 ,10 ) ;

g.setColor(Color.BLACK) ;
g.fillOval( 130+x ,90+y , 8 ,8 ) ;

g.fillOval( 160+x ,90+y , 8 ,8 ) ;

}
else
{
g.setColor(new Color(80,180,80)) ;

g.fillOval( 120+x , 80+y , 60 , 100 ) ;
g.setColor(new Color(200,0,0)) ;

g.fillOval( 130+x , 100+y , 40 ,50 ) ;
g.setColor(new Color(80,180,80)) ;
g.fillOval( 130+x , 80+y , 40 ,50 ) ;
g.setColor(Color.BLACK) ;
g.drawLine( 143+x ,83+y , 140+x , 90+y ) ;

g.drawLine( 158+x ,83+y , 160+x , 90+y ) ;
}
//``````````````````````````````````````````````````````````````````````````

if (b)
{
g.setColor(new Color(80,180,80)) ;
g.fillOval( 50+x ,140+y ,50 , 70 ) ;



g.fillOval( 200+x ,140+y ,50 , 70 ) ;



g.fillOval( 50+x ,280+y ,50 , 70 ) ;



g.fillOval( 200+x ,280+y ,50 , 70 ) ;

}
else
{ 
g.setColor(new Color(80,180,80)) ;

g.fillOval( 50+x ,140+y ,50 , 70 ) ;

g.setColor(new Color(250,100,150)) ;

g.fillOval( 55+x ,155+y ,5 , 7 ) ;
g.fillOval( 65+x ,148+y ,5 , 7 ) ;
g.fillOval( 75+x ,145+y ,5 , 7 ) ;
g.fillOval( 85+x ,150+y ,5 , 7 ) ;
g.fillOval( 65+x ,160+y ,20 , 20 ) ;


g.setColor(new Color(80,180,80)) ;

g.fillOval( 200+x ,140+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ;

g.fillOval( 210+x ,150+y ,5 , 7 ) ;
g.fillOval( 220+x ,145+y ,5 , 7 ) ;
g.fillOval( 230+x ,148+y ,5 , 7 ) ;
g.fillOval( 240+x ,155+y ,5 , 7 ) ;
g.fillOval( 215+x ,160+y ,20 , 20 ) ;


g.setColor(new Color(80,180,80)) ;

g.fillOval( 50+x ,280+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ;

g.fillOval( 50+x ,315+y ,5 , 7 ) ;
g.fillOval( 55+x ,328+y,5 , 7 ) ;
g.fillOval( 65+x ,340+y ,5 , 7 ) ;
g.fillOval( 80+x ,340+y ,5 , 7 ) ;
g.fillOval( 63+x ,310+y ,20 , 20 ) ;

g.setColor(new Color(80,180,80)) ;

g.fillOval( 200+x ,280+y ,50 , 70 ) ;
g.setColor(new Color(250,100,150)) ;

g.fillOval( 245+x ,320+y ,5 , 7 ) ;
g.fillOval( 240+x ,330+y ,5 , 7 ) ;
g.fillOval( 230+x ,340+y ,5 , 7 ) ;
g.fillOval( 215+x ,338+y ,5 , 7 ) ;
g.fillOval( 215+x ,310+y ,20 , 20 ) ;

}
//```````````````````````````````````````````````````````````````````````````
if (b)
{
g.setColor(new Color(50 ,160 , 50)) ;

g.fillOval( 60+x , 150+y , 180 ,200 ) ;

g.setColor(new Color(50,200,50)) ;
g.fillOval( 70+x , 160+y , 160 ,180 ) ;



g.setColor(new Color(0,0,0)) ;
g.drawLine( 130+x , 210+y , 170+x , 210+y ) ;

g.drawLine( 170+x , 210+y , 200+x , 240+y ) ;
g.drawLine( 200+x , 240+y , 170+x , 270+y ) ;
g.drawLine( 170+x , 270+y , 130+x , 270+y ) ;
g.drawLine( 130+x , 270+y , 100+x , 240+y ) ;
g.drawLine( 100+x , 240+y , 130+x , 210+y ) ;
 
g.drawLine( 100+x , 180+y , 130+x , 210+y ) ;

g.drawLine( 170+x , 210+y , 200+x ,180+y ) ;
g.drawLine( 200+x , 240+y , 230+x ,250+y ) ;
g.drawLine( 170+x , 270+y , 200+x ,320+y ) ;
g.drawLine( 130+x , 270+y , 100+x ,320+y ) ;
g.drawLine( 100+x , 240+y , 70+x ,250+y ) ;
}
else
{
g.setColor(new Color(80,180,80)) ;
g.fillOval( 60+x , 150+y , 180 ,200 ) ;

g.setColor(new Color(255,230,240)) ;
g.fillOval( 70+x , 160+y , 160 ,180 ) ;


g.setColor(new Color(0,0,0)) ;

g.drawLine( 100+x ,180+y , 115+x , 195+y ) ;
g.drawLine( 200+x ,180+y , 185+x , 195+y ) ;
g.drawLine( 115+x ,195+y , 185+x , 195+y ) ;
g.drawLine( 100+x ,240+y , 75+x , 220+y ) ;
g.drawLine( 200+x ,240+y , 225+x , 220+y ) ;
g.drawLine( 100+x ,240+y , 200+x , 240+y ) ;
g.drawLine( 120+x ,290+y , 90+x , 310+y ) ;
g.drawLine( 180+x ,290+y , 210+x , 310+y ) ;
g.drawLine( 120+x ,290+y , 180+x , 290+y ) ;
g.drawLine( 150+x ,160+y , 150+x , 340+y ) ;

//``````````````````````````````````````````````````````````````````````````
}
}


public void mouseClicked(MouseEvent e) 
{
int mousex=(int)e.getPoint().getX();
int mousey=(int)e.getPoint().getY();
}


public void mousePressed(MouseEvent e) 
{
if(e.getX()>x+54 && e.getX()<x+328 && e.getY()>y+99 && e.getY()<y+399)
{
b = false ;
this.repaint() ;
}
}


public void mouseReleased(MouseEvent e) 
{
b = true ;
this.repaint() ;
}


public void mouseEntered(MouseEvent e) 
{
// TODO: Add your code here
}


public void mouseExited(MouseEvent e) 
{
// TODO: Add your code here
}


public void mouseDragged(MouseEvent e) {
x = e.getX() - 155 ;
y = e.getY() - 264 ;
this.repaint() ;
}


public void mouseMoved(MouseEvent e) {
// TODO: Add your code here
}
}

<think>我们正在使用uni-app开发应用,需要根据用户角色跳转到不同页面。根据引用内容,我们可以使用uni-app的路由跳转API(如uni.navigateTo, uni.redirectTo, uni.switchTab)来实现。同时,需要注意页面栈的管理和参数传递。 步骤: 1. 获取用户角色(通常从登录后的接口返回或本地存储中获取) 2. 根据角色判断应该跳转的页面路径 3. 选择合适的跳转方式(保留当前页用navigateTo,关闭当前页用redirectTo,跳转到tabBar页面用switchTab) 4. 如果需要传递参数,可以在url中添加查询字符串 示例代码: 假设我们有两种角色:普通用户(user)和管理员(admin),分别跳转到首页(/pages/index/index)和管理后台(/pages/admin/index)。其中首页是tabBar页面,管理后台是普通页面。 注意:跳转到tabBar页面必须使用uni.switchTab,而跳转到非tabBar页面可以使用navigateTo或redirectTo。 代码实现如下: 在登录成功后的回调中: ```javascript // 假设从登录接口获取到用户角色信息,存储在变量userRole中 let userRole = 'admin'; // 实际开发中这里应该是动态获取 // 根据角色跳转 if (userRole === 'admin') { // 跳转到管理后台页面(非tabBar页面) // 如果当前登录页不需要保留,使用redirectTo关闭当前页 uni.redirectTo({ url: '/pages/admin/index' }); } else { // 普通用户跳转到首页(假设首页是tabBar页面) uni.switchTab({ url: '/pages/index/index' }); } ``` 如果需要传递参数,例如将用户ID传递给目标页面,可以在url中添加: ```javascript uni.redirectTo({ url: '/pages/admin/index?userId=' + userId }); ``` 在目标页面(如admin/index)的onLoad生命周期函数中接收参数: ```javascript onLoad(options) { // options包含传递的参数 console.log('userId:', options.userId); // 使用参数 } ``` 注意事项: 1. 如果跳转的目标页面是tabBar页面,必须使用switchTab,并且不能传递参数(实际上可以传递,但是在目标页的onLoad中无法获取,因为tabBar页面只在第一次加载时执行onLoad)。如果需要传递参数给tabBar页面,可以考虑使用全局变量(如Vuex)或本地存储。 2. 使用redirectTo会关闭当前页面,所以用户不能返回到登录页,这符合登录后跳转首页或管理后台后不需要返回登录页的场景。 3. 如果希望保留登录页(例如在登录页有“返回”按钮可以回到上一个页面),那么应该使用navigateTo。但通常登录后不需要再回到登录页,所以使用redirectTo更合适。 引用说明: 根据引用[1],我们使用uni.redirectTo和uni.switchTab进行跳转,并注意了跳转到tabBar页面的特殊要求。引用[4]展示了如何在目标页面接收参数。 针对传递参数给tabBar页面的问题,我们可以这样处理(使用全局存储): 例如,在登录成功后,将需要传递的数据存储到Vuex中,然后在tabBar页面从Vuex中获取。 示例: 1. 在登录成功后的回调: ```javascript // 存储用户信息到Vuex this.$store.commit('setUserInfo', userInfo); // 跳转到首页(tabBar页面) uni.switchTab({ url: '/pages/index/index' }); ``` 2. 在首页(tabBar页面)的mounted或onShow中读取Vuex中的数据: ```javascript onShow() { // 从Vuex获取用户信息 const userInfo = this.$store.state.userInfo; // 使用数据 } ``` 这样,即使tabBar页面不会重新执行onLoad,我们也可以在onShow中获取到最新数据。 总结: 根据用户角色跳转不同页面的关键在于: - 正确获取用户角色 - 选择合适的跳转API - 合理传递参数(对于非tabBar页面使用url参数,对于tabBar页面使用全局状态管理) 如果遇到跳转后页面监听(watch)未销毁的问题(如引用[3]所述),请确保在页面卸载时(onUnload)清除监听,或者使用跳转方式(redirectTo)来关闭当前页面以销毁监听。 相关问题: 1. 如何解决uni-app中tabBar页面传递参数的问题? 2. uni-app中如何监听页面跳转事件? 3. uni-app中如何动态修改tabBar? 4. 如何实现登录状态失效后跳转回登录页? 5. uni-app中如何实现全局路由守卫?</think>### UniApp 根据用户角色跳转不同页面的实现方案 在 UniApp 中根据用户角色跳转不同页面是常见的业务需求,以下是完整的实现方案: #### 1. 用户角色判断与跳转逻辑 ```javascript // 在登录成功后的回调中处理角色跳转 function handleLoginSuccess(userInfo) { // 获取用户角色 const userRole = userInfo.role; // 从登录返回数据中获取角色 // 根据角色跳转不同页面 switch(userRole) { case 'admin': // 管理员跳转到管理后台 uni.switchTab({ url: '/pages/admin/index' }); break; case 'teacher': // 教师跳转到教学中心 uni.navigateTo({ url: '/pages/teacher/dashboard' }); break; case 'student': // 学生跳转到学习中心 uni.redirectTo({ url: '/pages/student/learning' }); break; default: // 其他角色跳转到默认页面 uni.switchTab({ url: '/pages/home/index' }); } } ``` #### 2. 路由跳转方式选择建议 | 用户场景 | 推荐方法 | 说明 | |------------------------------|-------------------|----------------------------------------------------------------------| | 需要返回原页面 | `uni.navigateTo` | 保留页面栈,用户可返回[^1] | | 不需要返回(如登录后跳转) | `uni.redirectTo` | 关闭当前页面,减少页面栈层级[^1] | | 跳转到 tabBar 页面 | `uni.switchTab` | 关闭所有非 tabBar 页面[^1] | | 角色权限变化后跳转 | 全局路由守卫 | 在路由变化时检查权限 | #### 3. 参数传递与接收 **传递参数到目标页面:** ```javascript uni.navigateTo({ url: `/pages/user/profile?role=${userRole}&userId=${userId}` }); ``` **目标页面接收参数:** ```javascript export default { onLoad(options) { // 接收角色参数 this.userRole = options.role; this.userId = options.userId; // 根据角色初始化页面 this.initPageByRole(); }, methods: { initPageByRole() { if(this.userRole === 'admin') { // 加载管理员专属功能 } // 其他角色处理... } } } ``` #### 4. 路由守卫实现(全局权限控制) 在 `App.vue` 中添加全局路由拦截: ```javascript // App.vue export default { onLaunch() { // 全局路由拦截 uni.addInterceptor('navigateTo', { invoke(e) { // 检查目标页面是否需要权限 if(e.url.includes('/admin') && !isAdmin()) { uni.showToast({ title: '无访问权限', icon: 'none' }); return false; // 拦截跳转 } return true; } }); } } // 检查管理员权限 function isAdmin() { const userInfo = uni.getStorageSync('userInfo'); return userInfo && userInfo.role === 'admin'; } ``` #### 5. 监听问题处理(如引用[3]所述) 当页面跳转后需要停止监听: ```javascript export default { data() { return { watchHandler: null } }, onLoad() { // 设置监听 this.watchHandler = this.$watch('permissionData', (newVal) => { // 业务逻辑... }); }, onUnload() { // 页面卸载时停止监听 if(this.watchHandler) { this.watchHandler(); // 取消监听 } } } ``` #### 6. 跨平台注意事项 1. **H5与小程序传参差异**: H5页面使用URL传参,小程序需使用`web-view`组件特殊处理[^5] 2. **tabBar页面参数限制**: tabBar页面无法通过URL传参,需使用全局状态管理(如Vuex)或本地存储 ```javascript // 跳转前存储数据 uni.setStorageSync('roleData', roleInfo); // tabBar页面获取数据 const roleData = uni.getStorageSync('roleData'); ``` 3. **角色权限持久化**: 登录后应将用户角色存储在本地 ```javascript uni.setStorageSync('userRole', userRole); ``` ### 最佳实践总结 1. **角色判断时机**:在登录成功后立即判断角色并跳转 2. **路由选择原则**: - 需要返回:`navigateTo` - 不需要返回:`redirectTo` - tabBar页面:`switchTab` 3. **参数传递**: - 普通页面:URL参数 - tabBar页面:全局状态管理 4. **权限控制**: - 全局路由守卫拦截未授权访问 - 页面级权限校验(onLoad中) 5. **性能优化**: - 页面卸载时销毁监听器 - 避免不必要的全局状态更新 > **关键点**:根据业务场景选择正确的跳转方式,处理好角色数据的传递和持久化,使用路由守卫实现全局权限控制[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值