-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directive): 开启wxa指令功能,新增wxa:mock指令
- Loading branch information
Showing
21 changed files
with
17,845 additions
and
17,724 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,4 @@ | |
"publishConfig": { | ||
"access": "public" | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mock from './mock/index'; | ||
// import anotherDirective from './anotherDirective'; | ||
// ... | ||
|
||
export default directive; | ||
|
||
const directiveHandlerList = { | ||
mock | ||
// anotherDirective | ||
// ... | ||
} | ||
|
||
function directive(drc, element, mdl){ | ||
let drcName = drc.name; | ||
if(drcName && directiveHandlerList[drcName]) { | ||
directiveHandlerList[drcName](drc, element, mdl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
# wxa:mock | ||
为input自动填入响应字符,避免开发时重复花时间手动输入 | ||
*仅开发时启用(NODE_ENV为空或==='development')* | ||
指令格式1:`wxa:mock="占位符"` 或 `wxa:mock="占位符(参数[,参数])"` | ||
*/ | ||
|
||
import Mock from 'mockjs'; | ||
import wxaMockExtends from './mock-extends'; | ||
import wxaMock from './wxa-mock'; | ||
|
||
const Random = Mock.Random; | ||
Random.extend(wxaMockExtends(Random)); | ||
|
||
export default mock; | ||
|
||
function mock(drc, element, mdl){ | ||
// drc: {name, value} | ||
if(isDev()) { | ||
let targetList = findMockTarget(element); | ||
targetList.forEach(target => { | ||
let mockResult = getMockResult(drc); | ||
target.attribs.value = mockResult; | ||
setWarningStyle(target); | ||
}) | ||
} | ||
} | ||
|
||
function getMockResult(drc) { | ||
let mockResult = ''; | ||
let drcValuePrefix = drc.value[0]; | ||
|
||
if(drcValuePrefix === '$') { | ||
// wxa提供 | ||
mockResult = wxaMock.mock(drc.value); | ||
}else { | ||
// mock.js | ||
mockResult = Mock.mock(drc.value); | ||
} | ||
|
||
return mockResult; | ||
} | ||
|
||
function findMockTarget(el) { | ||
let targetList = []; | ||
let tagName = el.name; | ||
if(tagName === 'input') { | ||
targetList.push(el); | ||
}else { | ||
if(el.children && el.children.length) { | ||
el.children.forEach(child => { | ||
targetList = targetList.concat(findMockTarget(child)); | ||
}) | ||
} | ||
} | ||
return targetList; | ||
} | ||
|
||
|
||
|
||
// 设置警示样式,避免mock信息上生产环境 | ||
function setWarningStyle(el){ | ||
let originStyle = el.attribs.style || ''; | ||
el.attribs.style = originStyle + '; outline: 1px dashed rgba(255,0,0,0.2); text-shadow: #FC0 1px 0 2px !important;' | ||
} | ||
// 判断开发环境,避免mock信息上生产环境 | ||
function isDev() { | ||
let env = process.env.NODE_ENV; | ||
let isDevEnv = Boolean(~[undefined, 'development'].indexOf(env)); | ||
return isDevEnv; | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/wxa-cli/src/resolvers/directive/mock/mock-extends.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// wxa自定义拓展的mock规则 | ||
|
||
export default function extend(random) { | ||
return { | ||
// 身份证号 | ||
idNo, | ||
// 星座 | ||
constellation | ||
} | ||
} | ||
|
||
function idNo(oldest, youngest) { | ||
return getIDCardNo(this, oldest, youngest); | ||
} | ||
|
||
function constellation(rule) { | ||
let constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'] | ||
return this.pick(constellations) | ||
} | ||
|
||
|
||
// 生成身份证号 | ||
function getIDCardNo(mock, oldest, youngest) { | ||
let coefficientArray = [ "7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"];// 加权因子 | ||
let lastNumberArray = [ "1","0","X","9","8","7","6","5","4","3","2"];// 校验码 | ||
let address = "420101"; // 住址 | ||
let oldestYear = oldest ? oldest : 1900; | ||
let youngestYear = youngest ? youngest : 2019; | ||
// let year = getRandom(youngestYear, oldestYear); | ||
let year = mock.natural(youngestYear, oldestYear); | ||
let month = paddingZero(mock.natural(1, 12)); | ||
let date = paddingZero(mock.natural(1, 28)); | ||
let birthday = `${year}${month}${date}`; // 生日 | ||
|
||
let s = Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString(); | ||
let array = (address + birthday + s).split(""); | ||
let total = 0; | ||
for(let i in array){ | ||
total = total + parseInt(array[i])*parseInt(coefficientArray[i]); | ||
} | ||
let lastNumber = lastNumberArray[parseInt(total%11)]; | ||
let id_no_String = address + birthday + s + lastNumber; | ||
return id_no_String; | ||
} | ||
|
||
|
||
function paddingZero(num) { | ||
let number = num + ''; | ||
let res = (Number(num) > 9) ? number : 0 + number; | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// wxa自定义的mock规则 | ||
|
||
export default { | ||
mock | ||
} | ||
|
||
function mock(rule) { | ||
let ruleValue = rule.substr(1); | ||
// TODO | ||
return ''; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.