微信小程序开发实践

简介: 微信小程序开发实践

资源

视频

https://www.imooc.com/learn/1121


账号注册

https://mp.weixin.qq.com/wxopen/waregister?action=step1


开发工具

https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html


开发文档

https://developers.weixin.qq.com/miniprogram/dev/framework/

1、项目文件

  1. json 配置
  2. wxml 模板
  3. wxss 样式
  4. js 脚本逻辑

2、json 配置

project.config.json 项目配置

app.json 全局配置

page.json 页面配置

3、wxml 模板

标签:view

数据绑定:

动态数据 page.data

Mustache 语法

变量:{ {key}}

循环:wx:for

条件:wx:if

eg:

// pages/index/index.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "Tom",
    arr: ["cat", "dog"],
    isLogin: true,
  },
});

        



{ {name}}


{arr}}" wx:key="{ {index}}">
{ {index}} { {item}}



{isLogin}}">已登录
请登录

4、wxss 样式

responsive pixel 宽度以 750rpx 为标准, 以 iphone6 屏幕为测试标准

/ pages/index/index.wxss /
@import "../../style/guide.wxss";

.box {
width: 200rpx;
height: 200rpx;
background-color: #eeeeee;
}

第三方样式库

  1. weui
  2. iview weapp
  3. vant weapp

5、js 脚本逻辑


        
{ {count}}
// pages/index/index.js

Page({
handleButtonTap: function (event) {
console.log(event);

// 更新数据
this.setData({
count: this.data.count + 1,
});
},
});

云开发

  1. 云函数
  • 获取 appid
  • 获取 openid
  • 生成分享图
  • 调用腾讯云 SDK
  1. 云数据库
  • 数据增删改查
  1. 云存储
  • 管理文件
  • 上传文件
  • 下载文件
  • 分享文件

Serverless

云数据库

MongoBD

权限管理

示例:

// /pages/about/about.js

// 初始化云数据库
const db = wx.cloud.database();

// 添加数据
handleInsert: function(){
db.collection('data').add({
data: {
name: 'Tom'
}
}).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
},

// 更新数据
handleUpdate: function () {
db.collection('data').doc('a9bfcffc5eb78e810058cf6f4b4a03c5').update({
data: {
age: 20
}
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
},

// 查找数据
handleFind: function () {
db.collection('data').where({
name: 'Tom'
}).get().then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
},

// 移除数据
handleRemove: function () {
db.collection('data').doc('a9bfcffc5eb78e810058cf6f4b4a03c5').remove().then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
},

云函数

开发环境需要安装Node.js

node -v
v10.16.0

如果提示没有安装

npm install --save wx-server-sdk@latest

云函数求和

// cloudfunctions/sum/index.js

// 云函数 求和
exports.main = async (event, context) => {
return event.a + event.b;
}
// /pages/about/about.js

handleCallFunc: function() {
wx.cloud.callFunction({
name: 'sum',
data: {
a: 2,
b: 3
}
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
},

获取openid

// /pages/about/about.js

handleLoing: function(){
wx.cloud.callFunction({
name: 'login'
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
}

批量删除

// cloudfunctions/batchDelete/index.js

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
return await db.collection('data').where({
name: 'Tom'
}).remove()
}
// /pages/about/about.js

handleBatchDelete: function () {
wx.cloud.callFunction({
name: 'batchDelete'
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
},

云存储

图片上传

handleUploadFile: function(){
// 选择图片
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)

// 上传图片
wx.cloud.uploadFile({
// 上传至云端的路径
cloudPath: new Date().getTime() + '.png',
filePath: tempFilePaths[0], // 小程序临时文件路径
success: res => {
// 返回文件 ID
const fileID = res.fileID

// 存储文件路径
db.collection('files').add({
data:{
fileID: fileID
}
}).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
},
fail: console.error
})

}
})
}

图片显示和下载

{images}}" wx:key="index">
{item.fileID}}">



// pages/about/about.js

// 初始化云数据库
const db = wx.cloud.database();

Page({
data: {
images: []
},

getFileList: function() {
// 获取图片列表
wx.cloud.callFunction({
name: 'login',
}).then(res => {
db.collection('files').where({
_openid: res.result.openid
}).get().then(res => {
console.log(res)

this.setData({
images: res.data
})

})
})
},

handleDownloadFile: function(event) {
// 下载文件
console.log(event.target.dataset.fileid)

wx.cloud.downloadFile({
fileID: event.target.dataset.fileid, // 文件 ID
success: res => {
// 返回临时文件路径
console.log(res.tempFilePath)

// 保存图片到相册
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '保存成功',
})
}
})
},
fail: console.error
})

}
})

电影评分示例

引入Vant Weapp

参考 https://youzan.github.io/vant-weapp/#/quickstart

在 miniprogram 目录下

npm i @vant/weapp -S --production

工具设置

工具 -> 构建 npm

详情 -> 勾选 使用 npm 模块


发送请求

小程序端 需要icp备案

云函数 无需备案


request库

豆瓣电影

代码地址:

https://github.com/mouday/weixin-app-demo


            </div>
目录
相关文章
|
2月前
|
人工智能 小程序 前端开发
一个小程序轻量AR体感游戏,开发实现解决方案
针对青少年运动兴趣不足问题,AR体感游戏凭借沉浸式互动体验脱颖而出。结合小程序“AI运动识别”插件与WebGL渲染技术,可实现无需外设的轻量化AR健身游戏,如跳糕、切水果等,兼具趣味性与锻炼效果,适用于儿童健身及职工团建,即开即玩,低门槛高参与。
|
2月前
|
移动开发 小程序 前端开发
小程序开发平台有哪些?哪个好
小程序的开发方式丰富多元,开发团队可根据自身的技术背景、项目具体需求以及资源状况,灵活挑选最为适宜的开发路径。以下将详细介绍几种主流的小程序开发方式。
324 1
|
2月前
|
运维 小程序 数据可视化
小程序开发平台有哪些?SaaS小程序制作平台哪个好
小程序开发模式详解:自主开发、SaaS小程序制作平台与外包全对比 选择合适的小程序开发模式,是项目成功的基石。这三种模式在成本、周期、控制力和灵活性上各有千秋,适用于不同阶段和不同类型的企业。下面我们将逐一深入剖析。
284 7
|
2月前
|
移动开发 小程序 前端开发
小程序快速开发平台有哪些?
小程序开发并非“一刀切”,需结合技术储备、资金预算、时间规划及功能需求等多维度因素综合考量。以下为您详细拆解五种主流开发方案及其适用场景,助您精准匹配开发路径。
211 3
|
2月前
|
移动开发 小程序 前端开发
小程序开发平台有哪些?小程序开发制作软件推荐
小程序开发方案全解析:5种主流方式与选择指南 小程序开发需根据技术能力、预算、时间及功能需求综合决策。以下为5种主流开发方案及适用场景分析:
471 0
|
3月前
|
消息中间件 人工智能 Java
抖音微信爆款小游戏大全:免费休闲/竞技/益智/PHP+Java全筏开源开发
本文基于2025年最新行业数据,深入解析抖音/微信爆款小游戏的开发逻辑,重点讲解PHP+Java双引擎架构实战,涵盖技术选型、架构设计、性能优化与开源生态,提供完整开源工具链,助力开发者从理论到落地打造高留存、高并发的小游戏产品。
|
4月前
|
小程序 JavaScript API
uni-halo + 微信小程序开发实录:我的第一个作品诞生记
这篇文章介绍了使用uni-halo框架进行微信小程序开发的过程,包括选择该框架的原因、开发目标以及项目配置和部署的步骤。
204 0
uni-halo + 微信小程序开发实录:我的第一个作品诞生记
|
6月前
|
存储 缓存 运维
微信读书十周年,后台架构的技术演进和实践总结
微信读书经过了多年的发展,赢得了良好的用户口碑,后台系统的服务质量直接影响着用户的体验。团队多年来始终保持着“小而美”的基因,快速试错与迭代成为常态。后台团队在日常业务开发的同时,需要主动寻求更多架构上的突破,提升后台服务的可用性、扩展性,以不断适应业务与团队的变化。
268 0