在社区回复内容时候发送的代码片如果是较长的js或者其他语言,发送出去后查看会被截掉一大部分代码,是回复的代码块,不是提问题的代码块哈,可以复制下方内容回复试试。
export default {
data() {
return {
loading: false
}
},
methods: {
loginWithWechat() {
// 这里只适用于 App 端
// #ifdef APP-PLUS
this.loading = true
uni.login({
provider: 'weixin',
onlyAuthorize: true,
success: async (loginRes) => {
try {
const code = loginRes.code
if (!code) {
throw new Error('微信授权未返回 code')
}
const res = await this.requestWechatLogin(code)
const data = res.data
if (data.code !== 0) {
throw new Error(data.message || '登录失败')
}
uni.setStorageSync('token', data.data.token)
uni.setStorageSync('userInfo', data.data.userInfo)
uni.showToast({
title: '登录成功',
icon: 'success'
})
uni.reLaunch({
url: '/pages/index/index'
})
} catch (err) {
uni.showToast({
title: err.message || '微信登录失败',
icon: 'none'
})
} finally {
this.loading = false
}
},
fail: (err) => {
this.loading = false
uni.showToast({
title: err.errMsg || '用户取消或授权失败',
icon: 'none'
})
}
})
// #endif
// #ifndef APP-PLUS
uni.showToast({
title: '当前示例仅支持 App 端微信登录',
icon: 'none'
})
// #endif
},
requestWechatLogin(code) {
return new Promise((resolve, reject) => {
uni.request({
url: 'https://api.example.com/auth/wechat/app-login',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
code
},
success: resolve,
fail: reject
})
})
}
}
}
直接复制粘贴不用代码块时,内容是被转义的,发送出来是正常的。

测试
// #ifndef APP-PLUSuni.showToast({title: '当前示例仅支持 App 端微信登录',icon: 'none'})// #endif},requestWechatLogin(code) {return new Promise((resolve, reject) => {uni.request({url: 'https://api.example.com/auth/wechat/app-login',method: 'POST',header: {'content-type': 'application/json'},data: {code},success: resolve,fail: reject})})}}}</script>export default {
data() {
return {
loading: false
}
},
methods: {
loginWithWechat() {
// 这里只适用于 App 端
// #ifdef APP-PLUS
this.loading = true
uni.login({
provider: 'weixin',
onlyAuthorize: true,
success: async (loginRes) => {
try {
const code = loginRes.code
if (!code) {
throw new Error('微信授权未返回 code')
}
const res = await this.requestWechatLogin(code)
const data = res.data
if (data.code !== 0) {
throw new Error(data.message || '登录失败')
}
uni.setStorageSync('token', data.data.token)
uni.setStorageSync('userInfo', data.data.userInfo)
uni.showToast({
title: '登录成功',
icon: 'success'
})
uni.reLaunch({
url: '/pages/index/index'
})
} catch (err) {
uni.showToast({
title: err.message || '微信登录失败',
icon: 'none'
})
} finally {
this.loading = false
}
},
fail: (err) => {
this.loading = false
uni.showToast({
title: err.errMsg || '用户取消或授权失败',
icon: 'none'
})
}
})
// #endif
// #ifndef APP-PLUS
uni.showToast({
title: '当前示例仅支持 App 端微信登录',
icon: 'none'
})
// #endif
},
requestWechatLogin(code) {
return new Promise((resolve, reject) => {
uni.request({
url: 'https://api.example.com/auth/wechat/app-login',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
code
},
success: resolve,
fail: reject
})
})
}
}
}
</script>