



// 判断两时间段之间活动状态、判断活动还有多长时间开始、多长时间结束----添加定时器运行此方法可倒计时
// let startTime = new Date(item.startTime.replace(/-/g, '/')).getTime(); //转时间戳
// let closeTime = new Date(item.closeTime.replace(/-/g, '/')).getTime(); //转时间戳
// let djs = this.djsTime(startTime, closeTime);
export const djsTime = function (startTime, endTime) {
  let bbb = new Date().getTime(),
    leftTime = startTime - bbb,
    rightTime = endTime - bbb,
    djsTime = '',
    speed = 0,
    activityStatus = 0, //活动状态 1：未开始 2：进行中 3：已结束
    dd, hh, mm, ss;
  if (leftTime > 0) { //还未开始
    activityStatus = 1
    dd = Math.floor(leftTime / 1000 / 60 / 60 / 24);
    hh = Math.floor((leftTime / 1000 / 60 / 60) % 24) < 10 ? '0' + Math.floor((leftTime / 1000 / 60 / 60) % 24) : Math.floor((leftTime / 1000 / 60 / 60) % 24);
    mm = Math.floor((leftTime / 1000 / 60) % 60) < 10 ? '0' + Math.floor((leftTime / 1000 / 60) % 60) : Math.floor((leftTime / 1000 / 60) % 60);
    ss = Math.floor((leftTime / 1000) % 60) < 10 ? '0' + Math.floor((leftTime / 1000) % 60) : Math.floor((leftTime / 1000) % 60);
    if (dd != 0) {
      djsTime = dd + "天 " + hh + ":" + mm + ":" + ss;
    } else {
      djsTime = hh + ":" + mm + ":" + ss;
    }
  } else if (leftTime <= 0) {
    if (rightTime > 0) { //进行中
      activityStatus = 2;
      speed = (1 - rightTime / (endTime - startTime)) * 100;
      dd = Math.floor(rightTime / 1000 / 60 / 60 / 24);
      hh = Math.floor((rightTime / 1000 / 60 / 60) % 24) < 10 ? '0' + Math.floor((rightTime / 1000 / 60 / 60) % 24) : Math.floor((rightTime / 1000 / 60 / 60) % 24);
      mm = Math.floor((rightTime / 1000 / 60) % 60) < 10 ? '0' + Math.floor((rightTime / 1000 / 60) % 60) : Math.floor((rightTime / 1000 / 60) % 60);
      ss = Math.floor((rightTime / 1000) % 60) < 10 ? '0' + Math.floor((rightTime / 1000) % 60) : Math.floor((rightTime / 1000) % 60);
      if (dd != 0) {
        djsTime = dd + "天 " + hh + ":" + mm + ":" + ss;
      } else {
        djsTime = hh + ":" + mm + ":" + ss;
      }
    } else { //已结束
      speed = 100;
      djsTime = '已结束';
      activityStatus = 3;
    }
  }
  let item = {
    djsTime: djsTime, //距离当前时间差
    activityStatus: activityStatus, //活动状态 1：未开始 2：进行中 3：已结束
    speed: speed, //进度（单位%）
    dd: dd, //天
    hh: hh, //小时
    mm: mm, //分
    ss: ss, //秒
  }
  return item;
}

// 小程序获取定位权限判断
// isOpenSetting  默认false:不检验授权，true:检验授权后获取地址
function getMpLocation(successCallback, errCallback, isOpenSetting) {
  uni.getSetting({
    success: res => {
      if (res.authSetting['scope.userLocation'] || !isOpenSetting) {
        uni.getLocation({
          // #ifndef MP-ALIPAY
          type: 'gcj02',
          // #endif
          success(res) {
            console.log('successCallback')
            successCallback(res);
          },
          fail(err) {
            console.log("位置信息错误", err);
            errCallback("位置信息获取失败");
          }
        });
      } else {
        errCallback("“位置信息”未授权");
        isOpenSetting && uni.showModal({
          title: '提示',
          content: '请先在设置页面打开“位置信息”使用权限',
          confirmText: '去设置',
          cancelText: '再逛会',
          success: res => {
            if (res.confirm) {
              uni.openSetting();
            }
          }
        });
      }
    }
  });
}
// 获取地址信息
let locationAuthorize = true;
export const getAppLatLon = function (successCallback, errCallback, isOpenSetting) {
  const _this = this;
  // #ifdef MP-WEIXIN
  if (locationAuthorize && isOpenSetting) {
    uni.authorize({
      scope: 'scope.userLocation',
      success: () => {
        getMpLocation(successCallback, errCallback, isOpenSetting);
        locationAuthorize = false;
      },
      fail: () => {
        locationAuthorize = false;
      }
    });
  } else {
    getMpLocation(successCallback, errCallback, isOpenSetting);
  }
  // #endif
  // #ifdef MP-ALIPAY
  getMpLocation(successCallback, errCallback, false);
  // #endif
  // #ifdef H5
  uni.getLocation({
    type: 'gcj02',
    success(res) {
      console.log('successCallback')
      successCallback(res);
    },
    fail(err) {
      console.log("位置信息错误", err);
      errCallback("位置信息获取失败");
    }
  });
  // #endif
  // #ifdef APP-PLUS
  uni.getLocation({
    type: 'gcj02',
    success: res => {
      successCallback(res);
    },
    fail: (err) => {
      console.log("位置信息错误", err);
      errCallback("位置信息获取失败");
    }
  });
  // #endif
}

// 打开外链
export const openLink = function (href) {
  let that = this
  // #ifdef APP-PLUS
  plus.runtime.openURL(href)
  // #endif
  // #ifdef H5
  window.open(href)
  // #endif
  // #ifdef MP
  uni.setClipboardData({
    data: href,
    success: () => {
      uni.hideToast();
      that.$nextTick(() => {
        that.$u.toast('链接已复制，请在浏览器打开');
      })
    }
  });
  // #endif
}
// 保存图片
export const saveImg = function (url, callback, settingWritePhotosAlbum = false) {
  if (url) {
    // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO
    if (settingWritePhotosAlbum) {
      uni.getSetting({
        success: res => {
          if (res.authSetting['scope.writePhotosAlbum']) {
            uni.showLoading({
              title: '正在下载'
            });
            uni.downloadFile({
              url: url,
              success: data => {
                if (data.statusCode == 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: data.tempFilePath,
                    success: () => {
                      uni.hideLoading();
                      callback && callback();
                      uni.showToast({
                        title: '保存成功'
                      });
                    },
                    fail(e) {
                      uni.hideLoading();
                      tip({
                        title: '下载失败，错误原因：' + e
                          .errMsg,
                        icon: "none"
                      });
                    }
                  });
                } else {
                  uni.hideLoading();
                  uni.showToast({
                    title: '下载失败',
                    icon: "none"
                  });
                }
              },
              fail(e) {
                uni.hideLoading();
                uni.showToast({
                  title: '下载失败，错误原因：' + e.errMsg,
                  icon: "none"
                });
              }
            });
          } else {
            uni.showModal({
              title: '提示',
              content: '请先在设置页面打开“保存相册”使用权限',
              confirmText: '去设置',
              cancelText: '算了',
              success: data => {
                if (data.confirm) {
                  uni.openSetting();
                }
              }
            });
          }
        }
      });
    } else {
      uni.authorize({
        scope: 'scope.writePhotosAlbum',
        success: () => {
          uni.showLoading({
            title: '正在下载'
          });
          uni.downloadFile({
            url: url,
            success: data => {
              if (data.statusCode == 200) {
                uni.saveImageToPhotosAlbum({
                  filePath: data.tempFilePath,
                  success: () => {
                    uni.hideLoading();
                    callback && callback();
                    uni.showToast({
                      title: '保存成功'
                    });
                  },
                  fail(e) {
                    uni.hideLoading();
                    tip({
                      title: '下载失败，错误原因：' + e.errMsg,
                      icon: "none"
                    });
                  }
                });
              } else {
                uni.hideLoading();
                uni.showToast({
                  title: '下载失败',
                  icon: "none"
                });
              }
            },
            fail(e) {
              uni.hideLoading();
              uni.showToast({
                title: '下载失败，错误原因：' + e.errMsg,
                icon: "none"
              });
            }
          });
        }
      });
    }
    // #endif
    // #ifdef H5
    uni.showLoading({
      title: '正在下载'
    });
    uni.downloadFile({
      url: url,
      success: data => {
        uni.hideLoading();
        if (data.statusCode == 200) {
          callback && callback();
          window.open(data.tempFilePath);
        } else {
          uni.showToast({
            title: '下载失败',
            icon: "none"
          });
        }
      },
      fail(e) {
        uni.hideLoading();
        uni.showToast({
          title: '下载失败，错误原因：' + e.errMsg,
          icon: "none"
        });
      }
    });
    // #endif
    // #ifdef APP-PLUS || MP-ALIPAY
    uni.showLoading({
      title: '正在下载'
    });
    uni.saveImageToPhotosAlbum({
      filePath: url,
      success: () => {
        uni.hideLoading();
        callback && callback();
        uni.showToast({
          title: '保存成功'
        });
      },
      fail(e) {
        uni.hideLoading();
        uni.showToast({
          title: '下载失败，错误原因：' + e.errMsg,
          icon: "none"
        });
      }
    });
    // #endif
  } else {
    uni.showToast({
      title: '未找到图片',
      icon: 'none'
    });
  }
};
// 保存视频
function tip(data) {
  setTimeout(() => {
    uni.showToast(data);
  }, 500);
}
export const saveVideo = function (url, callback, settingWritePhotosAlbum = false) {
  if (url) {
    // #ifdef MP
    if (settingWritePhotosAlbum) {
      uni.getSetting({
        success: res => {
          if (res.authSetting['scope.writePhotosAlbum']) {
            // #ifdef MP-WEIXIN
            let urlArr = url.split("/");
            let updateUrl = urlArr[urlArr.length - 1];
            let filePath = wx.env.USER_DATA_PATH + '/' + (Math.random() * 100) + updateUrl;
            // #endif
            uni.showLoading({
              title: '正在下载'
            });
            uni.downloadFile({
              url: url,
              // #ifdef MP-WEIXIN
              filePath: filePath,
              // #endif
              success: data => {
                if (data.statusCode == 200) {
                  let tempFilePath = ''
                  // #ifdef MP-WEIXIN
                  tempFilePath = data.filePath
                  // #endif
                  // #ifndef MP-WEIXIN
                  tempFilePath = data.tempFilePath
                  // #endif
                  uni.saveVideoToPhotosAlbum({
                    filePath: tempFilePath,
                    success: () => {
                      uni.hideLoading();
                      callback && callback();
                      tip({
                        title: '保存成功'
                      });
                    },
                    fail(e) {
                      uni.hideLoading();
                      if (e.errMsg.indexOf('fail cancel') >= 0) {
                        tip({
                          title: '已取消',
                          icon: "none"
                        });
                      } else {
                        console.log('下载失败，错误原因1：' + e.errMsg)
                        tip({
                          title: '下载失败，错误原因：' + e.errMsg,
                          icon: "none"
                        });
                      }
                    }
                  });
                } else {
                  uni.hideLoading();
                  tip({
                    title: '下载失败',
                    icon: "none"
                  });
                }
              },
              fail(e) {
                uni.hideLoading();
                console.log('下载失败，错误原因2：' + e.errMsg)
                tip({
                  title: '下载失败，错误原因：' + e.errMsg,
                  icon: "none"
                });
              }
            });
          } else {
            uni.showModal({
              title: '提示',
              content: '请先在设置页面打开“保存相册”使用权限',
              confirmText: '去设置',
              cancelText: '算了',
              success: data => {
                if (data.confirm) {
                  uni.openSetting();
                }
              }
            });
          }
        }
      });
    } else {
      uni.authorize({
        scope: 'scope.writePhotosAlbum',
        success: () => {
          // #ifdef MP-WEIXIN
          let urlArr = url.split("/");
          let updateUrl = urlArr[urlArr.length - 1];
          let filePath = wx.env.USER_DATA_PATH + '/' + updateUrl;
          // #endif
          uni.showLoading({
            title: '正在下载'
          });
          uni.downloadFile({
            url: url,
            // #ifdef MP-WEIXIN
            filePath: filePath,
            // #endif
            success: data => {
              if (data.statusCode == 200) {
                let tempFilePath = ''
                // #ifdef MP-WEIXIN
                tempFilePath = data.filePath
                // #endif
                // #ifndef MP-WEIXIN
                tempFilePath = data.tempFilePath
                // #endif
                uni.saveVideoToPhotosAlbum({
                  filePath: tempFilePath,
                  success: () => {
                    uni.hideLoading();
                    callback && callback();
                    tip({
                      title: '保存成功'
                    });
                  },
                  fail(e) {
                    uni.hideLoading();
                    if (e.errMsg.indexOf('fail cancel') >= 0) {
                      tip({
                        title: '已取消',
                        icon: "none"
                      });
                    } else {
                      tip({
                        title: '下载失败，错误原因：' + e.errMsg,
                        icon: "none"
                      });
                    }
                  }
                });
              } else {
                uni.hideLoading();
                tip({
                  title: '下载失败，错误原因：' + data.errMsg,
                  icon: "none"
                });
              }
            },
            fail(e) {
              uni.hideLoading();
              tip({
                title: '下载失败，错误原因：' + e.errMsg,
                icon: "none"
              });
            }
          });
        }
      });
    }
    // #endif
    // #ifdef H5
    uni.showLoading({
      title: '正在下载'
    });
    uni.downloadFile({
      url: url,
      success: data => {
        uni.hideLoading();
        if (data.statusCode == 200) {
          callback && callback();
          window.open(data.tempFilePath);
        } else {
          tip({
            title: '下载失败',
            icon: "none"
          });
        }
      },
      fail(e) {
        uni.hideLoading();
        tip({
          title: '下载失败，错误原因：' + e.errMsg,
          icon: "none"
        });
      }
    });
    // #endif
    // #ifdef APP-PLUS
    uni.showLoading({
      title: '正在下载'
    });
    uni.saveVideoToPhotosAlbum({
      filePath: url,
      success: () => {
        uni.hideLoading();
        callback && callback();
        tip({
          title: '保存成功'
        });
      },
      fail(e) {
        uni.hideLoading();
        tip({
          title: '下载失败，错误原因：' + e.errMsg,
          icon: "none"
        });
      }
    });
    // #endif
  } else {
    tip({
      title: '未找到视频',
      icon: 'none'
    });
  }
};