add.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. "text":'',
  42. "is_qun":0,
  43. "busy1":false,
  44. "textarea":false,
  45. "expire":false,
  46. "imageAble":false,
  47. "upload_busy":false
  48. },
  49. onLoad: function () {
  50. console.log('onLoad');
  51. app.getUserInfo(function(userInfo){
  52. console.log(userInfo);
  53. });
  54. const ctx = wx.createCanvasContext('myCanvas')
  55. this.chooseImage();
  56. },
  57. onShow: function () {
  58. console.log('onShow');
  59. // this.chooseImage();
  60. },
  61. chooseImage: function() {
  62. var self = this;
  63. if(self.data.upload_busy||self.data.textarea){
  64. console.log(self.data.textarea)
  65. return;
  66. }
  67. wx.chooseImage({
  68. count: 1,
  69. sizeType: ['compressed'],
  70. sourceType: ['album','camera'],
  71. success: function(res) {
  72. // showMsg('提示信息','chooseImage successful,temp path is'+res.tempFilePaths[0]);
  73. console.log(res);
  74. console.log('chooseImage success, temp path is', res.tempFilePaths[0])
  75. wx.getImageInfo({
  76. src: res.tempFilePaths[0],
  77. success: function (res) {
  78. console.log(res)
  79. }
  80. })
  81. var imageSrc = res.tempFilePaths[0];
  82. // wx.saveFile({
  83. // tempFilePath: imageSrc,
  84. // success: function(res) {
  85. // console.log(res)
  86. // }
  87. // })
  88. // wx.getSavedFileList({
  89. // success: function(res) {
  90. // console.log(res.fileList)
  91. // }
  92. // })
  93. self.setData({
  94. 'upload_busy':true
  95. })
  96. //上传图片到服务器
  97. wx.uploadFile({
  98. url: config.service.uploadUrl,
  99. filePath: imageSrc,
  100. name: 'file',
  101. success: function(res) {
  102. console.log(res.statusCode);
  103. if(res.statusCode==200){
  104. var data=JSON.parse(res.data);
  105. console.log(data);
  106. if(data.success==true){
  107. self.setData({
  108. 'imageSrc':data.file.url,
  109. 'imageAble':false
  110. })
  111. showSuccess('上传成功');
  112. }else{
  113. showMsg('提示信息','上传失败');
  114. }
  115. }else if(res.statusCode==413){
  116. showMsg('提示信息','上传失败,文件过大');
  117. }else{
  118. showMsg('提示信息','上传失败');
  119. }
  120. },
  121. fail: function({errMsg}) {
  122. console.log('uploadImage fail, errMsg is', errMsg);
  123. showMsg('提示信息','上传失败,'+errMsg);
  124. },
  125. complete:function(){
  126. self.setData({
  127. 'upload_busy':false
  128. })
  129. }
  130. })
  131. },
  132. fail: function({errMsg}) {
  133. // showMsg('提示信息','chooseImage fail, err is'+errMsg)
  134. }
  135. })
  136. },
  137. expire_set:function(e){
  138. console.log(e.target.dataset);
  139. },
  140. formSubmit: function(e) {
  141. var self = this;
  142. if(self.data.upload_busy||self.data.busy1){
  143. return;
  144. }
  145. if(self.data.imageSrc==""&&self.data.text==""){
  146. showMsg("提示信息","文字和图片至少不为空");
  147. return;
  148. }
  149. var data={};
  150. if(self.data.imageSrc!=""){
  151. data.image_url=self.data.imageSrc;
  152. }
  153. if(self.data.text!=""){
  154. data.text=self.data.text;
  155. }
  156. if(e.detail.formId!='the formId is a mock one'){
  157. data.form_id=e.detail.formId;
  158. }
  159. if(self.data.text!=""&&self.data.imageSrc==""){
  160. data.type==1
  161. }else{
  162. data.type=2
  163. }
  164. self.setData({
  165. 'busy1':true
  166. })
  167. qcloud.request({
  168. // 要请求的地址
  169. url: config.service.channelMessageAddUrl,
  170. method:"POST",
  171. data: data,
  172. success(result) {
  173. console.log(result);
  174. if(result.data.success==true){
  175. showSuccess("成功");
  176. wx.switchTab({
  177. url: '/pages/channel/index/index'
  178. })
  179. }else{
  180. showMsg("提示信息","新建消息失败,"+result.data.message);
  181. }
  182. },
  183. fail(error) {
  184. console.log('request fail', error);
  185. showMsg("提示信息","新建消息失败");
  186. },
  187. complete() {
  188. self.setData({
  189. 'busy1':false
  190. })
  191. console.log('request complete');
  192. }
  193. });
  194. },
  195. textarea_toggle:function(){
  196. if(this.data.textarea==false){
  197. this.setData({
  198. 'textarea':true
  199. })
  200. }else{
  201. this.setData({
  202. 'textarea':false
  203. })
  204. }
  205. },
  206. bindTextAreaInput:function(e){
  207. console.log(e.detail.value)
  208. this.setData({
  209. 'text':e.detail.value
  210. })
  211. },
  212. bindTextAreaConfirm:function(e){
  213. console.log(e.detail.value)
  214. this.setData({
  215. 'text':e.detail.value
  216. })
  217. },
  218. imageLoad: function(e){
  219. console.log(e);
  220. this.setData({
  221. 'imageAble':true
  222. })
  223. var width=e.detail.width;
  224. var height=e.detail.height;
  225. try {
  226. var res = wx.getSystemInfoSync()
  227. if(res.system.indexOf('Android')!==-1){
  228. if(width>height){
  229. this.setData({
  230. 'imageSrc':this.data.imageSrc+'/rotate/90',
  231. })
  232. }
  233. }
  234. } catch (e) {
  235. // Do something when catch error
  236. }
  237. }
  238. })