egret微信小游戏在本地用户文件目录下创建文件并读取内容
<p>微信API参考</p><p><br></p><p>Platform.ts添加</p><p><br></p><p>declare interface Platform {</p><p> </p><p> getUserInfo(): Promise<any>;</p><p> </p><p> login(): Promise<any></p><p> </p><p> getFileData(fileName): Promise<any>;</p><p> </p><p> writeFileData(fileName,data): Promise<any>;</p><p>}</p><p> </p><p>class DebugPlatform implements Platform {</p><p> async getUserInfo() {</p><p> return { nickName: "username" }</p><p> }</p><p> async login() {</p><p> </p><p> }</p><p> /**获得文件内容 */</p><p> async getFileData(fileName) {</p><p> return { key: "key" }</p><p> }</p><p> /**文件中写入 */</p><p> async writeFileData(fileName,data) {</p><p> </p><p> }</p><p>}</p><p>使用</p><p><br></p><p> private async getFileData() {</p><p> console.log("读取");</p><p> let fileName = "score.txt";</p><p> let d = await platform.getFileData(fileName);</p><p> console.log(d);</p><p> }</p><p> private async writeFileData() {</p><p> let fileName = "score.txt";</p><p> let num = ~~(Math.random() * 100);</p><p> console.log("写入,内容是" + num);</p><p> await platform.writeFileData(fileName, num);</p><p> }</p><p>发布的微信项目中platform.js添加</p><p><br></p><p>fs = wx.getFileSystemManager()</p><p> getFileData(filePath) {</p><p> var self = this;</p><p> return new Promise((resolve, reject) => {</p><p> self.fs.readFile({</p><p> filePath: `${wx.env.USER_DATA_PATH}/` + filePath,</p><p> encoding: "utf8",</p><p> success: (data) => {</p><p> resolve(data.data);</p><p> },</p><p> fail: (errMsg) => {</p><p> console.log(errMsg);</p><p> self.fs.writeFileSync(`${wx.env.USER_DATA_PATH}/` + filePath, '0', 'utf8');</p><p> },</p><p> complete: () => { console.log("读取完成") }</p><p> });</p><p> })</p><p> }</p><p> writeFileData(fileName, num) {</p><p> var self = this;</p><p> return new Promise((resolve, reject) => {</p><p> self.fs.writeFileSync(`${wx.env.USER_DATA_PATH}/` + fileName, num , 'utf8');</p><p> })</p><p> }</p><p> </p><p>--------------------- </p><p>作者:M硕 </p><p>来源:CSDN </p><p>原文:https://blog.csdn.net/qq_39194398/article/details/81093158 </p><p>版权声明:本文为博主原创文章,转载请附上博文链接!</p><p></p>
页:
[1]