1. 程式人生 > >Kill the boy and let the man be born

Kill the boy and let the man be born

微信小遊戲API (doing)

1. 登入

登入流程

wx.login({
    success: function(response) {
        if (response.code) {
            wx.request({
                url: 'https://serverurl/login/',
                data: {
                    code: response.code
                }
            })
        } else {
            console.log("登入失敗"
+ response.errMsg); } }, timeout: 100, //超時時間,單位 ms fail: function() {}, // 失敗的回撥函式 complete: function() {}, //介面呼叫結束的回撥函式(呼叫成功、失敗都會執行) });

2. 使用者資訊

2.1授權

2.2 使用者資訊

  1. 需要許可權 scope.userInfo
wx.getUserInfo({
    success: function(res) {
        var userInfo = res.userInfo
        var
nickName = userInfo.nickName // 暱稱 var avatarUrl = userInfo.avatarUrl // 使用者頭像,最後一個數值代表正方形頭像大小(有0、46、64、96、132數值可選,0代表640*640正方形頭像),使用者沒有頭像時該項為空。若使用者更換頭像,原有頭像URL將失效。 var gender = userInfo.gender //性別 0:未知、1:男、2:女 var province = userInfo.province // 使用者所在省份 var city = userInfo.city var
country = userInfo.country } })

3. 轉發分享

轉發: 使用者在使用小遊戲過程中,可轉發訊息給其他使用者或群聊。

// 1. 被動轉發
// 點選右上角按鈕,會彈出選單,選單中的“轉發”選項預設不展示。通過 wx.showShareMenu() 和 wx.hideShareMenu() 可動態顯示、隱藏這個選項。
wx.onShareAppMessage(function () {
  // 使用者點選了“轉發”按鈕
  return {
    title: '轉發標題'
  }
});

// 2. 主動轉發
wx.shareAppMessage({
    title: '轉發標題'
})
// 轉發圖片 圖示最佳顯示比例為 5:4
wx.onShareAppMessage(function () {
    return {
        title: '轉發標題',
        imageUrl: canvas.toTempFilePathSync({
            destWidth: 500,
            destHeight: 400
        })
    }
})

// 3. withShareTicket 模式

4. 關係鏈

// 對使用者託管資料進行寫資料操作
wx.setUserCloudStorage(obj);
/**
 * obj
 * +- KVDataList: Array<KVData>
 * +- success
 * +- fail
 * +- complete
 */


// 開放資料域
// wx.getUserCloudStorage、wx.getFriendCloudStorage() 和 wx.getGroupCloudStorage() 只能在開放資料域中呼叫。
// game.json配置
{
  "deviceOrientation": "portrait",
  "openDataContext": "src/myOpenDataContext"
}

託管資料的限制

  • 每個openid所標識的微信使用者在每個遊戲上託管的資料不能超過128個key-value對。
  • 上報的key-value列表當中每一項的key+value長度都不能超過1K(1024)位元組。
  • 上報的key-value列表當中每一個key長度都不能超過128位元組。

4.2 開放域

開放域和主域的通訊問題:

// game.js
let openDataContext = wx.getOpenDataContext()
openDataContext.postMessage({
    text: 'hello',
    year: (new Date()).getFullYear()
})
// 在開放資料域中通過 wx.onMessage() 方法可以監聽從主域發來的訊息。
wx.onMessage(data => {
  console.log(data)
  /* {
    text: 'hello',
    year: 2018
  } */
})

4.3 展示關係鏈資料

如果想要展示通過關係鏈 API 獲取到的使用者資料,如繪製排行榜等業務場景,需要將排行榜繪製到 sharedCanvas 上,再在主域將 sharedCanvas 渲染上屏。

// src/myOpenDataContext/index.js
let sharedCanvas = wx.getSharedCanvas()

function drawRankList (data) {
  data.forEach((item, index) => {
    // ...
  })
}

wx.getFriendUserGameData({
  success: res => {
    let data = res.data
    drawRankList(data)
  }
})

// src/myOpenDataContext/index.js
let sharedCanvas = wx.getSharedCanvas()
let context = sharedCanvas.getContext('2d')
context.fillStyle = 'red'
context.fillRect(0, 0, 100, 100)

// 獲取開放域
let openDataContext = wx.getOpenDataContext()
let sharedCanvas = openDataContext.canvas

let canvas = wx.createCanvas()
let context = canvas.getContext('2d')
context.drawImage(sharedCanvas, 0, 0)

5. 支付

支付檔位

// 購買遊戲幣的時候,buyQuantity 不可任意填寫。
// 需滿足 buyQuantity * 遊戲幣單價 = 限定的價格等級
wx.requestMidasPayment({
    mode: 'game',
    offerId: '',  // 在米大師側申請的應用 id
    buyQuantity: 10, // 幣種
    zoneId: 1, // 分割槽 ID
    success() {
        // 支付成功
    },
    fail({ errMsg, errCode }) {
        // 支付失敗
        console.log(errMsg, errCode)
    }
})

6. 檔案系統

6.1 程式碼包檔案

程式碼包檔案指的是在專案目錄中新增的檔案。由於程式碼包檔案大小限制,程式碼包檔案適用於放置首次載入時需要的檔案,對於內容較大或需要動態替換的檔案,不推薦用新增到程式碼包中,推薦在小遊戲啟動之後再用下載介面下載到本地。

訪問程式碼包檔案

訪問程式碼包檔案

本地檔案:

本地檔案指的是小程式被使用者新增到手機後,會有一塊獨立的檔案儲存區域,以使用者維度隔離。

本地使用者檔案是從1.7.0 版本開始新增的概念。微信提供了一個使用者檔案目錄給開發者,開發者對這個目錄有完全自由的讀寫許可權。通過 wx.env.USER_DATA_PATH 可以獲取到這個目錄的路徑。

const fs = wx.getFileSystemManager()
fs.writeFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'hello, world', 'utf8')

本地快取檔案:

本地儲存檔案只能通過呼叫特定介面產生,不能直接寫入內容。本地快取檔案產生後,重啟之後仍可用。本地快取檔案只能通過 FileSystemManager.saveFile() 介面將本地臨時檔案儲存獲得。

fs.saveFile({
    tempFilePath: '', // 傳入一個本地臨時檔案路徑
    success(res) {
        console.log(res.savedFilePath) // res.savedFilePath 為一個本地快取檔案路徑
    }
})

本地臨時檔案:

本地臨時檔案只能通過呼叫特定介面產生,不能直接寫入內容。本地臨時檔案產生後,僅在當前生命週期內有效,重啟之後即不可用。因此,不可把本地臨時檔案路徑儲存起來下次使用。如果需要下次在使用,可通過 FileSystemManager.saveFile()FileSystemManager.copyFile() 介面把本地臨時檔案轉換成本地儲存檔案或本地使用者檔案。

介面、元件
程式碼包檔案
本地臨時檔案
本地快取檔案
本地使用者檔案

相關推薦

Kill the boy and let the man be born

微信小遊戲API (doing) 1. 登入 wx.login({ success: function(response) { if (response.code) {

多個jdk 變更 引起 tomcat插件 啟動不了 The JRE could not be found.Edit the server and change the JRE location.

變更 runtime win jdk nts nvi bsp 選擇 ould The JRE could not be found.Edit the server and change the JRE location. 在Windows->Preference

The JRE could not be found.Edit the server and change the JRE location.

之前更改了了一個較低的jdk的版本看了看一個專案的程式碼,不知所云,然後再改回來, 混亂之中只要啟動Tomcat就出現這種錯誤,還是無法找到JRE,最後如此解決: 在Windows->Preferences->Server->Runtime E

Artificial Intelligence Is the Wave of the Future (And It Always Will Be)

The recent revelation that Amazon tried to use artificial intelligence to recruit engineers, only to discover that its machine learning wound up discrimina

Want to Improve Your Memory? Science Tells Us the Key (and It Can Actually Be Fun)

Want to Improve Your Memory? Science Tells Us the Key (and It Can Actually Be Fun)Do you remember where you were when you had your first kiss?It’s funny, t

啟動報錯:The JRE could not be found. Edit the server and change the JRE location.

轉載內容,原文連結:http://blog.sina.com.cn/s/blog_7f865faf0100uvev.html Tomcat報錯: The JRE could not be found. Edit the server and change the JRE

How does the vuejs add the query and walk the object?

讓這個老實返回的頁面新增特殊路由,這個頁面常常都是登入註冊。這次我們根據登入舉例。 省略 { path:'/login?url=:url', name:'loginfirst', component:()=>import ('../views/logi

Codeforces Round #251(Div. 2) 439A. Devu, the Singer and Churu, the Joker 水題

A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory limit per test 256 meg

Codeforces 439A Devu, the Singer and Churu, the Joker

#include <iostream> #include <cstdio> using namespace std; int main() { int n, d, sum = 0, arr[120]; scanf("%d%d",

Talbot basically sweating the idea and can be happy for the repeat functionality

appear cas actual pear value edi fort lte finally Practically no one enjoyed more NHL hockey when compared with Cam Talbot last time. It

SQL server 導出平面文件時出錯: The code page on Destination - 3_txt.Inputs[Flat File Destination Input].Columns[UserId] is 936 and is required to be 1252.

log 解決辦法 驗證 AI inpu image ans post BE 我在導出平面文件時:Error 0xc00470d4: Data Flow Task 1: The code page on Destination - 3_txt.Inputs[Flat File

kafka報Commit cannot be completed since the group has already rebalanced and assigned the partitions

問題描述: 新版本的kafka訊息處理程式中,當訊息量特別大時不斷出現如下錯誤,並且多個相同groupId的消費者重複消費訊息。 2018-10-12 19:49:34,903 WARN [DESKTOP-8S2E5H7 id2-1-C-1] Caller+0 at org.apac

linux下sudo顯示sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

linux中一些特殊的許可權(setuid/setgid/sticky) 問題描述 今天在測試檔案系統的時候,發現新建立的檔案系統不能使用sudo命令,具體表現如下: sudo su sudo: /usr/bin/sudo must be owned by ui

mybatis foreach報錯It was either not specified and/or could not be found for the javaType Type handler

或許是慣性思維,在mybatis使用foreach迴圈呼叫的時候,很多時候都是傳一個物件,傳一個List的情況很少,所以寫程式碼有時候會不注意就用慣性思維方法做了。 今天向sql傳參,傳了一個List作為引數,然後在xml裡再foreach迴圈呼叫。然後報錯資訊如: myba

We Need to be Examining the Ethics and Governance of Artificial Intelligence

Growing up, one of my favorite movies was Steven Spielberg’s Minority Report. I was fascinated by the idea that a crime could be p

"Loading a plug-in failed The plug-in or one of its prerequisite plug-ins may be missing or damaged and may need to be reinstal

The Unarchiver 雖好,但存在問題比我們在mac上zip打包一個軟體xcode, 然後copy to another mac, 這時用The Unarchiver解壓縮出來的xcode包不能執行, 好像是裡面的檔案資訊結構被破壞,會出現而用archive utility 解壓就能正常執行。  通

The Languages and Frameworks You Should Learn in 2017

pan end req targe lov dev rapi automatic min Martin Angelov December 8th, 2016 The software development industry continues its relent

The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files

it is miss ati refers () imp -s tar pre 問題描述 Exception in thread "main" java.lang.Error: Unresolved compilation problems: The t

iOS模擬器URLWithString The operation couldn&#39;t be completed. (Cocoa error 256.)

track 獲取數據 tracking 模擬 con erro ring could per 我遇到這個問題是在iPad Air的模擬器上使用NSURL的URLWithString方法從Flickr獲取數據時,解決的方法是換一個模擬器,我換成iPad 2。就沒有問題了。

HDUOJ Let the Balloon Rise 1004

mit cit strcmp ever con inpu iss multipl you ?? /* Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3