123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // 引入 QCloud 小程序增强 SDK
- var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
- // 引入配置
- var config = require('../../config');
- // 显示繁忙提示
- var showBusy = text => wx.showToast({
- title: text,
- icon: 'loading',
- duration: 10000
- });
- // 显示成功提示
- var showSuccess = text => {
- wx.hideToast();
- wx.showToast({
- title: text,
- icon: 'success'
- });
- };
- // 显示失败提示
- var showModel = (title, content) => {
- wx.hideToast();
- wx.showModal({
- title,
- content: JSON.stringify(content),
- showCancel: false
- });
- };
- var showMsg = (title, content) => {
- wx.hideToast();
- wx.showModal({
- title,
- content: content,
- showCancel: false
- });
- };
- //获取应用实例
- var app = getApp()
- Page({
- data: {
- "imageSrc":'',
- "busy":false,
- "upload_busy":false
- },
- onLoad: function () {
- console.log('onLoad');
- },
- onShow: function () {
- console.log('onShow');
- },
- chooseImage: function() {
- var self = this;
- if(self.data.upload_busy){
- return;
- }
- wx.chooseImage({
- count: 1,
- sizeType: ['compressed'],
- sourceType: ['album'],
- success: function(res) {
- console.log('chooseImage success, temp path is', res.tempFilePaths[0])
- var imageSrc = res.tempFilePaths[0];
- self.setData({
- 'upload_busy':true
- })
- //上传图片到服务器
- wx.uploadFile({
- url: config.service.uploadUrl,
- filePath: imageSrc,
- name: 'file',
- success: function(res) {
- console.log('uploadImage success, res is:', res);
- var data=JSON.parse(res.data);
- console.log(data);
- if(data.success==true){
- self.setData({
- 'imageSrc':data.file.url
- })
- showSuccess('上传成功');
- }else{
- showMsg('提示信息','上传失败');
- }
- },
- fail: function({errMsg}) {
- console.log('uploadImage fail, errMsg is', errMsg)
- },
- complete:function(){
- self.setData({
- 'upload_busy':false
- })
- }
- })
- },
- fail: function({errMsg}) {
- console.log('chooseImage fail, err is', errMsg)
- }
- })
- },
- formSubmit: function(e) {
- var self = this;
- if(self.data.upload_busy||self.data.busy){
- return;
- }
- if(e.detail.value.text==""&&self.data.imageSrc==""){
- showMsg("提示信息","内容和图片至少有一个不能为空");
- return;
- }
- var data={};
- if(e.detail.value.text!=""){
- data.text=e.detail.value.text;
- }
- if(self.data.imageSrc!=""){
- data.image_url=self.data.imageSrc;
- }
- console.log('form发生了submit事件,携带数据为:', e.detail.value);
- self.setData({
- 'busy':true
- })
- qcloud.request({
- // 要请求的地址
- url: config.service.messageAddUrl,
- method:"POST",
- data: data,
- success(result) {
- console.log(result);
- if(result.data.success==true){
- showSuccess("成功");
- wx.navigateTo({
- url: '/pages/view/view?hash_key='+result.data.id
- })
- }else{
- showMsg("提示信息","新建消息失败");
- }
- },
- fail(error) {
- console.log('request fail', error);
- showMsg("提示信息","新建消息失败");
- },
- complete() {
- self.setData({
- 'busy':false
- })
- console.log('request complete');
- }
- });
- },
- onShareAppMessage: function () {
- return {
- title: '密件',
- desc: '密件',
- path: 'pages/index/index'
- }
- }
- })
|