repair.js 8.3 KB

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