add.js 7.6 KB

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