# @szmg-fe/community

> community app config

Latest version **0.2.4** (published 2022-06-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install @szmg-fe/community
pnpm add @szmg-fe/community
yarn add @szmg-fe/community
bun add @szmg-fe/community
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2022-06-14 |
| First published | 2022-05-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 66.3 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| Author | lemonzhang |
| Maintainers | lemonzhang |
| Keywords | functional, utils, point-free |

## Links

- npm: https://www.npmjs.com/package/@szmg-fe/community
- Repository: https://git.sztv.com.cn/nmt-fe/funba
- npm.io page: https://npm.io/package/@szmg-fe/community

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.27.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Recent versions

- 0.2.4 (latest) — 2022-06-14
- 0.2.3 — 2022-06-14
- 0.2.2 — 2022-05-17
- 0.2.1 — 2022-05-13
- 0.2.0 — 2022-05-11
- 0.1.1 — 2022-05-10
- 0.1.0 — 2022-05-10
- 0.0.15 — 2022-05-09
- 0.0.14 — 2022-05-09
- 0.0.13 — 2022-05-09
- 0.0.12 — 2022-05-09
- 0.0.11 — 2022-05-09
- 0.0.10 — 2022-05-09
- 0.0.9 — 2022-05-09
- 0.0.8 — 2022-05-07
- … 5 more at https://npm.io/package/@szmg-fe/community/versions

## README

# `@szmg-fe/community`

> 单纯通过script插入js的方式在小程序下并不适用，而且多端情况下window环境不一定存在，为了解决多端多业务快速接入community_data.js，并快速获得操作社区配置的能力，@szmg-fe/community提供api，能跨端实现社区对象的获取以及提供大量操作社区数据的方法。大部分api内部都有缓存策略，基于此，react应用中甚至可以不使用hook优化。

## Usage
> @szmg-fe/community诸多api能够work的前提，在于我们的环境中/内部的闭包持有community_data对象，使用api前需要在应用中完成对源数据的引入。
#### 引入community_data
1.web可通过script直接引入，community对象保存在全局的window.allCommunityConfig中
> 注意，勿改写原始的allCommunityConfig结构，避免未知的错误发生🙅
```js
<script src="https://shequ.scms.sztv.com.cn/sqt/community_data.js"></script>
```

2.通过npm包方式引入
> 注意，通过getCommunityDataByJs/getCommunityDataByJsInTaro 都是异步获取的方式，如果需要使用其他的api，需要在then内操作，或者使用await方式

以react应用举例
```js
import getCommunityDataByJs from '@szmg-fe/community/getCommunityDataByJs'

// 在应用入口处调用
function App() {
    useEffect(() => {
        getCommunityDataByJs().then(res => {
            // 这里可以直接拿到res 获取community对象
        })
    }, [])
}
```
以小程序应用-Taro举例
```js
import getCommunityDataByJsInTaro from '@szmg-fe/community/getCommunityDataByJsInTaro'

class App extends Component {
  componentDidMount() {
    getCommunityDataByJsInTaro().then(res => {
        // 这里可以直接拿到res 获取community对象
    }); 
  }

  componentDidShow() {
  }
  ...
}
```


## Api
### getAppCode

**根据社区/街道id，获得对应的appCode**

```js
import getAppCode from '@szmg-fe/community/getAppCode';
const appCode = getAppCode(16248) === 505; // 福新社区id
const appCode = getAppCode(17050) === 605; // 圆岭街道id
const appCode = getAppCode(17193) === 607; // 前海湾花园id
```
### getAreaData

**返回社区内区级数据列表**

```js
import getAreaData from '@szmg-fe/community/getAreaData';
const areaData = getAreaData() === [
    { "code": "440303", "name": "罗湖区" },
    ...
    ..
    .
]; 
```

### getAreaMap

**返回社区内所有区级数据包含的以name&code作为key的散列对象**

```js
import getAreaMap from '@szmg-fe/community/getAreaMap';
const areaMap = getAreaMap() === {
    '440305': {
          name: '南山区',
          streetList: [
              {
                  "id": 17314, 
                  "name": "蛇口街道",
                  ...
              },
              ...
           ]
    },
    '南山区': {
         name: '南山区',
         streetList: [
              {
                  "id": 17314, 
                  "name": "蛇口街道",
                  ...
              },
              ...
           ]
    }
    ...
};
```

### getCommunityDataByJs

**返回社区数据对象，该方法用在web中**
> 用的是异步获取的方式，推荐使用async/await

```js
import getCommunityDataByJs from '@szmg-fe/community/getCommunityDataByJs';
const communityData = getCommunityDataByJs().then(res) === {
    "440303": {
        "name": "罗湖区",
        "streetlist": {
            "qingshuihe": {
                "id": 17301,
                "name": "清水河街道",
                "community": [{
                    "id": 16457,
                    "name": "清水河",
                    ...
                },
                ...
                ]
            },
            ...
        }
	},
    "440304": {
        "name": "福田区",
        "streetlist": {
            "lianhua": {
                "name": "莲花街道",
                "appcode": 505,
                "isTemplate": false,
                "community": [{
                    "id": 16248,
                    "name": "福新",
                    ...
                },
                ...
                ]
            ...
            }
        },
    };
    ...
};
```

### getCommunityDataByJsInTaro

**返回社区数据对象，该方法用在Taro小程序中**
> 用的是taro request 异步获取的方式，推荐使用async/await

```js
import getCommunityDataByJsInTaro from '@szmg-fe/community/getCommunityDataByJsInTaro';
const communityData = getCommunityDataByJsInTaro().then(res) === {
    "440303": {
        "name": "罗湖区",
        "streetlist": {
            "qingshuihe": {
                "id": 17301,
                "name": "清水河街道",
                "community": [{
                    "id": 16457,
                    "name": "清水河",
                    ...
                },
                ...
                ]
            },
            ...
        }
	},
    "440304": {
        "name": "福田区",
        "streetlist": {
            "lianhua": {
                "name": "莲花街道",
                "appcode": 505,
                "isTemplate": false,
                "community": [{
                    "id": 16248,
                    "name": "福新",
                    ...
                },
                ...
                ]
            ...
            }
        },
    };
    ...
};
```

### getCommunityInfoById

**根据街道/社区id 返回街道/社区信息**

```js
import getCommunityInfoById from "@szmg-fe/community/getCommunityInfoById";
const communityMap = getCommunityInfoById(16457) === {
    "id": 16457, 
    "name": "清水河", 
    "ownStreet": {
        "appCode": 509, 
        "area": "罗湖区", 
        ...
        "areaInfo": {
            "name": "罗湖区", 
            "streetList": [
                {
                    "appcode": 509, 
                    "community": [
                        {
                            "id": 16457, 
                            "name": "清水河", 
                            ...
                        }, 
                        ...
                    ]
                },
            ]
        },
    },
    ...
}; // 清水河街道
```

### getCommunityLink

**根据街道/社区id 获取对应的地址url**

```js
import getCommunityLink from '@szmg-fe/community/getCommunityLink';
const url = getCommunityLink(17193) === 'https://www.sztv.com.cn/h5/yqzs_qianhai/'; // 前海湾街道id
const url = getCommunityLink(16248) === 'https://www.sztv.com.cn/h5/yqzs_index/v1?community=16248'; // 福新社区id
const url = getCommunityLink(17050) === 'https://www.sztv.com.cn/h5/yqzs_index/v4?community=17050'; // 圆岭街道id
const url = getCommunityLink(17551) === 'https://www.sztv.com.cn/h5/yqzs_index/v3?community=17551'; // 南湖街道id
```

### getCommunityMap

**返回所有社区的以id/name作为key的map**

```js
import getCommunityInMap from "@szmg-fe/community/getCommunityMap";
const communityInMap = getCommunityInMap() === {
    "福新": {
        ....
    },
    16248: {
        "id": 16248, 
        "name": "福新", 
        "ownStreet": {
            "appCode": 505, 
            "area": "福田区", 
            "name": "莲花街道"
            "areaInfo": {
                "name": "福田区", 
                "streetList": [
                    {
                        "appcode": 505, 
                        "community": [
                            {
                                "id": 16254, 
                                "link": "https://www.sztv.com.cn/h5/yqzs/?community=16254", 
                                "name": "福中", 
                                "ownStreet": [
                                    ...
                                ]
                            }, 
                            ...
                        ]
                    }
                    ...
                ]
            }
        }
    }
    ...
}
```

### getCommunityRefineMap

**获取社区精细化的map，支持按key/name获取map**

```js
import getCommunityRefineMap from "@szmg-fe/community/getCommunityRefineMap";
const map = getCommunityRefineMap('id') === {
    16248: {
        "id": 16248, 
        "name": "福新", 
        "ownStreet": {...}
    },
    16783: {
        ...
    }
}
const map = getCommunityRefineMap('name') === {
    "福新": {
         "id": 16248, 
        "name": "福新", 
        "ownStreet": {...}
    },
    "上沙": {
        ...
    }
}
```

### getStreetMap

**返回社区对象内的街道对象，以id/name/code为key的map'**

```js
import getStreetMap from '@szmg-fe/community/getStreetMap'
const streetMapKeyArr = getStreetMap() === {
    "海山街道": {
        ...
    },
    "haishan": {
        ...
    },
    16338: {
        "appCode": 506,
        "area": "盐田区",
        "community": [],
        "id": 16338,
        "link": "https://www.sztv.com.cn/h5/yqzs_hs_v2",
        "name": "海山街道",
        "areaInfo": {
            "name": "盐田区",
            "streetList": [
                {
                    "appcode": 506,
                    "community": [],
                    "name": "海山街道",
                    ...
                },
            ]
        },
    },
    ...
};
```

### getStreetRefineMap

**获取街道精细化的map，支持按id/code/name获取map**

```js
import getStreetRefineMap from "@szmg-fe/community/getStreetRefineMap";
const map = getStreetRefineMap('id') === {
    16338: {
        name: '海山街道',
        ...
    },
     17653: {
        name: '南湖街道',
        ...
    },
}; 
const map = getStreetRefineMap('name') === {
    "海山街道": {
        ...
    },
    "南湖街道": {
        ...
    }
}
const map = getStreetRefineMap('code') === {
    "haishan": {
        ...
    },
    "nanhu": {
        ...
    }
}
```


### map2Array

**将map 转化为数组**
```js
import map2Array from "@szmg-fe/community/map2Array";
import getStreetRefineMap from "@szmg-fe/community/getStreetRefineMap";

const map = getStreetRefineMap('id')
const list = map2Array(map) === [
      16338: {
        name: '海山街道',
        ...
    },
     17653: {
        name: '南湖街道',
        ...
    },
]
```

---
_Source: https://npm.io/package/@szmg-fe/community · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
