add.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // 引入 QCloud 小程序增强 SDK
  2. var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
  3. // 引入配置
  4. var config = require('../../config');
  5. // 显示繁忙提示
  6. var showBusy = text => wx.showToast({
  7. title: text,
  8. icon: 'loading',
  9. duration: 10000
  10. });
  11. // 显示成功提示
  12. var showSuccess = text => {
  13. wx.hideToast();
  14. wx.showToast({
  15. title: text,
  16. icon: 'success'
  17. });
  18. };
  19. // 显示失败提示
  20. var showModel = (title, content) => {
  21. wx.hideToast();
  22. wx.showModal({
  23. title,
  24. content: JSON.stringify(content),
  25. showCancel: false
  26. });
  27. };
  28. var showMsg = (title, content) => {
  29. wx.hideToast();
  30. wx.showModal({
  31. title,
  32. content: content,
  33. showCancel: false
  34. });
  35. };
  36. //获取应用实例
  37. var app = getApp()
  38. Page({
  39. data: {
  40. "imageSrc":'',
  41. "busy":false,
  42. "upload_busy":false
  43. },
  44. onLoad: function () {
  45. console.log('onLoad');
  46. },
  47. onShow: function () {
  48. console.log('onShow');
  49. },
  50. chooseImage: function() {
  51. var self = this;
  52. if(self.data.upload_busy){
  53. return;
  54. }
  55. wx.chooseImage({
  56. count: 1,
  57. sizeType: ['compressed'],
  58. sourceType: ['album'],
  59. success: function(res) {
  60. console.log('chooseImage success, temp path is', res.tempFilePaths[0])
  61. var imageSrc = res.tempFilePaths[0];
  62. self.setData({
  63. 'upload_busy':true
  64. })
  65. //上传图片到服务器
  66. wx.uploadFile({
  67. url: config.service.uploadUrl,
  68. filePath: imageSrc,
  69. name: 'file',
  70. success: function(res) {
  71. console.log('uploadImage success, res is:', res);
  72. var data=JSON.parse(res.data);
  73. console.log(data);
  74. if(data.success==true){
  75. self.setData({
  76. 'imageSrc':data.file.url
  77. })
  78. showSuccess('上传成功');
  79. }else{
  80. showMsg('提示信息','上传失败');
  81. }
  82. },
  83. fail: function({errMsg}) {
  84. console.log('uploadImage fail, errMsg is', errMsg)
  85. },
  86. complete:function(){
  87. self.setData({
  88. 'upload_busy':false
  89. })
  90. }
  91. })
  92. },
  93. fail: function({errMsg}) {
  94. console.log('chooseImage fail, err is', errMsg)
  95. }
  96. })
  97. },
  98. formSubmit: function(e) {
  99. var self = this;
  100. if(self.data.upload_busy||self.data.busy){
  101. return;
  102. }
  103. if(e.detail.value.text==""&&self.data.imageSrc==""){
  104. showMsg("提示信息","内容和图片至少有一个不能为空");
  105. return;
  106. }
  107. var data={};
  108. if(e.detail.value.text!=""){
  109. data.text=e.detail.value.text;
  110. }
  111. if(self.data.imageSrc!=""){
  112. data.image_url=self.data.imageSrc;
  113. }
  114. console.log('form发生了submit事件,携带数据为:', e.detail.value);
  115. self.setData({
  116. 'busy':true
  117. })
  118. qcloud.request({
  119. // 要请求的地址
  120. url: config.service.messageAddUrl,
  121. method:"POST",
  122. data: data,
  123. success(result) {
  124. console.log(result);
  125. if(result.data.success==true){
  126. showSuccess("成功");
  127. wx.navigateTo({
  128. url: '/pages/view/view?hash_key='+result.data.id
  129. })
  130. }else{
  131. showMsg("提示信息","新建消息失败");
  132. }
  133. },
  134. fail(error) {
  135. console.log('request fail', error);
  136. showMsg("提示信息","新建消息失败");
  137. },
  138. complete() {
  139. self.setData({
  140. 'busy':false
  141. })
  142. console.log('request complete');
  143. }
  144. });
  145. },
  146. onShareAppMessage: function () {
  147. return {
  148. title: '密件',
  149. desc: '密件',
  150. path: 'pages/index/index'
  151. }
  152. }
  153. })