首页 > 编程语言 >微信小程序:发布动态页面模板

微信小程序:发布动态页面模板

时间:2023-08-17 19:31:55浏览次数:34  
标签:function const tempFilePaths 微信 create res 模板 页面

在这里插入图片描述

1 前言

由于功能需求,需要在小程序中开发社区打卡模块。打卡模块中上传发布的界面是必不可少的。于是利用flex布局设计了上传动态的页面。页面截图如下: 在这里插入图片描述 由于是模板分享,这里也不做过多介绍了,通过代码来说明吧。 在这里插入图片描述 页面主要有四个文件,分别是create.js、create.json、create.wxml、create.wxss。

2 代码

create.wxml

<!--pages_sceond/create/create.wxml-->
<view class="boss1">
	<textarea bindinput='textinput' placeholder="这一刻的想法..." value="{{comment}}">
	</textarea>
</view>

<view class="boss2">
    <image src="{{imageList}}" style="width: 300rpx; height: 300rpx" bindtap="uploadImage"></image>
</view>

<text>\n</text>

<view class="boss3">
    <view class='line'></view>
</view>

<view class="boss4">
    <image src="/image/create_img/location.png" style="width: 70rpx; height: 70rpx"></image>
    <text>({{longitude}},{{latitude}})</text>
    <image src="/image/create_img/right.png" style="width: 70rpx; height: 70rpx"></image>
</view>

<view class="boss3">
    <view class='line'></view>
</view>

<view class="boss4">
    <image src="/image/create_img/time.png" style="width: 70rpx; height: 70rpx"></image>
    <view>{{startDate}}</view>
    <image src="/image/create_img/right.png" style="width: 70rpx; height: 70rpx"></image>
</view>

<view class="boss3">
    <view class='line'></view>
</view>

<button class="btn1" bindtap="loadto">发布</button>

create.json

{
  "usingComponents": {}
}

create.wxss

/* pages_sceond/create/create.wxss */
textarea{
    border: 3rpx solid rebeccapurple;
   }

   textarea{
    height:300rpx;
    border: 0rpx solid rebeccapurple;
    position: relative;
   }
   
   .botsum{
    position:absolute;
    top: 260rpx;
    font-size: 28rpx;
   }

   .line{
    background: #E0E3DA;
    width: 700rpx;
    height: 5rpx;
    }

    .boss3{
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
    }

.boss4{
    height: 120rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}

.btn1 {
    width: 80%;
    margin-top: 20rpx;
    background-color: rgb(30,144,255);
    color: white;
    border-radius: 98rpx; 
  }
  .btn1::after {
    border-radius: 98rpx;
  }
  

create.js

相关的逻辑代码注释会有解释!

// pages_sceond/create/create.js
var util=require('../../utils/util.js');
const app=getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
      imageList: "/image/create_img/picture.jpg",
      startDate: "获取捡拾时间",
      longitude: '',   //经度  
      latitude: '',    //纬度  
      comment:''
    },
    //点击照片上传图片
    uploadImage:function(){
      var that=this;
      wx.chooseImage({
        count: 1,
        sizeType:['original','compressed'],
        sourceType:['album','camera'],
        success:function(res){
          const tempFilePaths=res.tempFilePaths
          app.globalData.tempFilePaths = tempFilePaths
          that.setData({
            imageList:res.tempFilePaths,
            tempFilePaths:res.tempFilePaths
          })
        }
      })
    },
    //双向绑定文本框的内容
    textinput:function(e){
      this.setData({ comment:e.detail.value})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onl oad: function () {
        var TIME = util.formatTime(new Date());
        this.setData({
          startDate: TIME,
        });
        var that = this;
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
            that.setData({
                latitude: res.latitude,//经度
                longitude: res.longitude//纬度
            })
            console.log(res,'经纬度')
      
        },
        fail: function () {
            console.log('小程序得到坐标失败')
        }
    })
  },



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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    loadto:function(){
        wx.uploadFile({
          url:"****",//后端服务器url
          filePath: app.globalData.tempFilePaths[0],
          name: 'image',
          method:"POST",
          header:{
            'content-type':'application/x-www-form-urlencoded'
          },
          //将需要的内容上传至后端
          formData:{
            comment: this.data.comment,
            longitude:this.data.longitude,
            latitude:this.data.latitude,
            startDate:this.data.startDate,
          },
          })
          if (app.globalData.status == 1){
            // 弹窗
            wx.showToast({
              title: '发布成功',
              icon:"none", 
              success:function(){
                setTimeout(function(){
                  wx.reLaunch({
                    url: "/pages/trends/trends",//上传成功后期望跳转的页面可自行修改
                  })
                },1500);
              }
            })
      }

    
  }
})

为防止部分观众没有utils.js文件,这里特地进行附录供没有utils.js的观众使用~

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime
}

除此之外,页面中所用到的icon图标这里也贴出供参考: picture.jpg 在这里插入图片描述 location.png 在这里插入图片描述 right.png 在这里插入图片描述 time.png 在这里插入图片描述 在这里插入图片描述

标签:function,const,tempFilePaths,微信,create,res,模板,页面
From: https://blog.51cto.com/u_15229916/7126933

相关文章

  • 前端页面常见的布局方式有以下几种
    前端页面常见的布局方式有以下几种1、文档流--内联元素从左往右排列,块级元素从上往下排列。2、float(浮动)布局3、position(定位)布局4、displayinline-block(行向)布局5、margin布局6、display:flex弹性布局7、display:grid栅格布局1、文档流(正常流,也叫文档流)内联元......
  • 微信开发之一键删除好友的技术实现
    简要描述:删除联系人请求URL:http://域名地址/delContact请求方式:POST请求头Headers:Content-Type:application/jsonAuthorization:login接口返回参数:参数名必选类型说明wId是String微信实列IDwcId是String需删除的微信id返回数据:参数名类型说明codestring1000成功,1001失败msgstring反馈......
  • 微信开发之一键删除好友的技术实现
    简要描述:删除联系人请求URL:http://域名地址/delContact请求方式:POST请求头Headers:Content-Type:application/jsonAuthorization:login接口返回参数:参数名必选类型说明wId是String微信实列IDwcId是String需删除的微信id返回数据:参数名类型说......
  • 从输入URL到页面展示
    目录用户输入过程URL请求过过程重定向响应数据处理准备渲染进程提交文档渲染过程用户输入过程地址栏omnibox判断用户的输入是搜索内容还是URL链接:如果是搜索内容,地址栏使用默认的搜索引擎合成带搜索关键字的URL链接如果输入内容符合URL规则,则加上协议合成完整的URL链接......
  • 10.1 C++ STL 模板适配与迭代器
    STL(StandardTemplateLibrary)标准模板库提供了模板适配器和迭代器等重要概念,为开发者提供了高效、灵活和方便的编程工具。模板适配器是指一组模板类或函数,它们提供一种适配机制,使得现有的模板能够适应新的需求。而迭代器则是STL中的令一种重要的概念,它是一个抽象化的数据访问机制,......
  • 零代码搭建一个微信小程序
    本文分享自华为云社区《【新手指引】体验通过AstroZero零代码快速搭建微信小程序》,作者:华为云Astro。通过本文,您将学会如何基于Astro零代码能力,DIY开发,完成问卷、投票、信息收集、流程处理等工作,还能够在线筛选、分析数据。实现一站式快速开发个性化应用,体验轻松拖拽开发的乐趣。......
  • 面试八股回答通用模板
    面试八股回答通用模板1、整体回答思路面试的八股问题都可以尝试从以下几个方面来回答即:是什么、为什么、怎么做、举例说明、对比分析、结合实际2、举例说明面试官:介绍一下vector和list的区别以及优缺点?“是什么”:vector是一个动态数组,它在内存中以连续的方式存储元素。li......
  • 基于微信小程序的网上交易平台的设计与实现-计算机毕业设计源码+LW文档
    摘 要随着互联网技术的发展,传统的商品销售迎来了机遇,我国是个人口大国,商品的需求量大,如何推广商品的销售是企业非常关注的事情。随着电子商务多元化的发展,各种类型的商品逐渐转移到线上销售。在互联网的帮助下,带动企业打开销路,促进商品销售的可持续发展。同时,通过基于微信小程......
  • 基于微信小程序的景区服务系统-计算机毕业设计源码+LW文档
    摘要随着社会经济的发展,各行业竞争激烈,年轻群体工作压力大,越来越多的人希望通过旅游来缓解压力。而传统的旅行社都是通过事先定制的线路和固定时间,没有个性化定制服务,不能满足现代用户的需求。对于此,开发景区服务系统可以很好的解决用户个性化旅游的服务,通过系统查询各种景点信息,......
  • 零代码搭建一个微信小程序
    本文分享自华为云社区《【新手指引】体验通过AstroZero零代码快速搭建微信小程序》,作者:华为云Astro。您将学会如何基于Astro零代码能力,DIY开发,完成问卷、投票、信息收集、流程处理等工作,还能够在线筛选、分析数据。实现一站式快速开发个性化应用,体验轻松拖拽开发的乐趣。您需......