breakdown.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // pages/breakdown/breakdown.js
  2. var app = getApp();
  3. const util = require('../../utils/utils.js');
  4. var img = app.globalData.imgUrl;
  5. Page({
  6. data: {
  7. bikeError: [{
  8. id: 1,
  9. name: '刹车'
  10. }, {
  11. id: 2,
  12. name: '车胎'
  13. }, {
  14. id: 3,
  15. name: '链条'
  16. },
  17. {
  18. id: 4,
  19. name: '车座'
  20. },
  21. {
  22. id: 5,
  23. name: '二维码'
  24. },
  25. {
  26. id: 6,
  27. name: '其他'
  28. },
  29. ],
  30. cardTitle: 0,
  31. currentProblem: 0,
  32. count: 0, //已上传图片数
  33. arr: [],//图片列表
  34. currentNum: 0,//已输入的字数
  35. value: '',
  36. bike_no: '',//车辆编号
  37. problemName:'',//故障名称
  38. photos:[],
  39. },
  40. onLoad: function (options) {
  41. let index=this.data.currentProblem
  42. console.log(this.data.bikeError[index],'8989')
  43. let name=this.data.bikeError[index].name
  44. this.setData({
  45. problemName:name
  46. })
  47. },
  48. onReady: function () {
  49. },
  50. onShow: function () {
  51. },
  52. //扫描二维码获取车牌号
  53. saoma: function () {
  54. var that = this;
  55. my.scan({
  56. onlyFromCamera: true,
  57. success: function (res) {
  58. // console.log(res)
  59. var index = res.result.lastIndexOf("\=");
  60. var code = res.result.substring(index + 1, res.result.length);
  61. console.log(code.length)
  62. console.log(code)
  63. if (code.length == 9) {
  64. that.setData({
  65. bike_no: code
  66. })
  67. } else {
  68. my.showToast({
  69. content: '请扫描正确的车辆二维码!',
  70. icon: 'none'
  71. })
  72. }
  73. },
  74. fail: function (err) {
  75. my.showToast({
  76. content: '扫码失败~',
  77. icon: 'none'
  78. })
  79. }
  80. })
  81. },
  82. //选择故障类型
  83. selectProblem: function (e) {
  84. console.log(e)
  85. let index=e.currentTarget.dataset.index
  86. let name=this.data.bikeError[index].name
  87. this.setData({
  88. currentProblem: e.currentTarget.dataset.index,
  89. problemName:name
  90. })
  91. },
  92. //输入车辆编号
  93. inputBikeNo: function (e) {
  94. console.log(e, '--------------------')
  95. if (e.detail.value) {
  96. this.setData({
  97. cardTitle: 1,
  98. bike_no:e.detail.value
  99. })
  100. } else {
  101. this.setData({
  102. cardTitle: 0
  103. })
  104. }
  105. },
  106. //输入问题
  107. input: function (e) {
  108. if (e.detail.cursor == 240) {
  109. my.showToast({
  110. content: '最多只能输入240个字符',
  111. icon: 'none'
  112. })
  113. } else {
  114. console.log(e,'wenti')
  115. this.setData({
  116. value: e.detail.value,
  117. currentNum: e.detail.cursor,
  118. })
  119. }
  120. },
  121. //失去焦点时触发获取value
  122. blur: function (e) {
  123. this.setData({
  124. value: e.detail.value
  125. })
  126. },
  127. //点击小键盘上的完成时触发获取value
  128. firm: function (e) {
  129. this.setData({
  130. value: e.detail.value
  131. })
  132. },
  133. //拍照
  134. photo: function () {
  135. var that = this;
  136. var number = 3 - that.data.arr.length;
  137. console.log(number)
  138. if (number >= 0) {
  139. my.chooseImage({
  140. count: 1, // 默认9
  141. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  142. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  143. success: function (res) {
  144. console.log(res)
  145. let count = that.data.count + 1;
  146. that.setData({
  147. count
  148. })
  149. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  150. var tempFilePaths = res.tempFilePaths
  151. that.setData({
  152. arr: that.data.arr.concat(tempFilePaths)
  153. })
  154. that.uploadimg({
  155. url: app.globalData.url + '/upload/image',
  156. path: tempFilePaths
  157. });
  158. }
  159. })
  160. } else {
  161. my.showToast({
  162. title: '最多能上传4张图片',
  163. icon: 'none'
  164. })
  165. }
  166. },
  167. //上传图片
  168. uploadimg: function (data) {
  169. var that = this,
  170. i = data.i ? data.i : 0, //当前上传的哪张图片
  171. success = data.success ? data.success : 0, //上传成功的个数
  172. fail = data.fail ? data.fail : 0; //上传失败的个数
  173. my.uploadFile({
  174. url: data.url,
  175. filePath: data.path[i],
  176. header: {
  177. 'content-type': 'application/x-www-form-urlencoded',
  178. 'Authorization': my.getStorageSync({'key':'token'}).data,
  179. 'merchant-id': app.globalData.merchant_id
  180. },
  181. name: 'file', //这里根据自己的实际情况改
  182. formData: {
  183. type: 'trouble'
  184. }, //这里是上传图片时一起上传的数据
  185. success: (resp) => {
  186. success++; //图片上传成功,图片上传成功的变量+1
  187. console.log(resp)
  188. var data = JSON.parse(resp.data)
  189. var photo1 = [];
  190. if (data.length > 1) {
  191. for (var i = 0; i < data.length; i++) {
  192. photo1.push(data[i].id)
  193. }
  194. } else {
  195. photo1.push(data.id)
  196. }
  197. that.setData({
  198. photos: that.data.photos.concat(photo1)
  199. })
  200. //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
  201. },
  202. fail: (res) => {
  203. fail++; //图片上传失败,图片上传失败的变量+1
  204. console.log('fail:' + i + "fail:" + fail);
  205. },
  206. complete: () => {
  207. i++; //这个图片执行完上传后,开始上传下一张
  208. if (i == data.path.length) { //当图片传完时,停止调用
  209. //console.log('执行完毕');
  210. //console.log('成功:' + success + " 失败:" + fail);
  211. } else { //若图片还没有传完,则继续调用函数
  212. //console.log(i);
  213. data.i = i;
  214. data.success = success;
  215. data.fail = fail;
  216. that.uploadimg(data);
  217. }
  218. }
  219. });
  220. },
  221. //点击提交问题保修
  222. submitForm: util.throttle(function () {
  223. //点击提交
  224. // this.setData({show:true})
  225. var that = this;
  226. if (this.data.value == '') {
  227. my.alert({
  228. title: '提示',
  229. content: '问题描述不能为空'
  230. })
  231. return;
  232. } else if (this.data.value.length < 2) {
  233. my.alert({
  234. title: '提示',
  235. content: '请输入不少于2个字的描述',
  236. })
  237. return;
  238. } else if (that.data.problemName == '') {
  239. my.alert({
  240. title: '提示',
  241. content: '请选择故障部位',
  242. })
  243. return;
  244. }else if (that.data.bike_no.length != 9) {
  245. console.log(that.data.bike_no,'bike_No')
  246. my.showToast({
  247. content: '车牌号输入不正确',
  248. icon:'none'
  249. })
  250. return;
  251. } else {
  252. var tu = '';
  253. if (that.data.photos != '') {
  254. var photo = that.data.photos;
  255. var photo1 = [];
  256. for (var i = 0; i < photo.length; i++) {
  257. photo1.push(photo[i])
  258. }
  259. console.log(photo1)
  260. tu = photo1.toString();
  261. console.log(tu)
  262. }
  263. if (app.globalData.req) {
  264. var data = {
  265. bike_no: that.data.bike_no,
  266. area_id: my.getStorageSync({'key':'home'}).data.id,
  267. trouble_part: that.data.problemName,
  268. trouble_description: that.data.value,
  269. trouble_imgs: tu
  270. }
  271. console.log(data)
  272. app.request('/trouble', data, 'POST', app.globalData.req).then(res => {
  273. console.log(res)
  274. if (res.data.is_up_trouble == true) {
  275. my.showToast({
  276. content: '报修成功',
  277. success:{
  278. }
  279. })
  280. setTimeout(function(){
  281. my.reLaunch({
  282. url: '/pages/index/index',
  283. })
  284. },1500)
  285. }
  286. }).catch(err => {
  287. console.log(err)
  288. })
  289. } else {
  290. my.showToast({
  291. content: '您的操作过于频繁,请稍后再试~',
  292. icon: 'none'
  293. })
  294. }
  295. }
  296. }, 1000),
  297. //删除照片
  298. delete1: function (e) {
  299. var that = this;
  300. var index = e.currentTarget.dataset.index;
  301. var list = that.data.arr;
  302. var photos = that.data.photos
  303. console.log(photos)
  304. list.splice(index, 1);
  305. photos.splice(index, 1);
  306. that.setData({
  307. arr: list,
  308. photos,
  309. count:photos.length
  310. })
  311. },
  312. })