Skip to content

Commit

Permalink
feat: 🎸 增加 cdn, go 文件
Browse files Browse the repository at this point in the history
  • Loading branch information
shaodahong committed Jan 23, 2019
1 parent e6102fa commit 311c0be
Show file tree
Hide file tree
Showing 18 changed files with 3,018 additions and 114 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# AUTO-libs
# AUTO-libs 凹凸公用库

凹凸公用库
## 公用样式和脚本引入

```js
// reset 样式
import 'auto-libs/build/styles/reset.css'

// flexible 布局脚本
import 'auto-libs/build/scripts/flexible.js'
```

## http

Expand Down
4 changes: 2 additions & 2 deletions lib/as.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface ASData {

export async function AS(data: ASData = {}) {
if (typeof data.eventContent === 'object') {
let params: string[] = []
;(<any>Object)
let params: string[] = [];
( Object as any)
.entries(data.eventContent)
.forEach((key: string, value: any) => {
params.push(`${key}=${value}`)
Expand Down
4 changes: 4 additions & 0 deletions lib/cdn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const cdn =
process.env.PACKAGE === 'production'
? '//carphoto.atzuche.com/'
: '//at-images-test.oss-cn-hangzhou.aliyuncs.com/'
45 changes: 45 additions & 0 deletions lib/go.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import qs from 'qs'

export interface AddressParams {
redirectUrl: string
redirectParam: string
locationInfo?: {
address?: string;
name?: string;
code?: string;
lat?: string;
ng?: string;
telPrefix?: string;
}
telPrefix?: string
cityCode?: string
keyword?: string
servicePoint?: 1 | 0
searchTip?: string
selectCity?: 1 | 0
selectCityTip?: string
isOnDoor?: 1 | 0
}

export interface PayParmas {
token: string
orderNo: string
payKind: string
redirect_url: string
}
const go = {
address: (params: AddressParams) => {
window.location.href = `/m/address/?${qs.stringify(params)}`
},

pay: (params: PayParmas) => {
const { token, orderNo, payKind, redirect_url } = params

const baseurl =
process.env.ENV_NAME === 'production' ? 'https://m.atzuche.com' : ''

window.location.href = `${baseurl}/m/pay/?orderNo=${orderNo}&payKind=${payKind}&token=${token}&redirect_url=${redirect_url}`
}
}

export default go
15 changes: 7 additions & 8 deletions lib/http.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import axios from 'axios'
import { clearToken, toLogin, getToken } from './token'
import { clearToken, getToken, toLogin } from './token'

type HttpConfig = {
interface HttpConfig {
resCode?: string
resMsg?: string
data?: any
}

export interface ProcessEnv {
[key: string]: string | undefined
}

class HttpError extends Error {
msg: string
name: string = 'HttpError'
data: any
code?: string = '0'
constructor(message: string, data?: HttpConfig) {
public msg: string
public name = 'HttpError'
public data: any
public code ? = '0'
public constructor(message: string, data?: HttpConfig) {
super(message)

this.msg = message
Expand Down
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/// <reference path="../types/custom.d.ts" />

export * from './as'
export * from './wx'
export * from './http'
export * from './token'
export * from './cdn'
export { default as go } from './go'
136 changes: 136 additions & 0 deletions lib/scripts/flexible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* eslint-disable */
;(function(window, lib) {
var tid
var document = window.document
var documentEle = document.documentElement
var metaEl = document.querySelector('meta[name="viewport"]')
var flexibleMeta = document.querySelector('meta[name="flexible"]')
var dpr = 0
var scale = 0
var flexible = lib.flexible || (lib.flexible = {})

if (metaEl) {
console.warn('将根据已有的meta标签来设置缩放比例')
const match = metaEl
.getAttribute('content')
.match(/initial\-scale=([\d\.]+)/)
match && ((scale = parseFloat(match[1])), (dpr = parseInt(1 / scale)))
} else {
if (flexibleMeta) {
var content = flexibleMeta.getAttribute('content')
if (content) {
const initialDpr = content.match(/initial\-dpr=([\d\.]+)/)
const maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/)
initialDpr &&
((dpr = parseFloat(initialDpr[1])),
(scale = parseFloat((1 / dpr).toFixed(2))))
maximumDpr &&
((dpr = parseFloat(maximumDpr[1])),
(scale = parseFloat((1 / dpr).toFixed(2))))
}
}
}
if (!dpr && !scale) {
var userAgent = window.navigator.userAgent,
isIPhone = !!userAgent.match(/iphone/gi),
devicePixelRatio = window.devicePixelRatio
dpr = isIPhone
? devicePixelRatio >= 3 && (!dpr || dpr >= 3)
? 3
: devicePixelRatio >= 2 && (!dpr || dpr >= 2)
? 2
: 1
: 1
scale = 1 / dpr
}
if ((documentEle.setAttribute('data-dpr', dpr), !metaEl)) {
if (
((metaEl = document.createElement('meta')),
metaEl.setAttribute('name', 'viewport'),
metaEl.setAttribute(
'content',
'initial-scale=' +
scale +
', maximum-scale=' +
scale +
', minimum-scale=' +
scale +
', user-scalable=no, viewport-fit=cover'
),
documentEle.firstElementChild)
) {
documentEle.firstElementChild.appendChild(metaEl)
} else {
var wrap = document.createElement('div')
wrap.appendChild(metaEl), document.write(wrap.innerHTML)
}
}

function refreshRem() {
var width = documentEle.getBoundingClientRect().width
var d = ((width / (375 * dpr)) * 100 * dpr) / 2
d -= (d - 50 * dpr) / 2
d = Math.round(d)
documentEle.style.fontSize = d + 'px'
flexible.rem = window.rem = d
window.rem = d
}

window.addEventListener(
'resize',
function() {
clearTimeout(tid), (tid = setTimeout(refreshRem, 300))
},
false
)
window.addEventListener(
'pageshow',
function(event) {
event.persisted &&
(clearTimeout(tid), (tid = setTimeout(refreshRem, 300)))
},
false
)
refreshRem()
flexible.dpr = window.dpr = dpr
flexible.refreshRem = refreshRem
flexible.rem2px = function(d) {
var val = parseFloat(d) * this.rem
return typeof d === 'string' && d.match(/rem$/) && (val += 'px'), val
}
flexible.px2rem = function(d) {
var val = parseFloat(d) / this.rem
return typeof d === 'string' && d.match(/px$/) && (val += 'rem'), val
}
window.dpr = dpr

var ua = navigator.userAgent
window.isApp = /atzuche/gi.test(ua)
window.isWX = /MicroMessenger/gi.test(ua)

if (window.isWX) {
var search = window.location.search,
ls = window.localStorage,
ss = window.sessionStorage
var token = (/[?&]token=(.*?)(&|$)/.exec(search) || [])[1]
if (token) {
ls.setItem('_app_token_', token)
}
var openId = (/[?&]openId=(.*?)(&|$)/.exec(search) || [])[1]
if (openId) {
ss.setItem('_app_openId_', openId)
}
var unionId = (/[?&]unionId=(.*?)(&|$)/.exec(search) || [])[1]
if (unionId) {
ss.setItem('_app_unionId_', unionId)
}
var virtualNo = (/[?&]virtualNo=(.*?)(&|$)/.exec(search) || [])[1]
if (virtualNo) {
ss.setItem('_app_virtualNo_', virtualNo)
}
var memNo = (/[?&]memNo=(.*?)(&|$)/.exec(search) || [])[1]
if (memNo) {
ss.setItem('_app_memNo_', memNo)
}
}
})(window, window['lib'] || (window['lib'] = {}))
File renamed without changes.
12 changes: 4 additions & 8 deletions lib/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qs from 'qs'
/* tslint:disable:no-magic-numbers */
import at from 'at-js-sdk'
import qs from 'qs'

const token = '_app_token_'
const openId = '_app_openId_'
Expand Down Expand Up @@ -35,13 +36,9 @@ const getMemNo = () => ss.getItem(memNo)
const setMemNo = (e: string) => ss.setItem(memNo, e)
const clearMemNo = () => ss.removeItem(memNo)

/**
* 初始化token
* @returns {Promise<any>} resolve: 成功 reject: 失败
*/
const initToken = async () => {
return new Promise((resolve, reject) => {
if ((<any>window).isApp) {
if ((window as any).isApp) {
at.getToken({
callback(result: { token: string }) {
if (result.token && String(result.token).length > 20) {
Expand Down Expand Up @@ -71,7 +68,7 @@ const initToken = async () => {
* h5: 跳转到通用登录页面
*/
const toLogin = (params?: object) => {
if ((<any>window).isApp) {
if ((window as any).isApp) {
at.openLogin({
success(res: any) {
setToken(res.token)
Expand All @@ -87,7 +84,6 @@ const toLogin = (params?: object) => {
const search = {
redirect: window.location.href
}
params && Object.assign(search, params)
window.location.href = '/m/login/?' + qs.stringify(search)
}
}
Expand Down
Loading

0 comments on commit 311c0be

Please sign in to comment.