🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 插件使用 终于有时间更新插件使用相关的文章了,这里首先给大家介绍一个**内容替换模块**中使用的手机号脱敏插件 # 插件编写 1. 先看这个手机号脱敏插件的源代码 ``` local sub = string.sub local gsub = ngx.re.gsub local gmatch = ngx.re.gmatch local re = "(13[0-9]|14[5,7]|15[0-3,5-9]|17[0,3,5-8]|18[0-9]|166|198|199|147)\\d{8}" local TB = {} local tag = false local function Teltoasterisk(_tel) -- 中间 4 位 return sub(_tel,1,4).."****"..sub(_tel,9) end local function insert_Tb(Stel) if not TB[Stel] then TB[Stel] = Teltoasterisk(Stel) end end local function Telgmatch(tel_str) local it, err = gmatch(tel_str, re, "jo") if not it then return end while true do local m,err = it() if err then return end if not m then return end tag = true insert_Tb(m[0]) end end local _M = { _VERSION = "0.01" } local function tel_ast(str_all , _tb_args) Telgmatch(str_all) if tag then for k,v in pairs(TB) do str_all = gsub(str_all,k,v) end end return str_all end _M.tel_ast = tel_ast return _M ``` 代码编写的非常好理解,当然了还有写法不规范的地方,如果说您可以调用C代码编写的模块,其工作的效率应该是最高的。(希望有写好C模块的同学可以分享一下so文件或者源代码) 2. 性能测试测试 用于性能测试的代码如下 ``` local optl = require("optl") local str = [=[xxxxx]=] local re = "(13[0-9]|14[5,7]|15[0-3,5-9]|17[0,3,5-8]|18[0-9]|166|198|199|147)\\d{8}" local TB = {} local tag = false local function Teltoasterisk(_tel) -- 中间 4 位 return string.sub(_tel,1,4).."****"..string.sub(_tel,9) end local function insert_Tb(Stel) if not TB[Stel] then TB[Stel] = Teltoasterisk(Stel) end end local function Telgmatch(tel_str) local it, err = ngx.re.gmatch(tel_str, re, "jo") if not it then return end while true do local m,err = it() if err then return end if not m then return end tag = true insert_Tb(m[0]) end end local function Telngxfind(tel_str) local f,t = ngx.re.find(tel_str,re,"jo") if f then tag = true local tmptel = string.sub(tel_str,f,t) insert_Tb(string.sub(tel_str,f,t)) local tmp_str = string.sub(tel_str,t) Telngxfind(tmp_str) end end local function Telmatch(tel_str) local m,e = ngx.re.match(tel_str,re) if m then tag = true insert_Tb(m[0]) local f,t = string.find(tel_str,m[0],1,true) local tmp_str = string.sub(tel_str, t) Telmatch(tmp_str) end end local startTime = os.clock() local num = tonumber(optl.get_paramByName("num")) or 10000 local fuc = optl.get_paramByName("fuc") local T_fuc = {} T_fuc.ngxfind = Telngxfind T_fuc.match = Telmatch T_fuc.gmatch = Telgmatch f = T_fuc[fuc] or Telngxfind for i=1,num do f(str) end if tag then for k,v in pairs(TB) do str = ngx.re.gsub(str,k,v) end end local msg = string.format(">> This function cost: %.4f ms", os.clock()-startTime) optl.sayHtml_ext({code="ok",msg=msg,len=#str,tb=TB,tag=tag}) ``` 为了和真实的环境接近,找了一篇最佳实践的页面,在内容里面添加了一些手机号码,其分布跨度也是比较大的。 该页面内容字符串长度:70964 ![](https://i.vgy.me/mxhVnu.png) 大家也可以对比一下,使用这个 ngx.re.gmatch 效率最高,当然了如果有C模块的更好了。 完整代码 分享在[百度网盘 ](https://pan.baidu.com/s/1zIiRhw2zTAA5AMcc6XbF-A) # replace_Mod --- 插件编写 阅读到这里就想着怎么编写一个自己的插件,那就接着往下看吧。 先看一个最简单的 内容替换插件 ``` local _M = { _VERSION = "0.01" } local function replace_hello(str_all , _tb_args) -- 这里可以使用函数完成你想要的替换结果 return ngx.re.gsub(str_all, "hello", "HELLO") end _M.replace_hello = replace_hello return _M ``` 首先需要返回一个table,让后给这个 table 添加一个方法。 其次这个方法需要接收 2 个参数 str_all :这个是需要替换的内容(也就是 整个 Response ,该内容是 openstar 获取传递过来的) _tb_args:调用参数(使得插件的使用更加灵活,ps 传递的就是一个 table ,这里不需要序列化的,减少性能的损耗) 接下来就是编写你想要的逻辑和内容了,这里和普通编写lua脚本的一样的,可以调用其他人的lua模块,还有调用c代码等等...,当然不要把自己堵塞了!!!