记一次微信小程序的开发

使用工具:

1.微信Web开发者工具

2.Visual Studio 2019

前端采用color UI,后端采用c# .net

过程中的几个重点点记录

1.color UI使用

下载colorUI以后

将icon.wxss、colorui.wxss拷贝至项目根目录

在app.wxss中导入文件

@import "icon.wxss";

@import "colorui.wxss";

2.图片上传功能

wxml前端代码

<view class="cu-form-group">
  <view class="picture_list">
  <view wx:for="{{upload_picture_list}}" class="picture_item" wx:key="{{index}}">
    <image wx:if="{{item.upload_percent < 100}}" src="{{item.path}}" mode="aspectFill"></image>
    <image wx:if="{{item.upload_percent == 100}}" src="{{item.path_server}}" mode="aspectFill"></image>
    <view class="upload_progress" wx:if="{{item.upload_percent < 100}}" data-index="{{index}}" bindtap="previewImg">{{item.upload_percent}}%</view>
    <text class=‘del‘ bindtap=‘deleteImg‘ data-src=‘{{image}}‘ style=‘display:{{isDel}}‘ data-index="{{index}}">×</text>
  </view>

  <view class=‘picture_item‘>
    <view class="add-image" bindtap=‘uploadpic‘>
      <text>+</text>
    </view>
  </view>
</view>
</view>
<view class="cu-form-group">
<button bindtap=‘uploadimage‘ class=‘yes-upload‘>上传图片</button>
</view>

js代码

  //选择图片方法
  uploadpic: function (e) {
    let that = this //获取上下文
    let upload_picture_list = that.data.upload_picture_list
    //选择图片
    wx.chooseImage({
      count: 8, // 默认9,这里显示一次选择相册的图片数量
      sizeType: [‘compressed‘], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: [‘album‘, ‘camera‘], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        let tempFiles = res.tempFiles
        //把选择的图片 添加到集合里
        //console.log(tempFiles);
        for (let i in tempFiles) {
          tempFiles[i][‘upload_percent‘] = 0
          tempFiles[i][‘path_server‘] = ‘‘
          upload_picture_list.push(tempFiles[i])
        }
        //显示
        that.setData({
          upload_picture_list: upload_picture_list,
        });
      }
    })
  },
  //点击上传图片
  uploadimage() {
    let page = this
    let upload_picture_list = page.data.upload_picture_list
    //循环把图片上传到服务器 并显示进度
    for (let j in upload_picture_list) {
      if (upload_picture_list[j][‘upload_percent‘] == 0) {
        //console.log("进入上传if");
        //上传图片后端地址
        //upload_file_server(‘https://www.x..fds.af..a.fd.sa‘, page, upload_picture_list, j)
        //console.log(this.data.problemAttach);
        wx.uploadFile({
          url: this.data.domain+‘api/FirstAPI/uploadPicture?problemAttach=‘ + this.data.problemAttach, //上传的接口地址
          filePath: upload_picture_list[j].path,
          name: ‘file‘,
          formData: {
            problemAttach: this.data.problemAttach,
          },
          success: function (res) {
            console.log(res);
            upload_picture_list[j][‘upload_percent‘] = 100
            upload_picture_list[j][‘path_server‘] = ‘接口地址‘ + JSON.parse(res.data).data;
            page.setData({
                upload_picture_list: upload_picture_list,
                problemAttach: JSON.parse(res.data).msg
            });
          }
        })
      }
    }
    let imgs = wx.setStorageSync(‘imgs‘, upload_picture_list);
  },
  // 点击删除图片
  deleteImg(e) {
    let upload_picture_list = this.data.upload_picture_list;
    let index = e.currentTarget.dataset.index;
    if (upload_picture_list[index].upload_percent == 100) {
      //去服务器把对应的记录del
      this.data.delIndex = index;
      this.ajaxfunc(1);
    }
    upload_picture_list.splice(index, 1);
    this.setData({
      upload_picture_list: upload_picture_list
    });
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    //所有图片
    let imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

c#后端api接口

public IHttpActionResult uploadPicture(string problemAttach)
        {
            string mesg = problemAttach;
            if (problemAttach==null||problemAttach==""||problemAttach=="undefined" || problemAttach == "null")
            {
                problemAttach = CommonHelper.GetGuid;
                mesg = problemAttach;
            }
            Repository<MK_Base_AnnexesFile> re = new Repository<MK_Base_AnnexesFile>();

            HttpFileCollection files = HttpContext.Current.Request.Files;
            List<string> serversrc = new List<string>();
            foreach (string key in files.AllKeys)
            {
                MK_Base_AnnexesFile fileAnnexesEntity = new MK_Base_AnnexesFile();
                HttpPostedFile file = files[key];//file.ContentLength文件长度
                string src = HttpContext.Current.Server.MapPath("~/imgcoll/") + file.FileName;
                src=src.Replace("\\","/");
                if (string.IsNullOrEmpty(file.FileName) == false)
                {
                    file.SaveAs(src);
                    serversrc.Add("/imgcoll/"+file.FileName);
                }
                //str = str.Substring(0, str.LastIndexOf("/"));
                fileAnnexesEntity.F_Id = file.FileName.Substring(0, file.FileName.LastIndexOf("."));
                fileAnnexesEntity.F_FileName = file.FileName;
                fileAnnexesEntity.F_FilePath = src;
                fileAnnexesEntity.F_FileSize = "未知";
                fileAnnexesEntity.F_FileExtensions = file.FileName.Substring(file.FileName.LastIndexOf("."));
                fileAnnexesEntity.F_FileType = file.FileName.Substring(file.FileName.LastIndexOf(".")+1);
                fileAnnexesEntity.F_CreateUserId = "微信端上传";
                fileAnnexesEntity.F_CreateUserName = "微信端上传";
                fileAnnexesEntity.F_FolderId = problemAttach;
                re.Insert(fileAnnexesEntity);

            }

            return JsonData(true, serversrc[0], mesg);
        }

原文地址:https://www.cnblogs.com/lovejunjuan/p/11204961.html

时间: 2024-08-01 21:15:52

记一次微信小程序的开发的相关文章

微信公众号开发三级分销微商城,微信小程序定制开发

10年的技术开发团队,提供网站建设,APP开发,网站推广等服务,专业微信小程序定制开发,需要这方面的朋友,可以咨询一下. 案例演示: 成功案例: 联系方式:8582-36016 ,微信号:luenmicro ,电话:131-1221-5717

【小程序源码案例】微信小程序项目开发案例分享

作者:web小二本文标签: 微信小程序 小程序源码案例 小程序项目小程序的开发,并不是适合所有公司,我今天跟大家分享小程序方面的教程,主要是供大家学习使用.学习这种东西,有时候则是单纯的喜欢,没有任何目的,很单纯的为了好玩,记得很早之前学flash,没有想法,就是觉得好玩,纯娱乐爱好而已.到后来玩视频剪辑也是出于同样的原因,不图钱财名利,只是图自己个人爱好娱乐. 但是,学习,有时候则是需要有明确目的,特别是关系到自己吃饭问题的时候,你就需要非常有目的去学习,并且还需要制定好学习的计划与目标,希望

Mac上微信小程序官方开发工具卡死的问题

Mac上微信小程序官方开发工具打开后卡死,无法操作,也关不掉,解决方案: 三步: 1.在应用中删除"微信web开发者工具" 2.删除一下几个配置和缓存文件: 1.-/Library/Application Support/微信web开发者工具 2.~/Library/Preferences/com.tencent.wechat.devtools.plist 3.~/Library/Caches/微信web开发者工具 3.重新安装"微信web开发者工具"

微信小程序最新开发资源汇总,对学习微信小程序的新手有一定帮助

微信小程序最新开发资源汇总,希望给想学习或正在学习微信小程序开发的同学们带来一定帮助,汇总的小程序资源有点繁杂,各种类型的小程序demo都有,大家可以选择自己想要的demo进行下载学习.这些微信小程序资源大多是整理自github,如果可以,希望大家能够给github上的原作者一颗star,感谢原作者的无私奉献. 这里整理的是资源的原帖子,下载链接也在帖子里,当然本人也只体验了部分demo,有兴趣的同学可以都下载试试. 下载地址: 仿微信聊天,朋友圈小程序源码wepy框架开发的小程序商城源码,功能

微信小程序云开发

使用微信小程序云开发,可以不需要后端的参与,前端直接使用数据库. 第一步,新建一个空的云开发项目 在project.config.json 文件可以看见 "miniprogramRoot": "miniprogram/",   表示为小程序页面的文件 "cloudfunctionRoot": "cloudfunctions/", 表示云函数文件,即 在云端定义一些函数,运行环境为 nodejs, 可以做一些运算操作,然后将结果

微信小程序云开发-从0打造云音乐全栈小程序

第1章 首门小程序“云开发”课程,你值得学习本章主要介绍什么是小程序云开发以及学习云开发的重要性,并介绍项目的整体架构,真机演示项目功能,详细介绍整体课程安排.课程适用人群以及需要掌握的前置知识.通过本章的学习,能够使大家对本门课程有一个整体的了解.... 第2章 云开发介绍以及从0构建项目本章会详细介绍小程序云开发与Serverless,并介绍如何开通小程序云开发及控制台的功能,并且初始化项目代码,讲解airbnb/javascript代码规范. 第3章 播放列表功能实现本章完成歌单列表与歌曲

微信小程序的开发有什么用?有什么优势?

现如今,科技发展迅速个我们带来的便利性还是很大的.而微信小程序正是依赖科技进行发展的软件.那么微信小程序的开发究竟有什么用呢?又有哪些优势呢?小程序的定义是为商家做服务的,小程序的每个功能都在为商家获客做服务,从2017年至今,不过两年时间,小程序完成了上百次的更新升级,如今,小程序的需求越来越贴近用户.可以毫不夸张的说,小程序是当下互联网最大的流量聚集地.那么小程序究竟有哪些优势呢?1.无需安装,即开即用现如今,智能手机普及,不仅可以随时随地上网,还可以使用各种便利的软件.而微信作为当下流行的

记一次微信小程序开发

之前在网上看到博客园新闻服务开放接口,因为自己本身有看博客园IT新闻的习惯,为了能随时随地简洁方便的浏览新闻,于是萌生了一个利用开放API开发一个微信小程序的想法. 1. mpvue初探 平时技术栈有用到Vue,这个小程序功能也比较简单,用 mpvue 再合适不过了.mpvue 基于 Vue.js 核心,修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序环境中,获得完整的 Vue.js 开发体验,组件化代码复用.Vuex 数据管理.webpack 构建机制

记一次微信小程序人脸比对开发

由于公司要开发一个考试类型的项目用到了人脸比对,我就研究了一下相关了知识. 1.最开始想的是用微信小程序的媒体组件camera. const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) } }) 用ctx.takePhoto()拍照来获取实时图片和数据库中的人脸作比对,比对成功进入考试页面,一