当前位置: 首页 > 工具软件 > Gitter > 使用案例 >

Gitter---高颜值GitHub小程序客户端诞生记,2021年安卓社招面试题精选

强德厚
2023-12-01

const langColor = colorNode.length

? colorNode.css(‘background-color’)
null;

const langNode = $repo.find(’[itemprop=programmingLanguage]’);

const lang = langNode.length

? langNode.text().trim()
/* istanbul ignore next */ null;

return omitNil({

author: title.split(’ / ')[0],

name: title.split(’ / ')[1],

url: ${GITHUB_URL}${relativeUrl},

description:

$repo

.find(’.py-1 p’)

.text()

.trim() || /* istanbul ignore next */ ‘’,

language: lang,

languageColor: langColor,

stars: parseInt(

$repo

.find([href="${relativeUrl}/stargazers"])

.text()

.replace(’,’, ‘’) || /* istanbul ignore next */ 0,

10

),

forks: parseInt(

$repo

.find([href="${relativeUrl}/network"])

.text()

.replace(’,’, ‘’) || /* istanbul ignore next */ 0,

10

),

currentPeriodStars: parseInt(

currentPeriodStarsString.split(’ ‘)[0].replace(’,’, ‘’) ||

/* istanbul ignore next */ 0,

10

),

builtBy,

});

})

);

}

复制代码

  • 爬取Trending Developers

async function fetchDevelopers({ language = ‘’, since = ‘daily’ } = {}) {

const data = await fetch(

${GITHUB_URL}/trending/developers/${language}?since=${since}

);

const $ = cheerio.load(await data.text());

return $(’.explore-content li’)

.get()

.map(dev => {

const $dev = $(dev);

const relativeUrl = $dev.find(’.f3 a’).attr(‘href’);

const name = getMatchString(

$dev

.find(’.f3 a span’)

.text()

.trim(),

/^((.+))$/i

);

$dev.find(’.f3 a span’).remove();

const username = $dev

.find(’.f3 a’)

.text()

.trim();

const $repo = $dev.find(’.repo-snipit’);

return omitNil({

username,

name,

url: ${GITHUB_URL}${relativeUrl},

avatar: removeDefaultAvatarSize($dev.find(‘img’).attr(‘src’)),

repo: {

name: $repo

.find(’.repo-snipit-name span.repo’)

.text()

.trim(),

description:

$repo

.find(’.repo-snipit-description’)

.text()

.trim() || /* istanbul ignore next */ ‘’,

url: ${GITHUB_URL}${$repo.attr('href')},

},

});

});

}

复制代码

  • Trending列表云函数

// 云函数入口函数
exports.main = async (event, context) => {
const { type, language, since } = event
let res = null;
let date = new Date()
if (type === ‘repositories’) {
const cacheKey = repositories::${language || 'nolang'}::${since || 'daily'};
const cacheData = await db.collection(‘repositories’).where({
cacheKey: cacheKey
}).orderBy(‘cacheDate’, ‘desc’).get()
if (cacheData.data.length !== 0 &&
((date.getTime() - cacheData.data[0].cacheDate) < 1800 * 1000)) {
res = JSON.parse(cacheData.data[0].content)
} else {
res = await fetchRepositories({ language, since });
await db.collection(‘repositories’).add({
data: {
cacheDate: date.getTime(),
cacheKey: cacheKey,
content: JSON.stringify(res)
}
})
}
} else if (type === ‘developers’) {
const cacheKey = developers::${language || 'nolang'}::${since || 'daily'};
const cacheData = await db.collection(‘developers’).where({
cacheKey: cacheKey
}).orderBy(‘cacheDate’, ‘desc’).get()
if (cacheData.data.length !== 0 &&
((date.getTime() - cacheData.data[0].cacheDate) < 1800 * 1000)) {
res = JSON.parse(cacheData.data[0].content)
} else {
res = await fetchDevelopers({ language, since });
await db.collection(‘developers’).add({
data: {
cacheDate: date.getTime(),
cacheKey: cacheKey,
content: JSON.stringify(res)
}
})
}
}
return {
data: res
}
}
复制代码

Markdown解析

嗯,这是一个大坑。

在做技术调研的时候,发现小程序端Markdown解析主要有以下方案:

wxParse:作者最后一次提交已是两年前了,经过自己的尝试,也确实发现已经不适合如README.md的解析

wemark:一款很优秀的微信小程序Markdown渲染库,但经过笔者尝试之后,发现对README.md的解析并不完美

towxml:目前发现是微信小程序最完美的Markdown渲染库,已经能近乎完美的对README.md进行解析并展示

在Markdown解析这一块,最终采用的也是towxml,但发现在解析性能这一块,目前并不是很优秀,对一些比较大的数据解析也超出了小程序所能承受的范围,还好贴心的作者(sbfkcel)提供了服务端的支持,在此感谢作者的努力!

  • Markdown解析云函数

const Towxml = require(‘towxml’);
const towxml = new Towxml();

// 云函数入口函数
exports.main = async (event, context) => {
const { func, type, content } = event
let res
if (func === ‘parse’) {
if (type === ‘markdown’) {
res = await towxml.toJson(content || ‘’, ‘markdown’);
} else {
res = await towxml.toJson(content || ‘’, ‘html’);
}
}
return {
data: res
}
}
复制代码

  • markdown.js组件

import Taro, { Component } from ‘@tarojs/taro’
import PropTypes from ‘prop-types’
import { View, Text } from ‘@tarojs/components’
import { AtActivityIndicator } from ‘taro-ui’

import ‘./markdown.less’

import Towxml from ‘…/towxml/main’

const render = new Towxml()

export default class Markdown extends Component {
static propTypes = {
md: PropTypes.string,
base: PropTypes.string
}

static defaultProps = {
md: null,
base: null
}

constructor(props) {
super(props)
this.state = {
data: null,
fail: false
}
}

componentDidMount() {
this.parseReadme()
}

parseReadme() {
const { md, base } = this.props
let that = this
wx.cloud.callFunction({
// 要调用的云函数名称
name: ‘parse’,
// 传递给云函数的event参数
data: {
func: ‘parse’,
type: ‘markdown’,
content: md,
}
}).then(res => {
let data = res.result.data
if (base && base.length > 0) {
data = render.initData(data, {base: base, app: this.$scope})
}
that.setState({
fail: false,
data: data
})
}).catch(err => {
console.log(‘cloud’, err)
that.setState({
fail: true
})
})
}

render() {
const { data, fail } = this.state
if (fail) {
return (

load failed, try it again?

)
}
return (

{
data ? (




) : (



)
}

)
}
}
复制代码

Redux

其实,笔者在该项目中,对Redux的使用并不多。一开始,笔者觉得所有的接口请求都应该通过Redux操作,后面才发现,并不是所有的操作都必须使用Redux,最后,在本项目中,只有获取个人信息的时候使用了Redux。

// 获取个人信息
export const getUserInfo = createApiAction(USERINFO, (params) => api.get(’/user’, params))
复制代码

export function createApiAction(actionType, func = () => {}) {
return (
params = {},
callback = { success: () => {}, failed: () => {} },
customActionType = actionType,
) => async (dispatch) => {
try {
dispatch({ type: ${customActionType }_request, params });
const data = await func(params);
dispatch({ type: customActionType, params, payload: data });

callback.success && callback.success({ payload: data })
return data
} catch (e) {
dispatch({ type: ${customActionType }_failure, params, payload: e })

callback.failed && callback.failed({ payload: e })
}
}
}
复制代码

getUserInfo() {
if (hasLogin()) {
userAction.getUserInfo().then(()=>{
Taro.hideLoading()
Taro.stopPullDownRefresh()
})
} else {
Taro.hideLoading()
Taro.stopPullDownRefresh()
}
}

const mapStateToProps = (state, ownProps) => {
return {
userInfo: state.user.userInfo
}
}
export default connect(mapStateToProps)(Index)
复制代码

export default function user (state = INITIAL_STATE, action) {
switch (action.type) {
case USERINFO:
return {
…state,
userInfo: action.payload.data
}
default:
return state
}
}
复制代码

目前,笔者对Redux还是处于一知半解的状态,嗯,学习的路还很长。

四、结语篇

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

Gitter第一个版本通过审核的时候,心情是很激动的,就像自己的孩子一样,看着他一点一点的长大,笔者也很享受这样一个项目从无到有的过程,在此,对那些帮助过笔者的人一并表示感谢。

当然,目前功能和体验上可能有些不大完善,也希望大家能提供一些宝贵的意见,Gitter走向完美的路上希望有你!

最后,希望Gitter小程序能对你有所帮助!

 类似资料: