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.撸一个简单 ...
随机推荐
- C#将json字符串解析成对象
首先我们在客户端生成json字符串,通过ajax把该字符串传到服务器端 //这是一个以id,email,age的json字符串 var jdata="[{\"id\&quo ...
- 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null
报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...
- vs 代理登入
https://msdn.microsoft.com/zh-cn/vstudio/dn771556.aspx
- map-reduce任务的执行流程
(1).客户端提交一个mr的jar包给JobClient(提交方式:hadoop jar ...) (2).JobClient通过RPC和JobTracker进行通信,返回一个存放jar包的地址(HD ...
- Retrofit全攻略——基础篇
实际开发过程中一般都会选择一些网络框架提升开发效率.随着Google对HttpClient 摒弃和Volley框架的逐渐没落.OkHttp開始异军突起.而Retrofit则对OkHttp进行了强制依赖 ...
- POJ3525-Most Distant Point from the Sea(二分+半平面交)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3955 ...
- Python——网络编程,如何避免死锁?
问题描述:什么是死锁? 死锁发生在当一个服务器和客户端同时试图往一个连接上写东西或同时从一个连接上读的时候.在这种情况下,没有进程可以得到任何数据(如果它们都正在读),因此,如果它们正在写,向外的bu ...
- Python——Code Like a Pythonista: Idiomatic Python
Code Like a Pythonista: Idiomatic Python 如果你有C++基础,那学习另一门语言会相对容易.因为C++即面向过程,又面向对象.它很底层,能像C一样访问机器:它也很 ...
- css选择器和xpath对照表
- android开发中,在java中怎样使用c提供过来char*
这个char*假设是一般的字符串的话,作为string传回去就能够了.假设是含有'\0'的buffer,最好作为bytearray传出,由于能够制定copy的length.假设copy到string, ...