微信小程序开发实践

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

资源

视频

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,
  },
});
<!--pages/index/index.wxml-->


<!-- 变量 -->
<view>{ {name}}</view>

<!-- 列表 -->
<view wx:for="{ {arr}}" wx:key="{ {index}}">
{ {index}} { {item}}
</view>

<!-- 条件 -->
<view wx:if="{ {isLogin}}">已登录</view>
<view wx:else>请登录</view>

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 脚本逻辑

<!--pages/index/index.wxml-->
<view>{ {count}}</view>
<button bindtap="handleButtonTap" data-id="666" size="mini">点击</button>
// 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
})

}
})
}

图片显示和下载

<block wx:for="{
        {images}}" wx:key="index">
<image src="{ {item.fileID}}"></image>
<view>
<button data-fileid="{ {item.fileID}}" bindtap="handleDownloadFile">下载图片</button>
</view>
</block>
// 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库

豆瓣电影

电影列表API:

http://api.douban.com/v2/movie/in_theaters?apikey=;0df993c66c0c636e29ecbb5344252a4a&start=0&count=10`


代码地址:

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


            </div>
目录
相关文章
|
Android开发
Android Studio中修改gradle插件版本和Gradle版本
Android项目中,我们一般要设置gradle插件版本和gradle版本。 项目根目录下的build.gradle文件中,通过classpath可以指定gradle插件的版本。
|
Dart 搜索推荐 API
Flutter & 鸿蒙next版本:自定义对话框与表单验证的动态反馈与错误处理
在现代移动应用开发中,用户体验至关重要。本文探讨了如何在 Flutter 与鸿蒙操作系统(HarmonyOS)中创建自定义对话框,并结合表单验证实现动态反馈与错误处理,提升用户体验。通过自定义对话框和表单验证,开发者可以提供更加丰富和友好的交互体验,同时利用鸿蒙next版本拓展应用的受众范围。
254 1
|
XML JavaScript Java
NekoHTML 是一个基于Java的HTML扫描器和标签补全器
**NekoHTML** 是一个基于Java的HTML扫描器和标签补全器(tag balancer),由J. Andrew Clark开发。它主要用于解析HTML文档,并能够“修正”许多在编写HTML文档过程中常犯的错误,如增补缺失的父元素、自动用结束标签关闭相应的元素,以及处理不匹配的内嵌元素标签等。这使得程序能够以标准的XML接口来访问HTML文档中的信息。 ### NekoHTML的主要特点包括: 1. **错误修正**:能够自动修正HTML中的常见错误,如未闭合的标签等。 2. **DOM树生成**:将HTML源代码转化为DOM(Document Object Model)结构,便
231 1
|
安全
欢迎和阿里云一起使用国家顶级域名!
中国国家顶级域名包括“.CN”和“.中国”域名,已经成为我国政府、机构、企事业单位以及重大赛事活动的主用域名
665 0
|
机器学习/深度学习 自然语言处理 计算机视觉
【大模型】小样本学习的概念及其在微调 LLM 中的应用
【5月更文挑战第5天】【大模型】小样本学习的概念及其在微调 LLM 中的应用
|
自然语言处理 编译器 程序员
【Qt底层之 元对象的编译】Qt 元对象系统及其编译流程解析
【Qt底层之 元对象的编译】Qt 元对象系统及其编译流程解析
783 4
|
运维 新能源 数据处理
2022云栖精选—《云上新型电力系统白皮书》发布
摘要:本文整理自阿里云智能电力行业总经理吴明宸,在云栖大会的分享。本篇内容主要分为两个部分: 1. 白皮书发布背景 2. 白皮书内容简介
2022云栖精选—《云上新型电力系统白皮书》发布
|
JavaScript 前端开发 测试技术
【利用AI让知识体系化】TypeScript目标:扩展JavaScript能力(三)
【利用AI让知识体系化】TypeScript目标:扩展JavaScript能力
|
搜索推荐 算法 前端开发
基于用户特征的个性化网络小说推荐系统的设计与实现
基于用户特征的个性化网络小说推荐系统的设计与实现
436 0
|
存储 安全 搜索推荐
2022Android设备唯一标识(AndroidID,OAID等 )
2022Android设备唯一标识(AndroidID,OAID等 )
4870 0
2022Android设备唯一标识(AndroidID,OAID等 )