Files
youlegames/codes/agent/game/api/source/apis/coupon.php
2026-03-15 01:27:05 +08:00

195 lines
7.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Authorization, Content-Length, X-Requested-With");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: text/html; charset=utf-8");
/**
* 优惠券接口
*/
use phprs\util\Verify;
use phprs\util\exceptions\Forbidden;
use phprs\util\Logger;
use phprs\util\exceptions\NotFound;
use phprs\ezsql\Sql;
use phprs\util\exceptions\BadRequest;
require_once 'apiBase.php';
/**
*
* 优惠券接口
* @path("/coupon")
*/
class Coupon extends apiBase{
public $activityInfo; // 当前活动信息
public function commonVerify($appid,$devkey,$sid,$scode,$market_key,$activity_key) {
$result = array();
// 验证公共参数是否合法
parent::init($appid,$devkey,$sid,$scode,$market_key);
$verify_result = parent::verify();
// 判断公共验证是否存在错误
if( is_error_api($verify_result) ){
return $verify_result;
}
if( empty($activity_key) ) {
$result["error"] = '1';
$result["error_code"] = 13000;
$result["msg"] = "请传入对应的活动Key";
return $result;
}
$activityList = Sql::select('a.*')
->from('syweb_business_activity a')
->where('a.activity_key=?',$activity_key)
->get($this->db ,null);
if( empty($activityList) || count($activityList)<=0 ) {
$result["error"] = '1';
$result["error_code"] = 13001;
$result["msg"] = "指定的活动不存在或已经下架";
return $result;
}
$this->activityInfo = $activityList[0];
$activityMarketList = Sql::select('a.*')
->from('syweb_business_activity_market a')
->where('a.activity_key=? and a.market_key=?',$activity_key,$market_key)
->get($this->db ,null);
if( empty($activityMarketList) || count($activityMarketList)<=0 ) {
$result["error"] = '1';
$result["error_code"] = 13002;
$result["msg"] = "当前门店未参与该活动!";
return $result;
}
}
/**
* 获取优惠券(错误代码12000--12050)
* @route({"POST","/exchange"})
* @param({"appid","$._POST.appid"}) 所属应用
* @param({"devkey","$._POST.devkey"}) 开发者Key
* @param({"sid","$._POST.sid"}) 临时会员ID
* @param({"scode","$._POST.scode"}) 客户端票据
* @param({"market_key","$._POST.market_key"}) market_key
* @param({"activity_key","$._POST.activity_key"}) activity_key
* @param({"useprice","$._POST.useprice"}) useprice
* @param({"price","$._POST.price"}) price
* @param({"validDay","$._POST.validDay"}) validDay
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie不可用
*/
public function exchange($appid='', $devkey='',$sid='',$scode='',$market_key='',$activity_key='',$useprice=0,$price=0,$validDay=0) {
$result = array();
$P = $_POST;
// 判断公共验证是否存在错误
$verify_result = $this->commonVerify($appid,$devkey,$sid,$scode,$market_key,$activity_key);
if( is_error_api($verify_result) ){
return json_encode($verify_result,JSON_UNESCAPED_UNICODE);
}
if( $this->userInfo["auth_type"]==0 ) {
$result["error"] = "1";
$result["error_code"] = 12000;
$result["msg"] = "创建优惠券失败";
return json_encode($result,JSON_UNESCAPED_UNICODE);
} else if( $this->userInfo["auth_type"]==1 ) {
$result["error"] = "1";
$result["error_code"] = 12000;
$result["msg"] = "创建优惠券失败";
return json_encode($result,JSON_UNESCAPED_UNICODE);
} else if( $this->userInfo["auth_type"]==2 ) {
if ( !is_numeric($useprice) ) {
$result["error"] = "1";
$result["error_code"] = 12001;
$result["msg"] = "满多少可用必须为数字!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
if ( !is_numeric($price) ) {
$result["error"] = "1";
$result["error_code"] = 12002;
$result["msg"] = "减免金额必须为数字!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
if ( !is_numeric($validDay) ) {
$result["error"] = "1";
$result["error_code"] = 12003;
$result["msg"] = "有效天数必须为数字!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
$appid = "G3CI8FQF";
$appkey = "28de5f4a54cbbb62b2264ab555ff7f62";
$url = "http://www.0792it.com/partnerApi/GetSpidFromUid.ashx";
$url .= "?appid=".$appid;
$url .= "&appkey=".$appkey;
$url .= "&uid=".$this->userInfo["openid"];
$response = ihttp_get($url);
if( empty($response) ) {
$result["error"] = "1";
$result["error_code"] = 12004;
$result["msg"] = "获取授权信息错误,登录失败!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
$response = @json_decode($response['content'], true);
$spid = $response["spid"];
$accessToken = $response["accessToken"];
// 开始获取用户信息
$url = "http://www.0792it.com/partnerApi/CreatShopCoupon.ashx";
$paramers = array();
$paramers["accessToken"] = $accessToken;
$paramers["GameName"] = $this->activityInfo["activity_name"];
$paramers["s_price"] = $price;
$paramers["s_useprice"] = $useprice;
$paramers["Shopid"] = $this->marketInfo["jkx_market_key"];
$paramers["spid"] = $spid;
$paramers["validDay"] = $validDay;
$paramers["secret"] = "ecd10d48daf3138b88727bc65ca3e0bd";
$paramerStr = $this->ToUrlParams($paramers);
$sign = md5($paramerStr);
$paramerStr .= "&sign=".$sign;
$url .= "?".$paramerStr;
$response = ihttp_get($url);
if( empty($response) ) {
$result["error"] = "1";
$result["error_code"] = 12005;
$result["msg"] = "创建优惠券失败!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
$response = @json_decode($response['content'], true);
$retCode = $response["RetCode"];
if( $retCode==1 || $retCode==3 || $retCode==5 ) {
$result["error"] = '1';
$result["error_code"] = 12005;
$result["msg"] = "创建优惠券失败,错误消息为:".$response["RetMsg"];
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
$result["error"] = '0';
$result["msg"] = "创建优惠券成功!";
return json_encode($result,JSON_UNESCAPED_UNICODE);
}
}
/** @inject("ioc_factory") */
private $factory;
/**
* @property({"default":"@db"})
* @var PDO
*/
public $db;
// 此处删除了代码
}