rtuConstructionReport.js 12 KB

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