index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <view>
  3. <view class="status_bar"></view>
  4. <view class="" style="position: relative;top: 44px;">
  5. <view style="position: fixed;z-index: 100;">
  6. <uni-nav-bar @clickLeft="clickLeft" left-icon="back" title="病虫害识别"></uni-nav-bar>
  7. </view>
  8. <view class="image_box">
  9. <image :src="path" mode="" class="image"></image>
  10. </view>
  11. <view class="recognition" v-if="datasTF">
  12. <p class="recognition_title">{{name}}</p>
  13. <view :class="tishi?'recognition_img':'recognition_img_two'">
  14. <image :src="path2" mode="" @error="error"></image>
  15. </view>
  16. <button class="recognition_details" v-if="tishi" @click="examine">查看详情</button>
  17. <button class="recognition_details" v-if="!tishi" @click="show=!show">再拍一张</button>
  18. </view>
  19. <view class="datas" v-else>
  20. <p class="datas_title">{{name}}</p>
  21. <view v-for="(item,index) in preventionArr" :key="index" class="prevention">
  22. <view class="prevention_title">
  23. <u-icon name="play-right-fill" size="24" color="#55A92D"></u-icon>
  24. <p>{{regexptitle[index]}}:</p>
  25. </view>
  26. <p class="prevention_con">{{item}}</p>
  27. </view>
  28. <button class="recognition_details" v-if="tishi" @click="examine">查看图片</button>
  29. </view>
  30. <u-modal v-model="show" :mask-close-able="true" title="" :show-cancel-button="true" confirm-text="拍病害" cancel-text="拍虫害"
  31. content="拍照识别病虫害" @confirm="confirm" @cancel="cancel"></u-modal>
  32. <kps-image-cutter @ok="onok" @cancel="oncancle" :url="url" :fixed="false" :blob="false" :maxWidth="500" :maxHeight="500"></kps-image-cutter>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import kpsImageCutter from "@/components/ksp-image-cutter/ksp-image-cutter.vue";
  38. export default {
  39. components: {
  40. kpsImageCutter
  41. },
  42. data() {
  43. return {
  44. path: "",
  45. path2: "",
  46. tishi: true,
  47. name: "",
  48. prevention: "",
  49. datasTF: true,
  50. show: false,
  51. url: "",
  52. preventionArr: [],
  53. regexptitle: []
  54. }
  55. },
  56. methods: {
  57. // pest.pests.insect_discern 虫害
  58. async worm(data) {
  59. const res = await this.$myRequest({
  60. url: '/api/api_gateway?method=pest.pests.insect_discern',
  61. data: {
  62. img_file: data
  63. }
  64. })
  65. // this.res = res.data
  66. console.log(res)
  67. if (res.data.img_urls == undefined) {
  68. this.name = "识别失败,请换张图片"
  69. this.tishi = false
  70. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  71. } else {
  72. this.path2 = res.data.img_urls
  73. this.name = res.data.name
  74. this.tishi = true
  75. this.prevention = res.data.prevention
  76. }
  77. },
  78. //pest.pests.insect_discern病害识别
  79. async disease(data) {
  80. const res = await this.$myRequest({
  81. url: '/api/api_gateway?method=pest.pests.plant_discern',
  82. data: {
  83. img_file: data
  84. }
  85. })
  86. console.log(res)
  87. // this.res = res.data
  88. if (res.data.img_urls == undefined) {
  89. this.name = "识别失败,请换张图片"
  90. this.tishi = false
  91. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  92. } else {
  93. this.path2 = res.data.img_urls
  94. this.name = res.data.name
  95. this.tishi = true
  96. this.prevention = res.data.prevention
  97. }
  98. },
  99. clickLeft() {
  100. uni.switchTab({
  101. url: "../index/index"
  102. })
  103. },
  104. examine() {
  105. this.datasTF = !this.datasTF
  106. },
  107. confirm() {
  108. this.flag = 1
  109. uni.chooseImage({
  110. count: 1, //默认9
  111. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  112. sourceType: ['camera', 'album'], //从相册选择
  113. success: (res) => {
  114. this.url = res.tempFilePaths[0]
  115. }
  116. });
  117. },
  118. cancel() {
  119. this.flag = 2
  120. uni.chooseImage({
  121. count: 1, //默认9
  122. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  123. sourceType: ['camera', 'album'], //从相册选择
  124. success: (res) => {
  125. console.log(1)
  126. this.url = res.tempFilePaths[0]
  127. }
  128. });
  129. },
  130. onok(ev) {
  131. if (this.flag == 2) {
  132. // pest.pests.insect_discern 虫害
  133. uni.uploadFile({
  134. // url: 'http://182.92.193.64:8002/api/api_gateway?method=base.bases.base_photo', //仅为示例,非真实的接口地址
  135. url: 'http://182.92.193.64:8002/api/api_gateway?method=pest.pests.insect_discern', //仅为示例,非真实的接口地址
  136. filePath: ev.path,
  137. name: 'img_file',
  138. formData: {
  139. 'user': 'test'
  140. },
  141. success: (uploadFileRes) => {
  142. console.log(JSON.parse(uploadFileRes.data))
  143. this.optionverify(JSON.parse(uploadFileRes.data).data)
  144. }
  145. });
  146. } else if (this.flag == 1) {
  147. //pest.pests.insect_discern病害识别
  148. uni.uploadFile({
  149. // url: 'http://182.92.193.64:8002/api/api_gateway?method=base.bases.base_photo', //仅为示例,非真实的接口地址
  150. url: 'http://182.92.193.64:8002/api/api_gateway?method=pest.pests.plant_discern', //仅为示例,非真实的接口地址
  151. filePath: ev.path,
  152. name: 'img_file',
  153. formData: {
  154. 'user': 'test'
  155. },
  156. success: (uploadFileRes) => {
  157. console.log(JSON.parse(uploadFileRes.data))
  158. this.optionverify(JSON.parse(uploadFileRes.data).data)
  159. }
  160. });
  161. }
  162. this.url = "";
  163. },
  164. oncancle() {
  165. // url设置为空,隐藏控件
  166. this.url = ''
  167. },
  168. error() {
  169. this.path2 = "../../static/image/e1cd85dc59139760f43ddbac15136f2.png"
  170. },
  171. optionverify(data) {
  172. if (data == null) {
  173. this.name = "识别失败,请换张图片"
  174. this.tishi = false
  175. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  176. } else {
  177. this.path2 = data.img_urls
  178. this.name = data.name
  179. this.tishi = true
  180. this.prevention = data.prevention
  181. var regex2 = /\[(.+?)\]/g; // [] 中括号
  182. var str = this.prevention
  183. var arr = str.match(regex2)
  184. var arrindex = []
  185. for (var i = 0; i < arr.length; i++) {
  186. arrindex.push(str.indexOf(arr[i]))
  187. }
  188. for (var i = 0; i < arr.length; i++) {
  189. this.preventionArr.push(str.slice(arrindex[i] + arr[i].length, arrindex[i + 1]))
  190. }
  191. for (var i = 0; i < arr.length; i++) {
  192. arr[i] = arr[i].slice(1, arr[i].length - 1)
  193. }
  194. this.regexptitle = arr
  195. }
  196. }
  197. },
  198. onLoad(option) {
  199. console.log(option)
  200. this.path = option.path
  201. this.optionverify(JSON.parse(option.datas).data)
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. .image_box {
  207. position: absolute;
  208. top: 44px;
  209. width: 100%;
  210. height: 400rpx;
  211. .image {
  212. width: 100%;
  213. height: 400rpx;
  214. }
  215. }
  216. .recognition {
  217. position: absolute;
  218. top: 254px;
  219. width: 100%;
  220. height: 800rpx;
  221. .recognition_title {
  222. width: 100%;
  223. text-align: center;
  224. font-size: 36rpx;
  225. font-weight: 700;
  226. margin: 60rpx 0;
  227. }
  228. .recognition_img {
  229. border: 2rpx dashed #06B535;
  230. width: 450rpx;
  231. height: 450rpx;
  232. border-radius: 50%;
  233. margin: 0 auto;
  234. text-align: center;
  235. line-height: 450rpx;
  236. image {
  237. margin-top: 50rpx;
  238. width: 340rpx;
  239. height: 340rpx;
  240. }
  241. }
  242. .recognition_img_two {
  243. width: 450rpx;
  244. height: 450rpx;
  245. border-radius: 50%;
  246. margin: 0 auto;
  247. text-align: center;
  248. line-height: 450rpx;
  249. image {
  250. margin-top: 10rpx;
  251. width: 340rpx;
  252. height: 340rpx;
  253. }
  254. }
  255. .recognition_details {
  256. width: 400rpx;
  257. height: 80rpx;
  258. border-radius: 40rpx;
  259. margin: 30rpx auto;
  260. font-size: 30rpx;
  261. background-color: #67B25F;
  262. color: #FFFFFF;
  263. }
  264. }
  265. .datas {
  266. position: absolute;
  267. top: 284px;
  268. width: 95%;
  269. left: 2.5%;
  270. .datas_title {
  271. font-size: 32rpx;
  272. font-weight: 700;
  273. margin-left: 30rpx;
  274. }
  275. .recognition_details {
  276. width: 400rpx;
  277. height: 80rpx;
  278. border-radius: 40rpx;
  279. margin: 30rpx auto;
  280. font-size: 30rpx;
  281. background-color: #67B25F;
  282. color: #FFFFFF;
  283. }
  284. }
  285. .prevention {
  286. font-size: 28rpx;
  287. color: #919191;
  288. width: 95%;
  289. margin: 20rpx auto;
  290. .prevention_title {
  291. display: flex;
  292. p {
  293. margin-left: 20rpx;
  294. color: #000000;
  295. font-size: 28rpx;
  296. font-weight: 700;
  297. }
  298. }
  299. .prevention_con {
  300. padding-left: 6%;
  301. }
  302. }
  303. </style>