第1步:开通微信的云开发 javascript
环境ID
咱们稍后会使用。 第2步: 在 src/main.js
内初始化云服务html
if (mpvuePlatform === 'wx') {
wx.cloud.init({
env: '环境ID',
traceUser: true
})
}
复制代码
app.json
内添加一行配置{
"cloud": true
}
复制代码
static/functions/
, 而且在 project.config.json
内添加一行配置。{
"cloudfunctionRoot": "static/functions/"
}
复制代码
提示:能够在文件夹内写一个小文件,这样打包好的内容才会有functions
文件夹. vue
增长一个云函数,我这里就取名为getOpenId
java
// 获取openId
exports.main = async (event, context) => {
return event.userInfo
}
复制代码
上传部署json
小程序页面上调用小程序
wx.cloud.callFunction({
name: 'getOpenId'
}).then(res => { console.log('callFunction test result: ', res) })
复制代码
这里我忍不住omg!!一行代码获取openId,如此简单,不用触发按钮,也不用中台给我解密bla bla blabash
接下来,就是咱们传统的获取用户信息,我习惯吧用户信息存在本地的StorageSync
内,这样就拿到最最基本的微信用户信息啦(昵称/头像/地区/openId 全都有~)微信
<template>
<div>
<button open-type="getUserInfo" @getuserinfo="bindGetUserInfo">获取用户信息</button>
</div>
</template>
<script>
export default {
methods: {
bindGetUserInfo (e) {
wx.cloud.callFunction({
name: 'getOpenId'
}).then(res => {
e.target.userInfo.openId = res.result.openId
wx.setStorageSync("wxInfo", e.target.userInfo);
})
}
}
}
</script>
复制代码
这样请求以后,再到云开发控制台运营分析 - 用户访问
,就能够看到访问的用户了。微信开发
要是觉的这篇文章还不错的话,记的给我点赞⭐️遇到什么问题,或者有什么建议也能够在评论里告诉我。app