微信小程序中,如果没有参数,如何设置默认参数?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微信小程序中,如果没有参数,如何设置默认参数?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3356字,纯文字阅读大概需要5分钟。
内容图文
现在学会小程序,这方面的知识,需要积累。
现在的情况是这样:
如果想从后端获取产品列表,而这些列表是可以根据分类来获取的,也是可以获取所有产品的。
那么,为了不使小程序报错,那么,我们就可以将不传的参数设默认值为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
onl oad: 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(); }
内容总结
以上是互联网集市为您收集整理的微信小程序中,如果没有参数,如何设置默认参数?全部内容,希望文章能够帮你解决微信小程序中,如果没有参数,如何设置默认参数?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。