123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- /*
- * @Author: Rindy
- * @Date: 2019-08-06 16:25:08
- * @LastEditors: Rindy
- * @LastEditTime: 2021-08-27 12:33:49
- * @Description: Request
- */
- /** http请求模块
- * @category utils
- * @module utils/request
- */
-
-
- import browser from "./browser.js"
- import { base64ToBlob } from "./file.js"
- //import { $alert, $confirm, $loginTips } from "@/components/shared/message"
- //import { $waiting } from "@/components/shared/loading"
- //import { checkLogin } from "@/api"
- //import { LoginDetector } from "@/core/starter"
- //import { i18n } from "@/lang"
- //import { password } from "@/utils/string"
- //import store from "../Store"
- // 空函数
- const noop = function() {}
- // 请求回调队列
- let postQueue = []
- /**
- * @property {number} NEXT - 继续执行
- * @property {number} SUCCESS - 成功
- * @property {number} EXCEPTION - 异常错误
- * @property {number} FAILURE_CODE_3001 - 缺少必要参数
- * @property {number} FAILURE_CODE_3002 - 访问异常
- * @property {number} FAILURE_CODE_3003 - 非法访问
- * @property {number} FAILURE_CODE_3004 - 用户未登录
- * @property {number} FAILURE_CODE_3005 - 验证码已过期
- * @property {number} FAILURE_CODE_3006 - 验证码错误
- * @property {number} FAILURE_CODE_3007 - 昵称已存在
- * @property {number} FAILURE_CODE_3008 - 该手机已被注册
- * @property {number} FAILURE_CODE_3009 - 两次输入的密码不一致
- * @property {number} FAILURE_CODE_3010 - 昵称长度错误
- * @property {number} FAILURE_CODE_3011 - 密码长度错误
- * @property {number} FAILURE_CODE_3012 - 昵称包含敏感词
- * @property {number} FAILURE_CODE_3013 - 手机号码格式错误
- * @property {number} FAILURE_CODE_3014 - 账号或密码不正确
- * @property {number} FAILURE_CODE_3015 - 用户不存在
- * @property {number} FAILURE_CODE_3016 - 您没有权限,请联系管理员
- * @property {number} FAILURE_CODE_3017 - 空文件
- * @property {number} FAILURE_CODE_3018 - 需要上传或使用的文件不存在
- * @property {number} FAILURE_CODE_5010 - 找不到该场景对应的相机
- * @property {number} FAILURE_CODE_5012 - 数据不正常
- * @property {number} FAILURE_CODE_5014 - 无权操作该场景
- * @property {number} FAILURE_CODE_5005 - 场景不存在
- */
- export const statusCode = {
- NEXT: -999,
- SUCCESS: 0,
- EXCEPTION: -1,
- FAILURE_CODE_3001: 3001,
- FAILURE_CODE_3002: 3002,
- FAILURE_CODE_3003: 3003,
- FAILURE_CODE_3004: 3004,
- FAILURE_CODE_3005: 3005,
- FAILURE_CODE_3006: 3006,
- FAILURE_CODE_3007: 3007,
- FAILURE_CODE_3008: 3008,
- FAILURE_CODE_3009: 3009,
- FAILURE_CODE_3010: 3010,
- FAILURE_CODE_3011: 3011,
- FAILURE_CODE_3012: 3012,
- FAILURE_CODE_3013: 3013,
- FAILURE_CODE_3014: 3014,
- FAILURE_CODE_3015: 3015,
- FAILURE_CODE_3016: 3016,
- FAILURE_CODE_3017: 3017,
- FAILURE_CODE_3018: 3018,
- FAILURE_CODE_5010: 5010,
- FAILURE_CODE_5012: 5012,
- FAILURE_CODE_5014: 5014,
- FAILURE_CODE_5005: 5005,
- }
-
- /**
- * 获取Token
- * @function
- */
- export function getToken() {
- var urlToken = browser.urlHasValue("token", true)
- if (urlToken) {
- // 设置token共享给用户中心
- localStorage.setItem("token", urlToken)
- }
- return urlToken || localStorage.getItem("token") || ""
- }
- /**
- * 根据状态码的结果处理后续操作
- * @function
- * @param {number} code - 状态码
- * @param {function} callback - 回调
- */
- export function statusCodesHandler(code, callback) {
- if (code == statusCode.EXCEPTION) {
- return $alert({ content: i18n.t("tips.exception") })
- }
- if (
- code == statusCode.FAILURE_CODE_3002 ||
- code == statusCode.FAILURE_CODE_3003 ||
- code == statusCode.FAILURE_CODE_3004
- ) {
- callback(code)
- return showLoginTips()
- }
- if (code == statusCode.FAILURE_CODE_3001) {
- callback(code)
- return $alert({ content: i18n.t("tips.params_notfound") })
- }
- if (code == statusCode.FAILURE_CODE_3017) {
- callback(code)
- return $alert({ content: i18n.t("tips.file_notfound") })
- }
- if (code == statusCode.FAILURE_CODE_5005) {
- /* if (!config.isEdit) {
- return (location.href = config.pages.NotFound)
- } */
- callback(code)
- return $alert({ content: i18n.t("tips.scene_notfound") })
- }
- if (code == statusCode.FAILURE_CODE_5010) {
- callback(code)
- return $alert({ content: i18n.t("tips.camera_notfound") })
- }
- if (code == statusCode.FAILURE_CODE_5012) {
- callback(code)
- return $alert({ content: i18n.t("tips.data_error") })
- }
- if (code == statusCode.FAILURE_CODE_5014) {
- callback(code)
- return $alert({ content: i18n.t("tips.auth_deny") })
- }
- return statusCode.NEXT
- }
- $.ajaxSetup({
- headers: {},
- beforeSend: function(xhr) {
- const token = getToken()
- if (token) {
- xhr.setRequestHeader("token", token)
- } else if (!token && this.url.indexOf("isLogin") != -1) {
- showLoginTips()
- }
- /* if (config.oem == "localshow") {
- // 本地版本兼容当前目录
- if (this.url.indexOf("http") == -1 && this.url.indexOf("/") == 0) {
- this.url = this.url.substr(1)
- }
- } */
- // if(this.url.indexOf('http')==-1 && this.url.indexOf('/') !=0){
- // this.url = '/'+this.url
- // }
- },
- error: function(xhr, status, error) {
- // 出错时默认的处理函数
- if (this.url.indexOf("/scene.json") != -1 && xhr.status == 404) {
- return $alert({ content: i18n.t("tips.scene_notfound") })
- } else if (this.type === "POST") {
- return $alert({ content: i18n.t("tips.network_error") })
- }
- },
- success: function(result) {},
- complete: function() {
- // Post类型请求无论成功或失败都关闭等待提示
- if (this.type === "POST") {
- http.__loading && $waiting.hide()
- }
- http.__loading = true
- },
- })
- /**
- * @namespace http
- * @type {Object}
- */
- export const http = {
- statusCode,
- __loading: true,
- __request(xhr, method, url, data, done, fail) {
- if (typeof done != "function") {
- done = noop
- }
- if (typeof fail != "function") {
- fail = noop
- }
- xhr.done(result => {
- if (typeof result.code !== "undefined") {
- const flag = statusCodesHandler(result.code, function(code) {
- // 需要登录的状态
- if (
- code == statusCode.FAILURE_CODE_3001 ||
- code == statusCode.FAILURE_CODE_3002 ||
- code == statusCode.FAILURE_CODE_3003 ||
- code == statusCode.FAILURE_CODE_3004
- ) {
- if (url.indexOf("isLogin") == -1 && url.indexOf("openSceneBykey") == -1) {
- postQueue.push(function() {
- http[method](url, data, done, fail)
- })
- }
- }
- fail()
- })
- if (flag === statusCode.NEXT) {
- done(result, result.code == 0)
- }
- } else {
- done(result)
- }
- })
- xhr.fail(fail)
- xhr.always(() => (xhr = null))
- return xhr
- },
- /**
- * Get请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- get(url, data = {}, done, fail) {
- if (/\.json/.test(url)) {
- // json文件格式自动调用getJson方法
- return this.getJson(url, data, done, fail)
- }
- return this.__request($.get(url, data), "get", url, data, done, fail)
- },
- /**
- * Get Blob请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- getText(url, data = {}, done, fail) {
- return this.__request(
- $.ajax({
- url: url,
- dataType: "text",
- }),
- "getText",
- url,
- data,
- done,
- fail
- )
- },
- /**
- * GetJson请求 读取json文件数据
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- getJson(url, data = {}, done, fail) {
- return this.__request($.getJSON(url, data), "get", url, data, done, fail)
- },
- /**
- * Get Blob请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- getBlob(url, data = {}, done, fail) {
- return this.__request(
- $.ajax({
- url: url,
- dataType: "blob",
- }),
- "getBlob",
- url,
- data,
- done,
- fail
- )
- },
- /**
- * Get Arraybuffer请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- getArraybuffer(url, data = {}, done, fail) {
- return this.__request(
- $.ajax({
- url: url,
- dataType: "arraybuffer",
- }),
- "getArraybuffer",
- url,
- data,
- done,
- fail
- )
- },
- /**
- * Post 请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- post(url, data = {}, done, fail) {
- if (url.indexOf("isLogin") == -1) {
- http.__loading && $waiting.show()
- }
- return this.__request($.post(url, data), "post", url, data, done, fail)
- },
- /**
- * PostJson 请求
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- postJson(url, data = {}, done, fail) {
- http.__loading && $waiting.show()
- return this.__request(
- $.ajax({
- type: "POST",
- url: url,
- contentType: "application/json",
- data: JSON.stringify(data),
- }),
- "postJson",
- url,
- data,
- done,
- fail
- )
- },
- /**
- * Post 表单 支持文件上传
- * @param {String} url 请求地址
- * @param {FormData?} formData 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- postForm(url, formData, done, fail, onProgress) {
- if (typeof onProgress === "function") {
- return this.__request(
- $.ajax({
- type: "POST",
- url: url,
- processData: false,
- contentType: false,
- data: formData,
- xhr: function() {
- const xhr = new XMLHttpRequest()
- xhr.upload.addEventListener("progress", function(e) {
- onProgress((e.loaded / e.total) * 100 + "%")
- })
- return xhr
- },
- }),
- "postForm",
- url,
- formData,
- done,
- fail
- )
- } else {
- http.__loading && $waiting.show()
- return this.__request(
- $.ajax({
- type: "POST",
- url: url,
- processData: false,
- contentType: false,
- data: formData,
- }),
- "postForm",
- url,
- formData,
- done,
- fail
- )
- }
- },
- /**
- * 加载图片
- * @param {String} url 请求地址
- * @param {Number?} retry 重试次数,默认为3
- */
- loadImage(url, retry = 3) {
- const def = $.Deferred()
- const img = new Image()
- /* if (process.env.VUE_APP_REGION == "AWS" && url.indexOf("x-oss-process=image") != -1) {
- var arr = url.split("?")
- url = arr[0] + encodeURIComponent("?" + arr[1].replace(/\//g, "@"))
- } */
- const load = () => {
- console.warn("Retrying load image: " + url)
- this.loadImage(url, retry - 1)
- .done(def.resolve.bind(def))
- .progress(def.notify.bind(def))
- .fail(def.reject.bind(def))
- }
- img.onerror = function() {
- retry > 0 ? setTimeout(() => load(), 1e3) : def.reject(`[${url}]加载失败`)
- }
- img.onload = function() {
- def.resolve(img)
- }
- img.crossOrigin = "anonymous"
- img.src = url
- return def
- },
- /**
- * 上传文件
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- uploadFile(url, data = {}, done, fail, onProgress) {
- const form = new FormData()
- // if (file.needTransfer) { //ie和苹果都不支持dataURLtoFile得传送,所以只能用blob
- // form.append("file", common.dataURLtoBlob(file.file), file.name || file.file.name);
- // } else {
- // form.append("file", file.file, file.name || file.file.name);
- // }
- for (let key in data) {
- if (key == "file") {
- form.append("file", data[key], data.filename || data[key].name)
- } else if (key != "filename") {
- form.append(key, data[key])
- }
- }
- return this.postForm(url, form, done, fail, onProgress)
- },
- /**
- * 上传文件
- * @param {String} url 请求地址
- * @param {Object?} data 请求参数 {file:'base64 string',filename:'image.jpg',...}
- * @param {Function?} done 成功回调
- * @param {Function?} fail 失败回调
- */
- uploadBlobFile(url, data = {}, done, fail) {
- const form = new FormData()
- for (let key in data) {
- if (key === "file") {
- form.append("file", base64ToBlob(data.file), data.filename)
- } else if (key != "filename") {
- form.append(key, data[key])
- }
- }
- return this.postForm(url, form, done, fail)
- },
- }
|