JS判断当前是否平板安卓并是否支持cordova方法的示例代码

2022-10-07,,,,

需求:pc和安卓平板共用一套代码,平板的代码用了cordova做了一个壳子嵌套如果用了cordova就不支持elementui中的上传功能,所以要用判断,现用户在平板又会用浏览器打开项目所以要做两层判断

app内是用cordova中的 window.actionsheet方法调用上传读取相机和图库方法

上代码

<el-upload
                      class="avatar-uploader repost-pic-upload"
                      ref="uploadimgref"
                      action=""
                      :accept="filetype"
                      :disabled="isandroid"
                      :show-file-list="false"
                      :on-preview="picpreview"
                      :http-request="
                        file => {
                          beforeupload(file, index);
                          return false;
                        }
                      "
                      :before-remove="
                        (file, filelist) => {
                          handleremove(file, filelist, index);
                          return false;
                        }
                      "
                    >
                      <div
                        v-if="uploadflag"
                        class="progress-wrap"
                        @click.stop="handlecancelupload"
                      >
                        <p class="progress-rate">{{ uploadpercent }}%</p>
                        <p class="progress-cancel">取消上传</p>
                      </div>
                      <img
                        v-else-if="item.dstimagedata"
                        :src="item.dstimagedata"
                        class="avatar"
                      />
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                      <div
                        class="android-wrap"
                        v-if="isandroid"
                        @click="selectphotosheet(index)"
                      ></div>
                    </el-upload>

computed计算属性

  computed: {
    // 判断是否安卓app中打开应用还是浏览器
    isandroid() {
      if (
        /(iphone|ipad|ipod|ios|android)/i.test(navigator.useragent) &&
        typeof window.actionsheet == "function"
      ) {
        return true;
      } else {
        return false;
      }
    }
  },

安卓方法

  // 选择图片弹窗按钮
    selectphotosheet(arrindex) {
      let that = this;
      window.actionsheet(["拍照", "相册"]).then(index => {
        that.cameragetpicture(index, arrindex);
      });
    },
    // 拍照或相册选择
    cameragetpicture(data, arrindex) {
      // data == 2 为相册
      window
        .cameragetpicture(data)
        .then(base64 => {
          console.log(base64);
          this.uploadimg(arrindex, base64);
        })
        .then(err => {
          console.log(err);
        });
    },

pc方法

 // 上传图片
    beforeupload(file, index) {
      this.uploadflag = true;
      let pictype = file.file.type;
      if (
        !(
          pictype == "image/png" ||
          pictype == "image/jpg" ||
          pictype == "image/jpeg"
        )
      ) {
        this.$message.warning("只能jpg/png格式的照片");
        this.list[index].src = "";
        return false;
      }
      if (file.file.size > 2 * 1024 * 1024) {
        this.$message.warning("图片大小不能超多2m");
        return false;
      }
      // let params = new formdata();
      // params.append("file", file.file);
      common.getbase64(file.file).then(base64 => {
        // this.list[index].dstimagedata = base64;
        this.uploadimg(index, base64);
      });
 
      this.dialogvisible = true;
      return false;
    },
    // 上传图片
    uploadimg(index, base64) {
      let arr = base64.split(",");
      let params = {
        prefix: arr[0],
        datastring: arr[1]
      };
 
      let canceltoken = axios.canceltoken;
      let self = this;
      axios({
        url: this.imguploadurlbase,
        method: "post",
        data: params,
        headers: {
          "content-type": "application/json;charser=utf-8",
          authorization: `bearer${store.state.login.login.access_token}`
        },
        canceltoken: new canceltoken(function executor(c) {
          self.cancel = c;
        }),
        onuploadprogress: progressevent => {
          this.uploadpercent = math.round(
            (progressevent.loaded / progressevent.total) * 100
          );
        }
      })
        .then(({ data: { data } }) => {
          api
            .getrecognition({
              imgpath: data.filepath
            })
            .then(res => {
              this.list[index].dstimagedata = data.filepath;
              this.list[index].nameplatetablejson = res;
              this.$message.success("上传成功");
            });
        })
        .catch(() => {
          this.$message.error("上传失败");
        })
        .finally(() => {
          this.uploadflag = false;
          this.uploadpercent = 0;
        });
    
    },

到此这篇关于js判断当前是否平板安卓并是否支持cordova方法的文章就介绍到这了,更多相关js判断当前平板安卓内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《JS判断当前是否平板安卓并是否支持cordova方法的示例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。