mtl-js-sdk
Version:
ynf-fw-mtl-api
208 lines (201 loc) • 8 kB
JavaScript
/*
* @Author: wangyingliang@yonyou.com
* @Date: 2024-10-31 09:34:14
* @LastEditors: wangyingliang wangyingliang@yonyou.com
* @LastEditTime: 2024-10-31 09:51:04
* @FilePath: /mtl-api-project/src/platforms/wx/unique.js
* @Description: 微信 || 企微通用函数
* Copyright (c) 2024 by Yonyou, All Rights Reserved.
*/
import proxy from './wx.proxy'
import unique from '../../common/unique.js'
import uuitls from '../../common/uuid.js'
// eslint-disable-next-line no-undef
const SUCCESS_CODE = 200
const FAIL_CODE = 1
const { generateFunc } = proxy
// 微信跳转
function getInfo(obj = {}) {
const { methodName, params = "" } = obj
const _uuid = uuitls(8)
let str = ""
Object.keys(params).forEach((key) => {
str = `${str}&${key}=${params[key]}`
})
const hrefUrl = encodeURIComponent(window.location.href)
const hash = encodeURIComponent(window.location.hash)
const miniPageUrl = "/pages/"
const url = `${miniPageUrl}mtlSystemInfo/mtlSystemInfo?methodName=${methodName}${str}&hash=${hash}&uuid=${_uuid}&href=${hrefUrl}`
window?.wx?.miniProgram.navigateTo({
url: url,
})
function hashchangeFunc() {
let hashs = window.location.hash.split("?")
if (hashs.length <= 1) {
return
}
var { callback: result, uuid } = getUrlParams(window.location.hash)
// 防止其他代码改变hash值影响到当前回调
if (uuid == _uuid) {
// 防止回调参数未取到
try {
result = decodeURIComponent(result)
result = JSON.parse(result || "{}")
} catch (e) {
result = {}
}
var { res } = result
obj.success && obj.success(res)
// 由于小程序改变了页面的hash,需要返回
history.back()
// 移除当前hashchange的监听
window.removeEventListener("hashchange", hashchangeFunc)
} else {
obj.fail && obj.fail({ code: FAIL_CODE, message: "uuid is null" })
}
}
window.addEventListener("hashchange", hashchangeFunc)
}
function getUrlParams(url) {
const paramsRegex = /[?&]+([^=&]+)=([^&]*)/gi
const params = {}
let match
// eslint-disable-next-line no-cond-assign
while ((match = paramsRegex.exec(url))) {
params[match[1]] = match[2]
}
return params
}
const upesn = {
mdfCustomScanQRCode(obj = {}) {
obj["needResult"] = 1
let name = "scanQRCode"
let fn = generateFunc(name)
const originFn = fn
const originSuccess = obj.success
const originCallBack = obj.callback
const originComplete = obj.complete
const success = originSuccess && ((result) => {
let { resultStr } = result
console.log("mdfCustomScanQRCode,", result)
originSuccess({ resultStr, qrString: resultStr })
originCallBack && originCallBack({ resultStr, qrString: resultStr })
})
const complete = originComplete && ((result) => {
if (result.code === SUCCESS_CODE) {
let { resultStr } = result.data
result.data = { resultStr }
}
originComplete(result)
})
originFn({ ...obj, success: success, complete: complete })
},
getAppletCapsuleParams(obj = {}) {
getInfo({ ...obj, methodName: "getAppletCapsuleParams" })
},
getAuthorizationStatus(obj = {}) {
let { type = 0 } = obj
getInfo({
methodName: "getAuthorizationStatus",
success: function (res) {
let authStatus = 0
switch (type) {
case 0:
authStatus = Object.keys(res).includes("scope.writePhotosAlbum") && res["scope.writePhotosAlbum"] ? 1 : 0
break
case 1:
authStatus = Object.keys(res).includes("scope.camera") && res["scope.camera"] ? 1 : 0
break
case 2:
authStatus = Object.keys(res).includes("scope.record") && res["scope.record"] ? 1 : 0
break
case 3:
authStatus = Object.keys(res).includes("scope.userLocation") && res["scope.userLocation"] ? 1 : 0
break
case 4:
authStatus = Object.keys(res).includes("scope.address") && res["scope.address"] ? 1 : 0
break
default:
break
}
obj.success && obj.success({ authStatus })
obj.complete && obj.complete({ data: authStatus, code: SUCCESS_CODE })
},
fail: function (err) {
console.log("======---======", err)
obj.fail && obj.fail(err)
obj.complete && obj.complete({ message: err.message, code: FAIL_CODE })
},
})
},
downloadFile(obj = {}) {
let userAgent = window.navigator.userAgent
if (userAgent.indexOf("wxwork") !== -1) {
obj.fail && obj.fail({ message: "The current platform does not support", code: FAIL_CODE })
return
}
let { header, fileName, autoPreview = 0, url } = obj
if (unique.isEmpty("url", url, obj)) {
return
}
getInfo({
params: { header: JSON.stringify(header), filePath: fileName, autoPreview, url: encodeURIComponent(url) },
methodName: "downloadFile",
success: function (res) {
const { filePath, success = true, message } = res
if (!success) {
obj.fail && obj.fail({ message: message })
obj.complete && obj.complete({ message: message, code: FAIL_CODE })
} else {
obj.success && obj.success({ filePath })
obj.complete && obj.complete({ data: filePath, code: SUCCESS_CODE })
}
},
fail: function (err) {
obj.fail && obj.fail(err)
obj.complete && obj.complete({ message: err.message, code: FAIL_CODE })
},
})
},
uploadFile(obj = {}) {
let userAgent = window.navigator.userAgent
if (userAgent.indexOf("wxwork") !== -1) {
obj.fail && obj.fail({ message: "The current platform does not support", code: FAIL_CODE })
return
}
let { url, filePath, header = {}, formData = {} } = obj
if (unique.isEmpty("url", url, obj) || unique.isEmpty("filePath", filePath, obj)) {
return
}
getInfo({
params: { header: JSON.stringify(header), formData: JSON.stringify(formData), filePath: encodeURIComponent(filePath), url: encodeURIComponent(url) },
methodName: "uploadFile",
success: function (res) {
const { data, success = true, message } = res
if (!success) {
obj.fail && obj.fail({ message: message })
obj.complete && obj.complete({ message: message, code: FAIL_CODE })
} else {
obj.success && obj.success({ data })
obj.complete && obj.complete({ data: data, code: SUCCESS_CODE })
}
},
fail: function (err) {
obj.fail && obj.fail(err)
obj.complete && obj.complete({ message: err.message, code: FAIL_CODE })
},
})
},
}
const uniqueObj = unique.upesn
const arrayUpesn = Object.keys(upesn)
const arrayObj = Object.keys(uniqueObj)
arrayObj.forEach(function (key) {
if (!arrayUpesn.find((element) => element == key)) {
upesn[key] = uniqueObj[key]
}
})
let exports = {
upesn,
}
export default exports