request.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. const { host } = require('./env')
  2. /**
  3. * POST/GET/Put/Delete请求API
  4. */
  5. function POST(options) {
  6. return request({
  7. method: 'POST',
  8. ...options
  9. })
  10. }
  11. function GET(options) {
  12. return request({
  13. method: 'GET',
  14. ...options
  15. })
  16. }
  17. function PUT(options) {
  18. return request({
  19. method: 'PUT',
  20. ...options
  21. })
  22. }
  23. function DEL(options) {
  24. return request({
  25. method: 'DELETE',
  26. ...options
  27. })
  28. }
  29. /**
  30. * 请求API
  31. * @param {String} url 接口地址
  32. * @param {Object} params 请求的参数
  33. * @param {String} method 请求类型
  34. * @param {Object} page 来源页面对象
  35. * @param {boolean} successFun 回调函数是否自定义 默认false,实用默认成功回调
  36. * @param {boolean} failFun 回调函数是否自定义 默认false,实用默认成功回调
  37. * @param {Function} onStartFun 接口开始之前的回调函数
  38. * @param {Function} completeFun 接口调用结束的回调函数(调用成功、失败都会执行)
  39. * @param {String} contentType 请求header content-type的类型,只在无人酒店项目需要,其他项目可删除
  40. * @param {boolean} isToken 是否需要token
  41. * @param {String} isLoadingTxt 是否有加载动画
  42. * @param {String} successTxt 无成功回调函数,请求成功后的默认提示信息文字
  43. * @param {boolean} isSubmitting 表单提交的状态,表单页面需要设置为true
  44. */
  45. function request({
  46. url,
  47. params = {},
  48. method = 'GET',
  49. page,
  50. successFun = false,
  51. failFun = false,
  52. onStartFun,
  53. completeFun,
  54. contentType,
  55. isToken = true,
  56. isBearer = '',//token是否添加Bearer 前缀 Bearer 默认设置不加
  57. isLoadingTxt = '加载中...',
  58. successTxt = '',
  59. isSubmitting = false
  60. }) {
  61. //需要测试
  62. if (isSubmitting && page.data.isSubmitting){
  63. console.log('不可重复提交')
  64. return;
  65. }
  66. if (isSubmitting){
  67. page.setData({
  68. 'isSubmitting': true
  69. })
  70. }
  71. if (onStartFun) {
  72. onStartFun(); //request start
  73. } else {
  74. if (method == 'PUT' || method == 'POST' || method == 'GET') {
  75. if (isLoadingTxt) {
  76. wx.showLoading({
  77. title: isLoadingTxt,
  78. mask: true
  79. })
  80. }
  81. } else if (method == 'DELETE') {
  82. wx.showLoading({
  83. title: '正在删除...',
  84. mask: true
  85. })
  86. }
  87. }
  88. let myHeader = {
  89. 'content-type': contentType ? contentType : 'application/json',
  90. 'Token': wx.getStorageSync('Authorization') ? wx.getStorageSync('Authorization') : ''
  91. };
  92. //通过includes方法查找字符串中是否包含指定内容,进而判断是否要添加token
  93. if (!isToken) {
  94. delete myHeader.Token
  95. }
  96. return new Promise((resolve, reject) => {
  97. wx.request({
  98. url: host.BASE_URL + url,
  99. method: method,
  100. data: params,
  101. header: myHeader,
  102. success: function (res) {
  103. if (isLoadingTxt) wx.hideLoading();
  104. if (res.data.code == '0' || res.data.code == '200' || res.data.code == '0000') {
  105. if (successFun) {
  106. resolve(res)
  107. } else {
  108. if(!successTxt){
  109. switch (method) {
  110. case 'PUT':
  111. return successTxt = '修改成功'
  112. break;
  113. case 'POST':
  114. return successTxt = '新增成功'
  115. break;
  116. case 'DELETE':
  117. return successTxt = '删除成功'
  118. break;
  119. case 'GET':
  120. return successTxt = '查询成功'
  121. break;
  122. }
  123. }
  124. wx.showToast({
  125. title: successTxt,
  126. icon: 'success',
  127. duration: 2000,
  128. mask: true
  129. })
  130. }
  131. } else if (res.data.code == '401') {
  132. if (failFun) {
  133. reject(res)
  134. } else {
  135. wx.clearStorageSync()
  136. page.setData({
  137. loginStatus: ''
  138. })
  139. page.onShow()
  140. wx.showToast({
  141. title: '登录已过期',
  142. icon: 'none',
  143. duration: 2000,
  144. mask: true,
  145. success: function () {
  146. setTimeout(function () {
  147. wx.navigateTo({
  148. url: '/pages/login/index',
  149. })
  150. }, 2000)
  151. }
  152. })
  153. }
  154. } else {
  155. if (failFun) {
  156. reject(res)
  157. } else {
  158. wx.showToast({
  159. title: res.data.msg ? res.data.msg : res.data.message ? res.data.message : '请求出错了,稍后再试!',
  160. icon: 'none',
  161. duration: 2000,
  162. mask: true
  163. })
  164. }
  165. }
  166. //部分地磅会返回报错会返回html页面
  167. let getDataStr = res.data
  168. if(typeof getDataStr === 'string' && getDataStr.indexOf('DOCTYPE html'==-1)){
  169. wx.clearStorageSync()
  170. sourceObj.setData({
  171. loginStatus: ''
  172. })
  173. sourceObj.onShow()
  174. wx.showToast({
  175. title: '登录已过期',
  176. icon: 'none',
  177. duration: 2000,
  178. mask: true,
  179. success:function(){
  180. setTimeout(function(){
  181. // wx.switchTab({
  182. // url: '/pages/index/index',
  183. // })
  184. },2000)
  185. }
  186. })
  187. }
  188. },
  189. fail: function (res) {
  190. wx.clearStorageSync()
  191. page.onShow()
  192. if (isLoadingTxt) wx.hideLoading();
  193. if (failFun) {
  194. reject(res)
  195. } else {
  196. let content = '糟糕!网络奔溃了!刷新试试'
  197. if (res.errMsg === 'request:fail timeout') {
  198. content = '请求超时!再试一次'
  199. }
  200. if (page && page.data.showModal != true) {
  201. page.setData({
  202. showModal: true
  203. })
  204. wx.showModal({
  205. title: '提示!',
  206. content: content,
  207. showCancel: false,
  208. mask: true,
  209. success: function (res) {
  210. if (res.confirm) {
  211. page.setData({
  212. showModal: false
  213. })
  214. page.onLoad();
  215. page.onShow();
  216. } else if (res.cancel) {
  217. page.setData({
  218. showModal: false
  219. })
  220. }
  221. }
  222. })
  223. }
  224. }
  225. },
  226. complete: function (res) {
  227. if (isSubmitting) page.setData({
  228. 'isSubmitting': false
  229. })
  230. if (completeFun) {
  231. typeof completeFun == 'function' && completeFun(res, page);
  232. }
  233. }
  234. })
  235. })
  236. }
  237. /**
  238. * GET请求数据列表API 初始化加载第一页数据
  239. * @param {String} url 接口地址
  240. * @param {Object} data 请求的参数
  241. * @param {Object} page 来源对象this
  242. * @param {boolean} loadType 加载类型(滑动加载【true】&初始化加载【false】)
  243. * @param {Array} Array 数据存放位置
  244. * @param {String} pageSize 每页数量字段名
  245. * @param {String} pageNo 设置当前页码字段名
  246. * @param {String} totalPage 设置总页数字段名
  247. * @param {String} more 设置加载动画状态字段名
  248. * @param {String} nomore 设置加载状态显示文字字段名
  249. * @param {boolean} successFun 接口调用成功返回的回调函数,可用于处理数据,格式化数据
  250. * @param {boolean} failFun 接口调用失败的回调函数
  251. * @param {Function} completeFun 接口调用结束的回调函数(调用成功、失败都会执行)
  252. * @param {String} requestStatu 请求状态,为空时表示加载完成,为加载中等字样时表示处于加载中
  253. * @param {String} isLoadingTxt 请求时的加载文字提示
  254. */
  255. function pageFirst({
  256. url,
  257. params,
  258. page,
  259. requireType = 'GET',
  260. loadType = false,
  261. Array = 'resData',
  262. pageSize = 'limit',
  263. pageNo = 'page',
  264. totalPages = 'totalPages',
  265. more = 'more',
  266. nomore = 'nomore',
  267. requestStatu = 'requestStatu',
  268. isLoadingTxt = '加载中...',
  269. successFun = false,
  270. failFun = false,
  271. isToken = true,
  272. isBearer = '',//token是否添加Bearer 前缀 Bearer 默认设置不加
  273. completeFun
  274. }) {
  275. wx.showNavigationBarLoading();
  276. if (page.data[requestStatu]) {
  277. wx.showLoading({
  278. title: isLoadingTxt,
  279. mask: true
  280. })
  281. }
  282. let myHeader = {
  283. //'content-type': contentType ? contentType : 'application/json',
  284. 'Token': wx.getStorageSync('Authorization') ? wx.getStorageSync('Authorization') : ''
  285. };
  286. //通过includes方法查找字符串中是否包含指定内容,进而判断是否要添加token
  287. if (!isToken) {
  288. delete myHeader.Token
  289. }
  290. return new Promise((resolve, reject) => {
  291. wx.request({
  292. url: host.BASE_URL + url,
  293. method: requireType,
  294. data: params,
  295. header: myHeader,
  296. success: function (res) {
  297. if (page.data[requestStatu]) {
  298. wx.hideLoading()
  299. }
  300. if (res.statusCode == '200') {
  301. if (res.data.code == '0000' || res.data.code == '20' || res.data.code == '0' || res.data.code == '200') {
  302. page.setData({
  303. showModal: false
  304. })
  305. var data = [];
  306. if (successFun) {
  307. data = resolve(res);
  308. //console.log('Data:', data); // 处理成功返回的数据
  309. } else {
  310. data = res.data.data.records;
  311. }
  312. if (loadType) { //滑动加载时
  313. page.setData({
  314. [Array]: page.data[Array].concat(data),
  315. [more]: true
  316. })
  317. } else { //初始化加载
  318. page.setData({
  319. [Array]: data,
  320. //requestStatu: ''
  321. })
  322. if (!(typeof successFun == 'function')) {
  323. page.setData({
  324. [requestStatu]: ''
  325. })
  326. }
  327. if (totalPages) {
  328. page.setData({
  329. [totalPages]: Math.ceil(res.data.data.total / page.data[pageSize])
  330. })
  331. }
  332. }
  333. if(url=='/biz/record/page'&&page.data.activeIndex=='1'){
  334. page.setData({
  335. sendRecordNum:res.data.data.total
  336. })
  337. }
  338. //当结果只有一页时,并且小于5条时
  339. var getTotalRows = 0;
  340. getTotalRows = res.data.data.total;
  341. if (getTotalRows < page.data[pageSize] && getTotalRows > 3) {
  342. page.setData({
  343. [nomore]: '没有更多数据了!',
  344. [more]: false
  345. })
  346. } else if (getTotalRows < 3) {
  347. page.setData({
  348. [nomore]: '',
  349. [more]: false
  350. })
  351. } else {
  352. page.setData({
  353. [nomore]: ''
  354. })
  355. }
  356. } else if (res.data.code == '401') {
  357. wx.clearStorageSync()
  358. page.setData({
  359. loginStatus: ''
  360. })
  361. page.onShow()
  362. wx.showToast({
  363. title: '登录已过期',
  364. icon: 'none',
  365. duration: 2000,
  366. mask: true,
  367. success: function () {
  368. setTimeout(function () {
  369. wx.navigateTo({
  370. url: '/pages/login/index',
  371. })
  372. }, 2000)
  373. }
  374. })
  375. if (!loadType) {
  376. wx.hideLoading()
  377. }
  378. } else {
  379. wx.clearStorageSync()
  380. page.onShow()
  381. if (failFun) {
  382. reject(res)
  383. } else {
  384. wx.showToast({
  385. title: res.data.msg ? res.data.msg : res.data.message ? res.data.message : '出错了',
  386. icon: 'none',
  387. duration: 2000,
  388. mask: true
  389. })
  390. }
  391. if (!loadType) {
  392. wx.hideLoading()
  393. }
  394. }
  395. //部分地磅会返回报错会返回html页面
  396. let getDataStr = res.data
  397. if(typeof getDataStr === 'string' && getDataStr.indexOf('DOCTYPE html'==-1)){
  398. wx.clearStorageSync()
  399. sourceObj.setData({
  400. loginStatus: ''
  401. })
  402. sourceObj.onShow()
  403. wx.showToast({
  404. title: '登录已过期',
  405. icon: 'none',
  406. duration: 2000,
  407. mask: true,
  408. success:function(){
  409. setTimeout(function(){
  410. // wx.switchTab({
  411. // url: '/pages/index/index',
  412. // })
  413. },2000)
  414. }
  415. })
  416. }
  417. } else if (res.statusCode == '302' || res.statusCode == '400' || res.statusCode == '404') {
  418. wx.clearStorageSync()
  419. page.setData({
  420. loginStatus: ''
  421. })
  422. page.onShow()
  423. wx.showToast({
  424. title: '登录已过期',
  425. icon: 'none',
  426. duration: 2000,
  427. mask: true,
  428. success: function () {
  429. setTimeout(function () {
  430. // wx.switchTab({
  431. // url: '/pages/index/index',
  432. // })
  433. wx.navigateTo({
  434. url: '/pages/login/index',
  435. })
  436. }, 2000)
  437. }
  438. })
  439. if (!loadType) {
  440. wx.hideLoading()
  441. }
  442. } else {
  443. if (failFun) {
  444. reject(res)
  445. } else {
  446. wx.showToast({
  447. title: res.data.msg ? res.data.msg : res.data.message ? res.data.message : '出错了,请稍后再试',
  448. icon: 'none',
  449. duration: 2000,
  450. mask: true
  451. })
  452. if (page.onShow) {
  453. page.onShow()
  454. }
  455. }
  456. }
  457. },
  458. fail: function (res) {
  459. wx.clearStorageSync()
  460. page.onShow()
  461. if (page.data[requestStatu]) {
  462. wx.hideLoading()
  463. }
  464. if (failFun) {
  465. reject(res)
  466. } else {
  467. let content = '糟糕!网络奔溃了!刷新试试'
  468. if (res.errMsg === 'request:fail timeout') {
  469. content = '请求超时!再试一次'
  470. }
  471. if (page.data.showModal != true) {
  472. page.setData({
  473. showModal: true
  474. })
  475. wx.showModal({
  476. title: '提示!',
  477. content: content,
  478. showCancel: false,
  479. mask: true,
  480. success: function (res) {
  481. if (res.confirm) {
  482. page.setData({
  483. showModal: false
  484. })
  485. //page.onLoad();
  486. } else if (res.cancel) {
  487. page.setData({
  488. showModal: false
  489. })
  490. }
  491. }
  492. })
  493. }
  494. }
  495. },
  496. complete: function (res) {
  497. wx.hideNavigationBarLoading();
  498. // if(!loadType){wx.hideLoading()}
  499. if (completeFun) {
  500. typeof completeFun == 'function' && completeFun(res, page);
  501. }
  502. wx.stopPullDownRefresh();
  503. if (loadType) { //数据加载完成后隐藏加载动画
  504. page.setData({
  505. [more]: false
  506. })
  507. }
  508. }
  509. })
  510. })
  511. }
  512. /**
  513. * GET滑动请求数据列表API 触底加载
  514. * @param {Object} page 来源对象this
  515. * @param {String} pageNo 设置当前页码字段名
  516. * @param {String} totalPage 设置总页数字段名
  517. * @param {String} more 设置加载动画状态
  518. * @param {boolean} totalPages 设置总页数字段
  519. */
  520. function pageOther(options) {
  521. let {
  522. page,
  523. pageNo = 'page',
  524. totalPages = 'totalPages',
  525. more = 'more',
  526. nomore = 'nomore'
  527. } = options
  528. if (page.data[pageNo] > page.data[totalPages]) { //当前页大于总页数
  529. page.setData({
  530. [more]: false,
  531. [nomore]: '没有更多数据了!'
  532. })
  533. return;
  534. } else {
  535. if (page.data[pageNo] >= page.data[totalPages]) {
  536. page.setData({
  537. [nomore]: '没有更多数据了!'
  538. })
  539. }
  540. page.setData({
  541. [more]: true
  542. })
  543. return pageFirst({
  544. ...options
  545. });
  546. }
  547. }
  548. /**
  549. * GET请求数据列表API 不带分页的请求全部数据
  550. * @param {String} url 接口地址
  551. * @param {Object} data 请求的参数
  552. * @param {Object} header 请求的头
  553. * @param {Object} page 来源对象this
  554. * @param {String} Array 数据存放位置
  555. * @param {Boolean} successFun 接口调用成功返回的回调函数
  556. * @param {Boolean} failFun 接口调用失败的回调函数
  557. * @param {Function} completeFun 接口调用结束的回调函数(调用成功、失败都会执行)
  558. */
  559. function pageAll({
  560. url,
  561. data,
  562. page,
  563. Array,
  564. requestStatu,
  565. successFun,
  566. failFun,
  567. completeFun
  568. }) {
  569. if (page.data[requestStatu]) {
  570. wx.showLoading({
  571. title: '加载中...',
  572. mask: true
  573. })
  574. }
  575. return new Promise((resolve, reject) => {
  576. wx.request({
  577. url: host.BASE_URL + url,
  578. method: 'GET',
  579. data: data,
  580. header: {
  581. //'content-type': 'application/json', // 默认值
  582. 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  583. 'Token': wx.getStorageSync('Authorization') ? wx.getStorageSync('Authorization') : ''
  584. },
  585. success: function (res) {
  586. if (page.data[requestStatu]) wx.hideLoading();
  587. if (res.statusCode == '200') {
  588. if (res.data.code == '0' || res.data.code == '200' || res.data.code == '0000') {
  589. page.setData({
  590. showModal: false
  591. })
  592. var data = [];
  593. if (successFun) {
  594. data = resolve(res);
  595. } else {
  596. data = res.data.data.records;
  597. }
  598. page.setData({
  599. [Array]: data,
  600. more: false,
  601. nomore: '没有更多数据了!'
  602. })
  603. if (!(typeof successFun == 'function')) {
  604. page.setData({
  605. [requestStatu]: ''
  606. })
  607. }
  608. } else if (res.data.code == '401') {
  609. if (failFun) {
  610. reject(res)
  611. } else {
  612. wx.clearStorageSync()
  613. page.setData({
  614. loginStatus: ''
  615. })
  616. page.onShow()
  617. wx.showToast({
  618. title: '登录已过期',
  619. icon: 'none',
  620. duration: 2000,
  621. mask: true,
  622. success: function () {
  623. setTimeout(function () {
  624. // wx.switchTab({
  625. // url: '/pages/index/index',
  626. // })
  627. wx.navigateTo({
  628. url: '/pages/login/index',
  629. })
  630. }, 2000)
  631. }
  632. })
  633. }
  634. } else {
  635. if (failFun) {
  636. reject(res)
  637. } else {
  638. wx.showToast({
  639. title: res.data.msg ? res.data.msg : res.data.message ? res.data.message : '请求出错了,稍后再试!',
  640. icon: 'none',
  641. duration: 2000,
  642. mask: true
  643. })
  644. }
  645. }
  646. } else {
  647. if (failFun) {
  648. reject(res)
  649. } else {
  650. wx.showToast({
  651. title: res.data.msg ? res.data.msg : res.data.message ? res.data.message : '出错了,请稍后再试',
  652. icon: 'none',
  653. duration: 2000,
  654. mask: true
  655. })
  656. if (page.onShow) {
  657. page.onShow()
  658. }
  659. }
  660. }
  661. },
  662. fail: function (res) {
  663. if (page.data[requestStatu]) wx.hideLoading();
  664. if (failFun) {
  665. reject(res)
  666. } else {
  667. let content = '糟糕!网络奔溃了!刷新试试'
  668. if (res.errMsg === 'request:fail timeout') {
  669. content = '请求超时!再试一次'
  670. }
  671. if (page.data.showModal != true) {
  672. page.setData({
  673. showModal: true
  674. })
  675. wx.showModal({
  676. title: '提示!',
  677. content: content,
  678. showCancel: false,
  679. mask: true,
  680. success: function (res) {
  681. if (res.confirm) {
  682. page.setData({
  683. showModal: false
  684. })
  685. page.onLoad();
  686. } else if (res.cancel) {
  687. page.setData({
  688. showModal: false
  689. })
  690. }
  691. }
  692. })
  693. }
  694. }
  695. },
  696. complete: function (res) {
  697. if (completeFun) {
  698. typeof completeFun == 'function' && completeFun(res, page);
  699. }
  700. }
  701. })
  702. })
  703. }
  704. /**
  705. * 单张图片上传
  706. * seturl 设置URL
  707. * setfiled 字段名
  708. * data 设置data数据
  709. * refresh 上传完成后是否刷新
  710. * 最终返回res
  711. */
  712. function uploadPhoto(that, seturl, setfiled, data, refresh) {
  713. wx.chooseImage({
  714. count: 1, // 默认9
  715. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  716. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  717. success: function (res) {
  718. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  719. wx.uploadFile({
  720. url: seturl,
  721. filePath: res.tempFilePaths[0],
  722. name: setfiled,
  723. dataType: 'json',
  724. header: {
  725. 'content-type': 'application/json', // 默认值
  726. 'Token': wx.getStorageSync('Authorization') ? wx.getStorageSync('Authorization') : ''
  727. },
  728. formData: data,
  729. success: function (res) {
  730. wx.hideLoading();
  731. var data = res.data
  732. if (refresh) {
  733. that.onLoad();
  734. }
  735. },
  736. complete: function (res) {
  737. // app.globalData.loginCheck(e.data.code, e.data.desc);
  738. return res
  739. }
  740. })
  741. }
  742. })
  743. }
  744. /**
  745. * 多图片上传
  746. * seturl 设置URL
  747. * setfiled 字段名
  748. * data 设置data数据
  749. */
  750. function uploadMorePhoto(seturl, that, setfiled, data, successFun) {
  751. wx.chooseImage({
  752. count: 9, // 默认9
  753. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  754. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  755. success: function (res) {
  756. var successUp = 0; //成功个数
  757. var failUp = 0; //失败个数
  758. var length = res.tempFilePaths.length; //总共个数
  759. var i = 0; //第几个
  760. /**
  761. * seturl 设置URL
  762. * res.tempFilePaths 所有土图片路径
  763. * setfiled 字段名
  764. * data 设置data数据
  765. * successUp 上传成功的的数量
  766. * failUp 上传失败的数量
  767. * i 当前的索引
  768. * length 上传的总数
  769. */
  770. uploadDIY(seturl, that, res.tempFilePaths, setfiled, data, successUp, failUp, i, length, successFun);
  771. }
  772. })
  773. }
  774. /**
  775. * vant组件的图片上传
  776. * @param {*} url
  777. * @param {*} page
  778. * @param {*} filePaths
  779. * @param {*} setfiled
  780. * @param {*} params
  781. * @param {*} successUp
  782. * @param {*} failUp
  783. * @param {*} i
  784. * @param {*} length
  785. * @param {*} successFun //是否重新自定义回调函数
  786. */
  787. function uploadDIY({
  788. url,
  789. page,
  790. filePaths,
  791. setfiled = "file",
  792. params = {},
  793. successUp = 0,
  794. failUp = 0,
  795. i = 0,
  796. length
  797. }) {
  798. return new Promise((resolve, reject) => {
  799. wx.uploadFile({
  800. url: host.BASE_URL + url,
  801. filePath: filePaths,
  802. name: setfiled,
  803. formData: params,
  804. header: {
  805. 'content-type': 'application/json', // 默认值
  806. 'Token': wx.getStorageSync('Authorization') ? wx.getStorageSync('Authorization') : ''
  807. },
  808. success: (res) => {
  809. successUp++;
  810. if (i == length) {
  811. wx.showToast({
  812. title: '上传成功!',
  813. icon: 'success',
  814. duration: 2000
  815. })
  816. }
  817. resolve(res)
  818. },
  819. fail: (err) => {
  820. failUp++;
  821. reject(err)
  822. },
  823. complete: (res) => {
  824. i++;
  825. if (i == length) {
  826. //console.log('总共' + i +'张' + successUp + '张上传成功,' + failUp + '张上传失败!');
  827. } else { //递归调用uploadDIY函数
  828. //uploadDIY(url, page, filePaths, setfiled, data, successUp, failUp, i, length, successFun);
  829. }
  830. },
  831. });
  832. })
  833. }
  834. module.exports = {
  835. POST,
  836. GET,
  837. PUT,
  838. DEL,
  839. pageFirst,
  840. pageOther,
  841. pageAll,
  842. uploadPhoto, //单图片上传
  843. uploadMorePhoto, //多图片上传
  844. uploadDIY
  845. }