第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