repair.js 8.4 KB

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