first commit
This commit is contained in:
174
app/Api/Controllers/ApiResponse.php
Normal file
174
app/Api/Controllers/ApiResponse.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Facades\PingAn\Log as LogFacade;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Response;
|
||||
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
|
||||
use XuanChen\Coupon\Traits\Log as LogTraits;
|
||||
|
||||
trait ApiResponse
|
||||
{
|
||||
|
||||
use LogTraits;
|
||||
|
||||
/**
|
||||
* [$statusCode description]
|
||||
* @var [type]
|
||||
*/
|
||||
protected $statusCode = FoundationResponse::HTTP_OK;
|
||||
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param <type> $value
|
||||
* @return <type>
|
||||
*/
|
||||
public function keyasc($value)
|
||||
{
|
||||
$iv = substr($this->user->des3key, 0, 8);
|
||||
$ret = openssl_encrypt($value, 'DES-EDE3-CBC', $this->user->des3key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
return openssl_error_string();
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param <type> $value
|
||||
* @return <type>
|
||||
*/
|
||||
public function keydesc($value)
|
||||
{
|
||||
$iv = substr($this->user->des3key, 0, 8);
|
||||
$ret = openssl_decrypt($value, 'DES-EDE3-CBC', $this->user->des3key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
return openssl_error_string();
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function checkSign(Request $request)
|
||||
{
|
||||
$server_id = $request->server_id ?? false;
|
||||
$key = $request->key ?? false;
|
||||
$data = $request->data ?? false;
|
||||
$addcode = $request->addcode ?? false;
|
||||
$sign = $request->sign ?? false;
|
||||
if (!$server_id) {
|
||||
return '参数server_id不能为空';
|
||||
}
|
||||
|
||||
if (!$key) {
|
||||
return '参数key不能为空';
|
||||
}
|
||||
|
||||
$this->user = User::where('server_id', $server_id)
|
||||
->where('server_key', $key)->first();
|
||||
|
||||
if (!$this->user) {
|
||||
return '参数server_id与key不匹配';
|
||||
}
|
||||
|
||||
if ($this->user->status != 1) {
|
||||
return '渠道商状态不正确';
|
||||
}
|
||||
|
||||
if (!$sign) {
|
||||
return '参数sign不能为空';
|
||||
}
|
||||
|
||||
if (!$data) {
|
||||
return '参数data不能为空';
|
||||
}
|
||||
|
||||
if (!$addcode) {
|
||||
return '参数addcode不能为空';
|
||||
}
|
||||
|
||||
$data = str_replace('\\', '', $data);
|
||||
$data = str_replace(' ', '+', $data);
|
||||
$checksign = $this->keysign($data, $addcode);
|
||||
if ($checksign != $sign) {
|
||||
return '参数sign不正确';
|
||||
}
|
||||
$keydesc = $this->keydesc($data);
|
||||
$keydescArr = json_decode($this->keydesc($data), true);
|
||||
if (empty($keydescArr) && !empty($keydesc)) {
|
||||
return '传递的json数据不对';
|
||||
}
|
||||
|
||||
return $keydescArr;
|
||||
}
|
||||
|
||||
public function keysign($jsonData = '', $addcode = '')
|
||||
{
|
||||
$signStr = 'data=' . $jsonData . '&addcode=' . $addcode . '&key=' . $this->user->server_key;
|
||||
$sign = hash_hmac('sha256', $signStr, $this->user->server_key);
|
||||
|
||||
return $sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功的返回
|
||||
* @Author :<C.Jason>
|
||||
* @Date :2018-05-22
|
||||
* @param [type] $data [description]
|
||||
* @param string $status [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function success($data, $log = '')
|
||||
{
|
||||
$jsonData = json_encode($data); //数据JSON化
|
||||
$ascdata = $this->keyasc($jsonData); //加密
|
||||
$addcode = sprintf("%08d", mt_rand(0, 99999999)); //随机code 验证签名用
|
||||
$sign = $this->keysign($ascdata, $addcode);
|
||||
$data = [
|
||||
'code' => 1,
|
||||
'message' => 'SUCCESS',
|
||||
'data' => $ascdata,
|
||||
'addcode' => $addcode,
|
||||
'sign' => $sign,
|
||||
];
|
||||
|
||||
if ($log) {
|
||||
if (!is_array($data)) {
|
||||
$data = [$data];
|
||||
}
|
||||
|
||||
$this->updateLog($log, $data); //更新日志
|
||||
}
|
||||
|
||||
return $this->respond(200, $data);
|
||||
}
|
||||
|
||||
public function error($message = '', $log = '')
|
||||
{
|
||||
$data = [
|
||||
'code' => 0,
|
||||
'message' => $message,
|
||||
];
|
||||
|
||||
if ($log) {
|
||||
$this->updateLog($log, [$message]); //更新日志
|
||||
}
|
||||
|
||||
return $this->respond(200, $data);
|
||||
}
|
||||
|
||||
public function respond($code, $data, $header = [])
|
||||
{
|
||||
$rt = microtime(true) - LARAVEL_START;
|
||||
|
||||
$header = array_merge($header, ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1)]);
|
||||
|
||||
return Response::json($data, $code, $header);
|
||||
}
|
||||
|
||||
}
|
||||
12
app/Api/Controllers/Controller.php
Normal file
12
app/Api/Controllers/Controller.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use ValidatesRequests, ApiResponse;
|
||||
|
||||
}
|
||||
191
app/Api/Controllers/UserController.php
Normal file
191
app/Api/Controllers/UserController.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use XuanChen\Coupon\Coupon;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->user = User::find(1);
|
||||
$data = [
|
||||
'user_id' => $this->user->id,
|
||||
'nickname' => $this->user->info->nickname,
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function check(Request $request)
|
||||
{
|
||||
$res = $this->checkSign($request);
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res);
|
||||
}
|
||||
$user_id = $res['user_id'];
|
||||
$user = User::find($user_id);
|
||||
|
||||
return $this->success($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 发券
|
||||
* @Author: 玄尘
|
||||
* @Date : 2020/6/29 13:57
|
||||
*/
|
||||
public function grant(Request $request)
|
||||
{
|
||||
$inputdata = $request->all();
|
||||
$res = $this->checkSign($request);
|
||||
$inputdata['jiemi'] = $res;
|
||||
$log = $this->createLog($request->url(), 'POST', $inputdata, 'grant'); //添加日志
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'activityId' => 'required',
|
||||
'outletId' => 'required',
|
||||
'mobile' => 'required',
|
||||
], [
|
||||
'activityId.required' => '缺少活动编码',
|
||||
'outletId.required' => '缺少网点id',
|
||||
'mobile.required' => '缺少手机号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$res = Coupon::Grant($res['activityId'], $res['outletId'], $res['mobile']);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
return $this->success($res, $log);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 查询
|
||||
* @Author: 玄尘
|
||||
* @Date : 2020/7/22 11:23
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function query(Request $request)
|
||||
{
|
||||
$inputdata = $request->all();
|
||||
$res = $this->checkSign($request);
|
||||
$inputdata['jiemi'] = $res;
|
||||
|
||||
$log = $this->createLog($request->url(), 'POST', $inputdata, 'grant'); //添加日志
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'redemptionCode' => 'required',
|
||||
'outletId' => 'required',
|
||||
], [
|
||||
'redemptionCode.required' => '缺少卡券兑换码',
|
||||
'outletId.required' => '缺少网点id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$redemptionCode = $res['redemptionCode'];
|
||||
$outletId = $res['outletId'];
|
||||
|
||||
$res = Coupon::Query($redemptionCode, $outletId);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
//作废
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$inputdata = $request->all();
|
||||
$res = $this->checkSign($request);
|
||||
$inputdata['jiemi'] = $res;
|
||||
$log = $this->createLog($request->url(), 'POST', $inputdata, 'grant'); //添加日志
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'redemptionCode' => 'required',
|
||||
], [
|
||||
'redemptionCode.required' => '缺少卡券兑换码',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$redemptionCode = $res['redemptionCode'];
|
||||
|
||||
$res = Coupon::Destroy($redemptionCode);
|
||||
|
||||
if ($res !== true) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
return $this->success('作废成功', $log);
|
||||
}
|
||||
|
||||
//核销
|
||||
public function freezecoupon(Request $request)
|
||||
{
|
||||
$inputdata = $request->all();
|
||||
|
||||
$res = $this->checkSign($request);
|
||||
$inputdata['jiemi'] = $res;
|
||||
//插入日志表
|
||||
$log = $this->createLog($request->url(), 'POST', $inputdata, 'self'); //添加日志
|
||||
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'redemptionCode' => 'required',
|
||||
'total' => 'required',
|
||||
'outletId' => 'required',
|
||||
], [
|
||||
'redemptionCode.required' => '缺少卡券兑换码',
|
||||
'total.required' => '缺少订单总额',
|
||||
'outletId.required' => '缺少网点id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$redemptionCode = $res['redemptionCode'] ?? ''; //'915400693355';
|
||||
$total = $res['total'] ?? ''; //订单总额;
|
||||
$outletId = $res['outletId'] ?? ''; //网点id;
|
||||
$redemptionCode = trim($redemptionCode);
|
||||
$outletId = trim($outletId);
|
||||
|
||||
$coupon = Coupon::Redemption($this->user, $redemptionCode, $total, $outletId);
|
||||
if (is_string($coupon)) {
|
||||
return $this->error($coupon, $log);
|
||||
}
|
||||
|
||||
return $this->success($coupon, $log);
|
||||
}
|
||||
|
||||
}
|
||||
263
app/Api/Controllers/WoController.php
Normal file
263
app/Api/Controllers/WoController.php
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Controllers;
|
||||
|
||||
use App\Facades\Wo\Log as LogFacade;
|
||||
use Illuminate\Http\Request;
|
||||
use Wo;
|
||||
|
||||
class WoController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 180元的话费券 一次发放 活动编号 97LJ202018
|
||||
* @author 玄尘 2020-03-09
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function grant180(Request $request)
|
||||
{
|
||||
$type = 'phoneTicket';
|
||||
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志
|
||||
|
||||
$res = $this->checkSign($request);
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'mobile' => 'required',
|
||||
], [
|
||||
'mobile.required' => '缺少手机号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$res['user'] = $this->user;
|
||||
$res['activity'] = '97LJ202018';
|
||||
$res['ticketAmt'] = '180';
|
||||
$res['type'] = $type;
|
||||
|
||||
$res = Wo::ticketGrant($res);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
/**
|
||||
* 电子提货券 一共600 每期发放25元 活动编号 97LJ202025
|
||||
* @author 玄尘 2020-03-09
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function grant25(Request $request)
|
||||
{
|
||||
|
||||
$type = 'pickTicket25';
|
||||
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志
|
||||
|
||||
$res = $this->checkSign($request);
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'mobile' => 'required',
|
||||
], [
|
||||
'mobile.required' => '缺少手机号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$res['user'] = $this->user;
|
||||
$res['activity'] = '97LJ202025';
|
||||
$res['ticketAmt'] = '25';
|
||||
$res['type'] = $type;
|
||||
|
||||
$res = Wo::ticketGrant($res);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
/**
|
||||
* 电子提货券 每次100元 活动编号 97LJ202010
|
||||
* @author 玄尘 2020-03-09
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function grant100(Request $request)
|
||||
{
|
||||
$type = 'pickTicket100';
|
||||
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志
|
||||
|
||||
$res = $this->checkSign($request);
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'mobile' => 'required',
|
||||
], [
|
||||
'mobile.required' => '缺少手机号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$res['user'] = $this->user;
|
||||
$res['activity'] = '97LJ202010';
|
||||
$res['ticketAmt'] = '100';
|
||||
$res['type'] = $type;
|
||||
|
||||
$res = Wo::ticketGrant($res);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
return $this->success($res, $log);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 10元购物券
|
||||
* @author 玄尘 2020-06-11
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function grant10(Request $request)
|
||||
{
|
||||
|
||||
$type = 'pickTicket10';
|
||||
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), $type); //添加日志
|
||||
|
||||
$res = $this->checkSign($request);
|
||||
if (!is_array($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($res, [
|
||||
'mobile' => 'required',
|
||||
], [
|
||||
'mobile.required' => '缺少手机号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
$res['user'] = $this->user;
|
||||
$res['activity'] = '97LJ202001';
|
||||
$res['ticketAmt'] = '10';
|
||||
$res['type'] = $type;
|
||||
|
||||
$res = Wo::ticketGrant($res);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
//退业务
|
||||
public function cancel(Request $request)
|
||||
{
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), 'refund'); //添加日志
|
||||
|
||||
$postData = $this->checkSign($request);
|
||||
if (!is_array($postData)) {
|
||||
return $this->error($postData, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($postData, [
|
||||
'logNo' => 'required',
|
||||
], [
|
||||
'logNo.required' => '缺少发券流水号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
//查询状态
|
||||
$res = Wo::ticketQuery($postData, $this->user->id);
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
|
||||
$ConfigTicketState = config('wo.ticketState');
|
||||
$ConfigTicketStateText = config('wo.ticketStateText');
|
||||
$ticketState = $res['ticketState'];
|
||||
|
||||
if (!in_array($ticketState, ['A', 'U'])) {
|
||||
$msg = (array_key_exists($ticketState, $ConfigTicketStateText)) ? $ConfigTicketStateText[$ticketState] : '未知状态';
|
||||
return $this->error($msg, $log);
|
||||
}
|
||||
|
||||
//判断数组中是否存在下标
|
||||
if (!array_key_exists($ticketState, $ConfigTicketState)) {
|
||||
return $this->error('未知状态', $log);
|
||||
}
|
||||
|
||||
$postData['service'] = $ConfigTicketState[$ticketState];
|
||||
$postData['ologNo'] = $postData['logNo'];
|
||||
$postData['user'] = $this->user;
|
||||
|
||||
//退业务
|
||||
$res = Wo::ticketBack($postData, $this->user);
|
||||
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询状态
|
||||
* @author 玄尘 2020-03-13
|
||||
* @param Request $request [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function query(Request $request)
|
||||
{
|
||||
$log = LogFacade::insert($request->url(), 'POST', $request->all(), 'refund'); //添加日志
|
||||
|
||||
$postData = $this->checkSign($request);
|
||||
if (!is_array($postData)) {
|
||||
return $this->error($postData, $log);
|
||||
}
|
||||
|
||||
$validator = \Validator::make($postData, [
|
||||
'logNo' => 'required',
|
||||
], [
|
||||
'logNo.required' => '缺少发券流水号',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first(), $log);
|
||||
}
|
||||
|
||||
//查询状态
|
||||
$res = Wo::ticketQuery($postData, $this->user->id);
|
||||
if (is_string($res)) {
|
||||
return $this->error($res, $log);
|
||||
}
|
||||
return $this->success($res, $log);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user