publishPhoto.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. var app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. defaultSize: 'mini',
  8. src: null,
  9. photos: [],
  10. inputValue: '',
  11. orgId: '',
  12. orgName: '',
  13. // value: "", // 文本的内容
  14. placeholder: "请输入项目施工说明",
  15. maxlength: -1, // 最大输入长度,设置为 -1 的时候不限制最大长度
  16. focus: true,
  17. weather:'',
  18. lat:'',
  19. lng:'',
  20. address:''
  21. },
  22. onLoad: function (options) {
  23. var img = "/images/add.png";
  24. var tmpphoto = this.data.photos;
  25. var tp = {};
  26. tp['path'] = img;
  27. tp['type'] = 0;
  28. tp['fileName'] = '';
  29. tmpphoto.push(tp);
  30. this.setData({
  31. photos: tmpphoto,
  32. orgId: options.orgId,
  33. orgName: options.orgName,
  34. weather: options.weather,
  35. address: options.address,
  36. lng: options.lng,
  37. lat: options.lat
  38. });
  39. },
  40. takePhoto: function (event) {
  41. console.debug("takePhoto");
  42. var that = this;
  43. wx.showActionSheet({
  44. itemList: ['拍摄', '相册'],
  45. success: function (res) {
  46. if (!res.cancel) {
  47. console.log(res.tapIndex);
  48. if (res.tapIndex == 0) {
  49. wx.chooseImage({
  50. count: 1, // 默认9
  51. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  52. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  53. success: function (res) {
  54. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  55. var tempFilePaths = res.tempFilePaths
  56. console.debug(tempFilePaths);
  57. wx.uploadFile({
  58. url: app.globalData.cloudBase + "/Wx/Project/Implementation/Upload/Image/"+that.data.orgId, //仅为示例,非真实的接口地址
  59. filePath: tempFilePaths[0],
  60. name: 'image',
  61. success: function (res) {
  62. var data = res.data;
  63. var jsonStr = data.replace(" ", "");
  64. var json;
  65. if (typeof jsonStr != 'object') {
  66. jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
  67. json = JSON.parse(jsonStr);
  68. }
  69. if (json.code == "0") {
  70. var tmpphoto = that.data.photos;
  71. var img = tempFilePaths[0];
  72. console.log("path " + img);
  73. var tp = {};
  74. tp['path'] = img;
  75. tp['type'] = 1;
  76. tp['fileName'] = json.filename;
  77. tmpphoto.unshift(tp);
  78. that.setData({
  79. photos: tmpphoto
  80. })
  81. }
  82. //console.log("upload back " + res.data);
  83. // var jsonStr = data.replace(" ", "");
  84. // var json;
  85. // if (typeof jsonStr != 'object') {
  86. // jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
  87. // json = JSON.parse(jsonStr);
  88. // }
  89. // if (json.status === 'ok') {
  90. // var tmpphoto = that.data.photos;
  91. // var img = tempFilePaths[0];
  92. // console.log("path " + img);
  93. // var tp = {};
  94. // tp['path'] = img;
  95. // tp['type'] = 1;
  96. // tp['fileName'] = json.filename;
  97. // tmpphoto.unshift(tp);
  98. // that.setData({
  99. // photos: tmpphoto
  100. // })
  101. // }
  102. }
  103. })
  104. }
  105. })
  106. } else if (res.tapIndex == 1) {
  107. wx.chooseImage({
  108. count: 1, // 默认9
  109. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  110. sourceType: ['album'], // 可以指定来源是相册还是相机,默认二者都有
  111. success: function (res) {
  112. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  113. var tempFilePaths = res.tempFilePaths
  114. console.debug(tempFilePaths);
  115. // var tmpphoto = that.data.photos;
  116. // var img = tempFilePaths[0];
  117. // var tp = {};
  118. // tp['path'] = img;
  119. // tp['type'] = 1;
  120. // tp['fileName'] = '';
  121. // tmpphoto.unshift(tp);
  122. // that.setData({
  123. // photos: tmpphoto
  124. // })
  125. wx.uploadFile({
  126. url: app.globalData.cloudBase + "/Wx/Upload/Image", //仅为示例,非真实的接口地址
  127. filePath: tempFilePaths[0],
  128. name: 'image',
  129. success: function (res) {
  130. var data = res.data;
  131. var jsonStr = data.replace(" ", "");
  132. var json;
  133. if (typeof jsonStr != 'object') {
  134. jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
  135. json = JSON.parse(jsonStr);
  136. }
  137. if (json.code == "0") {
  138. var tmpphoto = that.data.photos;
  139. var img = tempFilePaths[0];
  140. console.log("path " + img);
  141. var tp = {};
  142. tp['path'] = img;
  143. tp['type'] = 1;
  144. tp['fileName'] = json.filename;
  145. tmpphoto.unshift(tp);
  146. that.setData({
  147. photos: tmpphoto
  148. })
  149. }
  150. // console.log("upload back " + res.data);
  151. // var jsonStr = data.replace(" ", "");
  152. // var json;
  153. // if (typeof jsonStr != 'object') {
  154. // jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
  155. // json = JSON.parse(jsonStr);
  156. // }
  157. // if (json.status === 'ok') {
  158. // var tmpphoto = that.data.photos;
  159. // var img = tempFilePaths[0];
  160. // console.log("path " + img);
  161. // var tp = {};
  162. // tp['path'] = img;
  163. // tp['type'] = 1;
  164. // tp['fileName'] = json.filename;
  165. // tmpphoto.unshift(tp);
  166. // that.setData({
  167. // photos: tmpphoto
  168. // })
  169. // }
  170. }
  171. })
  172. }
  173. })
  174. }
  175. }
  176. }
  177. })
  178. },
  179. editPhoto: function (event) {
  180. console.debug("editPhoto");
  181. wx.navigateBack({
  182. delta: 1
  183. })
  184. },
  185. sendPhoto: function (event) {
  186. if (this.data.inputValue.length > 0 || (this.data.photos.length - 1) > 0) {
  187. var that = this;
  188. try {
  189. wx.showModal({
  190. title: '提示',
  191. content: '确定上报项目情况?',
  192. success: function (res) {
  193. if (res.confirm) {
  194. wx.showNavigationBarLoading()
  195. var data = {};
  196. data['orgId'] = that.data.orgId;
  197. var openid = wx.getStorageSync('openid')
  198. if (openid) {
  199. data['openid'] = openid;
  200. }
  201. if (that.data.inputValue.length > 0) {
  202. data['desc'] = that.data.inputValue;
  203. } else {
  204. data['desc'] = '';
  205. }
  206. if (that.data.address.length > 0) {
  207. data['address'] = that.data.address;
  208. } else {
  209. data['address'] = '';
  210. }
  211. data['lat'] = that.data.lat;
  212. data['lng'] = that.data.lng;
  213. data['weather'] = that.data.weather;
  214. var photos = that.data.photos;
  215. var tmpphotos = [];
  216. for (var i = 0; i < photos.length; i++) {
  217. var photo = photos[i];
  218. if (photo.type !== 0) {
  219. var photoobj = {
  220. id: i,
  221. showBoxId: 0,
  222. photo: photo.fileName
  223. }
  224. tmpphotos.push(photoobj);
  225. }
  226. }
  227. data['photos'] = tmpphotos;
  228. console.log(data);
  229. wx.request({
  230. url: app.globalData.cloudBase + "/Wx/Porject/Report/"+openid,
  231. data: data,
  232. method: "POST",
  233. header: {
  234. 'content-type': 'application/json' // 默认值
  235. },
  236. success: function (res) {
  237. console.log(res.data)
  238. if (res.data.code === '0') {
  239. wx.showModal({
  240. title: '提示',
  241. content: '上报成功!',
  242. showCancel: false,
  243. success: function (res) {
  244. if (res.confirm) {
  245. wx.navigateBack({
  246. delta: 1
  247. })
  248. }
  249. }
  250. })
  251. } else {
  252. wx.showModal({
  253. title: '提示',
  254. content: '上报失败!',
  255. showCancel: false,
  256. success: function (res) {}
  257. })
  258. }
  259. },
  260. fail: function (res) {
  261. console.log(res.errMsg)
  262. }
  263. })
  264. } else {
  265. console.log('用户点击取消')
  266. }
  267. }
  268. });
  269. } catch (e) {
  270. console.log(e);
  271. }
  272. } else {
  273. console.debug("sendPhoto fail");
  274. wx.showToast({
  275. title: '请检查是否正确录入项目情况!',
  276. icon: 'none',
  277. duration: 3000
  278. })
  279. }
  280. },
  281. bindKeyInput: function (e) {
  282. this.setData({
  283. inputValue: e.detail.value
  284. })
  285. },
  286. bindAddressKeyInput: function (e) {
  287. this.setData({
  288. address: e.detail.value
  289. })
  290. },
  291. error(e) {
  292. console.log(e.detail)
  293. },
  294. })