Code implementation of adding, deleting, modifying, and checking operations of WeChat mini programs

不言
Release: 2018-06-23 15:39:58
Original
6891 people have browsed it

This article mainly introduces the relevant information on the detailed operation examples of adding, deleting, modifying and checking the WeChat applet. Here is the example code. Friends in need can refer to it

WeChat applet Detailed explanation of adding, deleting, modifying and checking operation examples

1. Take the adding, deleting, modifying and checking of the delivery address as an example

2.File directory

  1. js file is a logical control, mainly it sends requests and receives data.

  2. json is used for local configuration of this page and covers the global app .json configuration,

  3. wxss is used for page style setting,

  4. wxml is the page, equivalent to html

<form bindsubmit="addSubmit">

<view class="consignee">

<text class="consignee-tit">收货人信息</text>

<view class="consignee-main">

<view class="flex flex-align-center flex-pack-justify">

<text>姓名</text>

<input class="flex-1" name="name" type="number" maxlength="11" placeholder="请输入收货人姓名" />

</view>

<view class="flex flex-align-center flex-pack-justify">

<text>电话</text>

<input class="flex-1" name="mobile" type="number" maxlength="11" placeholder="请输入手机号" />

</view>

<view class="flex flex-align-center flex-pack-justify">

<text>地址</text>

<input class="flex-1" name="address" type="text" maxlength="11" placeholder="请输入地址" />

</view>

</view>

</view>

<view class="delivery-time flex flex-align-center flex-pack-justify">

<text>送货时间</text>

<picker mode="date"></picker>

</view>

<view class="receipt-address">

<view class="receipt-address-tit">收货地址信息</view>

<view wx:for="{{addressInfo}}" wx:key="unique">

<view class="receipt-address-list clearfix">

<image src="../../images/address-icon.png"></image>

<view class="address-list-main">

<view class="clearfix"><text>收货地址{{item.address}}</text><text>1km</text></view>

<view class="address-info">收货人{{item.name}}</view>

<view class="address-time">收货人电话{{item.mobile}}</view>

<view data-deleteid="{{item.id}}" bindtap="deleteClick">删除</view>

<view data-editid="{{item.id}}" bindtap="editClick">编辑</view>

</view>

</view>

</view>

</view>

<view class="receipt-true">

<button class="btn_login" formType="submit">保存</button>

</view>

</form>
Copy after login

The front-end page mainly displays a form and existing consignee information

1. Several key points need to be understood

a.Form form needs to bind a submit event. In the applet, the attribute is bindsubmit,

bindsubmit=”formSubmit” The attribute value here is formSubmit, and the naming can be consistent with Any value of the specification is equivalent to onsubmit="formSubmit()" in previous HTML. is a function name. The formSubmit function event is triggered when submitted. This function is written in js.

b. Other attributes are similar to the previous HTML. Note that the form must have name="value", and the back-end processing is the same as before. For example, name="username" PHP can use $_POST[ 'username'] to receive.

c. Since the mini program does not have an input submit button, there must be a submit button in each form,

,这个按钮就是用来开启提交事件的。

d.由于添加地址和编辑地址都是在一个页面的,所以我需要在每个表单中,加一个默认值变量,当点击修改的时候,默认值就显示在输入框中。

e.表单中有一个编辑,绑定了事件editClick,当点击这个按钮的时候,就会进入编辑模式

添加和修改的放在一个函数里面,但是修改数据的显示是另外一个函数

先说修改,点点击编辑的时候,触发editClick事件

JS如下:

editClick:function(event){

  var that = this;

  var id = event.currentTarget.dataset.editid;

  wx.request({

   url: 'https://shop.yunapply.com/home/shipping/edit?id='+id,

   data: {},

   method: 'GET',

   success: function(res){

    if(res.data.status == 0){

     wx.showToast({

      title: res.data.info,

      icon: 'loading',

      duration: 1500

     })

    }else{

     that.setData({

       "addressEdit": res.data.info,

     })

    }

   },

   fail:function(){

       wx.showToast({

        title: &#39;服务器网络错误!&#39;,

        icon: &#39;loading&#39;,

        duration: 1500

       })

      }

  })

 },
Copy after login

为了更好理解,贴个图

最下面有一个保存按钮,当点击编辑的时候,触发editClick:function(event),这个event是当前触发事件的对象,

var id = event.currentTarget.dataset.editid; 就是获取当前事件对象的dataset中的editid的值,这里id是当前地址的id

url: 'https://shop.com/home/shipping/edit?id='+id

Wx.request 的url,将id值放在url上,作为GET参数,传递到服务器。

data: {},是需要额外传递的数据

method: 'GET', 是数据传递方式 默认是“GET”,保持大写

data:{mobile:e.detail.value.mobile,password:e.detail.value.password},

这里的data就是POST给服务器端的数据 以{name:value}的形式传送

success:function()是请求状态成功触发是事件,也就是200的时候,注意,请求成功不是操作成功,请求只是这个程序到服务器端这条线的通的。

fail:function()就是网络请求不成功,触发的事件。

这里的一段代码是和PHP后端程序有关系的,具体流程是这样的,

1.GET通过数据到https://shop.com/home/Shipping/edit这个接口,用过THINKPHP的就会知道是HOME模块下的Shipping控制下的edit方法

2.后端PHP代码如下:

控制器 ShippingController.class.php

public function edit($id)
{
  $res = D(&#39;Shipping&#39;)->find($id);
  $this->success($res,&#39;&#39;,true);
}
Copy after login

也就是说将这条数据取出来,没什么好说的。

 that.setData({

       "addressEdit": res.data.info,

     })

    }
Copy after login

这里请求成功以后,调用小程序 setData方法,将服务器端返回的信息放到addressEdit[]中,然后在前端页面调用{{addressEdit.id}}、{addressEdit.name}}、{addressEdit.mobile}}、{addressEdit.address}}将数据展示出来,这就是修改时候的操作。

接下来是提交表单的操作

Js代码如下

addSubmit:function(e){

  if(e.detail.value.mobile.length==0||e.detail.value.name.length==0 ||e.detail.value.address.length==0){

   wx.showToast({

    title: '收货人所有信息不得为空!',

    icon: 'loading',

    duration: 1500

   })

  }else if(e.detail.value.mobile.length != 11){

    wx.showToast({

    title: '请输入11位手机号码!',

    icon: 'loading',

    duration: 1500

   })

  }else{

   wx.request({ 

      url: &#39;https://shop.yunapply.com/home/shipping/save&#39;, 

      header: { 

       "Content-Type": "application/x-www-form-urlencoded" 

      },

      method: "POST",

      data:{id:e.detail.value.id,mobile:e.detail.value.mobile,name:e.detail.value.name,address:e.detail.value.address},

      success: function(res) {

       if(res.data.status == 0){

         wx.showToast({

          title: res.data.info,

          icon: 'loading',

          duration: 1500

         })

       }else{

         wx.showToast({

          title: res.data.info,

          icon: 'success',

          duration: 1000

         })

         setTimeout(function(){

          wx.navigateTo({

           url:'../address/index'

          })

         },1000)

       }

      },

      fail:function(){

       wx.showToast({

        title: &#39;服务器网络错误!&#39;,

        icon: &#39;loading&#39;,

        duration: 1500

       })

      }  

     })

  }

 }
Copy after login

在前端的FORM表单中,当点击formtype=“submit”这个按钮的时候,触发addSubmit事件,前面的if都是JS验证,防止用户不填写信息。

1.其他的request请求差不多,找几个不一样的

url: &#39;https://shop.yunapply.com/home/shipping/save&#39;,
Copy after login

调用服务器端的save方法

  header: { 

    "Content-Type": "application/x-www-form-urlencoded" 

   },
Copy after login

由于POST和GET传送数据的方式不一样,POST的header必须是

"Content-Type": "application/x-www-form-urlencoded"
Copy after login

GET的header可以是 'Accept': 'application/json'

 data:{id:e.detail.value.id,mobile:e.detail.value.mobile,name:e.detail.value.name,address:e.detail.value.address},
Copy after login

这里是需要POST到服务器端的数据

Save方法代码

public function save()
{
  //$user_id
  $user_id = 2;
  if (IS_POST){
    $shipping = D(&#39;Shipping&#39;);
    if (!$shipping->create()){
      $this->error($shipping->getError(),&#39;&#39;,true);
    }else{
      if (is_numeric($_POST[&#39;id&#39;])){
        if ($shipping->editAddress($_POST[&#39;id&#39;])){
          $this->success(&#39;地址修改成功&#39;,&#39;&#39;,true);
        }else{
          $this->error(&#39;地址修改失败&#39;,&#39;&#39;,true);
        }
      }else{
        if ($shipping->addAddress($user_id)){
          $this->success(&#39;添加地址成功&#39;,&#39;&#39;,true);
        }else{
          $this->error(&#39;添加地址失败&#39;,&#39;&#39;,true);
        }
      }
    }
  }
}
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

微信小程序 向左滑动删除功能的实现

微信小程序button组件的使用介绍

The above is the detailed content of Code implementation of adding, deleting, modifying, and checking operations of WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template