开发工具
-
微信开发者工具(官方)
准备
-
开通云开发
-
配置环境
- 开通云开发
选择免费版
- 配置
wx.cloud.init({
env: '*********',
traceUser: true,
})
- 搭建框架
index.wxml
<view class="index">
<view class="sousou">
<searchBar></searchBar>
</view>
<view class="swiper-img">
<swiper class="swiper" indicator-dots="true" autoplay="false" circular="false" interval="3000" duration="1000">
<block wx:for="{{banner}}" wx:for-item="item">
<swiper-item>
<image src="{{item.url}}" mode="widthFix"></image>
</swiper-item>
</block>
</swiper>
</view>
<view class="group">
<scroll-img></scroll-img>
</view>
<ad unit-id="000000"></ad>
<view class="footer">
<view class="component" wx:for="{{touxiang}}" wx:for-item="item" wx:key="key" data-list="{{touxiang}}">
<image class="touxiang-img" wx:if="{{item.fileID}}" src="{{item.fileID}}" data-list="{{touxiang}}"
data-index="{{index}}" data-src="{{item.fileID}}" bindtap="previewImage" mode="widthFix"></image>
</view>
</view>
</view>
index.wxss
.swiper{
height: 300rpx;
width: 100%;
}
.swiper image{
height: 300rpx;
width: 100%;
}
.banner_warp .wx-swiper-dot{width: 40rpx;height: 6rpx;background: rgba(255,255,255,.2);border-radius: inherit;}
.banner_warp .wx-swiper-dot-active{width: 40rpx;height: 6rpx;background: #fff;}
.footer{
width: 100%;
height: 100%;
}
.component{
width: 100%;
height: 100%;
}
.touxiang-img{
width: 30%;
height: 30%;
float: left;
margin-left: 10px;
margin-top: 10px;
}
index.js
const app = getApp()
const db = wx.cloud.database();
const touxiang_col = db.collection("touxiang")
const banner = db.collection("banner")
let interstitialAd = null
let videoAd = null
import {
ml_showLoading,
ml_showToast,
ml_hideLoading
} from '../../utils/asyncWX.js'
Page({
data: {
isHide: false,
banner: [],
touxiang: [],
fileIDlist: [],
_page: 0,
hasMore: true,
},
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
var that = this;
console.log("用户的信息如下:");
console.log(e.detail.userInfo);
that.setData({
isHide: false
});
} else {
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
showCancel: false,
confirmText: '返回授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击了“返回授权”');
}
}
});
}
},
onLoad: function (options) {
this.loadListData()
this.loadBannerList()
if (wx.createInterstitialAd) {
interstitialAd = wx.createInterstitialAd({
adUnitId: '--------'
})
interstitialAd.onError((err) => {
})
interstitialAd.onClose(() => {
})
}
},
async loadListData() {
const limit = 12
let {
_page,
touxiang
} = this.data
await ml_showLoading()
let res = await touxiang_col.limit(limit).skip(_page * limit).orderBy('addtime', 'desc').get()
await ml_hideLoading()
this.setData({
touxiang: [...touxiang, ...res.data],
_page: ++_page,
hasMore: res.data.length === limit
})
},
async onReachBottom() {
if (interstitialAd) {
interstitialAd.show().catch((err) => {
console.error(err)
});
interstitialAd.onError(err => {
console.log(err);
})
}
if (!this.data.hasMore) {
await ml_showToast("到底啦!")
return 0
}
console.log('上拉刷新')
this.loadListData()
},
onPullDownRefresh() {
console.log("shuax")
this.setData({
touxiang: [],
_page: 0,
hasMore: true
})
this.loadListData()
},
upload() {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
wx.cloud.uploadFile({
cloudPath: 'boy/' + new Date().getTime() + '.png',
filePath: tempFilePaths[0],
success: res => {
console.log(res.fileID)
db.collection('touxiang').add({
data: {
fileID: res.fileID
}
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
},
fail: console.error
})
}
})
},
async loadBannerList() {
banner.get().then(res => {
this.setData({
banner: res.data
})
})
.catch(err => {
console.log(err)
})
},
previewImage: function (event) {
var src = event.currentTarget.dataset.src;
var imgList = event.currentTarget.dataset.list;
var touxiang = [];
for (var i = 0; i < imgList.length; i++) {
touxiang.push(imgList[i].fileID)
}
console.log(touxiang);
wx.previewImage({
current: src,
urls: touxiang
})
},
})
项目预览图:
项目文件地址
(仅供参考)
|