Commit af0b376b by liimin

改rollup打包

1 parent 601704a6
module.exports = {
root: true,
env: {
node: true,
},
parserOptions: {
ecmaVersion: 7,
sourceType: "module",
},
rules: {
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
},
};
This diff could not be displayed because it is too large.
{
"name": "glsx-map-sdk",
"version": "0.0.7",
"version": "0.1.4",
"description": "glsx-map-sdk",
"main": "dist/glsx-map-sdk.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "parcel build src/index.js --out-file dist/glsx-map-sdk.js --no-source-maps --no-cache --detailed-report"
"build": "rollup -c"
},
"author": "liwk",
"license": "ISC",
"devDependencies": {
"parcel-bundler": "^1.12.5"
"rollup": "^1.32.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-eslint": "^7.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.4"
}
}
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import { eslint } from 'rollup-plugin-eslint'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import { uglify } from 'rollup-plugin-uglify'
const packages = require('./package.json')
export default {
input: `src/index.js`,
output: {
file: `dist/${packages.name}.js`,
format: 'umd',
name: packages.name
},
plugins: [
resolve(),
commonjs(),
eslint({
include: ['src/**'],
exclude: ['node_modules/**']
}),
babel({
exclude: 'node_modules/**',
runtimeHelpers: true
}),
replace({
exclude: 'node_modules/**',
ENV: JSON.stringify(process.env.NODE_ENV)
}),
uglify()
]
}
......@@ -4,7 +4,7 @@
* @Author: liweikang
* @Date: 2023-07-04 17:22:43
* @LastEditors: limin
* @LastEditTime: 2023-07-05 09:06:58
* @LastEditTime: 2023-07-05 15:04:01
* @FilePath: \glsx-map-sdk\src\index.js
*/
import Request from './request'
......@@ -22,18 +22,17 @@ class GlMapApi {
*/
async calculateDistance(from, to, type = 1) {
try {
// 使用数组解构和map方法,将from和to参数转换为字符串,并反转数组顺序,然后用逗号连接,得到目的地坐标和出发地坐标
const [destination, origins] = [from, to].map(coord => JSON.stringify(coord).reverse().join(','))
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const { results } = await this.request.get('/v3/distance', {
destination,
origins,
type
})
// 返回一个包含elements属性的对象,值为results
return { elements: results }
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(to)).reverse().join(','),
{ results } = await this.request.get('/v3/distance', {
destination,
origins,
type
})
return {
elements: results
}
} catch (error) {
// 如果发生异常,则抛出错误对象
throw error
}
}
......@@ -46,23 +45,26 @@ class GlMapApi {
*/
async calculateDistanceMatrix(from, toList, type = 1) {
try {
// 使用数组解构和map方法,将from和toList参数转换为字符串,并反转数组顺序,然后用逗号或竖线连接,得到目的地坐标和出发地坐标
const [destination, origins] = [from, toList].map(coord =>
JSON.stringify(coord)
.reverse()
.join(coord === from ? ',' : '|')
)
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const { results } = await this.request.get('/v3/distance', {
destination,
origins,
type
})
// 返回一个包含rows属性的对象,值为一个包含elements属性的对象数组,值为results
return { rows: [{ elements: results }] }
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(toList))
.map(e => (e = e.reverse()))
.join('|'),
{ results } = await this.request.get('/v3/distance', {
destination,
origins,
type
}),
res = {
rows: [
{
elements: results
}
]
}
return Promise.resolve(res)
} catch (error) {
// 如果发生异常,则抛出错误对象
throw error
return Promise.reject(error)
}
}
/**
......@@ -82,17 +84,12 @@ class GlMapApi {
*/
async detailedAdrress(location) {
try {
// 使用数组解构,将location参数分解为纬度和经度
const [lat, lng] = location
// 调用request对象的get方法,传入请求路径为/v3/geocode/regeo,请求数据为坐标字符串(反转数组顺序并用逗号连接),并等待异步结果,并解构响应数据中的regeocode属性
const { regeocode } = await this.request.get('/v3/geocode/regeo', {
location: JSON.stringify(location).reverse().join(',')
location: JSON.parse(JSON.stringify(location)).reverse().join(',')
}),
// 解构regeocode对象中的addressComponent和formatted_address属性
{ addressComponent, formatted_address } = regeocode,
// 拼接省市区乡镇作为地址
address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`,
// 定义一个返回数据对象,包含regeocode对象中的所有属性,以及一些格式化后的地址信息和坐标信息
data = {
...regeocode,
address,
......@@ -128,12 +125,9 @@ class GlMapApi {
lng
}
}
// 返回一个成功的Promise对象,值为返回数据对象
return data
return Promise.resolve(data)
} catch (error) {
// 如果发生异常,则抛出错误对象
throw error
return Promise.reject(error)
}
}
/**
......@@ -155,7 +149,9 @@ class GlMapApi {
*/
translate(locationsList, coordsys = 'autonavi') {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
const [locations] = [locationsList].map(coord => JSON.stringify(coord).reverse().join('|'))
const locations = JSON.parse(JSON.stringify(locationsList))
.map(e => (e = e.reverse()))
.join('|')
// 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果
return this.request.get('/v3/assistant/coordinate/convert', {
locations,
......
......@@ -4,11 +4,11 @@
* @Author: limin
* @Date: 2023-07-04 17:25:31
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:48:04
* @LastEditTime: 2023-07-05 14:50:37
* @FilePath: \glsx-map-sdk\src\request.js
*/
import md5 from 'md5'
import qs from 'qs'
import md5 from './lib/md5'
import qs from './lib/qs'
import { MAP_OPTIONS } from './constants'
import { isMiniprogram } from './helper'
class Request {
......@@ -21,41 +21,35 @@ class Request {
/**对参数进行排序:按参数名升序 */
setLocaleCompare(params) {
// 使用Object.assign和展开运算符,将key添加到参数对象中,并返回一个新的对象
return Object.fromEntries(Object.entries(Object.assign({ key: this.key }, params)).sort(([a], [b]) => a.localeCompare(b)))
// 将key添加到参数对象中
params.key = this.key
return Object.fromEntries(Object.entries(params).sort(([a], [b]) => a.localeCompare(b)))
}
/**签名计算(sig) */
getSign(params) {
return md5(decodeURIComponent(qs.stringify(this.setLocaleCompare(params))) + this.secret)
return md5(`${decodeURIComponent(qs.stringify(this.setLocaleCompare(params)))}${this.secret}`)
}
/**封装的GET方法 */
get(method, data) {
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return this.request('GET', method, data)
}
/**定义一个异步方法,用于发送请求到高德地图API */
/**封装的请求方法 */
async request(RequestMethods, method, data = {}) {
// 调用getSign方法,传入请求数据和请求路径,得到签名
const sig = this.getSign(data, method),
// 将签名添加到请求数据中,并对请求数据进行排序
obj = {
...this.setLocaleCompare(data),
sig
}
// 判断是否是小程序环境
if (isMiniprogram) {
// 显示导航栏加载动画
wx.showNavigationBarLoading()
try {
// 使用Promise封装wx.request方法,返回一个异步结果
const res = await new Promise((resolve, reject) => {
wx.request({
url: `${MAP_OPTIONS.domain}${method}`,
// 设置请求数据
data: obj,
// 设置请求方式
method: RequestMethods,
// 设置请求头,如果是POST请求,则设置content-type为application/json
header:
RequestMethods === 'POST'
? {
......@@ -68,7 +62,6 @@ class Request {
fail: reject
})
})
// 解构响应结果中的状态码,数据和错误信息
const { statusCode, data, errMsg } = res
if (statusCode == '200') {
if (data.status == '1') {
......@@ -94,16 +87,16 @@ class Request {
})
throw error
} finally {
// 最后,隐藏导航栏加载动画
wx.hideNavigationBarLoading()
}
} else {
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
const params = []
Object.keys(obj).map(key => {
params.push(`${key}=${encodeURIComponent(obj[key])}`)
})
try {
// 使用qs.stringify将请求数据转换为查询字符串,并使用encodeURIComponent进行编码,然后拼接到请求地址后面
const url = `${MAP_OPTIONS.domain}${method}?${encodeURIComponent(qs.stringify(obj))}`
// 使用await等待异步结果,并解构响应对象
const res = await window.fetch(url, {
const res = await window.fetch(`${MAP_OPTIONS.domain}${method}?${params.join('&')}`, {
method: RequestMethods
})
const data = await res.json()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!