applyRefund.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // pages/applyRefund/applyRefund.js
  2. var http = require('../../utils/http.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. applyType: 1, //退款方式(1:仅退款 2退款退货)
  9. refundItem: {}, //订单项数据
  10. show:false,
  11. isAll:false,
  12. photoFiles: [], //凭证图片列表
  13. buyerDesc: '', //备注说明
  14. loading:false,
  15. goodList:[],
  16. reasonList:[
  17. {name:'暂不需要商品(买错/多买/漏买)'},
  18. {name:'冰品融化'},
  19. {name:'订单中有商品发错'},
  20. {name:'商品斤两不足'},
  21. {name:'商品临期到期'},
  22. {name:'商品破损/包装破损'},
  23. {name:'商品质量问题'},
  24. {name:'实物与图文描述不符'},
  25. {name:'商家通知我卖完了'},
  26. {name:'所有商品未收到'},
  27. {name:'整个订单送错'},
  28. {name:'订单少选且商家未通知我'},
  29. {name:'未在约定时间送达'},
  30. ],
  31. buyerReason: '', //退款原因
  32. total:0,
  33. refundId:0,//再次申请售后id
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad(options) {
  39. let refundId = options.refundId || 0
  40. var refundItem
  41. if(refundId){//再次申请
  42. this.loadOrderDetail(options.orderNumber,refundId)
  43. }else{
  44. refundItem = wx.getStorageSync("refundItem");
  45. console.log('拿到缓存里的订单项数据refundItem:',refundItem)
  46. let orderItemDtos = []
  47. refundItem.orderItemDtos.forEach((item1,index)=>{
  48. let item = item1
  49. if(item.prodCount-item.refundCount){//剩余商品数量大于0
  50. item.num = item.prodCount-item.refundCount
  51. item.checked = true
  52. orderItemDtos.push(item)
  53. }
  54. })
  55. refundItem.orderItemDtos = orderItemDtos
  56. this.setData({
  57. refundId,
  58. refundItem: refundItem,
  59. orderNumber: refundItem.orderNumber,
  60. goodsNum: refundItem.prodCount,
  61. actualTotal: refundItem.actualTotal, //总额
  62. isLastProd: refundItem.isLastProd, //是否最后一样商品
  63. addTransfee: refundItem.addTransfee, //只有一件商品可以退运费
  64. orderScore: refundItem.useScore // 单个退积分
  65. })
  66. this.totalPrice()
  67. this.checkAll()
  68. }
  69. },
  70. /**
  71. * 评价图片预览
  72. */
  73. picPreView(e){
  74. var index = e.currentTarget.dataset.index
  75. var urls = []
  76. this.data.photoFiles.forEach(el => {
  77. urls.push(el.url)
  78. })
  79. wx.previewImage({
  80. current: urls[index],
  81. urls: urls
  82. })
  83. },
  84. // 实时监听输入
  85. onInput: function(e) {
  86. // e.detail.value 即为当前输入的内容
  87. this.setData({
  88. buyerDesc: e.detail.value
  89. });
  90. },
  91. /**
  92. * 加载订单数据
  93. */
  94. loadOrderDetail: function(orderNumber,refundId) {
  95. var ths = this;
  96. wx.showLoading();
  97. //加载订单详情
  98. var params = {
  99. url: "/p/myOrder/orderDetail",
  100. method: "GET",
  101. data: {
  102. orderNumber
  103. },
  104. callBack: function(res) {
  105. let orderItemDtos = []
  106. res.orderItemDtos.forEach((item1,index)=>{
  107. let item = item1
  108. if(item.prodCount-item.refundCount){//剩余商品数量大于0
  109. item.num = item.prodCount-item.refundCount
  110. item.checked = true
  111. orderItemDtos.push(item)
  112. }
  113. })
  114. res.orderItemDtos = orderItemDtos
  115. wx.hideLoading();
  116. ths.setData({
  117. refundId,
  118. refundItem: res,
  119. orderNumber,
  120. goodsNum: res.prodCount,
  121. actualTotal: res.actualTotal, //总额
  122. isLastProd: res.isLastProd, //是否最后一样商品
  123. addTransfee: res.addTransfee, //只有一件商品可以退运费
  124. orderScore: res.useScore // 单个退积分
  125. })
  126. ths.totalPrice()
  127. ths.checkAll()
  128. }
  129. };
  130. http.request(params);
  131. },
  132. /**
  133. * 提交退款
  134. */
  135. apply(){
  136. console.log(this.data.refundItem.orderItemDtos,this.data.refundItem.orderItemDtos.some(i=>i.checked));
  137. let that = this
  138. // 检查数据完整性
  139. if (this.data.buyerReason === '') {
  140. return wx.showToast({
  141. icon: 'none',
  142. title: '请选择退款原因',
  143. })
  144. }else if(!this.data.refundItem.orderItemDtos.some(i=>i.checked)){
  145. return wx.showToast({
  146. icon: 'none',
  147. title: '请选择退款商品',
  148. })
  149. }else if(this.loading){
  150. return
  151. }
  152. this.loading = true
  153. wx.showLoading();
  154. var pics = '';
  155. this.data.photoFiles.forEach(function (item) {
  156. pics += item.path + ',';
  157. });
  158. if (pics != '') {
  159. pics = pics.substring(0, pics.length - 1)
  160. }
  161. let goodsNum = 0
  162. let refundAmount = this.data.total
  163. let orderRefundSkuList = []
  164. this.data.refundItem.orderItemDtos.forEach(item=>{
  165. if(item.checked){
  166. orderRefundSkuList.push({
  167. "productCount": item.num,
  168. "skuId": item.skuId,
  169. "skuPrice": item.price,
  170. "orderItemId": item.orderItemId,
  171. })
  172. goodsNum+=item.num
  173. }
  174. })
  175. if(this.data.refundItem.actualTotal == 0){//积分支付 不用管退款金额
  176. refundAmount = 0
  177. }else if(refundAmount>=this.data.refundItem.actualTotal){//支付了钱的订单 并且退款钱超过实际支付钱 传实际支付金额
  178. refundAmount = this.data.refundItem.actualTotal
  179. }
  180. let data = {
  181. orderNumber: this.data.orderNumber, //订单编号
  182. applyType: this.data.applyType, //退款方式(1:仅退款 2退款退货)
  183. isReceiver: this.data.applyType == 1?0:1, //货物状态(1:已收到货 0:未收到货)
  184. buyerReason: this.data.reasonList[this.data.buyerReason].name, //退款原因
  185. goodsNum, //退款数量(0或不传值则为全部数量)
  186. refundAmount: refundAmount, //退款金额
  187. freightAmount:this.data.refundItem.transfee,
  188. buyerMobile: this.data.refundItem.userAddrDto.mobile, //手机号码(默认当前订单手机号码)
  189. buyerDesc: this.data.buyerDesc, //备注说明
  190. photoFiles: pics, //凭证图片列表
  191. refundType: goodsNum == this.data.refundItem.totalNum ?1:2, //退款单类型(1:整单退款,2:单个物品退款)
  192. orderRefundSkuList: orderRefundSkuList
  193. }
  194. if(this.data.refundId){//再次申请
  195. data.refundId = this.data.refundId
  196. console.log('再次申请');
  197. }else{
  198. console.log('申请');
  199. }
  200. var params = {
  201. url:this.data.refundId?"/p/orderRefund/applyAgain": "/p/orderRefund/apply",
  202. method: "POST",
  203. data,
  204. callBack: (res) => {
  205. that.loading = false
  206. wx.hideLoading();
  207. if(res.code == 500){
  208. return wx.showToast({
  209. title: res.msg,
  210. icon:'none'
  211. })
  212. }
  213. // 去到我的退款订单页面
  214. wx.redirectTo({
  215. url: '/pages/afterSales/afterSales',
  216. })
  217. }
  218. };
  219. http.request(params);
  220. },
  221. /**
  222. * 打开选择原因弹窗
  223. */
  224. choose(){
  225. this.setData({
  226. show:true
  227. })
  228. },
  229. close(){
  230. this.setData({
  231. show:false
  232. })
  233. },
  234. /**
  235. * 选择原因
  236. */
  237. chooseReason(e){
  238. console.log(e);
  239. this.setData({
  240. buyerReason:Number(e.detail.value)
  241. })
  242. },
  243. /**
  244. * 确认原因
  245. */
  246. submitReason(){
  247. this.setData({
  248. show:false
  249. })
  250. },
  251. /** */
  252. delImg(e){
  253. let index = e.currentTarget.dataset.index
  254. let photoFiles = this.data.photoFiles
  255. photoFiles.splice(index,1)
  256. this.setData({
  257. photoFiles: photoFiles
  258. })
  259. },
  260. /**
  261. * 上传图片
  262. */
  263. getUploadImg: function(e) {
  264. console.log('上传图片开始');
  265. if(this.data.photoFiles.length == 5){
  266. return wx.showToast({
  267. title: '最多可上传5张图片',
  268. })
  269. }
  270. var ths = this;
  271. wx.chooseMedia({
  272. count: 1, // 默认9
  273. mediaType: ['image'],
  274. sourceType: ['album', 'camera'],
  275. maxDuration: 30,
  276. success: function(res) {
  277. console.log('选择图片完成:',res);
  278. // 图片的本地临时文件路径列表
  279. var tempFilePaths = res.tempFiles[0].tempFilePath;
  280. wx.showLoading({
  281. mask: true
  282. })
  283. var params = {
  284. url: "/p/file/upload",
  285. filePath: tempFilePaths,
  286. name: 'file',
  287. callBack: function(res2) {
  288. console.log('上传接口:',res2);
  289. wx.hideLoading();
  290. var img = {};
  291. img.path = JSON.parse(res2).filePath;
  292. img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
  293. var photoFiles = ths.data.photoFiles;
  294. photoFiles.push(img);
  295. ths.setData({
  296. photoFiles: photoFiles
  297. })
  298. }
  299. };
  300. http.upload(params);
  301. }
  302. })
  303. },
  304. /**
  305. * 选择类型
  306. */
  307. radioChange(e){
  308. console.log(e);
  309. this.setData({
  310. applyType:e.detail.value
  311. })
  312. },
  313. /**
  314. * 全选
  315. */
  316. onSelectedAll(e){
  317. console.log(e,this.data);
  318. let isAll = !this.data.isAll
  319. let refundItem = this.data.refundItem
  320. refundItem.orderItemDtos.forEach(item=>{
  321. item.checked = isAll
  322. })
  323. this.setData({
  324. isAll,
  325. refundItem
  326. })
  327. this.totalPrice()
  328. },
  329. /**
  330. * 单选
  331. */
  332. onSelectedItem(e){
  333. let index = e.currentTarget.dataset.index
  334. let refundItem = this.data.refundItem
  335. refundItem.orderItemDtos[index].checked = !refundItem.orderItemDtos[index].checked
  336. this.setData({
  337. refundItem
  338. })
  339. this.checkAll()
  340. this.totalPrice()
  341. },
  342. /**
  343. * 检查全选状态
  344. */
  345. checkAll(){
  346. let isAll = this.data.refundItem.orderItemDtos.every(i=>i.checked)
  347. this.setData({
  348. isAll
  349. })
  350. },
  351. /**
  352. * 计算总价
  353. */
  354. totalPrice(){
  355. let total = 0
  356. let num = 0
  357. this.data.refundItem.orderItemDtos.forEach(item=>{
  358. if(item.checked){
  359. total += Math.round(item.price*100)*item.num
  360. num+=item.num
  361. }
  362. })
  363. if(num == this.data.refundItem.totalNum){//全部商品退款 +运费
  364. total += Math.round(this.data.refundItem.transfee* 100)
  365. }
  366. this.setData({
  367. total:total/100
  368. })
  369. },
  370. /**
  371. * 操作数量
  372. */
  373. changeNum(e){
  374. let index = e.currentTarget.dataset.index
  375. let num = e.currentTarget.dataset.num
  376. let refundItem = this.data.refundItem
  377. if(num == -1&&refundItem.orderItemDtos[index].num == 1){
  378. return wx.showToast({
  379. title: '数量不能小于1',
  380. duration: 1200,
  381. icon: 'none',
  382. })
  383. }else if(num == 1&&refundItem.orderItemDtos[index].num == (refundItem.orderItemDtos[index].prodCount-refundItem.orderItemDtos[index].refundCount)){
  384. return wx.showToast({
  385. title: '不能超过购买数量',
  386. duration: 1200,
  387. icon: 'none',
  388. })
  389. }
  390. refundItem.orderItemDtos[index].num +=num
  391. this.setData({
  392. refundItem
  393. })
  394. this.totalPrice()
  395. },
  396. /**
  397. * 生命周期函数--监听页面初次渲染完成
  398. */
  399. onReady() {
  400. },
  401. /**
  402. * 生命周期函数--监听页面显示
  403. */
  404. onShow() {
  405. },
  406. /**
  407. * 生命周期函数--监听页面隐藏
  408. */
  409. onHide() {
  410. },
  411. /**
  412. * 生命周期函数--监听页面卸载
  413. */
  414. onUnload() {
  415. },
  416. /**
  417. * 页面相关事件处理函数--监听用户下拉动作
  418. */
  419. onPullDownRefresh() {
  420. },
  421. /**
  422. * 页面上拉触底事件的处理函数
  423. */
  424. onReachBottom() {
  425. },
  426. /**
  427. * 用户点击右上角分享
  428. */
  429. onShareAppMessage() {
  430. }
  431. })