码迷,mamicode.com
首页 > 微信 > 详细

微信小程序中,如果没有参数,如何设置默认参数?

时间:2019-06-26 22:04:18      阅读:1522      评论:0      收藏:0      [点我收藏+]

标签:objectid   现在   art   www   des   没有   false   头部   reac   

现在学会小程序,这方面的知识,需要积累。

现在的情况是这样:

如果想从后端获取产品列表,而这些列表是可以根据分类来获取的,也是可以获取所有产品的。

那么,为了不使小程序报错,那么,我们就可以将不传的参数设默认值为0,然后,传到后端。

var objectId = options.title||‘所有商品‘;

var cat_id = options.cat_id||0;
var ptype = options.ptype||0;
var brandId = options.brandId||0;

这种方式,就可以设置默认值。

在后端,根据参数是不是为0,来作条件过滤。

if(intval($id)){
             $where.=" AND cid=".intval($id);
         }
这样的写法,如果$id为0,则会返回false,而不会执行里面的逻辑。

搞定

小程序前端wxml

    <navigator url="../listdetail/listdetail"  class="item">
    <image src="../../static/images/more.png" background-size="cover"></image>
      <text>更多</text>
     </navigator>

或是

<navigator url="../listdetail/listdetail?cat_id={{item.id}}&title={{item.name}}" class="item {{(index+1) % 3 == 0 ? ‘last‘ : ‘‘}}" wx:for="{{typeTree}}"  wx:key="" wx:for-item="item">
                <image class="icon" src="{{item.bz_1}}"></image>
                <text class="txt">{{item.name}}</text>
            </navigator>

小程序js

  onLoad: function (options) {
    console.log(options);
    console.log(options.title);
    var objectId = options.title||‘所有商品‘;
    //更改头部标题
    wx.setNavigationBarTitle({
      title: objectId,
      success: function () {
      },
    });

    //页面初始化 options为页面跳转所带来的参数
    var cat_id = options.cat_id||0;
    var ptype = options.ptype||0;
    var brandId = options.brandId||0;
    var that = this;
    that.setData({
      ptype: ptype,
      catId: cat_id,
      brandId: brandId
    })
    //ajax请求数据
    wx.request({
      url: app.d.ceshiUrl + ‘/Api/Product/lists‘,
      method: ‘post‘,
      data: {
        cat_id: cat_id,
        ptype: ptype,
        brand_id: brandId
      },
      header: {
        ‘content-type‘: ‘application/x-www-form-urlencoded‘
      },
      success: function (res) {
        var shoplist = res.data.pro;
        that.setData({
          shopList: shoplist
        })
      },
      error: function (e) {
        wx.showToast({
          title: ‘网络异常!‘,
          duration: 2000
        });
      }
    })

  },

php后端:

    /**
     * 获取商品列表接口
     */
       public function lists(){
         $json="";
         $id=intval($_POST[‘cat_id‘]);//获得分类id 这里的id是pro表里的cid
         $brand_id = intval($_POST[‘brand_id‘]);
         // $id=44;
         $type=I(‘post.type‘);//排序类型

         $page= intval($_POST[‘page‘]) ? intval($_POST[‘page‘]) : 0;
         $keyword=I(‘post.keyword‘);
         //排序
         $order="addtime desc";//默认按添加时间排序
         if($type==‘ids‘){
             $order="id desc";
         }elseif($type==‘sale‘){
             $order="shiyong desc";
         }elseif($type==‘price‘){
             $order="price_yh desc";
         }elseif($type==‘hot‘){
             $order="renqi desc";
         }
         //条件
         $where="1=1 AND pro_type=1 AND del=0 AND is_down=0";
         if(intval($id)){
             $where.=" AND cid=".intval($id);
         }
         if (intval($brand_id)) {
             $where.=" AND brand_id=".intval($brand_id);
         }
         if($keyword) {
            $where.=‘ AND name LIKE "%‘.$keyword.‘%"‘;
        }
        if (isset($_REQUEST[‘ptype‘]) && $_REQUEST[‘ptype‘]==‘new‘) {
            $where .=‘ AND is_show=1‘; 
        }
        if (isset($_REQUEST[‘ptype‘]) && $_REQUEST[‘ptype‘]==‘hot‘) {
            $where .=‘ AND is_hot=1‘; 
        }
        if (isset($_REQUEST[‘ptype‘]) && $_REQUEST[‘ptype‘]==‘zk‘) {
            $where .=‘ AND is_sale=1‘; 
        }

         $product=M(‘product‘)->where($where)->order($order)->limit($page.‘,20‘)->select();
         //echo M(‘product‘)->_sql();exit;
         $json = array();$json_arr = array();
         foreach ($product as $k => $v) {
             $json[‘id‘]=$v[‘id‘];
             $json[‘name‘]=$v[‘name‘];
             $json[‘photo_x‘]=__DATAURL__.$v[‘photo_x‘];
             $json[‘price‘]=$v[‘price‘];
             $json[‘price_yh‘]=$v[‘price_yh‘];
             $json[‘shiyong‘]=$v[‘shiyong‘];
             $json[‘intro‘]=$v[‘intro‘];
             $json_arr[] = $json;
         }
         $cat_name=M(‘category‘)->where("id=".intval($id))->getField(‘name‘);
        $cat_pic=M(‘category‘)->where("id=".intval($id))->getField(‘bz_2‘);
         echo json_encode(array(‘status‘=>1,‘pro‘=>$json_arr,‘cat_name‘=>$cat_name,‘cat_pic‘=>$cat_pic));
         exit();
    }

 

微信小程序中,如果没有参数,如何设置默认参数?

标签:objectid   现在   art   www   des   没有   false   头部   reac   

原文地址:https://www.cnblogs.com/aguncn/p/11094094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!