欧联穆 發表於 2020-4-17 13:55:00

微信小程序个人入门开发

<p>以前做的程序主要是PC端网页,或者手机端H5页面,对于小程序开发还未接触过,今天抽点时间对小程序开发进行学习。</p>
<p>下面我将详细的记录从账号注册,设置,开发的每个步骤</p>
<p>&nbsp;</p>
<p>&nbsp;<strong>1. 注册开发账号</strong></p>
<p>&nbsp; 打开微信开发者页面,选择小程序注册&nbsp;&nbsp;https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&amp;lang=zh_CN&amp;token=</p>
<p>&nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416163013250-442455555.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; 选择个人开发者</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416163225451-1783350222.png"></p>
<p>&nbsp;</p>
<p>&nbsp; 注册成功后登陆系统&nbsp;</p>
<p>&nbsp;&nbsp;</p>
<p><strong>&nbsp;2. 后台设置</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp;2.1 新建一个小程序</strong></p>
<p><strong>&nbsp; &nbsp; <span style="color: rgba(255, 0, 0, 1)">注意&nbsp; &nbsp;个人账号下,一个账号只能创建一个小程序,需要创建多个小程序就需要在去注册不同的账号</span><span style="background-color: rgba(0, 0, 0, 1)"><br></span></strong></p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416170634060-1101360258.png"></strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; 2.2 AppId和AppSecret</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong>申请后这两个配置后续需要在程序中使用,一定要安全的保管</p>
<p>&nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416165915431-1135583256.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; 2.3&nbsp; 设置后台接口的域名</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; 只有设置后,前台调用后台的api接口才能调通,并且该域名必须是备案通过的</strong></p>
<p><strong>&nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416171055512-215943860.png"></strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>&nbsp; 3. 开发</strong></p>
<p><strong>&nbsp; &nbsp; 3.1 下载小程序开发工具</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp;https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html&nbsp; 下载自己电脑对应的版本</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200416171341778-1913573380.png"></strong></p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; 3.2 创建小程序前端项目</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 打开小程序开发工具,项目---》新建项目----》 小程序&nbsp; ---》 填入AppID</p>
<p>&nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200417094040883-95655652.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp;3.3 小程序前端开发</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; </strong>下面我将以一个表单录入为例,展示小程序的开发,里面不涉及到微信的一些权限,只是一个表单的保存动作,后面我们将通过其他章节再来学习小程序的一些鉴权或者微信支付等</p>
<p>&nbsp; &nbsp; &nbsp; </p>
<p>&nbsp; &nbsp;<strong> &nbsp; 3.3.1 项目的结构</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;其中<strong>页面在index.wxml</strong> 中&nbsp; ,<strong>逻辑在 index.js</strong> 中</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200417103621378-1025143774.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;<strong> &nbsp;3.3.2 页面 index.wxml</strong></p>
<p><strong>&nbsp; &nbsp;&nbsp;</strong>页面中使用了时间控件,下拉框控件,二级联动,单选按钮,以及选择控件</p>
<p>&nbsp; &nbsp; 修改页面或者js后,直接在左边就可以进行调试</p>
<p>&nbsp; &nbsp; 页面代码 <strong>index.wxml</strong> 如下</p>
<p>&nbsp; &nbsp; &nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">引入时间控件页面</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">import </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">="../picker/picker.wxss"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="container"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="bg-fff p-lr30 border-t"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>   
<span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tui-picker-content ipt-wrap border-b flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>日期<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">picker </span><span style="color: rgba(255, 0, 0, 1)">mode</span><span style="color: rgba(0, 0, 255, 1)">="date"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{dateSelectd}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)"> start</span><span style="color: rgba(0, 0, 255, 1)">="1975-01-01"</span><span style="color: rgba(255, 0, 0, 1)"> end</span><span style="color: rgba(0, 0, 255, 1)">="2099-01-01"</span><span style="color: rgba(255, 0, 0, 1)"> bindchange</span><span style="color: rgba(0, 0, 255, 1)">="changeDate"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tui-picker-detail ml40 flex1"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">          {{dateSelectd}}
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">16</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">picker</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="ipt-wrap border-b flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">20</span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>部门<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">picker </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="picker_hx"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="ml40 flex1 region"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{depts.deptId}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)">range</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{depts}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)"> range-key</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{'deptName'}}</span><span style="color: rgba(0, 0, 255, 1)">"</span>   
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(255, 0, 0, 1)">   bindchange</span><span style="color: rgba(0, 0, 255, 1)">="bindPickerChange_hx"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="picker"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">         {{depts.deptName}}
</span><span style="color: rgba(0, 128, 128, 1)">25</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">26</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">picker</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">27</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span>
<span style="color: rgba(0, 128, 128, 1)">30</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="ipt-wrap border-b flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">31</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>姓名<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">32</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">picker </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="picker_emp"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="ml40 flex1 region"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{emps.empId}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)">range</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{emps}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)"> range-key</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{'empName'}}</span><span style="color: rgba(0, 0, 255, 1)">"</span>   
<span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(255, 0, 0, 1)">bindchange</span><span style="color: rgba(0, 0, 255, 1)">="bindEmp"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">34</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="picker"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(0, 0, 0, 1)">         {{emps.empName}}
</span><span style="color: rgba(0, 128, 128, 1)">36</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">37</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">picker</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">38</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">39</span>
<span style="color: rgba(0, 128, 128, 1)">40</span>
<span style="color: rgba(0, 128, 128, 1)">41</span>
<span style="color: rgba(0, 128, 128, 1)">42</span>
<span style="color: rgba(0, 128, 128, 1)">43</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">44</span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">=" bg-fff p30 flex ai-center jc-sb mt20"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">45</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">46</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>用餐时间<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">47</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">48</span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">radio-group </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="radio-group"</span><span style="color: rgba(255, 0, 0, 1)"> bindchange</span><span style="color: rgba(0, 0, 255, 1)">="lunchOrDinerBind"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>   
<span style="color: rgba(0, 128, 128, 1)">49</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="radio"</span><span style="color: rgba(255, 0, 0, 1)"> wx:for</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{lunchOrDiner}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>      
<span style="color: rgba(0, 128, 128, 1)">50</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">radio </span><span style="color: rgba(255, 0, 0, 1)">value</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{item.name}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)"> checked</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{item.checked}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span><span style="color: rgba(0, 0, 0, 1)">{{item.name}}   
</span><span style="color: rgba(0, 128, 128, 1)">51</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">52</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">radio-group</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">53</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">54</span>
<span style="color: rgba(0, 128, 128, 1)">55</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">=" bg-fff p30 flex ai-center jc-sb mt20"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">56</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">57</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>饭菜类型<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">58</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">59</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">radio-group </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="radio-group"</span><span style="color: rgba(255, 0, 0, 1)"> bindchange</span><span style="color: rgba(0, 0, 255, 1)">="mealTypeBind"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>   
<span style="color: rgba(0, 128, 128, 1)">60</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="radio"</span><span style="color: rgba(255, 0, 0, 1)"> wx:for</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{mealType}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>      
<span style="color: rgba(0, 128, 128, 1)">61</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">radio </span><span style="color: rgba(255, 0, 0, 1)">value</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{item.name}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(255, 0, 0, 1)"> checked</span><span style="color: rgba(0, 0, 255, 1)">="</span><span style="color: rgba(128, 128, 0, 1)">{{item.checked}}</span><span style="color: rgba(0, 0, 255, 1)">"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span><span style="color: rgba(0, 0, 0, 1)">{{item.name}}   
</span><span style="color: rgba(0, 128, 128, 1)">62</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">63</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">radio-group</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">64</span>   
<span style="color: rgba(0, 128, 128, 1)">65</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">66</span>
<span style="color: rgba(0, 128, 128, 1)">67</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">=" bg-fff p30 flex ai-center jc-sb mt20"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">68</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="flex ai-center"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">69</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">label </span><span style="color: rgba(255, 0, 0, 1)">for</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="font14"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>是否用餐<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">label</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">70</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">71</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="flex"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">72</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">switch </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="switch"</span><span style="color: rgba(255, 0, 0, 1)"> bindchange</span><span style="color: rgba(0, 0, 255, 1)">="eatOrNotBind"</span><span style="color: rgba(255, 0, 0, 1)"> color</span><span style="color: rgba(0, 0, 255, 1)">="#279efd"</span><span style="color: rgba(255, 0, 0, 1)"> checked</span><span style="color: rgba(0, 0, 255, 1)">="checked"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">switch</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">73</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">74</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">75</span>
<span style="color: rgba(0, 128, 128, 1)">76</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="combtn font16 color-fff _w100 bg-btn"</span><span style="color: rgba(255, 0, 0, 1)"> data-url</span><span style="color: rgba(0, 0, 255, 1)">="../addNewAddress/addNewAddress"</span><span style="color: rgba(255, 0, 0, 1)"> bindtap</span><span style="color: rgba(0, 0, 255, 1)">="saveNewAddress"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">77</span> <span style="color: rgba(0, 0, 0, 1)">    保存
</span><span style="color: rgba(0, 128, 128, 1)">78</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">79</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;<strong>&nbsp;&nbsp;3.3.3 页面 index.js</strong></p>
<p>&nbsp; &nbsp; 页面控件的绑定数据,绑定时间,校验,从后台取数据都在改方法中</p>
<p>&nbsp; &nbsp; 我们定义了一个eat.js 所有的对后台接口的操作都在改js中执行 ,在index.js 中对eat.js做了引入</p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp;3.3.3.1&nbsp; index.js 代码</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp;</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> const common = require('../../utils/common');</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> const api = require('../../utils/api');</span>
const eat = require("../../utils/eat.js"<span style="color: rgba(0, 0, 0, 1)">);
const dateTimePicker </span>= require('../../utils/dateTimePicker.js'<span style="color: rgba(0, 0, 0, 1)">);
const app </span>=<span style="color: rgba(0, 0, 0, 1)"> getApp();

Page({
data: {
    dateSelectd:</span>''<span style="color: rgba(0, 0, 0, 1)">,
    depts:[],
    dept_index: </span>0<span style="color: rgba(0, 0, 0, 1)">,
    deptId:</span>0<span style="color: rgba(0, 0, 0, 1)">,
    emps:[],
    emp_index:</span>0<span style="color: rgba(0, 0, 0, 1)">,
    lunchOrDiner: [{id:</span>1,name:'午餐',checked:<span style="color: rgba(0, 0, 255, 1)">true</span>},{id:2,name:'晚餐'<span style="color: rgba(0, 0, 0, 1)">}],
    lunchOrDinerValue:</span>'午餐'<span style="color: rgba(0, 0, 0, 1)">,
    mealType:[{id:</span>1,name:'荤素',checked:<span style="color: rgba(0, 0, 255, 1)">true</span>},{id:2,name:'纯素'<span style="color: rgba(0, 0, 0, 1)">}],
    mealTypeValue :</span>"荤素"<span style="color: rgba(0, 0, 0, 1)">,
    eatOrNot:</span>'是'<span style="color: rgba(0, 0, 0, 1)">,
    userID: </span>0<span style="color: rgba(0, 0, 0, 1)">
},
onLoad: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (){
    let self </span>= <span style="color: rgba(0, 0, 255, 1)">this</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({ userID: app.globalData.userID });
    let dateDefault </span>=<span style="color: rgba(0, 0, 0, 1)"> eat.getDate();
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({ dateSelectd: dateDefault });
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.getDept() <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)">},
changeDate:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e){
    let dateSelectd </span>=<span style="color: rgba(0, 0, 0, 1)"> e.detail.value;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({ dateSelectd:dateSelectd });
},
getDept: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
    eat.getDept().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({ depts:res });
   </span><span style="color: rgba(0, 0, 255, 1)">if</span>(res &amp;&amp; res.length &gt; 0<span style="color: rgba(0, 0, 0, 1)">){
      eat.getEmp(res[</span>0].deptId).then(res =&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)">.setEmp(res);
       });
   }
    })
},
bindPickerChange_hx: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (e) {
    console.log(</span>'picker发送选择改变,下标为'<span style="color: rgba(0, 0, 0, 1)">, e.detail.value);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.setData({   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给变量赋值</span>
      dept_index: e.detail.value,<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)">   });
   let deptId </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.data.depts.deptId;
   console.log(</span>"deptId:"+<span style="color: rgba(0, 0, 0, 1)">deptId);
   eat.getEmp(deptId).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)">.setEmp(res);
   });
   
   },
   setEmp:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(emps){
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({ emps:emps });
   },
   bindEmp: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (e) {
    console.log(</span>'picker发送选择改变,下标为'<span style="color: rgba(0, 0, 0, 1)">, e.detail.value);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.setData({   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给变量赋值</span>
      emp_index: e.detail.value,<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)">   });
   },
   lunchOrDinerBind:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {
   let lunchOrDinerValue </span>=<span style="color: rgba(0, 0, 0, 1)"> e.detail.value;
   console.log(</span>"lunchOrDinerValue:"+<span style="color: rgba(0, 0, 0, 1)">lunchOrDinerValue);
   </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({ lunchOrDinerValue: lunchOrDinerValue });
   },
   mealTypeBind:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (e) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData({   
      mealTypeValue: e.detail.value,
   });
   },
   eatOrNotBind:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event){
    let checkedValue </span>=<span style="color: rgba(0, 0, 0, 1)"> event.detail.value;
    console.log(</span>"checkedValue:"+<span style="color: rgba(0, 0, 0, 1)">checkedValue);
    let eatOrNot;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(checkedValue){
      eatOrNot </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)">{
      eatOrNot </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({   
      eatOrNot:eatOrNot,
   });
   },
saveNewAddress: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
    let self </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
       let dateStr </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.dateSelectd
       let deptId </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.depts.deptId;
       let deptName </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.depts.deptName;
       let empId </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.emps.empId;
       let empName </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.emps.empName;
       let lunchOrDinerValue </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.lunchOrDinerValue;
       let mealTypeValue </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.mealTypeValue;
       let eatOrNot </span>=<span style="color: rgba(0, 0, 0, 1)"> self.data.eatOrNot;

       let today </span>= eat.getDateStr(0<span style="color: rgba(0, 0, 0, 1)">);
       let tommorrow </span>= eat.getDateStr(1<span style="color: rgba(0, 0, 0, 1)">);
       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(dateStr !=today &amp;&amp; dateStr !=<span style="color: rgba(0, 0, 0, 1)"> tommorrow){
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('只能定今天和明天的餐哦'<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, 0, 1)">;
       }
       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(dateStr ==<span style="color: rgba(0, 0, 0, 1)"> today){
      let date</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
      let hour </span>=<span style="color: rgba(0, 0, 0, 1)"> date.getHours();
      let minute </span>=<span style="color: rgba(0, 0, 0, 1)"> date.getMinutes();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(lunchOrDinerValue == "晚餐"<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)">晚餐时间必须在13:30-17:30</span>
          <span style="color: rgba(0, 0, 255, 1)">if</span>(hour&lt;13 || hour&gt;17<span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('晚餐订购时间为13:30-17:30'<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, 0, 1)">;
          }
          </span><span style="color: rgba(0, 0, 255, 1)">if</span>(hour == 13 &amp;&amp; minute &lt; 30<span style="color: rgba(0, 0, 0, 1)"> ){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('晚餐订购时间为13:30-17:30'<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, 0, 1)">;
          }
          </span><span style="color: rgba(0, 0, 255, 1)">if</span>(hour == 17 &amp;&amp; minute&gt;30<span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('晚餐订购时间为13:30-17:30'<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, 0, 1)">;
          }
      }</span><span style="color: rgba(0, 0, 255, 1)">else</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)">午餐订购时间为09:00-11:30</span>
          <span style="color: rgba(0, 0, 255, 1)">if</span>(hour &lt;9 || hour &gt; 11<span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('午餐订购时间为09:00-11:30'<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, 0, 1)">;
          }
          </span><span style="color: rgba(0, 0, 255, 1)">if</span>(hour == 11 &amp;&amp; minute &gt;30<span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('午餐订购时间为09:00-11:30'<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, 0, 1)">;
          }
      }
       };
       wx.showModal({
         title: </span>'提示'<span style="color: rgba(0, 0, 0, 1)">,
         content: </span>'确定订餐么'<span style="color: rgba(0, 0, 0, 1)">,
         success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.confirm) {
</span><span style="color: rgba(0, 0, 0, 1)">            wx.showLoading({
                title: </span>'订餐中...'<span style="color: rgba(0, 0, 0, 1)">,
                });
            eat.save(deptId,deptName,empId,empName,dateStr,lunchOrDinerValue,eatOrNot,mealTypeValue).then(res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)">{
                wx.hideLoading();
                console.log(</span>"result:"+<span style="color: rgba(0, 0, 0, 1)">res);

                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(res == 0<span style="color: rgba(0, 0, 0, 1)">){
                  self.alertError(</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, 0, 1)">;
                }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
                  self.alertError(</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, 0, 1)">;
                }
                });
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.cancel) {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.alertError('用户撤销订餐'<span style="color: rgba(0, 0, 0, 1)">);
            }
      }
      });
},

alertSucess:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(msg){
    wx.showToast({
      title: msg</span>+''<span style="color: rgba(0, 0, 0, 1)">,
      icon: </span>'success'<span style="color: rgba(0, 0, 0, 1)">,
      duration: </span>2000<span style="color: rgba(0, 0, 0, 1)">
   })
},
alertError:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(msg){
    wx.showToast({
      title: msg</span>+''<span style="color: rgba(0, 0, 0, 1)">,
      icon: </span>'none'<span style="color: rgba(0, 0, 0, 1)">,
      duration: </span>2000<span style="color: rgba(0, 0, 0, 1)">
   })
}
});</span></pre>
</div>
<p>&nbsp;&nbsp;<strong>&nbsp; &nbsp;</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp;3.3.3.2&nbsp; eat.js 代码</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong></p>
<div class="cnblogs_code">
<pre>let backend_url = 'https://xxxx.com'<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)">*
* 查询部门列表,数据格式如下
* [{"deptId":33,"deptName":"开发部"}]
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getDept(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve,reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    wx.request({
      url: backend_url</span>+'/eat/getDeptList'<span style="color: rgba(0, 0, 0, 1)">,
      data: {
      },
      header: {
          </span>'content-type': 'application/json'<span style="color: rgba(0, 0, 0, 1)">
      },
      success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
      resolve(res.data)
      }
    })
})
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 查询部门下的员工,返回格式如下,
* [{"empId":1,"empName":"devin","deptId":33},{"empId":2,"empName":"zbb","deptId":33}]
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getEmp(deptId){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve,reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    wx.request({
      url: backend_url</span>+'/eat/getEmpList?deptId='+<span style="color: rgba(0, 0, 0, 1)">deptId,
      data: {
      },
      header: {
          </span>'content-type': 'application/json'<span style="color: rgba(0, 0, 0, 1)">
      },
      success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
      resolve(res.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, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> save(dept,deptName,emp,empName,today,eatType,eatIf,eatMealType){

</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve,reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    wx.request({
      url: backend_url</span>+'/eat/save'<span style="color: rgba(0, 0, 0, 1)">,
      data: {
      deptId: dept,
      deptName: deptName,
      empId: emp,
      empName: empName,
      eatDate: today,
      eatType: eatType,
      eatIf: eatIf,
      eatMealType : eatMealType
      },
      header: {
          </span>'content-type': 'application/json'<span style="color: rgba(0, 0, 0, 1)">
      },
      success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
      resolve(res.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, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getDate(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> date = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> mon = date.getMonth() + 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> day =<span style="color: rgba(0, 0, 0, 1)"> date.getDate();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> nowDay = date.getFullYear() + "-" + (mon&lt;10?"0"+mon:mon) + "-" +(day&lt;10?"0"+<span style="color: rgba(0, 0, 0, 1)">day:day);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> nowDay;
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 获取日期
* getDateStr(0) 今天
* getDateStr(-1) 昨天
* getDateStr(1) 明天
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getDateStr(addDayCount) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> dd = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
dd.setDate(dd.getDate()</span>+addDayCount);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取addDayCount天后的日期</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> y =<span style="color: rgba(0, 0, 0, 1)"> dd.getFullYear();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> m = dd.getMonth()+1;<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)">var</span> d =<span style="color: rgba(0, 0, 0, 1)"> dd.getDate();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> mStr = m&gt;=10?m:'0'+<span style="color: rgba(0, 0, 0, 1)">m;
</span><span style="color: rgba(0, 0, 255, 1)">return</span> y+"-"+mStr+"-"+<span style="color: rgba(0, 0, 0, 1)">d;
}

module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> {
getDate:getDate,
getDateStr:getDateStr,
getDept: getDept,
getEmp:getEmp,
save:save,
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp;后端代码不在此做展示,只需要返回正确格式的json即可</p>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp;4. 发布</strong></p>
<p>&nbsp; &nbsp; &nbsp;程序开发完成后,点击开发工具中的上传按钮,即可将代码上传到服务端,此时登录微信的后台管理端,就可以看到我们上传的前端程序,点击发布,待微信审核通过后,就可以在微信中使用小程序了。</p>
<p>&nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200417115958002-486171915.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1917367/202004/1917367-20200417120302557-595980370.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;</p>
<p>&nbsp;5. 代码</p>
<p>&nbsp; 完整的代码已经上传到github上了,有需要的小伙伴可以下载参考使用</p>
<p>&nbsp; &nbsp;https://github.com/devinzhang0209/miniprogram-eat.git</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p></p>
<p></audio></p><br><br>
来源:https://www.cnblogs.com/DevinZhang1990/p/12714748.html
頁: [1]
查看完整版本: 微信小程序个人入门开发