没有往事 發表於 2019-12-12 17:47:00

微信小程序开发(二)----- 云开发

<p><span style="font-size: 16px"><strong>1、概念</strong></span></p>
<ul>
<li>微信小程序的云开发是腾讯云与微信团队深度合作推出的一个全新的小程序的解决方案,它提供了云函数、云数据库与云存储这三大基础能力支持,随着云开发的出现,小程序的开发者可以将服务端的部署和运营的环节进行服务端的托管,让腾讯云去管理,而不需要在运维和管理方面投入太多的精力。</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212120026921-815705458.png"></p>
<p><span style="font-size: 16px"><strong>2、传统小程序开发与云开发的区别</strong></span></p>
<ul>
<li>小程序传统开发模式:(开发效率低、运维成本高)
<div class="cnblogs_code">
<pre>       对于小程序传统的开发模式而言,我们需要一个客户端(也就是前端页面),而前端页面展示的数据大多来自于数据库,因此我们还需要一个服务端,把后端的代码以及数<br>据库部署上去,并且前后端联调的过程也是沟通的过程,时间成本比较高;小程序在部署的时候,我们需要购买相对应的域名以及服务器,并且还要进行备案(非常麻烦、耗时长),<br>部署成功后,在运维小程序的过程中,也会遇到很多很多的问题,比如DB运维、文件存储、内容加速(CDN)、网络防护、容器服务、负载均衡以及安全加固等;虽然有的公司没有<br>购买服务器,使用的是阿里云或者腾讯云上面的服务,但是依旧需要维护,因此运维成本非常高。</pre>
</div>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212134923482-1592642578.png"></p>
<ul>
<li>小程序云开发模式:采用serverless(无服务)模式
<div class="cnblogs_code">
<pre>      对于小程序的云开发模式而言,依旧需要一个客户端,但是由于小程序提供了云开发,而云开发提供了云函数、云数据库以及云存储三大基础能力支持,我们可以直接在客户<br>端调用云数据库里面的内容,当然也可以通过客户端调用云函数,在云函数里面处理一些业务逻辑,并在云函数里面调用云数据库,同时我们可以在客户端上传相应的文件到云存储当<br>中,或者将云存储中的图片下载到客户端给用户去展示,由于云开发是部署在腾讯云上面的,因此我们不需要额外的运维人员,运维成本也会降低。除此之外,小程序中的云函数采用<br>的是Node.js,云数据库采用的是mongoDB,而node调用mongoDB也是很方便的。</pre>
</div>
</li>
</ul>
<p>&nbsp;&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212135023112-581455612.png"></p>
<ul>
<li style="list-style-type: none">
<ul>
<li>三大基础能力支持:
<ul>
<li>云数据库:是一个JSON数据库(文档型数据库),提供了2GB的免费存储空间,实现对数据的增删改查的操作;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212152144436-1319387718.png">
<ul>
<li>支持的数据类型:Number、String、Object、Array、Boolean、Date、Null以及GeoPoint(地理位置点);</li>
<li>如何通过代码操作数据库:
<ul>
<li>初始化:
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> db = wx.cloud.database()</pre>
</div>
</li>
<li>切换环境
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> testDB =<span style="color: rgba(0, 0, 0, 1)"> wx.cloud.database({
env: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'//环境名称</span><span style="color: rgba(0, 0, 0, 1)">
})</span></pre>
</div>
</li>
<li>简单数据的增删改查
<ul>
<li>WXML
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_23fd4d0c-e08f-414b-9753-78e758c97ec9" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_23fd4d0c-e08f-414b-9753-78e758c97ec9" class="cnblogs_code_hide">
<pre>&lt;view&gt;数据库的增删改查&lt;/view&gt;
&lt;button size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span> catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchPostHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;增加&lt;/button&gt;
&lt;button size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span> catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchDeleteHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;删除&lt;/button&gt;
&lt;button size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span> catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchUpdateHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;修改&lt;/button&gt;
&lt;button size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span> catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchGetHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;查找&lt;/button&gt;</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>JS
<div class="cnblogs_code"><img alt="" id="code_img_closed_d4a77884-2545-4bf4-9ef8-03445217c3b7" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_d4a77884-2545-4bf4-9ef8-03445217c3b7" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_d4a77884-2545-4bf4-9ef8-03445217c3b7" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库的初始化</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> db =<span style="color: rgba(0, 0, 0, 1)"> wx.cloud.database();

Page({

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 页面的初始数据
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
data: {

},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 数据库的添加
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchPostHandler() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 回调函数写法</span>
    <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * db.collection('user').add({
      data: {
      name: 'wxh',
      age: '18',
      job: 'IT'
      },
      success: (res) =&gt; {
      // 使用了ES6中的箭头函数,目的是为了改变this指向
      console.log(res)
      },
      fail: (error) =&gt; {
      console.log(error)
      }
    })
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> promise的写法</span>
    db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .add({
      data: {
          name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">jerry</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
          age: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">22</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
          job: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">teacher</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
      }
      })
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 数据库的删除
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchDeleteHandler() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除一条可以通过小程序端进行控制,而删除多条我们需要小程序调用云函数来操作数据库</span>
    db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .doc(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">b040a67a5df1f6d1029170ef7e785160</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      .remove()
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 数据库的修改
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchUpdateHandler() {
    db.collection(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .doc(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b040a67a5df1f6d1029170ef7e785160</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .update({
      data: {
          age: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">19</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
      }
      })
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 数据库的查找
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchGetHandler() {
    db.collection(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .</span><span style="color: rgba(0, 0, 255, 1)">where</span><span style="color: rgba(0, 0, 0, 1)">({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wxh</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">({})
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>注意:如果是在数据库手动加入记录,会出现查不到的情况,这个时候我们需要设置</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img alt="" width="589" height="171" data-src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212165904194-1845811163.png"></p>
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>云函数:相当于小程序在服务端的后台代码,可以非常方便的获取到当前登录用户的信息(appid、openid、生成分享图或者调用腾讯云的SDK);
<ul>
<li>去Node官网(http://nodejs.cn/),安装Node环境(需要nodev8.0及以上版本);</li>
<li>安装成功的标志:windows+r,输入cmd, 打开命令输入&nbsp; node -v 显示版本号;</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212165124375-714629537.png"></p>
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>&nbsp;创建云函数</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212170436344-955570281.png"></p>
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>很有可能提醒是否需要安装<strong>wx-server-sdk&nbsp; &nbsp; ----- &gt;&nbsp; &nbsp;&nbsp;</strong>点击确定;</li>
<li>每次修改云函数都需右键点击<span style="color: rgba(128, 0, 0, 1)"><strong>“上传并部署,云端安装依赖”</strong></span>;</li>
<li>在调用云函数的过程中提示没有安装<strong>wx-server-sdk包,</strong>这个时候在cloudfunctions中,<span style="color: rgba(128, 0, 0, 1)"><strong>右键在终端打开</strong></span>,输入安装命令<strong><span style="color: rgba(128, 0, 0, 1)">npm install --save wx-server-sdk@latest(表示安装最新版本)</span></strong></li>
</ul>
</li>
<li>简单的调用一个云函数:求a+b的和;
<ul>
<li>sum下的index.js
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 云函数入口函数</span>
exports.main = <span style="color: rgba(0, 0, 255, 1)">async</span> (<span style="color: rgba(0, 0, 255, 1)">event</span>, context) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> event 中包括小程序调用云函数传过来的对象
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> context 指当前调用的上下文,同时也包括当前用户的一些信息</span>
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
    sum: </span><span style="color: rgba(0, 0, 255, 1)">event</span>.a + <span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">.b
}
}</span></pre>
</div>
</li>
<li>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_e7314859-4bca-4e3d-b8c4-ce6993655726" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_e7314859-4bca-4e3d-b8c4-ce6993655726" class="cnblogs_code_hide">
<pre>&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchSumHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span> size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;调用云函数sum&lt;/button&gt;
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 云函数的调用
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchSumHandler() {
    wx.cloud
      .callFunction({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sum</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当前云函数的名称</span>
<span style="color: rgba(0, 0, 0, 1)">      data: {
          a: </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">,
          b: </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">
      }
      })
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(error =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
</ul>
</li>
<li>获取当前用户的openid:
<ul>
<li>传统的微信登录方式与小程序云开发登录方式的区别:
<ul>
<li>传统的微信登录方式
<div class="cnblogs_code">
<pre>    首先用户端小程序通过调用wx.login,从微信服务端获取一个code,然后用户小程序端调用wx.request将code传递给后端服务器,后端<br>获得到code之后,对微信服务端发起请求,通过code获取openid与session_key,最后将小程序的唯一标识发送给小程序本地存储。</pre>
</div>
<p><img alt="" width="407" height="199" data-src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191213100645344-1646388287.png"></p>
</li>
<li>小程序云开发登录方式
<div class="cnblogs_code">
<pre>    用户通过点击按钮,从小程序获取用户的信息,而小程序通过云函数获得用户的信息,云函数给小程序端返回用户的openid,小程序获取到<br>用户的信息之后,将用户的信息存储到云数据库。</pre>
</div>
<p><img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191213101023926-1860200480.png"></p>
</li>
</ul>
</li>
<li>云函数会自带一个login文件夹,它的作用就是获取当前用户的信息;
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_df6436e3-731a-41af-9429-47ee552a8fc3" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_df6436e3-731a-41af-9429-47ee552a8fc3" class="cnblogs_code_hide">
<pre>&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchOpenIdHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span> size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;获取当前用户openid&lt;/button&gt;

<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 获取当前用户的openid
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchOpenIdHandler() {
    wx.cloud
      .callFunction({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">login</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
      })
      .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
</ul>
</li>
<li>批量删除
<ul>
<li>定义云函数batchDelete
<div class="cnblogs_code"><img alt="" id="code_img_closed_05403d19-6eaf-482a-a572-a1d0da5959fc" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_05403d19-6eaf-482a-a572-a1d0da5959fc" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_05403d19-6eaf-482a-a572-a1d0da5959fc" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 云函数入口文件</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> cloud = require(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wx-server-sdk</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

cloud.init()

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取云数据库</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> db =<span style="color: rgba(0, 0, 0, 1)"> cloud.database();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 云函数入口函数</span>
exports.main = <span style="color: rgba(0, 0, 255, 1)">async</span> (<span style="color: rgba(0, 0, 255, 1)">event</span>, context) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库是一个异步的操作,等到删除成功或者失败才可以返回数据给小程序因此需要ES7的await</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">await</span> db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
      .</span><span style="color: rgba(0, 0, 255, 1)">where</span><span style="color: rgba(0, 0, 0, 1)">({
      name: </span><span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">.name
      })
      .remove();
}
</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
    console.log(error)
}
}</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>调用云函数
<div class="cnblogs_code"><img alt="" id="code_img_closed_2869a5d7-61a4-4e9c-947b-dd786aa04fc0" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_2869a5d7-61a4-4e9c-947b-dd786aa04fc0" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_2869a5d7-61a4-4e9c-947b-dd786aa04fc0" class="cnblogs_code_hide">
<pre>&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchBatchDeleteIdHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span> size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mini</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;批量删除数据&lt;/button&gt;


<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 批量删除
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchBatchDeleteIdHandler() {
    wx.cloud.callFunction({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">batchDelete</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
      data: {
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wxh</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
      }
    }).then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
    }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
    })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="_mce_tagged_br">
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>云存储:管理、上传、下载以及分享文件等操作;
<ul>
<li>云存储能力
<ul>
<li>wx.cloud.uploadFile&nbsp; -----&nbsp; &nbsp;上传文件
<ul>
<li>步骤示意图</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img alt="" width="430" height="225" data-src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191213112549084-1224690028.png"></p>
<ul class="_mce_tagged_br">
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>上传并展示的代码
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_6e7fb1b6-3f4c-4ccd-ac47-f10ba2dcdb1a" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_6e7fb1b6-3f4c-4ccd-ac47-f10ba2dcdb1a" class="cnblogs_code_hide">
<pre>&lt;view&gt;云存储&lt;/view&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchUploadHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;上传图片&lt;/button&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchShowHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;展示图片&lt;/button&gt;
&lt;view wx:<span style="color: rgba(0, 0, 255, 1)">for</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{showImgPath}}</span><span style="color: rgba(128, 0, 0, 1)">"</span> wx:key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">index</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
&lt;image src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{item.fileID}}</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/image&gt;
&lt;/view&gt;




<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 页面的初始数据
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
data: {
    showImgPath: []
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 图片上传
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchUploadHandler() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1、从本地相册选择图片或使用相机拍照</span>
<span style="color: rgba(0, 0, 0, 1)">    wx.chooseImage({
      count: </span><span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最多选择的图片张数,最大值为9</span>
      sizeType: [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">original</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">compressed</span><span style="color: rgba(128, 0, 0, 1)">'</span>], <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">所选图片的尺寸(原图、压缩)</span>
      sourceType: [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">album</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">camera</span><span style="color: rgba(128, 0, 0, 1)">'</span>], <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">图片来源(相册、相机),电脑端只会打开相册,而手机端会有两个选项(拍照或者相册)</span>
<span style="color: rgba(0, 0, 0, 1)">      success(res) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> tempFilePath可以作为img标签的src属性显示图片</span>
      <span style="color: rgba(0, 0, 255, 1)">const</span> tempFilePaths = res.tempFilePaths[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">];
      wx.cloud.uploadFile({
          cloudPath: </span><span style="color: rgba(0, 0, 255, 1)">new</span> Date().getTime() + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">png</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 上传至云端的路径可以指定文件夹以及文件名称如果写死每次上传都会覆盖原有图片</span>
          filePath: tempFilePaths, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 小程序临时文件路径</span>
          success: res =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">上传成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回文件 ID</span>
            db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">imgFile</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
            .add({
                data: {
                  fileID: res.fileID
                },
                success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  console.log(res)
                  console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">保存成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
                },
                fail: (error) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  console.log(error)
                }
            })
          },
          fail: console.error
      })
      }
    })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 图片展示
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchShowHandler() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1、通过云存储查找用户本人管理的图片</span>
<span style="color: rgba(0, 0, 0, 1)">    wx.cloud.callFunction({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">login</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
      success: res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">const</span> openid =<span style="color: rgba(0, 0, 0, 1)"> res.result.openid;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2、去数据库中查找</span>
      db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">imgFile</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
          .</span><span style="color: rgba(0, 0, 255, 1)">where</span><span style="color: rgba(0, 0, 0, 1)">({
            _openid: openid
          })
          .</span><span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">()
          .then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
            showImgPath: res.data
            })
            console.log(res)
          })
          .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            console.log(error)
          })
      },
      fail: error </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      }
    })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>上传多张图片
<ul>
<li>WXML
<div class="cnblogs_code"><img alt="" id="code_img_closed_aabd1df1-7148-4dcc-aff2-2abae21aeb1e" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_aabd1df1-7148-4dcc-aff2-2abae21aeb1e" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_aabd1df1-7148-4dcc-aff2-2abae21aeb1e" class="cnblogs_code_hide">
<pre> &lt;!-- 评价 --&gt;
&lt;view <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">comment-container</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    &lt;van-field value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{ content }}</span><span style="color: rgba(128, 0, 0, 1)">"</span> placeholder=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">写一些评价吧</span><span style="color: rgba(128, 0, 0, 1)">"</span> bind:change=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onContentChange</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
    &lt;van-rate value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{ score }}</span><span style="color: rgba(128, 0, 0, 1)">"</span> bind:change=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onScoreChange</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
    &lt;view style=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">margin-bottom:20rpx</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
      &lt;van-button type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">warning</span><span style="color: rgba(128, 0, 0, 1)">"</span> bindtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">uploadImg</span><span style="color: rgba(128, 0, 0, 1)">"</span> size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">small</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;上传图片&lt;/van-button&gt;
    &lt;/view&gt;
    &lt;view&gt;
      &lt;image <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">comment-img</span><span style="color: rgba(128, 0, 0, 1)">"</span> src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{item}}</span><span style="color: rgba(128, 0, 0, 1)">"</span> wx:<span style="color: rgba(0, 0, 255, 1)">for</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{uploadImages}}</span><span style="color: rgba(128, 0, 0, 1)">"</span> wx:key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">index</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/image&gt;
    &lt;/view&gt;
    &lt;van-button size=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">large</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">danger</span><span style="color: rgba(128, 0, 0, 1)">"</span> bindtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">submit</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;提交评价&lt;/van-button&gt;
&lt;/view&gt;
&lt;/view&gt;</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>JS
<p><img alt="" loading="lazy" src="https://img2023.cnblogs.com/blog/1730357/202310/1730357-20231011161520867-709747020.png"></p>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_51451636-2aac-4010-80f6-227dad66c243" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_51451636-2aac-4010-80f6-227dad66c243" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对数据库进行初始化</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> db =<span style="color: rgba(0, 0, 0, 1)"> wx.cloud.database();

Page({

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 页面的初始数据
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
data: {
    movieid: </span><span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对应电影的movieid</span>
<span style="color: rgba(0, 0, 0, 1)">    movieDetail: {},
    content: </span><span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">评价的内容</span>
    score: <span style="color: rgba(128, 0, 128, 1)">5</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">电影评分</span>
    uploadImages: [], <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上传的图片</span>
    fileIds: [], <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">云存储返回的图片id</span>
<span style="color: rgba(0, 0, 0, 1)">},

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 生命周期函数--监听页面加载
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onLoad: function(options) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取上一个页面传过来的参数</span>
    <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
      movieid: options.movieid
    })
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getMovieDetail(options.movieid)
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 获取电影详情信息
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
getMovieDetail(movieid) {
    wx.showLoading({
      title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">加载中</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
    })
    wx.cloud.callFunction({
      name: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">getDetail</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
      data: {
      movieid: movieid
      },
      success: res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
          movieDetail: JSON.parse(res.result)
      })
      wx.hideLoading()
      },
      fail: error </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      }
    })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 输入评价
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onContentChange(</span><span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
      content: </span><span style="color: rgba(0, 0, 255, 1)">event</span>.detail <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入框的内容</span>
<span style="color: rgba(0, 0, 0, 1)">    })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 进行评分
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onScoreChange(</span><span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
      score: </span><span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">.detail
    });
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 上传图片
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
uploadImg() {
    wx.chooseImage({
      count: </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">,
      sizeType: [</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">original</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">compressed</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">],
      sourceType: [</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">album</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">camera</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">],
      success: res </span>=&gt; { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可以改变this指向
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> tempFilePath可以作为img标签的src属性显示图片</span>
      <span style="color: rgba(0, 0, 255, 1)">const</span> tempFilePaths =<span style="color: rgba(0, 0, 0, 1)"> res.tempFilePaths;
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
          uploadImages: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.uploadImages.concat(tempFilePaths)
      })
      }
    })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 提交评价
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
submit() {
    wx.showLoading({
      title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">评价中...</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
    })
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 首先获取到评价的内容分数以及上传的图片
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 涉及到了异步(非阻塞)的问题,由于上传时间不确定,因此需要成功上传到云存储才能将fileid存到云数据库</span>
    let promiseArr =<span style="color: rgba(0, 0, 0, 1)"> [];
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1、上传图片到云存储</span>
    <span style="color: rgba(0, 0, 255, 1)">for</span> (let i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; <span style="color: rgba(0, 0, 255, 1)">this</span>.data.uploadImages.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
      promiseArr.push(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve, reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      let item </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.uploadImages;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过正则表达式取出文件对应的扩展名</span>
      let suffix = /\.\w+$/.exec(item)[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">];
      wx.cloud.uploadFile({
          cloudPath: </span><span style="color: rgba(0, 0, 255, 1)">new</span> Date().getTime() + suffix, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 上传至云端的路径</span>
          filePath: item, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 小程序临时文件路径</span>
          success: res =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回文件 ID</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({
            fileIds: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.fileIds.concat(res.fileID)
            });
            resolve()
          },
          fail: console.error
      })
      }))
    }
    Promise.all(promiseArr).then((res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2、插入数据</span>
      db.collection(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">comment</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).add({
      data: {
          content: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.content,
          score: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.score,
          movieid: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.movieid,
          fileIds: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.fileIds
      },
      success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
          wx.showToast({
            title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">评价成功</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
          });
          wx.hideLoading()
      },
      fail: (error) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
          wx.hideLoading();
          wx.showToast({
            title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">评价失败</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
          })
      }
      })

    }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
    })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>效果图</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img alt="" width="297" height="445" data-src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191217103539049-761719057.png"><img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191217103632203-110395907.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<ul class="_mce_tagged_br">
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>wx.cloud.downloadFile&nbsp; -----&nbsp; &nbsp;下载文件
<ul>
<li>步骤示意图</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191213155450529-780602169.png"></p>
<ul class="_mce_tagged_br">
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li style="list-style-type: none">
<ul>
<li>代码
<ul>
<li>WXML
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_eacaa8c0-d889-441f-88ba-5cd06bbf41d8" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_eacaa8c0-d889-441f-88ba-5cd06bbf41d8" class="cnblogs_code_hide">
<pre>&lt;view&gt;云存储&lt;/view&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchUploadHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;上传图片&lt;/button&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchShowHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;展示图片&lt;/button&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchSettingAgainHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;再次调起配置页&lt;/button&gt;
&lt;view wx:<span style="color: rgba(0, 0, 255, 1)">for</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{showImgPath}}</span><span style="color: rgba(128, 0, 0, 1)">"</span> wx:key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">index</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
&lt;image src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{item.fileID}}</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/image&gt;
&lt;button catchtap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onCatchDownLoadHandler</span><span style="color: rgba(128, 0, 0, 1)">"</span> data-fileID=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{{item.fileID}}</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;下载图片&lt;/button&gt;
&lt;/view&gt;</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
<li>JS
<div class="cnblogs_code"><img alt="" id="code_img_closed_a0f7d700-515f-4a06-bd54-0f6cdc34d3be" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img alt="" id="code_img_opened_a0f7d700-515f-4a06-bd54-0f6cdc34d3be" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_a0f7d700-515f-4a06-bd54-0f6cdc34d3be" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 图片下载
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchDownLoadHandler(</span><span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(0, 0, 255, 1)">const</span> fileID = <span style="color: rgba(0, 0, 255, 1)">event</span><span style="color: rgba(0, 0, 0, 1)">.target.dataset.fileid;
    wx.cloud.downloadFile({
      fileID: fileID,
      success: res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      wx.showToast({
          title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">下载成功</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
      })
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 保存到手机相册</span>
<span style="color: rgba(0, 0, 0, 1)">      wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            wx.showToast({
            title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">保存成功</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
            })
          },
          fail: (error) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (error.errMsg === <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">saveImageToPhotosAlbum:fail:auth denied</span><span style="color: rgba(128, 0, 0, 1)">"</span> || error.errMsg === <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">saveImageToPhotosAlbum:fail auth deny</span><span style="color: rgba(128, 0, 0, 1)">"</span> || error.errMsg === <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">saveImageToPhotosAlbum:fail authorize no response</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) {
            wx.showToast({
                title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">用户拒绝了</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
            });
            wx.openSetting({
                success: res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  console.log(res)
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (res.authSetting[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">scope.writePhotosAlbum</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]) {
                  wx.showModal({
                      title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">提示</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                      content: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">获取权限成功,再次点击图片即可保存</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                      showCancel: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                  })
                  } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                  wx.showModal({
                      title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">提示</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                      content: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">获取权限失败,将无法保存到相册哦~</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
                      showCancel: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                  })
                  }
                },
                fail: error </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  console.log(error)
                }
            })
            }
          }
      })
      },
      fail: error </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      }
    })
},
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 打开配置
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
onCatchSettingAgainHandler() {
    wx.openSetting({
      success: res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res)
      },
      fail: error </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(error)
      }
    })
},</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
</li>
</ul>
</li>
</ul>
</li>
<li>wx.cloud.deleteFile&nbsp; -----&nbsp; &nbsp;删除文件</li>
<li>wx.cloud.getTempFileURL&nbsp; -----&nbsp; &nbsp;获取临时链接</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong><span style="font-size: 16px">3、云开发的开通</span></strong></p>
<ul>
<li>打开微信开发工具,点击云开发 -----&gt; 开通 -----&gt; 确定
<p>&nbsp;<img alt="" data-src="https://img2018.cnblogs.com/i-beta/1730357/201912/1730357-20191212145958603-533470633.png"></p>
</li>
<li>会出现一个环境配置,每个小程序账号可免费创建两个环境,建议是一、开发环境;二、生产环境;
<ul>
<li><span style="color: rgba(128, 0, 0, 1)"><strong>注意:开发工具右上角点击 “详情” -----&gt; 本地设置(调试基础库的版本必须在2.2.3以上,才可以支持云开发)</strong></span></li>
</ul>
</li>
<li>云开发提供了一个<span style="color: rgba(128, 0, 0, 1)"><strong>可视化的控制台</strong></span>,点击云开发即可出现;</li>
</ul>
<p>4、遇到的问题</p>

</div>
<div id="MySignature" role="contentinfo">
    北栀女孩儿<br><br>
来源:https://www.cnblogs.com/wxh0929/p/12030677.html
頁: [1]
查看完整版本: 微信小程序开发(二)----- 云开发