add.js 6.3 KB

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