jekeyhui99 发表于 2018-10-23 18:39:15

egret微信小游戏在本地用户文件目录下创建文件并读取内容

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