addData.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <!-- -->
  2. <template>
  3. <div class="addData_box" :style="'height:' + fullHeight + 'px'">
  4. <!-- 头部 -->
  5. <div class="addData_head">
  6. <div class="addData_head_returnA" @click="FanHui()">
  7. <img src="../../assets/image/left.png" alt class="addData_head_returnA_img" />
  8. </div>
  9. <!-- 标题 -->
  10. <div class="head_title">
  11. <span class="head_title_text">采集</span>
  12. </div>
  13. </div>
  14. <!-- 线 -->
  15. <div class="addData_segment"></div>
  16. <!-- 线 -->
  17. <!-- 主内容 -->
  18. <div class="addData_main">
  19. <!-- 输入作物或昆虫 -->
  20. <div class="radio_box" v-show="typeShow">
  21. <el-radio v-model="radio" label="plant">作物</el-radio>
  22. <el-radio v-model="radio" label="insect">昆虫</el-radio>
  23. </div>
  24. <!-- 科目 -->
  25. <div class="course_box">
  26. <el-input maxlength="25" v-model="course" placeholder="请输入您要填写的病虫害科目"></el-input>
  27. <div class="course_num">{{ this.course.length }} / 25</div>
  28. </div>
  29. <!-- 科目 -->
  30. <!-- 病虫害名称 -->
  31. <div class="insectName_box">
  32. <el-input maxlength="20" v-model="insectName" placeholder="请输入您要填写的病虫害名称"></el-input>
  33. <div class="insectName_num">{{ this.insectName.length }} / 20</div>
  34. </div>
  35. <!-- 病虫害名称 -->
  36. <!-- 内容input -->
  37. <div class="addData_IptBox">
  38. <el-input
  39. maxlength="200"
  40. type="textarea"
  41. :rows="10"
  42. placeholder="请输入该病虫害的详细描述"
  43. :onkeyup="textarea = textarea.replace(/\s+/g,'')"
  44. v-model="textarea"
  45. ></el-input>
  46. <div class="addData_IptNum">{{ this.textarea.length }} / 200</div>
  47. </div>
  48. <!-- 内容input -->
  49. <!-- 定位地址 -->
  50. <div class="orientationAddr_box">
  51. <img src="../../assets/image/ding.png" alt class="orientationAddr_img" />
  52. <div class="orientationAddr_text">{{ this.addr }}</div>
  53. </div>
  54. <!-- 定位地址 -->
  55. <!-- 上传图片 -->
  56. <div class="uploading_box" v-loading="loading">
  57. <el-upload
  58. ref="upload"
  59. action="/send_answer_img"
  60. list-type="picture-card"
  61. :on-preview="handlePictureCardPreview"
  62. :http-request="handleHttpRequest"
  63. :limit="imgLimit"
  64. :on-remove="clearFiles"
  65. >
  66. <i class="el-icon-plus"></i>
  67. </el-upload>
  68. <el-dialog :visible.sync="dialogVisible">
  69. <img width="100%" :src="dialogImageUrl" alt />
  70. </el-dialog>
  71. </div>
  72. <!-- 删除按钮 -->
  73. <button @click="handleRemove" class="delBtn" v-show="delImage">删除图片</button>
  74. <!-- 上传图片 -->
  75. <!-- 发布 -->
  76. <button v-bind:class="{ 'nav-hide' : hideClass }" @click="bomNextBtn()" class="bomNextBtn">采集</button>
  77. <!-- 发布 -->
  78. </div>
  79. <!-- 主内容 -->
  80. </div>
  81. </template>
  82. <script>
  83. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  84. //例如:import 《组件名称》 from '《组件路径》';
  85. export default {
  86. //import引入的组件需要注入到对象中才能使用
  87. name: "addData",
  88. components: {},
  89. data() {
  90. //这里存放数据
  91. return {
  92. isClick: true, //点赞开关
  93. hideClass: false, //按钮被顶上去
  94. loading: false,
  95. fullHeight: document.documentElement.clientHeight,
  96. showHeight: document.documentElement.clientHeight, //按钮被顶上去
  97. textarea: "", //内容
  98. addImage: [], //图片
  99. delImage: false, //删除按钮
  100. imgLimit: "", //限制上传图片张数
  101. dialogVisible: false,
  102. dialogImageUrl: "",
  103. addr: localStorage.getItem("dizhi"), //地址
  104. radio: "plant", //作物或昆虫单选框
  105. course: "", //科目
  106. insectName: "", //病虫害名称
  107. typeShow: false, //判断是识别失败还是手动输入的进行隐藏、显示数据类型
  108. isClick: true //开关
  109. };
  110. },
  111. //监听属性 类似于data概念
  112. computed: {},
  113. //监控data中的数据变化
  114. watch: {
  115. fullHeight(val) {
  116. //监控浏览器高度变化
  117. if (!this.timer) {
  118. this.fullHeight = val;
  119. this.timer = true;
  120. let that = this;
  121. setTimeout(function() {
  122. //防止过度调用监测事件,导致卡顿
  123. that.timer = false;
  124. }, 400);
  125. }
  126. },
  127. //描述
  128. textarea(val) {
  129. // console.log(val);
  130. },
  131. //科目
  132. course(val) {
  133. this.course = this.course.replace(/[^A-Za-z0-9\u4e00-\u9fa5]/g, "");
  134. },
  135. //病虫害名称
  136. insectName(val) {
  137. this.insectName = this.insectName.replace(
  138. /[^A-Za-z0-9\u4e00-\u9fa5]/g,
  139. ""
  140. );
  141. },
  142. addImage(val) {
  143. if (val !== null) {
  144. this.delImage = true;
  145. this.loading = false;
  146. } else {
  147. this.delImage = false;
  148. this.loading = false;
  149. }
  150. },
  151. //按钮被顶上去
  152. showHeight: "inputType"
  153. },
  154. //方法集合
  155. methods: {
  156. //动态获取浏览器高度
  157. get_boderHeight() {
  158. const that = this;
  159. window.onresize = () => {
  160. return (() => {
  161. window.fullHeight = document.documentElement.clientHeight;
  162. that.fullHeight = window.fullHeight;
  163. })();
  164. };
  165. },
  166. //返回主页
  167. FanHui() {
  168. //判断是识别失败进来的还是直接点击的手动输入
  169. if (localStorage.getItem("memoryCompileID") !== null) {
  170. //识别失败
  171. // this.$router.push("/memory");
  172. this.$router.replace("/memory");
  173. localStorage.removeItem("memoryCompileID");
  174. localStorage.removeItem("memoryDistinction");
  175. } else {
  176. //手动输入
  177. // this.$router.push("/person");
  178. this.$router.replace("/person");
  179. }
  180. localStorage.removeItem("add");
  181. },
  182. //上传图片-删除
  183. handleRemove() {
  184. //判断是识别失败进来的还是直接点击的手动输入
  185. if (localStorage.getItem("memoryCompileID") !== null) {
  186. let postData = this.$qs.parse({
  187. img_list: localStorage.getItem("add"),
  188. ret: this.radio
  189. });
  190. //识别失败
  191. return this.$confirm("确定要删除图片吗", "提示", {
  192. confirmButtonText: "确定",
  193. cancelButtonText: "取消",
  194. type: "warning"
  195. })
  196. .then(() => {
  197. this.$axios({
  198. method: "post",
  199. url: "http://120.27.222.26:5555/del_insect_photo",
  200. data: postData
  201. })
  202. .then(res => {
  203. if (res.data === 0) {
  204. this.$notify({
  205. title: "成功",
  206. message: "删除成功",
  207. type: "success",
  208. duration: 1500
  209. });
  210. this.$refs.upload.clearFiles();
  211. localStorage.removeItem("add");
  212. } else if (res.data === 1) {
  213. this.$notify.error({
  214. title: "错误",
  215. message: "删除失败",
  216. duration: 1500
  217. });
  218. }
  219. })
  220. .catch(error => {
  221. console.log(error);
  222. });
  223. })
  224. .catch(() => {
  225. throw new Error("取消成功!");
  226. });
  227. } else {
  228. let postData = this.$qs.parse({
  229. pic_list: localStorage.getItem("add"),
  230. ret: this.radio
  231. });
  232. //手动输入
  233. return this.$confirm("确定要删除图片吗", "提示", {
  234. confirmButtonText: "确定",
  235. cancelButtonText: "取消",
  236. type: "warning"
  237. })
  238. .then(() => {
  239. this.$axios({
  240. method: "post",
  241. url: "http://120.27.222.26:5555/del_person_img",
  242. data: postData
  243. })
  244. .then(res => {
  245. if (res.data === 0) {
  246. this.$notify({
  247. title: "成功",
  248. message: "删除成功",
  249. type: "success",
  250. duration: 1500
  251. });
  252. this.$refs.upload.clearFiles();
  253. localStorage.removeItem("add");
  254. } else if (res.data === 1) {
  255. this.$notify.error({
  256. title: "错误",
  257. message: "删除失败",
  258. duration: 1500
  259. });
  260. }
  261. })
  262. .catch(error => {
  263. console.log(error);
  264. });
  265. })
  266. .catch(() => {
  267. throw new Error("取消成功!");
  268. });
  269. }
  270. },
  271. handlePictureCardPreview(file) {
  272. this.dialogImageUrl = file.url;
  273. this.dialogVisible = true;
  274. },
  275. //上传
  276. handleHttpRequest(file) {
  277. let _this = this;
  278. _this.loading = true;
  279. let form = new FormData();
  280. form.append("file", file.file);
  281. form.append("ret", _this.radio);
  282. //判断是识别失败进来的还是直接点击的手动输入
  283. if (localStorage.getItem("memoryCompileID") !== null) {
  284. _this.imgLimit = 1; //限制上传图片张数
  285. //识别失败
  286. _this
  287. .$axios({
  288. method: "post",
  289. url: "http://120.27.222.26:5555/upload_photos",
  290. anync: true,
  291. data: form,
  292. headers: {
  293. "Content-Type": "multipart/form-data"
  294. }
  295. })
  296. .then(res => {
  297. _this.addImage.push(res.data.news_photo);
  298. _this.btnShow = true;
  299. localStorage.setItem("add", _this.addImage);
  300. })
  301. .catch(error => {
  302. // console.log("222");
  303. });
  304. } else {
  305. _this.imgLimit = 3; //限制上传图片张数
  306. //手动输入
  307. _this
  308. .$axios({
  309. method: "post",
  310. url: "http://120.27.222.26:5555/person_harm_img",
  311. anync: true,
  312. data: form,
  313. headers: {
  314. "Content-Type": "multipart/form-data"
  315. }
  316. })
  317. .then(res => {
  318. _this.addImage.push(res.data.src);
  319. _this.btnShow = true;
  320. localStorage.setItem("add", _this.addImage);
  321. })
  322. .catch(error => {
  323. // console.log("222");
  324. });
  325. }
  326. },
  327. clearFiles() {
  328. // console.log("删除");
  329. },
  330. //发布
  331. bomNextBtn() {
  332. let _this = this;
  333. var isClick = _this.isClick;
  334. if (isClick === true) {
  335. _this.isClick = false;
  336. //判断input是否为空
  337. if (_this.textarea === "") {
  338. this.$notify({
  339. title: "警告",
  340. message: "请填写您分享的文字",
  341. type: "warning",
  342. duration: 1500
  343. });
  344. } else if (_this.course === "") {
  345. this.$notify({
  346. title: "警告",
  347. message: "请填写病虫害的科目",
  348. type: "warning",
  349. duration: 1500
  350. });
  351. } else if (_this.insectName === "") {
  352. this.$notify({
  353. title: "警告",
  354. message: "请填写病虫害名称",
  355. type: "warning",
  356. duration: 1500
  357. });
  358. } else if (localStorage.getItem("add") === null) {
  359. this.$notify({
  360. title: "警告",
  361. message: "请上传您分享的照片",
  362. type: "warning",
  363. duration: 1500
  364. });
  365. } else if (
  366. _this.textarea !== "" &&
  367. localStorage.getItem("add") !== null
  368. ) {
  369. //判断是识别失败进来的还是直接点击的手动输入
  370. if (localStorage.getItem("memoryCompileID") !== null) {
  371. //识别失败
  372. let postData = _this.$qs.parse({
  373. addr: localStorage.getItem("dizhi"), //地址
  374. lng: localStorage.getItem("jingdu"), //经度
  375. lat: localStorage.getItem("weidu"), //纬度
  376. img_urls: localStorage.getItem("add"), //
  377. content: _this.textarea, //描述信息
  378. name: _this.insectName, //病虫害名称
  379. ret: localStorage.getItem("memoryDistinction"), //病虫害区别
  380. course: _this.course, //科目
  381. id: localStorage.getItem("memoryCompileID") //id
  382. });
  383. _this
  384. .$axios({
  385. method: "post",
  386. url: "http://120.27.222.26:5555/make_data",
  387. data: postData
  388. })
  389. .then(res => {
  390. if (res.data === 0) {
  391. this.$notify({
  392. title: "成功",
  393. message: "发布成功",
  394. type: "success",
  395. duration: 1500
  396. });
  397. //判断是识别失败进来的还是直接点击的手动输入
  398. if (localStorage.getItem("memoryCompileID") !== null) {
  399. //识别失败
  400. this.$router.replace("/memory");
  401. localStorage.removeItem("memoryCompileID");
  402. localStorage.removeItem("commandIndex");
  403. localStorage.setItem("commandLuru", "全部");
  404. } else {
  405. //手动输入
  406. // this.$router.push("/home");
  407. localStorage.setItem("commandIndex", "all");
  408. localStorage.setItem("commandLuru", "全部");
  409. this.$router.replace("/home");
  410. }
  411. const timeA = setInterval(() => {
  412. location.reload();
  413. }, 1500);
  414. } else if (res.data === 1) {
  415. this.$notify.error({
  416. title: "错误",
  417. message: "发布失败,请重试",
  418. duration: 1500
  419. });
  420. }
  421. })
  422. .catch(err => {
  423. console.log(err);
  424. });
  425. } else {
  426. //手动输入
  427. let postData = _this.$qs.parse({
  428. addr: localStorage.getItem("dizhi"), //地址
  429. lng: localStorage.getItem("jingdu"), //经度
  430. lat: localStorage.getItem("weidu"), //纬度
  431. img_list: localStorage.getItem("add"), //
  432. problem: _this.textarea, //描述信息
  433. name: _this.insectName, //病虫害名称
  434. ret: _this.radio, //病虫害区别
  435. sort: _this.course //科目
  436. });
  437. _this
  438. .$axios({
  439. method: "post",
  440. url: "http://120.27.222.26:5555/save_disease",
  441. data: postData
  442. })
  443. .then(res => {
  444. if (res.data === 0) {
  445. this.$notify({
  446. title: "成功",
  447. message: "发布成功",
  448. type: "success",
  449. duration: 1500
  450. });
  451. //判断是识别失败进来的还是直接点击的手动输入
  452. if (localStorage.getItem("memoryCompileID") !== null) {
  453. //识别失败
  454. // this.$router.push("/memory");
  455. this.$router.replace("/memory");
  456. localStorage.removeItem("memoryCompileID");
  457. localStorage.setItem("commandIndex", "all");
  458. localStorage.setItem("commandLuru", "全部");
  459. } else {
  460. //手动输入
  461. // this.$router.push("/home");
  462. localStorage.setItem("commandIndex", "all");
  463. localStorage.setItem("commandLuru", "全部");
  464. this.$router.replace("/home");
  465. }
  466. const timeA = setInterval(() => {
  467. location.reload();
  468. }, 1500);
  469. } else if (res.data === 1) {
  470. this.$notify.error({
  471. title: "错误",
  472. message: "发布失败,请重试",
  473. duration: 1500
  474. });
  475. }
  476. })
  477. .catch(err => {
  478. console.log(err);
  479. });
  480. }
  481. }
  482. setTimeout(function() {
  483. _this.isClick = true;
  484. }, 1500);
  485. }
  486. },
  487. //按钮被顶上去
  488. inputType() {
  489. if (!this.timer) {
  490. this.timer = true;
  491. let that = this;
  492. setTimeout(() => {
  493. if (that.fullHeight > that.showHeight) {
  494. //显示class
  495. this.hideClass = true;
  496. } else if (that.fullHeight <= that.showHeight) {
  497. //显示隐藏
  498. this.hideClass = false;
  499. }
  500. that.timer = false;
  501. }, 20);
  502. }
  503. }
  504. },
  505. //生命周期 - 创建完成(可以访问当前this实例)
  506. created() {
  507. var _this = this;
  508. //判断是识别失败进来的还是直接点击的手动输入
  509. if (localStorage.getItem("memoryCompileID") !== null) {
  510. //识别失败
  511. _this.typeShow = false;
  512. } else {
  513. //手动输入
  514. _this.typeShow = true;
  515. }
  516. },
  517. //生命周期 - 挂载完成(可以访问DOM元素)
  518. mounted() {
  519. this.get_boderHeight();
  520. // window.onresize监听页面高度的变化
  521. window.onresize = () => {
  522. return (() => {
  523. window.screenHeight = document.body.clientHeight;
  524. this.showHeight = window.screenHeight;
  525. })();
  526. };
  527. }
  528. };
  529. </script>
  530. <style lang='scss'>
  531. @import "../../assets/scss/bus.scss";
  532. @import "../../assets/scss/addData.scss";
  533. </style>