applyRefund.js 11 KB

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