export function wxSaveCode(imgSrc: string) {
  wx.getSetting({
    success(res: any) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            wx.getImageInfo({
              src: imgSrc,
              success: function (res: any) {
                //图片保存到本地
                wx.saveImageToPhotosAlbum({
                  filePath: res.path,
                  success: function (data: any) {
                    console.log(data)
                    uni.showToast({
                      title: '保存成功',
                      icon: 'success',
                      duration: 2000
                    })
                  },
                  fail: function (err: any) {
                    console.log(err)
                    if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                      wx.openSetting({
                        success(settingdata: any) {
                          if (settingdata.authSetting['scope.writePhotosAlbum']) {
                            console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                            uni.showToast({
                              title: '请重新点击下载按钮',
                              duration: 1000
                            })
                          } else {
                            uni.showModal({
                              title: '提示',
                              content:
                                '您已禁止小程序授权相册存储权限，请到设置开启权限或清除小程序缓存',
                              showCancel: false
                            })
                          }
                        }
                      })
                    }
                    if (err.errMsg === 'saveImageToPhotosAlbum:fail:system permission denied') {
                      uni.showModal({
                        title: '提示',
                        content: '您已禁止应用存储权限，请到系统设置开启应用存储权限',
                        showCancel: false
                      })
                    }
                  }
                })
              }
            })
          },
          fail() {
            uni.showModal({
              title: '提示',
              content: '您已禁止小程序授权相册存储权限，请到设置开启权限或清除小程序缓存',
              showCancel: false
            })
          }
        })
      } else {
        wx.getImageInfo({
          src: imgSrc,
          success: function (res: any) {
            console.log(res)
            //图片保存到本地
            wx.saveImageToPhotosAlbum({
              filePath: res.path,
              success: function (data: any) {
                console.log(data)
                uni.showToast({
                  title: '保存成功',
                  icon: 'success',
                  duration: 2000
                })
              },
              fail: function (err: any) {
                if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                  wx.openSetting({
                    success(settingdata: any) {
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                        uni.showToast({
                          title: '请重新点击下载按钮',
                          duration: 1000
                        })
                      } else {
                        uni.showModal({
                          title: '提示',
                          content:
                            '您已禁止小程序授权相册存储权限，请到设置开启权限或清除小程序缓存',
                          showCancel: false
                        })
                      }
                    }
                  })
                }
                if (err.errMsg === 'saveImageToPhotosAlbum:fail:system permission denied') {
                  uni.showModal({
                    title: '提示',
                    content: '您已禁止应用存储权限，请到系统设置开启应用存储权限',
                    showCancel: false
                  })
                }
              }
            })
          }
        })
      }
    }
  })
}
