refund.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // pages/order/refund.js
  2. var app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. xarray: ['点击选择退款理由', '商品有质量问题', '没有收到货', '商品少发漏发发错', '商品与描述不一致', '收到商品时有划痕或破损', '质疑假货', '其他'],
  9. index: 0,
  10. refund_type: 1,
  11. refund_imgs: [],
  12. complaint_mobile: '',
  13. refund_thumb_imgs: [],
  14. complaint_desc: '',
  15. order_id: 0,
  16. order_status_id: -1,
  17. complaint_name: '',
  18. ref_id: 0,
  19. complaint_money: 0,
  20. refund_money: 0,
  21. selArr: [],
  22. refund_score: 0,
  23. can_shipping_fare: 0
  24. },
  25. canRefund: true,
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. let { id, order_goods_id, ref_id, delivery } = options;
  31. let that = this;
  32. this.setData({
  33. order_id: id || 0,
  34. order_goods_id: order_goods_id || 0,
  35. ref_id: ref_id || 0,
  36. delivery: delivery || ''
  37. }, ()=> {
  38. that.getData();
  39. })
  40. },
  41. bindPickerChange: function (e) {
  42. this.setData({
  43. index: e.detail.value
  44. })
  45. },
  46. choseImg: function () {
  47. var self = this;
  48. var refund_imgs = this.data.refund_imgs;
  49. if (refund_imgs.length >= 3) {
  50. wx.showToast({
  51. title: '最多三张图片',
  52. icon: 'success',
  53. duration: 1000
  54. })
  55. return false;
  56. }
  57. wx.chooseImage({
  58. count: 1, // 默认9
  59. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  60. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  61. success: function (res) {
  62. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  63. var tempFilePaths = res.tempFilePaths;
  64. wx.showLoading({
  65. title: '上传中',
  66. })
  67. wx.uploadFile({
  68. url: app.util.url('entry/wxapp/index', {'m':'lionfish_comshop','controller':'goods.doPageUpload'}), //仅为示例,非真实的接口地址
  69. filePath: tempFilePaths[0],
  70. name: 'upfile',
  71. formData: {
  72. 'name': tempFilePaths[0]
  73. },
  74. header: {
  75. 'content-type': 'multipart/form-data'
  76. },
  77. success: function (res) {
  78. wx.hideLoading();
  79. var data = JSON.parse(res.data);
  80. var image_thumb = data.image_thumb;
  81. var orign_image = data.image_o;
  82. var new_img = self.data.refund_imgs;
  83. var new_thumb_img = self.data.refund_thumb_imgs;
  84. new_img.push(orign_image);
  85. new_thumb_img.push(image_thumb);
  86. self.setData({
  87. refund_thumb_imgs: new_thumb_img,
  88. refund_imgs: new_img
  89. })
  90. }
  91. })
  92. }
  93. })
  94. },
  95. chose_type: function (event) {
  96. let stype = event.currentTarget.dataset.rel;
  97. this.setData({
  98. refund_type: stype
  99. })
  100. },
  101. cancle_img: function (event) {
  102. let sr = event.currentTarget.dataset.sr;
  103. var j = 0;
  104. var refund_imgs = this.data.refund_imgs;
  105. var refund_thumb_imgs = this.data.refund_thumb_imgs;
  106. var new_refund_imgs = [];
  107. var new_refund_thumb_imgs = [];
  108. for (var i in refund_thumb_imgs) {
  109. if (refund_thumb_imgs[i] == sr) {
  110. console.log('find');
  111. j = i;
  112. } else {
  113. new_refund_thumb_imgs.push(refund_thumb_imgs[i]);
  114. }
  115. }
  116. for (var i in refund_imgs) {
  117. if (i != j) {
  118. new_refund_imgs.push(refund_imgs[i]);
  119. }
  120. }
  121. this.setData({
  122. refund_thumb_imgs: new_refund_thumb_imgs,
  123. refund_imgs: new_refund_imgs
  124. })
  125. console.log(new_refund_thumb_imgs.length);
  126. console.log(new_refund_imgs.length);
  127. },
  128. wenti_input: function (event) {
  129. var content = event.detail.value;
  130. //pinjia_text
  131. this.setData({
  132. complaint_desc: content
  133. })
  134. },
  135. mobile_input: function (event) {
  136. var content = event.detail.value;
  137. this.setData({
  138. complaint_mobile: content
  139. })
  140. },
  141. name_input: function (event) {
  142. var content = event.detail.value;
  143. this.setData({
  144. complaint_name: content
  145. })
  146. },
  147. refund_money_input: function (event) {
  148. var content = parseFloat(event.detail.value);
  149. let refund_money = this.data.refund_money;
  150. let data = {};
  151. if (content > refund_money) {
  152. let tip = `最大退款金额为${refund_money}`;
  153. if(this.data.order_goods.type=='integral') { tip = `最大可退积分为${refund_money}` }
  154. wx.showToast({
  155. title: tip,
  156. icon: 'none',
  157. duration: 1000
  158. })
  159. content = refund_money;
  160. data.refund_money = refund_money;
  161. }
  162. data.complaint_money = content ? content : 0;
  163. this.setData(data)
  164. },
  165. sub_refund: function () {
  166. let that = this;
  167. if (!that.canRefund) return;
  168. console.log(that.canRefund)
  169. let {
  170. index,
  171. xarray,
  172. order_id,
  173. order_goods_id,
  174. refund_type,
  175. refund_imgs,
  176. complaint_desc,
  177. complaint_mobile,
  178. total,
  179. complaint_name,
  180. complaint_money,
  181. // refund_money,
  182. ref_id,
  183. real_refund_quantity
  184. } = this.data;
  185. complaint_money = parseFloat(complaint_money);
  186. if(real_refund_quantity<=0) {
  187. this.errorToast('请选择退款商品');
  188. return false;
  189. }
  190. if (index == 0) {
  191. this.errorToast('请选择问题类型');
  192. return false;
  193. }
  194. var rfund_name = xarray[index];
  195. if (complaint_money < 0) {
  196. this.errorToast('请填写正确退款金额');
  197. return false;
  198. }
  199. if (complaint_money > total) complaint_money = total;
  200. if (complaint_desc == '') {
  201. this.errorToast('请填写正确问题描述');
  202. return false;
  203. }
  204. if (complaint_name == '') {
  205. this.errorToast('请填写正确联系人');
  206. return false;
  207. }
  208. if (complaint_mobile == '') {
  209. this.errorToast('请填写正确手机号');
  210. return false;
  211. }
  212. var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
  213. if (!myreg.test(complaint_mobile)) {
  214. this.errorToast('请填写正确手机号');
  215. return false;
  216. }
  217. let complaint_shipping_fare = this.data.order_goods.shipping_fare;
  218. that.canRefund = false;
  219. var token = wx.getStorageSync('token');
  220. app.util.request({
  221. url: 'entry/wxapp/index',
  222. data: {
  223. controller: 'afterorder.refund_sub',
  224. token,
  225. ref_id,
  226. order_id,
  227. order_goods_id,
  228. complaint_type: refund_type,
  229. complaint_images: refund_imgs,
  230. complaint_desc,
  231. complaint_mobile,
  232. complaint_reason: rfund_name,
  233. complaint_money,
  234. complaint_name,
  235. real_refund_quantity,
  236. complaint_shipping_fare
  237. },
  238. method: 'POST',
  239. dataType: 'json',
  240. success: function (msg) {
  241. wx.hideLoading();
  242. if (msg.data.code == 3) {
  243. wx.showToast({
  244. title: '未登录',
  245. icon: 'loading',
  246. duration: 1000
  247. })
  248. }
  249. else if (msg.data.code == 0) {
  250. wx.showToast({
  251. title: msg.data.msg,
  252. icon: 'success',
  253. duration: 1000
  254. })
  255. return;
  256. } else {
  257. wx.showToast({
  258. title: '申请成功',
  259. icon: 'success',
  260. duration: 3000,
  261. success: function (res) {
  262. wx.redirectTo({
  263. url: "/lionfish_comshop/pages/order/order?id=" + that.data.order_id + '&delivery='+that.data.delivery
  264. })
  265. }
  266. })
  267. }
  268. },
  269. complete: ()=>{
  270. setTimeout(()=>{
  271. that.canRefund = true;
  272. }, 1500)
  273. }
  274. })
  275. },
  276. errorToast: function(msg) {
  277. wx.showToast({
  278. title: msg,
  279. icon: 'none',
  280. duration: 1000
  281. })
  282. },
  283. /**
  284. * 获取详情 20190711
  285. */
  286. getData: function () {
  287. const token = wx.getStorageSync('token');
  288. var that = this;
  289. const { order_id, order_goods_id, ref_id } = this.data;
  290. app.util.request({
  291. 'url': 'entry/wxapp/index',
  292. 'data': {
  293. controller: 'afterorder.get_order_money',
  294. token: token,
  295. order_id,
  296. order_goods_id,
  297. ref_id
  298. },
  299. dataType: 'json',
  300. success: function (res) {
  301. if (res.data.code == 1) {
  302. const { order_goods, order_status_id, refund_image, refund_info, shipping_name, shipping_tel, total } = res.data;
  303. let xarray = that.data.xarray;
  304. let ref_name = refund_info.ref_name;
  305. let index = xarray.findIndex((item) => (item == ref_name));
  306. index = (index <= 0) ? 0 : index;
  307. let { ref_description, ref_mobile, complaint_name, ref_money } = refund_info;
  308. //商品数量
  309. let { quantity, has_refund_quantity, has_refund_money, type, total_score } = order_goods;
  310. let selArr = new Array(parseInt(quantity));
  311. has_refund_quantity = parseInt(has_refund_quantity);
  312. for(let i=0; i<selArr.length; i++){
  313. if(i>=has_refund_quantity) {
  314. selArr[i] = { isselect: true, isrefund: false };
  315. } else {
  316. selArr[i] = { isselect: true, isrefund: true };
  317. }
  318. }
  319. let can_refund_money = (total*1 - has_refund_money*1).toFixed(2);
  320. if(type=='integral') can_refund_money = (total_score*1 - has_refund_money*1).toFixed(2);
  321. let real_refund_quantity = parseInt(quantity) - has_refund_quantity;
  322. let can_shipping_fare = real_refund_quantity*order_goods.shipping_fare/parseInt(quantity);
  323. if(order_goods && order_goods.price) {
  324. order_goods.price = (order_goods.price*1).toFixed(2);
  325. }
  326. that.setData({
  327. order_goods,
  328. order_status_id,
  329. refund_image,
  330. refund_info,
  331. shipping_name,
  332. shipping_tel,
  333. total: can_refund_money,
  334. index: index || 0,
  335. complaint_desc: ref_description || '',
  336. complaint_mobile: ref_mobile || shipping_tel,
  337. complaint_name: complaint_name || shipping_name,
  338. complaint_money: ref_money || can_refund_money,
  339. refund_money: ref_money || can_refund_money,
  340. selArr,
  341. real_refund_quantity,
  342. max_can_refund: ref_money || can_refund_money,
  343. can_shipping_fare: can_shipping_fare.toFixed(2)
  344. })
  345. } else if (res.data.code == 3) {
  346. //un login
  347. }
  348. }
  349. })
  350. },
  351. goodsselect: function(e) {
  352. let idx = e.target.dataset.idx;
  353. let { selArr, order_goods, total } = this.data;
  354. let selItem = { isselect: !selArr[idx].isselect, isrefund: selArr[idx].isrefund};
  355. selArr[idx] = selItem;
  356. // 均分计算可退款金额
  357. let totNum = selArr.length || 0;
  358. let num = 0;
  359. let canfunnum = 0;
  360. selArr.forEach(item=>{
  361. if(!item.isrefund) canfunnum += 1;
  362. if(item.isselect&&!item.isrefund) num += 1;
  363. })
  364. let danPrice = total/canfunnum;
  365. let can_refund_money = danPrice*num;
  366. let refund_score = order_goods.one_goods_score*num;
  367. // 均分配送费
  368. let shipping_fare = order_goods.shipping_fare || 0;
  369. let dan_shipping_fare = shipping_fare/totNum;
  370. let can_shipping_fare = dan_shipping_fare*num;
  371. if(can_refund_money>total || totNum==num){
  372. can_refund_money = total*1;
  373. refund_score = order_goods.use_score;
  374. }
  375. if(can_shipping_fare>shipping_fare || totNum==num){
  376. can_shipping_fare = shipping_fare*1;
  377. }
  378. let max_can_refund = can_refund_money.toFixed(2);
  379. can_shipping_fare = can_shipping_fare.toFixed(2);
  380. this.setData({
  381. selArr,
  382. real_refund_quantity: num,
  383. complaint_money: can_refund_money,
  384. refund_money: can_refund_money.toFixed(2),
  385. refund_score,
  386. max_can_refund,
  387. can_shipping_fare
  388. })
  389. }
  390. })