1介绍
使用vant组件库
2 index.html
<form bindsubmit='res'>
<view style="position:relative;top:0rpx;float:left;">
<van-uploader max-count="1" bind:after-read="afterRead" />
</view>
<image src="" style="width:200rpx;height:200rpx;"></image>
<van-button form-type='submit' type="primary" size="large" custom-style="width:100%;">提交信息</van-button>
</form>
3 index.json
{
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"van-uploader": "@vant/weapp/uploader/index"
}
}
4 index.js
var app = getApp();
var imageUrl = null;
Page({
data: {
fileList: "",
},
afterRead(event) {
var that = this;
that.setData({
fileList: []
})
const { file } = event.detail;
console.log(event.detail)
wx.uploadFile({
url: app.globalData.url + 'upload',
filePath: file.url,
name: 'file',
formData: { user: 'test' },
success(res) {
var res1 = JSON.parse(res.data)
console.log(res1.results)
imageUrl = res1.url;
that.setData({
fileList: res1.results,
url: res1.url
})
},
});
},
onLoad: function (options) {
},
onShow: function () {
},
res: function (e) {
console.log("提交信息")
if (imageUrl == null) {
wx.showToast({
image: '../../images/toast/error.png',
title: "请上传图片",
})
return;
}
wx.showModal({
title: '提示',
content: '确认添加',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
wx.request({
url: app.globalData.url + "uploadReport",
method: 'POST',
data: {
sessionId: wx.getStorageSync("sessionId"),
imageUrl: imageUrl
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success(res) {
console.log("添加商品结果")
console.log(res)
if (res.data.status == 1) {
wx.showToast({
image: '../../images/toast/success.png',
title: "添加成功",
})
wx.switchTab({
url: '/pages/index/index'
});
}
if (res.data.data == 0) {
wx.showToast({
image: '../../images/toast/error.png',
title: "添加失败",
})
}
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
})
5 效果

<h2><a id="1_0"></a>1介绍</h2>
<p>使用vant组件库</p>
<h2><a id="2_indexhtml_2"></a>2 index.html</h2>
<pre><div class="hljs"><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">bindsubmit</span>=<span class="hljs-string">'res'</span>></span>
<span class="hljs-tag"><<span class="hljs-name">view</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"position:relative;top:0rpx;float:left;"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">van-uploader</span> <span class="hljs-attr">max-count</span>=<span class="hljs-string">"1"</span> <span class="hljs-attr">bind:after-read</span>=<span class="hljs-string">"afterRead"</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">view</span>></span>
<span class="hljs-tag"><<span class="hljs-name">image</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width:200rpx;height:200rpx;"</span>></span><span class="hljs-tag"></<span class="hljs-name">image</span>></span>
<span class="hljs-tag"><<span class="hljs-name">van-button</span> <span class="hljs-attr">form-type</span>=<span class="hljs-string">'submit'</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"primary"</span> <span class="hljs-attr">size</span>=<span class="hljs-string">"large"</span> <span class="hljs-attr">custom-style</span>=<span class="hljs-string">"width:100%;"</span>></span>提交信息<span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>
<span class="hljs-tag"></<span class="hljs-name">form</span>></span>
</code></div></pre>
<h2><a id="3_indexjson_14"></a>3 index.json</h2>
<pre><div class="hljs"><code class="lang-json"><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"usingComponents"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"van-button"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"@vant/weapp/button/index"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"van-uploader"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"@vant/weapp/uploader/index"</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span>
</code></div></pre>
<h2><a id="4_indexjs_23"></a>4 index.js</h2>
<pre><div class="hljs"><code class="lang-js"><span class="hljs-keyword">var</span> app = <span class="hljs-title function_">getApp</span>();
<span class="hljs-keyword">var</span> imageUrl = <span class="hljs-literal">null</span>; <span class="hljs-comment">// 上传图片后获得的链接</span>
<span class="hljs-title class_">Page</span>({
<span class="hljs-attr">data</span>: {
<span class="hljs-attr">fileList</span>: <span class="hljs-string">""</span>,
},
<span class="hljs-comment">// 读取到图片 即上传</span>
<span class="hljs-title function_">afterRead</span>(<span class="hljs-params">event</span>) {
<span class="hljs-keyword">var</span> that = <span class="hljs-variable language_">this</span>;
that.<span class="hljs-title function_">setData</span>({
<span class="hljs-attr">fileList</span>: []
})
<span class="hljs-keyword">const</span> { file } = event.<span class="hljs-property">detail</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">detail</span>)
<span class="hljs-comment">// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式</span>
wx.<span class="hljs-title function_">uploadFile</span>({
<span class="hljs-attr">url</span>: app.<span class="hljs-property">globalData</span>.<span class="hljs-property">url</span> + <span class="hljs-string">'upload'</span>, <span class="hljs-comment">// 仅为示例,非真实的接口地址</span>
<span class="hljs-attr">filePath</span>: file.<span class="hljs-property">url</span>,
<span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>,
<span class="hljs-attr">formData</span>: { <span class="hljs-attr">user</span>: <span class="hljs-string">'test'</span> },
<span class="hljs-title function_">success</span>(<span class="hljs-params">res</span>) {
<span class="hljs-comment">// 上传完成需要更新 fileList</span>
<span class="hljs-comment">// console.log(res.data)</span>
<span class="hljs-keyword">var</span> res1 = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(res.<span class="hljs-property">data</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res1.<span class="hljs-property">results</span>)
imageUrl = res1.<span class="hljs-property">url</span>;
that.<span class="hljs-title function_">setData</span>({
<span class="hljs-attr">fileList</span>: res1.<span class="hljs-property">results</span>,
<span class="hljs-attr">url</span>: res1.<span class="hljs-property">url</span> <span class="hljs-comment">// 这里的参数传到 微信小程序前端 可以即时加载出 我们刚才上传的图片</span>
})
},
});
},
<span class="hljs-attr">onLoad</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">options</span>) {
},
<span class="hljs-attr">onShow</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) {
},
<span class="hljs-comment">// 提交信息</span>
<span class="hljs-attr">res</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"提交信息"</span>)
<span class="hljs-comment">// var now_price = e.detail.value.now_price</span>
<span class="hljs-comment">// console.log(title)</span>
<span class="hljs-keyword">if</span> (imageUrl == <span class="hljs-literal">null</span>) {
wx.<span class="hljs-title function_">showToast</span>({
<span class="hljs-attr">image</span>: <span class="hljs-string">'../../images/toast/error.png'</span>,
<span class="hljs-attr">title</span>: <span class="hljs-string">"请上传图片"</span>,
})
<span class="hljs-keyword">return</span>;
}
wx.<span class="hljs-title function_">showModal</span>({
<span class="hljs-attr">title</span>: <span class="hljs-string">'提示'</span>,
<span class="hljs-attr">content</span>: <span class="hljs-string">'确认添加'</span>,
<span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) {
<span class="hljs-keyword">if</span> (res.<span class="hljs-property">confirm</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'用户点击确定'</span>)
wx.<span class="hljs-title function_">request</span>({
<span class="hljs-attr">url</span>: app.<span class="hljs-property">globalData</span>.<span class="hljs-property">url</span> + <span class="hljs-string">"uploadReport"</span>,
<span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
<span class="hljs-attr">data</span>: {
<span class="hljs-attr">sessionId</span>: wx.<span class="hljs-title function_">getStorageSync</span>(<span class="hljs-string">"sessionId"</span>),
<span class="hljs-comment">// title: title,</span>
<span class="hljs-attr">imageUrl</span>: imageUrl
},
<span class="hljs-attr">header</span>: {
<span class="hljs-comment">// 'content-type': 'application/json' // GET</span>
<span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/x-www-form-urlencoded"</span><span class="hljs-comment">// POST</span>
},
<span class="hljs-title function_">success</span>(<span class="hljs-params">res</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"添加商品结果"</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res)
<span class="hljs-keyword">if</span> (res.<span class="hljs-property">data</span>.<span class="hljs-property">status</span> == <span class="hljs-number">1</span>) {
wx.<span class="hljs-title function_">showToast</span>({
<span class="hljs-attr">image</span>: <span class="hljs-string">'../../images/toast/success.png'</span>,
<span class="hljs-attr">title</span>: <span class="hljs-string">"添加成功"</span>,
})
wx.<span class="hljs-title function_">switchTab</span>({
<span class="hljs-attr">url</span>: <span class="hljs-string">'/pages/index/index'</span>
});
}
<span class="hljs-keyword">if</span> (res.<span class="hljs-property">data</span>.<span class="hljs-property">data</span> == <span class="hljs-number">0</span>) {
wx.<span class="hljs-title function_">showToast</span>({
<span class="hljs-attr">image</span>: <span class="hljs-string">'../../images/toast/error.png'</span>,
<span class="hljs-attr">title</span>: <span class="hljs-string">"添加失败"</span>,
})
}
}
})
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (res.<span class="hljs-property">cancel</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'用户点击取消'</span>)
}
}
})
},
})
</code></div></pre>
<h2><a id="5__138"></a>5 效果</h2>
<p><img src="https://static.couragesteak.com/article/72601ef37b633aded10f8fe4c66df342.png" alt="https://static.couragesteak.com/article/72601ef37b633aded10f8fe4c66df342.png" /></p>
<pre><code class="lang-"></code></pre>
留言