123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- const App = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- // 订单商品id
- order_goods_id: null,
- // 订单商品详情
- detail: {},
- // 图片列表
- imageList: [],
- // 服务类型
- serviceType: 10,
- },
- disable: false,
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- // 记录页面参数
- this.data.order_goods_id = options.order_goods_id;
- // 获取订单商品详情
- this.getGoodsDetail();
- },
- /**
- * 获取订单商品详情
- */
- getGoodsDetail: function() {
- let _this = this;
- App._get('user.refund/apply', {
- order_goods_id: this.data.order_goods_id
- }, function(result) {
- _this.setData(result.data);
- });
- },
- /**
- * 切换标签
- */
- onSwitchService: function(e) {
- this.setData({
- serviceType: e.detail.target.dataset.type
- });
- },
- /**
- * 跳转商品详情
- */
- onGoodsDetail: function(e) {
- wx.navigateTo({
- url: '../../../goods/index?goods_id=' + e.detail.target.dataset.id
- });
- },
- /**
- * 选择图片
- */
- chooseImage: function(e) {
- let _this = this,
- index = e.currentTarget.dataset.index,
- imageList = _this.data.imageList;
- // 选择图片
- wx.chooseImage({
- count: 6 - imageList.length,
- sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
- success: function(res) {
- _this.setData({
- imageList: imageList.concat(res.tempFilePaths)
- });
- }
- });
- },
- /**
- * 删除图片
- */
- deleteImage: function(e) {
- let dataset = e.currentTarget.dataset,
- imageList = this.data.imageList;
- imageList.splice(dataset.imageIndex, 1);
- this.setData({
- imageList
- });
- },
- /**
- * 表单提交
- */
- onSubmit: function(e) {
- let _this = this;
- if (!e.detail.value.content) {
- App.showError('申请原因不能为空');
- return false;
- }
- // 判断是否重复提交
- if (_this.disable === true) {
- return false;
- }
- // 表单提交按钮设为禁用 (防止重复提交)
- _this.disable = true;
- wx.showLoading({
- title: '正在处理...',
- mask: true
- });
- // form参数
- let postParams = {
- order_goods_id: _this.data.order_goods_id,
- type: _this.data.serviceType,
- content: e.detail.value.content,
- };
- // form提交执行函数
- let fromPostCall = function(params) {
- console.log('fromPostCall');
- App._post_form('user.refund/apply', params, function(result) {
- if (result.code === 1) {
- App.showSuccess(result.msg, function() {
- // 跳转售后管理页面
- wx.navigateTo({
- url: "../index"
- });
- });
- } else {
- App.showError(result.msg);
- }
- },
- false,
- function() {
- wx.hideLoading();
- _this.disable = false;
- });
- };
- // 统计图片数量
- let imagesLength = _this.data.imageList.length;
- // 判断是否需要上传图片
- imagesLength > 0 ? _this.uploadFile(imagesLength, fromPostCall, postParams) : fromPostCall(postParams);
- },
- /**
- * 上传图片
- */
- uploadFile: function(imagesLength, callBack, formData) {
- let uploaded = [];
- // 文件上传
- let i = 0;
- this.data.imageList.forEach(function(filePath, fileKey) {
- wx.uploadFile({
- url: App.api_root + 'upload/image',
- filePath: filePath,
- name: 'iFile',
- formData: {
- wxapp_id: App.getWxappId(),
- token: wx.getStorageSync('token')
- },
- success: function(res) {
- let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
- if (result.code === 1) {
- uploaded[fileKey] = result.data.file_id;
- }
- },
- complete: function() {
- i++;
- if (imagesLength === i) {
- // 所有文件上传完成
- console.log('upload complete');
- formData['images'] = uploaded;
- // 执行回调函数
- callBack && callBack(formData);
- }
- }
- });
- });
- },
- })
|