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", "name": "glsx-map-sdk",
"version": "0.0.7", "version": "0.1.4",
"description": "glsx-map-sdk", "description": "glsx-map-sdk",
"main": "dist/glsx-map-sdk.js", "main": "src/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "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", "author": "liwk",
"license": "ISC", "license": "ISC",
"devDependencies": { "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 @@ ...@@ -4,7 +4,7 @@
* @Author: liweikang * @Author: liweikang
* @Date: 2023-07-04 17:22:43 * @Date: 2023-07-04 17:22:43
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2023-07-05 09:06:58 * @LastEditTime: 2023-07-05 15:04:01
* @FilePath: \glsx-map-sdk\src\index.js * @FilePath: \glsx-map-sdk\src\index.js
*/ */
import Request from './request' import Request from './request'
...@@ -22,18 +22,17 @@ class GlMapApi { ...@@ -22,18 +22,17 @@ class GlMapApi {
*/ */
async calculateDistance(from, to, type = 1) { async calculateDistance(from, to, type = 1) {
try { try {
// 使用数组解构和map方法,将from和to参数转换为字符串,并反转数组顺序,然后用逗号连接,得到目的地坐标和出发地坐标 const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
const [destination, origins] = [from, to].map(coord => JSON.stringify(coord).reverse().join(',')) origins = JSON.parse(JSON.stringify(to)).reverse().join(','),
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性 { results } = await this.request.get('/v3/distance', {
const { results } = await this.request.get('/v3/distance', { destination,
destination, origins,
origins, type
type })
}) return {
// 返回一个包含elements属性的对象,值为results elements: results
return { elements: results } }
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象
throw error throw error
} }
} }
...@@ -46,23 +45,26 @@ class GlMapApi { ...@@ -46,23 +45,26 @@ class GlMapApi {
*/ */
async calculateDistanceMatrix(from, toList, type = 1) { async calculateDistanceMatrix(from, toList, type = 1) {
try { try {
// 使用数组解构和map方法,将from和toList参数转换为字符串,并反转数组顺序,然后用逗号或竖线连接,得到目的地坐标和出发地坐标 const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
const [destination, origins] = [from, toList].map(coord => origins = JSON.parse(JSON.stringify(toList))
JSON.stringify(coord) .map(e => (e = e.reverse()))
.reverse() .join('|'),
.join(coord === from ? ',' : '|') { results } = await this.request.get('/v3/distance', {
) destination,
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性 origins,
const { results } = await this.request.get('/v3/distance', { type
destination, }),
origins, res = {
type rows: [
}) {
// 返回一个包含rows属性的对象,值为一个包含elements属性的对象数组,值为results elements: results
return { rows: [{ elements: results }] } }
]
}
return Promise.resolve(res)
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象 return Promise.reject(error)
throw error
} }
} }
/** /**
...@@ -82,17 +84,12 @@ class GlMapApi { ...@@ -82,17 +84,12 @@ class GlMapApi {
*/ */
async detailedAdrress(location) { async detailedAdrress(location) {
try { try {
// 使用数组解构,将location参数分解为纬度和经度
const [lat, lng] = location const [lat, lng] = location
// 调用request对象的get方法,传入请求路径为/v3/geocode/regeo,请求数据为坐标字符串(反转数组顺序并用逗号连接),并等待异步结果,并解构响应数据中的regeocode属性
const { regeocode } = await this.request.get('/v3/geocode/regeo', { 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, { addressComponent, formatted_address } = regeocode,
// 拼接省市区乡镇作为地址
address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`, address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`,
// 定义一个返回数据对象,包含regeocode对象中的所有属性,以及一些格式化后的地址信息和坐标信息
data = { data = {
...regeocode, ...regeocode,
address, address,
...@@ -128,12 +125,9 @@ class GlMapApi { ...@@ -128,12 +125,9 @@ class GlMapApi {
lng lng
} }
} }
return Promise.resolve(data)
// 返回一个成功的Promise对象,值为返回数据对象
return data
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象 return Promise.reject(error)
throw error
} }
} }
/** /**
...@@ -155,7 +149,9 @@ class GlMapApi { ...@@ -155,7 +149,9 @@ class GlMapApi {
*/ */
translate(locationsList, coordsys = 'autonavi') { translate(locationsList, coordsys = 'autonavi') {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串 // 使用数组解构和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),并返回结果 // 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果
return this.request.get('/v3/assistant/coordinate/convert', { return this.request.get('/v3/assistant/coordinate/convert', {
locations, locations,
......
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
* @Author: limin * @Author: limin
* @Date: 2023-07-04 17:25:31 * @Date: 2023-07-04 17:25:31
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2023-07-05 08:48:04 * @LastEditTime: 2023-07-05 14:50:37
* @FilePath: \glsx-map-sdk\src\request.js * @FilePath: \glsx-map-sdk\src\request.js
*/ */
import md5 from 'md5' import md5 from './lib/md5'
import qs from 'qs' import qs from './lib/qs'
import { MAP_OPTIONS } from './constants' import { MAP_OPTIONS } from './constants'
import { isMiniprogram } from './helper' import { isMiniprogram } from './helper'
class Request { class Request {
...@@ -21,41 +21,35 @@ class Request { ...@@ -21,41 +21,35 @@ class Request {
/**对参数进行排序:按参数名升序 */ /**对参数进行排序:按参数名升序 */
setLocaleCompare(params) { setLocaleCompare(params) {
// 使用Object.assign和展开运算符,将key添加到参数对象中,并返回一个新的对象 // 将key添加到参数对象中
return Object.fromEntries(Object.entries(Object.assign({ key: this.key }, params)).sort(([a], [b]) => a.localeCompare(b))) params.key = this.key
return Object.fromEntries(Object.entries(params).sort(([a], [b]) => a.localeCompare(b)))
} }
/**签名计算(sig) */ /**签名计算(sig) */
getSign(params) { getSign(params) {
return md5(decodeURIComponent(qs.stringify(this.setLocaleCompare(params))) + this.secret) return md5(`${decodeURIComponent(qs.stringify(this.setLocaleCompare(params)))}${this.secret}`)
} }
/**封装的GET方法 */ /**封装的GET方法 */
get(method, data) { get(method, data) {
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data // 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return this.request('GET', method, data) return this.request('GET', method, data)
} }
/**定义一个异步方法,用于发送请求到高德地图API */ /**封装的请求方法 */
async request(RequestMethods, method, data = {}) { async request(RequestMethods, method, data = {}) {
// 调用getSign方法,传入请求数据和请求路径,得到签名 // 调用getSign方法,传入请求数据和请求路径,得到签名
const sig = this.getSign(data, method), const sig = this.getSign(data, method),
// 将签名添加到请求数据中,并对请求数据进行排序
obj = { obj = {
...this.setLocaleCompare(data), ...this.setLocaleCompare(data),
sig sig
} }
// 判断是否是小程序环境
if (isMiniprogram) { if (isMiniprogram) {
// 显示导航栏加载动画
wx.showNavigationBarLoading() wx.showNavigationBarLoading()
try { try {
// 使用Promise封装wx.request方法,返回一个异步结果
const res = await new Promise((resolve, reject) => { const res = await new Promise((resolve, reject) => {
wx.request({ wx.request({
url: `${MAP_OPTIONS.domain}${method}`, url: `${MAP_OPTIONS.domain}${method}`,
// 设置请求数据
data: obj, data: obj,
// 设置请求方式
method: RequestMethods, method: RequestMethods,
// 设置请求头,如果是POST请求,则设置content-type为application/json
header: header:
RequestMethods === 'POST' RequestMethods === 'POST'
? { ? {
...@@ -68,7 +62,6 @@ class Request { ...@@ -68,7 +62,6 @@ class Request {
fail: reject fail: reject
}) })
}) })
// 解构响应结果中的状态码,数据和错误信息
const { statusCode, data, errMsg } = res const { statusCode, data, errMsg } = res
if (statusCode == '200') { if (statusCode == '200') {
if (data.status == '1') { if (data.status == '1') {
...@@ -94,16 +87,16 @@ class Request { ...@@ -94,16 +87,16 @@ class Request {
}) })
throw error throw error
} finally { } finally {
// 最后,隐藏导航栏加载动画
wx.hideNavigationBarLoading() wx.hideNavigationBarLoading()
} }
} else { } else {
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式 // 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
const params = []
Object.keys(obj).map(key => {
params.push(`${key}=${encodeURIComponent(obj[key])}`)
})
try { try {
// 使用qs.stringify将请求数据转换为查询字符串,并使用encodeURIComponent进行编码,然后拼接到请求地址后面 const res = await window.fetch(`${MAP_OPTIONS.domain}${method}?${params.join('&')}`, {
const url = `${MAP_OPTIONS.domain}${method}?${encodeURIComponent(qs.stringify(obj))}`
// 使用await等待异步结果,并解构响应对象
const res = await window.fetch(url, {
method: RequestMethods method: RequestMethods
}) })
const data = await res.json() 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!