后台过程详见:前端传入数字后台生成二维码,后台使用mockjs生成数据
一
.后台的代码
var qr
= require('qr-image');
var fs
= require('fs')
var express
= require('express')
var app
= express()
var mock
= require('mockjs')
let typeList
= mock
.mock({
'list|10': [
{
'id|+1': 1,
name
: "@cword(2,4)",
}
]
})
app
.get('/typeList', (req
, res
) => {
res
.json(typeList
)
})
app
.listen(8989, () => {
console
.log("程序已启动");
})
二
.前端:详见小程序demo1
1.data里要有数组接收
data
: {
menu
: [],
}
2.需要引入utils
/index
,修改页面的开头
import Router
from '../../utils/index'
Router({
3.在onLoad里面调用函数
onLoad
: function (options
) {
this.getTypeList()
},
4.编写函数:getTypeList
async getTypeList() {
let {
list
} = await this.$
get('http://localhost:8989/typeList')
this.$
set('menu', list
)
},
5.$
get的来源 在utils下建立index
.js,内代码:
import qs
from "qs"
function request(methods
, url
, data
) {
let start
= +new Date()
return new Promise((resolve
, reject
) => {
wx
.showLoading({
title
: '加载中',
})
if (!url
.startsWith('http'))
url
= "https://simbajs.com:996" + url
wx
.request({
methods
,
url
,
data
,
success
: res
=> {
console
.log(`${methods}了${url},参数是 ${qs.stringify(data)}耗时${+new Date - start}毫秒`);
resolve(res
.data
)
},
complete() {
wx
.hideLoading({})
console
.log(`耗时${+new Date - start}毫秒`);
}
})
})
}
export function $
get(url
, data
) {
return request("GET", url
, data
)
}
export function $post(url
, data
) {
return request("POST", url
, data
)
}
export function $d(e
, key
) {
let res
= e
.currentTarget
.dataset
return key
? res
[key
] : res
}
export function $msg(msg
) {
wx
.showToast({
title
: msg
,
icon
: 'none'
})
}
export function $
set(key
, val
) {
this.setData({
[key
]: val
})
}
export function $setVal(e
) {
let {
key
} = this.$d(e
);
this.$
set(key
, e
.detail
)
}
export function $confirm(title
) {
return new Promise((resolve
, reject
) => {
wx
.showModal({
title
,
success
: (res
) => {
resolve(res
.confirm
)
}
})
})
}
export default function Router(option
) {
option
.$setVal
= $setVal
option
.$
get = $
get
option
.$post
= $post
option
.$d
= $d
option
.$msg
= $msg
option
.$
set = $
set
option
.$confirm
= $confirm
return Page(option
)
}
转载请注明原文地址:https://tech.qufami.com/read-1008.html