add.js 7.8 KB

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