禍兮福之所倚 福兮禍之所伏html
cloudfunctions | - login | - index.js | - package.json | - package-lock.json | - arthurSlog_getInfo | - index.js | - package.json | - package-lock.json miniprogram | - images | - code-db-inc-dec.png | - code-db-onAdd.png | - code-db-onQuery.png | - code-db-onRemove.png | - code-func-sum.png | - console-entrance.png | - create-collection.png | - pages | - addFunction | - addFunction.js | - addFunction.json | - addFunction.wxml | - addFunction.wxss | - chooseLib | - chooseLib.js | - chooseLib.json | - chooseLib.wxml | - chooseLib.wsxx | - databaseGuide | - databaseGuide.js | - databaseGuide.json | - databaseGuide.wxml | - databaseGuide.wxss | - deployFunctions | - deployFunctions.js | - deployFunctions.json | - deployFunctions.wxml | - deployFunctions.wxss | - index | - index.js | - index.wxml | - index.wxss | - user-unlogin.png | - storageConsole | - storageConsole.js | - storageConsole.json | - storageConsole.wxml | - storageConsole.wxss | - userConsole | - userConsole.js | - userConsole.json | - userConsole.wxml | - userConsole.wxss | - style | - guide.wxss | - app.js | - app.json | - app.wxss README.md project.config.json
Client:
miniprogram/app.js前端
//app.js App({ onLaunch: function () { if (!wx.cloud) { console.error('請使用 2.2.3 或以上的基礎庫以使用雲能力') } else { wx.cloud.init({ env: 'arthurslog-test-d4537a', traceUser: true, }) } this.globalData = {} } })
Server:
cloudfunctions/arthurSlog_getInfo/index.jsnode
// 雲函數入口文件 const cloud = require('wx-server-sdk') cloud.init() // 雲函數入口函數 exports.main = async(event, context) => { return event.userInfo }
Server:
cloudfunctions/arthurSlog_getInfo/index.jsgit
// 雲函數入口文件 //const cloud = require('wx-server-sdk') //cloud.init() // 雲函數入口函數 exports.main = async(event, context) => { return event.userInfo }
Client
miniprogram/pages/index/index.jsgithub
//index.js const app = getApp() Page({ data: { avatarUrl: './user-unlogin.png', userInfo: {}, logged: false, takeSession: false, requestResult: '' }, onLoad: function() { if (!wx.cloud) { wx.redirectTo({ url: '../chooseLib/chooseLib', }) return } // 獲取用戶信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已經受權,能夠直接調用 getUserInfo 獲取頭像暱稱,不會彈框 wx.getUserInfo({ success: res => { this.setData({ avatarUrl: res.userInfo.avatarUrl, userInfo: res.userInfo }) } }) } } }) }, onGetUserInfo: function(e) { if (!this.logged && e.detail.userInfo) { this.setData({ logged: true, avatarUrl: e.detail.userInfo.avatarUrl, userInfo: e.detail.userInfo }) } }, onGetOpenid: function() { // 調用雲函數 wx.cloud.callFunction({ name: 'login', data: {}, success: res => { console.log('[雲函數] [login] user openid: ', res.result.openid) app.globalData.openid = res.result.openid wx.navigateTo({ url: '../userConsole/userConsole', }) }, fail: err => { console.error('[雲函數] [login] 調用失敗', err) wx.navigateTo({ url: '../deployFunctions/deployFunctions', }) } }) }, // 上傳圖片 doUpload: function() { // 選擇圖片 wx.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: function(res) { wx.showLoading({ title: '上傳中', }) const filePath = res.tempFilePaths[0] // 上傳圖片 const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0] wx.cloud.uploadFile({ cloudPath, filePath, success: res => { console.log('[上傳文件] 成功:', res) app.globalData.fileID = res.fileID app.globalData.cloudPath = cloudPath app.globalData.imagePath = filePath wx.navigateTo({ url: '../storageConsole/storageConsole' }) }, fail: e => { console.error('[上傳文件] 失敗:', e) wx.showToast({ icon: 'none', title: '上傳失敗', }) }, complete: () => { wx.hideLoading() } }) }, fail: e => { console.error(e) } }) }, // 添加前端代碼,向後端服務發起 名爲」arthurSlog_getInfo「方法的請求 // 請求的結果會返回,並保存再 res對象中 // 這裏咱們把結果再控制檯打印出來 // 返回appId 和 openId的數據,並保存在res對象中 arthurSlog_getInfo: function() { wx.cloud.callFunction({ name: 'arthurSlog_getInfo', complete: res => { console.log('callFunction test result: ', res) } }) }, })
Client
miniprogram/pages/index/index.js數據庫
//index.js const app = getApp() Page({ data: { avatarUrl: './user-unlogin.png', userInfo: {}, logged: false, takeSession: false, requestResult: '' }, // 添加前端代碼,向後端服務發起 名爲」arthurSlog_getInfo「方法的請求 // 請求的結果會返回,並保存再 res對象中 // 這裏咱們把結果再控制檯打印出來 // 返回appId 和 openId的數據,並保存在res對象中 arthurSlog_getInfo: function() { wx.cloud.callFunction({ name: 'arthurSlog_getInfo', complete: res => { console.log('callFunction test result: ', res) } }) }, })
Server:
cloudfunctions/arthurSlog_getInfo/index.jsnpm
// 雲函數入口文件 //const cloud = require('wx-server-sdk') //cloud.init() // 雲函數入口函數 exports.main = async(event, context) => { return event.userInfo }
Client
miniprogram/pages/index/index.jsjson
//index.js const app = getApp() Page({ data: { avatarUrl: './user-unlogin.png', userInfo: {}, logged: false, takeSession: false, requestResult: '' }, // //...省略中間的代碼......... // // 添加前端代碼,向後端服務發起 名爲」arthurSlog_methodAdd「方法的請求 // 請求的結果會返回,並保存再 res對象中 // 這裏咱們把結果在控制檯打印出來 // 返回appId 和 openId的數據,並保存在res對象中 arthurSlog_methodAdd: function() { wx.cloud.callFunction({ // 雲函數名稱 name: 'arthurSlog_methodAdd', // 傳給雲函數的參數 data: { a: 8, b: 8, }, }) .then(res => { console.log(res.result) }) .catch(console.error) }, })
Server
cloudfunctions/arthurSlog_methodAdd/index.js小程序
// 雲函數入口文件 const cloud = require('wx-server-sdk') cloud.init() // 雲函數入口函數 // event中包含了來自小程序端的「data對象」的數據 // data對象中有 a對象和 b對象,兩個對象的值都爲8 // 返回一個sum對象給小程序端,返回的對象就是來自小程序端的兩個對象a和b的和 // 返回的數據將會保存在小程序端的res.result對象內 exports.main = async(event, context) => { return { sum: event.a + event.b } }
Client
miniprogram/pages/index/index.wxmlsegmentfault
!--index.wxml--> <view class="container"> <!-- 用戶 openid --> <view class="userinfo"> <button open-type="getUserInfo" bindgetuserinfo="onGetUserInfo" class="userinfo-avatar" style="background-image: url({{avatarUrl}})"></button> <view> <button class="userinfo-nickname" bindtap="onGetOpenid">點擊獲取 openid</button> </view> </view> <!-- 上傳圖片 --> <view class="uploader"> <view class="uploader-text" bindtap="doUpload"> <text>上傳圖片</text> </view> <view class="uploader-container" wx:if="{{imgUrl}}"> <image class="uploader-image" src="{{imgUrl}}" mode="aspectFit" bindtap="previewImg"></image> </view> </view> <!-- 操做數據庫 --> <view class="uploader"> <navigator url="../databaseGuide/databaseGuide" open-type="navigate" class="uploader-text"> <text>前端操做數據庫</text> </navigator> </view> <!-- 新建雲函數 --> <view class="uploader"> <navigator url="../addFunction/addFunction" open-type="navigate" class="uploader-text"> <text>快速新建雲函數</text> </navigator> </view> <!-- 獲取 openid --> <view class="uploader"> <button class="userinfo-nickname" bindtap="arthurSlog_getInfo">點擊獲取 openid</button> </view> <!-- 返回兩個對象之和 --> <view class="uploader"> <button class="userinfo-nickname" bindtap="arthurSlog_methodAdd">點擊返回兩個對象之和</button> </view> </view>
{sum: 16}