lua demo
ngx.header.content_type = "text/plain"
local cjson = require "cjson";
local funova_libs = require "funova_libs";
local Code_Result =
{
success = 1, --礼包领取成功
not_exists = 2, --礼包码无效
used = 3, --礼包码已被使用
channel_not_match = 4, --礼包码无效
channel_used = 5, --已经领取过此类礼包
channel_error = 6, --礼包码无效
error = 7 --兑换失败
}
redis_instance = funova_libs:redis_connect();
function UpdateCode(param)
local result = {}
if redis_instance then
local pid = param['pid'];
local code = param['code'];
local channel = param['ch'];
--ngx.say("pid:"..pid .. " code:"..code .. " ch:"..channel)
if pid and code and channel then
local usepid = redis_instance:hget("code_hash",code)
result["statu"] = 0;
if usepid == ngx.null then
result["msg"] = "Code not exists!";
result["statu"] = Code_Result.not_exists
elseif usepid ~= "0" then
result["msg"] = "Code has been used!";
result["statu"] = Code_Result.used
else
local code_type_ch = string.sub(code, 1,6)
local code_ch = string.sub(code, 4,6)
local ch = redis_instance:hget("channel_hash", code_ch)
if ch == ngx.null then
result["msg"] = "Code not exists, channel error";
result["statu"] = Code_Result.channel_error
elseif ch ~= channel then
result["msg"] = "Code can not use on this channel : "..ch;
result["statu"] = Code_Result.channel_not_match
else
local pid_code_type = pid.."|"..code_type_ch
local usecnt = redis_instance:hget("pid_use_code_hash",pid_code_type)
if usecnt == ngx.null then --默认 一种类型的激活码只能使用一次
redis_instance:hset("code_hash", code, pid)
redis_instance:hincrby("pid_use_code_hash", pid_code_type,1);
result["statu"] = Code_Result.success
result["msg"] = "ok";
elseif usecnt >= "1" then
result["msg"] = "Has been used code on this channel";
result["statu"] = Code_Result.channel_used
end
end
end
else
result["msg"] = "parm error"
result["statu"] = Code_Result.error
--ngx.say("{\"statu\" : 0, \"msg\" : 'param error'}")
end
else
result["msg"] = "redis connect error"
result["statu"] = Code_Result.error
end
res_json = cjson.encode(result)
ngx.say(res_json)
end
function GetArgs()
local request_method = ngx.var.request_method
local args = nil
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
return args;
end
local args = GetArgs()
UpdateCode(args)
lua demo的更多相关文章
- lua学习笔记(一)
lua是一种嵌入式的语言,首先安装. lua安装依赖于readline库,下载readline tar -zxvf readline-6.2.tar.gz cd readline-6.2 ./conf ...
- cocos2d-x 使用Lua
转自:http://www.benmutou.com/blog/archives/49 1. Lua的堆栈和全局表 我们来简单解释一下Lua的堆栈和全局表,堆栈大家应该会比较熟悉,它主要是用来让C++ ...
- 转载一篇将C/C++ 与lua混合使用入门讲的比较好的文章
转自 http://www.open-open.com/home/space-6246-do-blog-id-1426.html Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用 ...
- 一篇个人感觉比较好的lua入门的文章
原文转自www.cppprog.com,由三篇文章组成 Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用.Lua与其它脚本语言相比,其突出优势在于: 1. 可扩展性.Lua的扩 ...
- VS项目属性配置实验过程
(原创,转载注明出处:http://www.cnblogs.com/binxindoudou/p/4017975.html ) 一.实验背景 cocos2d-x已经发展的相对完善了,从项目的创建.编译 ...
- Lua-Async 协程的高级用法
Lua-Async 这是一个基于协程的异步调用库, 该库的设计思路类似JavaScript的Promise, 但相比Promise, 它有更多的灵活性. -- 引入Async local Async ...
- lua模块demo(redis,http,mysql,cjson,本地缓存)
1. lua模块demo(redis,http,mysql,cjson,本地缓存) 1.1. 配置 在nginx.conf中设置lua_shared_dict my_cache 128m; 开启ngi ...
- lua入门demo(HelloWorld+redis读取)
1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...
- lua入门之环境搭建、第一个demo
前言 前段时间因为有些项目功能需要,自己研究了下lua,今天整理下,并以一个demo为示例演示 手机上的运行效果 分为几个步骤来逐步讲解. 1.lua介绍,为什么选择它? 2.环境安装 3.撸一个简单 ...
随机推荐
- javascript 定时器 笔记
最近想看下定时器,发现这东西越看越牵连的东西越多,比如js单线程,EVent loop 什么的 看到了几篇比较好的文章 http://ejohn.org/blog/how-javascript-tim ...
- zk client获取数据
获取数据 它返回znode的关联数据和指定znode的元数据.你将获得信息,例如上次修改数据的时间,修改的位置以及数据的相关信息.此CLI还用于分配监视器以显示数据相关的通知. 语法 get /pat ...
- 怎么在windows7系统我的电脑中添加快捷方式
在我的电脑中添加一些快捷方式,这样不用每次在开始菜单中去找了 2 选择开始菜单运行 3 输入:Regedit命令 4 进入路径地址:HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
- 【java】为数组全部元素赋同样的值 以及 数组之间的复制
为数组全部元素赋同样的值 : boolean[] resArray=new boolean[100]; Arrays.fill(resArray, true); 数组之间的复制: System.arr ...
- Hadoop之Hbase详解
1.什么是Hbase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统, hbase是列式的分布式数据库 1.2.HBASE优势: 1)线性扩展,随着数据量增多可以通过节点扩展进行支撑 ...
- iOS 系统框架
iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch l ...
- 一个简单的JS函数,用于判断文本是否数字
/****************************************************** 判断是否是数字(整数,小数均可,不包括负数)* 2014年10月10日22:38:19* ...
- 解决apt-get的E: Could not get lock /var/lib/dpkg/lock方法
使用apt-get进行软件的install或update时,有时会出现以下提示信息: E: Could not get lock /var/lib/dpkg/lock - open (11 Resou ...
- How to get the url of a page in OpenERP?
How to get the url of a page in OpenERP? User is using OpenERP. I have a button on one web page. The ...
- linux 下 查看进程的启动时间和运行时间
$ps -A -opid,stime,etime,args | grep switcher : : grep switcher Jan22 :: /home/a/search/switcher//bi ...