You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
< script >
import { API } from './config/index.js'
export default {
onLaunch : function ( ) {
console . log ( 'App Launch' )
this . wxLogin ( )
} ,
onShow : function ( ) {
console . log ( 'App Show' )
} ,
onHide : function ( ) {
console . log ( 'App Hide' )
} ,
methods : {
wxLogin ( ) {
// const token = uni.getStorageSync('token')
// if (token) {
// console.log('本地已有 token, 直接使用:', token)
// return
// }
uni . login ( {
provider : 'weixin' ,
success : ( loginRes ) => {
const code = loginRes . code
console . log ( '微信登录成功, code:' , code )
// 上传 code 到服务器获取 token
uni . request ( {
url : API . LOGIN ,
method : 'POST' ,
data : { code } ,
success : ( res ) => {
const result = res . data
if ( result . errcode === 0 ) {
const token = result . data . access _token
console . log ( '获取 token 成功:' , token )
uni . setStorageSync ( 'token' , token )
// 登录成功后获取用户信息
uni . request ( {
url : API . GET _USER _INFO ,
method : 'POST' ,
data : { token } ,
success : ( res ) => {
console . log ( '获取用户信息成功:' , res . data )
// 可存储用户信息到本地或全局
} ,
fail : ( err ) => {
console . error ( '获取用户信息失败:' , err )
}
} )
} else {
console . error ( '登录失败:' , result . errmsg )
// 可选: uni.showToast({ title: result.errmsg, icon: 'none' })
}
} ,
fail : ( err ) => {
console . error ( '获取 token 失败:' , err )
}
} )
} ,
fail : ( err ) => {
console . error ( '微信登录失败:' , err )
}
} )
}
}
}
< / script >
< style lang = "scss" >
/*每个页面公共css */
@ import "uview-ui/index.scss" ;
< / style >