微信小程序/小鸡单词/单词测试功能

页面初始数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
data: {
da1:"",
da2: "",
da3: "",
da4: "",
daan:false,
showDaan:false,
complete:false,
true_num:0,
score:0,
currentTab: 0,
friendsData: \[\],
globalData: \[\],
loadNumber: 0,
//全球排名数据加载次数
history:0
},

生命周期函数–监听页面加载

1
2
3
4
5
6
7
8
onLoad: function (options) {

this.search()
app.appData.fromClickId = options.currentClickId app.upDateUser\_networkFromClickId = require('../../utils/upDateUser\_networkFromClickId.js').upDateUser_networkFromClickId
wx.showShareMenu({
withShareTicket: true })
app.pageGetUserInfo(this,
this.getScore)

//单词选择函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
choice(e){

console.log(e)
if(e.currentTarget.id===this.data.true\_word){

this.setData({
daan: true,
true\_num: this.data.true\_num + 1})
const innerAudioContext = wx.createInnerAudioContext() innerAudioContext.autoplay = true innerAudioContext.src = 'http://media-audio1.qiniu.baydn.com/us/n/ni/nice\_v3.mp3'
innerAudioContext.onPlay(() => {
})
}else{

this.setData({
daan:false})
this.setData({
complete: true })
const innerAudioContext = wx.createInnerAudioContext() innerAudioContext.autoplay = true innerAudioContext.src = 'https://media-audio1.baydn.com/us%2Fs%2Fsa%2Fsad\_v4.mp3'
innerAudioContext.onPlay(() => {
})
if(this.data.true\_num>this.data.score){

this.set\_score(this.data.true\_num) this.setData({
history: this.data.true_num})
}else{

this.setData({
history: this.data.score }) }
app.pageGetUserInfo(this,
this.getScore)
wx.showShareMenu({
withShareTicket: true })
app.pageGetUserInfo(this) this.getRankGlobalData();
}
this.setData({
showDaan:true})
},

下一组单词分配函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
search(){

var idx = Math.floor(Math.random() * 12345) + 1
var word = list.wordList\[idx\]
var that=this wx.request({
url: 'https://api.shanbay.com/bdc/search/?word=' + word,
data: {
},
method: 'GET',
success: function (res) {
that.setData({
title: res.data.data.definition.split(","
)\[0\].split("\\n")\[0\],
true_word:word }) var num = Math.floor(Math.random() * 400) + 1 if(num<100){
that.setData({
da1:res.data.data.content,
da2: list.wordList\[Math.floor(Math.random() * 12345) + 1\] ,
da3: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da4: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
}) } if (100<num&&num<200) {
that.setData({
da2: res.data.data.content,
da1: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da3: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da4: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
}) } if (num < 300&&num>200) {
that.setData({
da3: res.data.data.content,
da2: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da1: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da4: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
}) } if (num>300) {
that.setData({
da4: res.data.data.content,
da2: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da3: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
da1: list.wordList\[Math.floor(Math.random() * 12345) + 1\],
}) } } }) },

成绩上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
set\_score(score) {
var openId = this.data.openId if (openId) {
qcloud.request({
login: false,
url: `${
app.appData.baseUrl}set\_score`,
data: {
openId,
score,
},
success: (res) => {
console.log(res) },
fail(error) {
util.showModel('请求失败',
error); },
}); } },

获取成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
getScore(openId) {
if (openId) {
qcloud.request({
login: false,
url: `${
app.appData.baseUrl}get_score`,
data: {
openId },
success: (res) => {
let score = res.data.data; this.setData({
score }) },
fail(error) {
util.showModel('请求失败',
error); },
}); } },
onReachBottom: function () {
//下拉加载 const that = this if (that.data.currentTab) {
that.getRankGlobalData() } },

//加载全球排名的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
![](https://www.idealli.com/wp-content/uploads/2018/04/092204rcpp42avah2zon2m-169x300.jpg) getRankGlobalData() {
const that = this qcloud.request({
login: false,
url: app.appData.baseUrl + 'getRankGlobalData',
data: {
loadNumber: that.data.loadNumber },
success: (res) => {
that.setData({
globalData: that.data.globalData.concat(res.data.data),/
/数据叠加 loadNumber: that.data.loadNumber + 1 }) },
fail(error) {
util.showModel('请求失败',
error); console.log('request fail',
error); },
}) },
})

上面写了部分的函数功能,全部代码在这里 https://github.com/six-3/-/blob/master/pages/test/test.js








我是小鸡,这篇文章的标题是——微信小程序/小鸡单词/单词测试功能
喜欢的话可以转载,不过记得标记出处

2018年05月01日