微信小程序-简单出售商品示例

首先打开app.json修改如下

{
  "pages":[
    "pages/index/index",
    "pages/search/search",
    "pages/publish/publish"

  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#03bbd5",
    "navigationBarTitleText": "标题",
    "navigationBarTextStyle":"white",
    "enablePullDownRefresh":true,
    "backgroundColor":"#ccc"
  }
}

布局第一个界面,如图所示

图片名称

代码如下,index.wxml

<view>
    <view class="mapArea">
    <map 
    id="map" 
    longitude="{{longitude}}" 
    latitude="{{latitude}}" 
    scale="10" 
    controls="{{controls}}" 
    bindcontroltap="handleControlTap" 
    markers="{{markers}}" 
    bindmarkertap="markertap" 
    polyline="{{polyline}}" 
    bindregionchange="regionchange" 
    show-location 
    style="width: 100%; height: 100%;">

    </map>
    </view>

    <!-- <button bindtap="bindViewTap">tap</button> -->
    <view class="nav">
        <view class="publish"><navigator url="../publish/publish">发布</navigator></view>
        <view class="search">搜索</view>
    </view>
</view>

首先布局下方”发布”,”搜索”部分,index.wxss文件如下

.nav
{ 
  height: 42px;
  width: 100%;
  position: absolute;
  /* top: 100%; */
  bottom: 0px;
  display: flex;
  color: #fff;
}
.mapArea{
  /* height: 500px; */
  bottom: 42px;  
  width: 100%;
  top: 0px;
  left: 0px;
  right: 0px;
  position: absolute;
  /* background-color: black; */
}
.publish,.search{
  text-align: center;
  line-height: 42px;
  height: 42px;
  flex: 1; /*占用1/2,当改成2时,则占用2/3*/

}
/* 以下的内容可以整合到上方.publish,.search{}中 */
.publish
{
  background-color: #ff9700;
  /* width: 50%; */ 
  /* float: left;  当下方方块多了的时候就不好用了*/
}
.search
{
  text-align: center;
  line-height: 42px;
  /* width: 50%; */
  background-color: #03bbd5;
  height: 42px;
  /* float: right; */
  flex: 1;
}

然后我们给地图打点,添加定位图标,以及可以回到原点的控件,需要再index.js中
打点是采用ajax向后端获取数据,然后循环写入markers数组中,
定位图标,回到原点的控件这个需要获取屏幕的高度和宽度

Page({
  onReady:function(){
    this.mapContext = wx.createMapContext('map')
  },

  data: {
    markers: [],

    latitude:'',
    longitude:'',

    controls: [{
      id: 1,
      iconPath: 'center.png',
      position: {
        left: 10,
        top: wx.getSystemInfoSync().windowHeight-100,
        width: 30,
        height: 30
      },
      clickable: true
    },
    {
     id: 2,
      iconPath: 'pin.png',
      position: {
        left: wx.getSystemInfoSync().windowWidth/2-10,
        top: (wx.getSystemInfoSync().windowHeight-42)/2 - 30,
        width: 22,
        height: 31
      },
      clickable: true
      }
    ]
  },

  handleControlTap: function (e) {
    console.log(e.controlId)
    if (e.controlId ==1) {
      this.mapContext.moveToLocation()
    }
  },

  onLoad: function() {
    var that = this;  

    wx.getLocation({
      type:'gcj02',
      success:function(res){
        that.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
      }
    })


    var obj = null
    var markerss = [];

    wx.request({
  url: 'https://felixlu.duapp.com/index.php/trade/get_list', //仅为示例,并非真实的接口地址
  method:'GET',
  header: {
      'content-type': 'application/x-www-form-urlencoded' //告诉提交给后台的文件类型
  },
  success: function(res) {
    for (var i =0;i<res.data.data.length;i++){

     if(res.data.data[i].type == 'buy'){
    obj = {
      iconPath: "buy.png",
      id: res.data.data[i].id,
      latitude:res.data.data[i].latitude,
      longitude:res.data.data[i].longitude,
      width: 20,
      height: 20
    }
    markerss.push(obj)
  }else{
     obj = {
      iconPath: "test1.png",
      id: res.data.data[i].id,
      latitude:res.data.data[i].latitude,
      longitude:res.data.data[i].longitude,
      width: 20,
      height: 20
    }
    markerss.push(obj)

  }
  }
     that.setData({
     markers:markerss
   })
}
})
  },
})


第二个界面,如图

图片名称

我们新建publish文件夹

<view class="wrap">
    <view class="form-area">
    <view class="label">我的地址</view>
    <view class="text" bindtap="handleChooseTap">{{addr}}</view>
    </view>

    <view class="form-area">
    <view class="label">类型</view>
    <view class="text">
    <radio-group bindchange="handleChangeType" class="radio-group" >
      <label class="radio">
      <!-- 点击后获取e.detail.value值buy -->
        <radio value="buy" />求购
      </label>
       <label class="radio">
        <radio value="sell"/>转让
      </label>
    </radio-group>
</view>
    </view>

    <view class="form-area">
    <view class="label">说明</view>
    <view class="text">
        <input bindinput="handleInputMessage" placeholder="请输入说明" />
    </view> 
    </view>

    <view class="form-area">
    <view class="label">联系方式</view>
    <view class="text">
    <view class="text">
        <input bindinput="handleInputContact"placeholder="请输入联系方式" />
    </view> 
    </view> 
    </view>

    <view class="button">
    <button class="btn" type="primary"
        bindtap="handleInputSubmit"> 发布信息 </button>
    </view>
</view>

publish.wxss

.form-area{
    height: 100rpx;
    width: 100%;
    border-bottom: 1px #ccc solid;/*如果是border则是双线,上下两个边框*/
    display: flex;
    font-size: 18px;
    color: #666;
    background-color: #FAF5F5;
}
.label,.text{
    line-height: 100rpx;
}
.label{
    padding-left: 10px; 
    width: 100px;

}
.text{
    flex: 1;

}

input
{   /* width: 300px;
    margin: 10px; */
    /* background: #ff9700; */
    /* text-align: center;
    line-height: 46px; */
    margin-top: 11px;
}
.btn{
    margin: 10px;
    text-align: center;
    line-height: 46px;
    color: ;
    font-size: ;

}

这个界面有几个关键操作,
1.利用微信给的组件选择位置后,第一行需要显示出来
2.发布商品后,需要再地图上打点
publish.js代码如下

// pages/publish/publish.js
Page({
  data: {
    addr:'点击选择,要勾选哦'
   },
   staticData:{
    type:'',
    message:'',
    contact:'',
    latitude:'',
    longitude:''
   },
  handleChooseTap: function(){
    var that = this;
    wx.chooseLocation({
    success: function(res){
      // console.log(that.data.address);返回点击选择,要勾选哦
      that.setData({
        addr: res.address  //替换addr
      })
      that.staticData.latitude = res.latitude;
      that.staticData.longitude = res.longitude;
    }
  })
 },
 handleChangeType: function(e){
  // console.log(e)
  this.staticData.type = e.detail.value
 },
 handleInputMessage: function(e){
  // console.log(e)
  this.staticData.message = e.detail.value
 },
 handleInputContact:function(e){
  this.staticData.contact = e.detail.value

 },
// 绑定按钮
   handleInputSubmit: function(){
    //这就是ajax
    // console.log(0)
    wx.request({
  url: 'https://felixlu.duapp.com/index.php/trade/add_item', //仅为示例,并非真实的接口地址
  method:'POST',
  data: {
     address: this.data.addr,
     latitude:this.staticData.latitude,
     longitude: this.staticData.longitude,
     message: this.staticData.message,
     contact: this.staticData.contact,
     type: this.staticData.type
  },
  header: {
      'content-type': 'application/x-www-form-urlencoded' //告诉提交给后台的文件类型
  },
  success: function(res) {
    // console.log(res.data)
    if (res.data.ret) {
      // console.log('success');
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000
})
    }else{
      condole.log('fail');
    }
  }
})
    wx.navigateTo({
      url: '../index/index'
})
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

搜索界面

图片名称

此部分尚未完成

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序是一种基于微信平台的应用的开发模式,可以快速的开发出符合用户需求的小程序。在小程序的开发中,组件是一个非常重要的概念,通过组件可以实现复用性和模块化编程思想。 组件应用是小程序开发的基础。通过组件可以将某一模块化并封装起来,使得组件可以在不同的页面间得到复用,大大提升了开发效率并减少了代码冗余。微信小程序提供了丰富的自带组件,包括文本、图片、按钮、输入框等等,开发者也可以自己开发组件来满足自己的需求。实际开发中,通过组件可以快速搭建页面框架和业务逻辑。 Demo是一个演示小程序的示例程序。在小程序的实际开发过程中,一个好的Demo非常重要。通过Demo,开发人员可以更深入的了解小程序的开发流程、组件的应用和实际的业务开发等等。在Demo中,通常会包括小程序的一些基础操作,如页面跳转、数据绑定、组件的使用等。而在实际开发中,Demo还会包括一些复杂的业务场景,如支付、登录、数据列表展示等等。Demo不仅为开发者提供了学习和实践的机会,也方便了使用者了解该小程序的功能和特点。 总之,微信小程序组件的应用和Demo的开发都是小程序开发过程中非常重要的两个部分。良好的组件应用和精心设计的Demo,可以在极短的时间内实现小程序开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值