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.撸一个简单 ...
随机推荐
- [JQuery]用InsertAfter实现图片走马灯展示效果2——js代码重构
写在前面 前面写过一篇文章<[JQuery]用InsertAfter实现图片走马灯展示效果>,自从写过那样的也算是使用面向对象的写法吧,代码实在丑陋,自从写过那样的代码,就是自己的一块心病 ...
- 使用Spring Boot集成FastDFS
原文:http://www.cnblogs.com/ityouknow/p/8298358.html#3893468 上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Sp ...
- saltstack之salt event事件用法
event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息通知salt或其他的操作系统.每个event都有一个标签.事件标签允许快速制定过滤事件.除了标签 ...
- DL380 G6 BIOS刷新方法
bios下载地址SP44873.exe (5.9 MB) http://h20000.www2.hp.com/bizsupport/TechSupport/SoftwareDescription.js ...
- Oracle架构全图
- 验收测试 - WebDriver 5
验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...
- HDU 2191悼念512汶川大地震遇难同胞——珍惜如今,感恩生活(多重背包)
HDU 2191悼念512汶川大地震遇难同胞--珍惜如今.感恩生活(多重背包) http://acm.hdu.edu.cn/showproblem.php?pid=2191 题意: 如果你有资金n元, ...
- ssh之<context:component-scan base-package="com.xx" />
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能, 同时还启用了注释驱动自动注入的功能 ( 即还隐式地在内部注册了 A ...
- Django——如何使用Template以及如何向template传递变量
如何使用模板系统 在Python代码中使用Django模板的最基本方式如下: 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Temp ...
- bye 2013 hello 2014
最近两个月除了必要的工作外,其余时间都在干一些我其实平时很少干的事, 喝酒.唱歌.打麻将.玩牌.以及到处跑找朋友玩,也许是过年的原因我放纵了自己,也许是自己心中的烦恼.我的博客记录着我每次看书学习的笔 ...