432 行
17 KiB
PHP
可执行文件
432 行
17 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace SmallProgramApi\Controller;
|
|
|
|
//[1.7.15]
|
|
|
|
class GiftCardController extends BaseController
|
|
{
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
if (empty(self::$store_id)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '缺少门店参数';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 礼品卡列表
|
|
*/
|
|
public function giftCardList()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
$GiftCardModel = D('RetailGiftCard');
|
|
$map[$GiftCardModel::MERCHANT_ID] = $merchant_id;
|
|
$map[$GiftCardModel::STATUS] = 1;
|
|
$map[$GiftCardModel::STATE] = 1;
|
|
$map['_string'] = $GiftCardModel::START_TIME . ' < ' . time() . ' AND ' . $GiftCardModel::END_TIME . ' > ' . time();
|
|
$list = $GiftCardModel->getSelect($map);
|
|
$RGCPModel = D('RetailGiftCardPropaganda');
|
|
$img = $RGCPModel->getFind(array($RGCPModel::MERCHANT_ID => $map[$GiftCardModel::MERCHANT_ID], $RGCPModel::STATUS => 1));
|
|
$res['banner'] = $this->getGoodsImage($img[$RGCPModel::IMG_URL_ID]);
|
|
if (!empty($list)) {
|
|
foreach ($list as &$v) {
|
|
$GCCModel = D('RetailGiftCardCover');
|
|
$img_url = $GCCModel->getOrderFind(array($GCCModel::GIFT_CARD_ID => $v[$GiftCardModel::ID], $GCCModel::STATUS => 1, $GCCModel::STATE => 1), '*', $GCCModel::ID . ' desc');
|
|
$v['img_url'] = $this->getGoodsImage($img_url[$GCCModel::IMG_URL_ID]);
|
|
}
|
|
$res['status'] = 1;
|
|
$res['list'] = $list;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '暂无礼品卡';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/*
|
|
* 购买时单个礼品卡的详情
|
|
*/
|
|
public function getGiftDetail()
|
|
{
|
|
$id = I('post.id');
|
|
$GiftCardModel = D('RetailGiftCard');
|
|
$GiftCardCoverModel = D('RetailGiftCardCover');
|
|
$GiftCardPriceModel = D('RetailGiftCardPrice');
|
|
$card = $GiftCardModel->getFind(array($GiftCardModel::ID => $id));
|
|
$cover = $GiftCardCoverModel->getSelect(array($GiftCardCoverModel::GIFT_CARD_ID => $id, $GiftCardCoverModel::STATUS => 1, $GiftCardCoverModel::STATE => 1));
|
|
$price = $GiftCardPriceModel->getSelect(array($GiftCardPriceModel::GIFT_CARD_ID => $id, $GiftCardPriceModel::STATUS => 1, $GiftCardPriceModel::STATE => 1, $GiftCardPriceModel::LESS_NUM => array('gt', 0)));
|
|
if (!empty($cover)) {
|
|
$res['status'] = 1;
|
|
foreach ($cover as &$v) {
|
|
$v['img_url'] = $this->getGoodsImage($v[$GiftCardCoverModel::IMG_URL_ID]);
|
|
}
|
|
$card['start_time'] = date('Y-m-d', $card[$GiftCardModel::START_TIME]);
|
|
$card['end_time'] = date('Y-m-d', $card[$GiftCardModel::END_TIME]);
|
|
$card['notes'] = explode('<br/>', $card[$GiftCardModel::NOTES]);
|
|
$res['card'] = $card;
|
|
$res['cover'] = $cover;
|
|
$res['price'] = $price;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '数据错误';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/*
|
|
* 购买礼品卡
|
|
*/
|
|
public function createOrder()
|
|
{
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
$store_id = self::$store_id;
|
|
if (empty($uid) || self::$user['login_state'] != 1) {
|
|
$res['status'] = -1;
|
|
$res['msg'] = '用户未登录';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$id = I('post.id');
|
|
$cover_id = I('post.cover_id');
|
|
$price_id = I('post.price_id');
|
|
if (empty($uid) || empty($merchant_id) || empty($id) || empty($cover_id) || empty($price_id)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '缺少参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GiftCardModel = D('RetailGiftCard');
|
|
$GiftCardCoverModel = D('RetailGiftCardCover');
|
|
$GiftCardPriceModel = D('RetailGiftCardPrice');
|
|
$map['g.' . $GiftCardModel::ID] = $id;
|
|
$map['c.' . $GiftCardCoverModel::ID] = $cover_id;
|
|
$map['p.' . $GiftCardPriceModel::ID] = $price_id;
|
|
$map['g.' . $GiftCardModel::MERCHANT_ID] = $merchant_id;
|
|
$map['p.' . $GiftCardPriceModel::LESS_NUM] = array('gt', 0);
|
|
$field = array(
|
|
'p.' . $GiftCardPriceModel::PRICE,
|
|
'p.' . $GiftCardPriceModel::MONEY,
|
|
'g.' . $GiftCardModel::START_TIME,
|
|
'g.' . $GiftCardModel::END_TIME
|
|
);
|
|
$gift = $GiftCardModel->getGiftDetail($map, $field);
|
|
if (!empty($gift)) {
|
|
$PayOrderModel = D('RetailPayOrder');
|
|
$Order[$PayOrderModel::PAY_TYPE] = 1;
|
|
$Order[$PayOrderModel::ORDER_TYPE] = 4;
|
|
$Order[$PayOrderModel::TRADE_AMOUNT] = $gift[$GiftCardPriceModel::PRICE];
|
|
$Order[$PayOrderModel::MERCHANT_ID] = $merchant_id;
|
|
$Order[$PayOrderModel::STORE_ID] = $store_id;
|
|
$Order[$PayOrderModel::ORDER_STATE] = 'WAIT_BUYER_PAY';
|
|
$Order[$PayOrderModel::ADD_TIME] = time();
|
|
$Order[$PayOrderModel::UID] = $uid;
|
|
$payOrder = $PayOrderModel->addPayOrder($Order);
|
|
if (!$payOrder) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '下单失败';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$item[$GCRModel::PAY_ORDER_ID] = $payOrder[$PayOrderModel::ID];
|
|
$item[$GCRModel::XCX_VID] = $uid;
|
|
$item[$GCRModel::MERCHANT_ID] = $merchant_id;
|
|
$item[$GCRModel::GIFT_ID] = $id;
|
|
$item[$GCRModel::GIFT_COVER_ID] = $cover_id;
|
|
$item[$GCRModel::GIFT_PRICE_ID] = $price_id;
|
|
$item[$GCRModel::ADD_TIME] = time();
|
|
$item[$GCRModel::STATUS] = 0;
|
|
$item[$GCRModel::STATUS_TYPE] = 1;
|
|
$item[$GCRModel::TYPE] = 1;
|
|
$item[$GCRModel::START_TIME] = $gift[$GiftCardModel::START_TIME];
|
|
$item[$GCRModel::END_TIME] = $gift[$GiftCardModel::END_TIME];
|
|
$item[$GCRModel::MONEY] = $gift[$GiftCardPriceModel::MONEY];
|
|
$item[$GCRModel::PRICE] = $gift[$GiftCardPriceModel::PRICE];
|
|
$item[$GCRModel::STATE] = 0;
|
|
$add = $GCRModel->addOrder($item);
|
|
if ($add) {
|
|
$result['order_id'] = $add;
|
|
$result['status'] = 1;
|
|
$result['msg'] = '订单创建成功';
|
|
$notify_url = $this->REQUEST_URL . '/index.php/SmallProgramApi/WxPay/gift_notify_url_v1';//普通服务商支付回调地址
|
|
$notify_func = "PayApi/Pay/gift_continent_pay_v1";//新大陆回调函数
|
|
$payData = A('SmallProgramApi/Pay')->getWxPayData_v1($payOrder, $notify_url, $notify_func);
|
|
if ($payData['status'] == 1) {
|
|
$result['status'] = 1;
|
|
$result['payData'] = $payData['data'];
|
|
} else {
|
|
$result = $payData;
|
|
}
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '订单创建失败';
|
|
}
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '该价格礼品卡已售完';
|
|
}
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/*
|
|
*微信支付关闭后查询订单
|
|
*/
|
|
public function searchOrder()
|
|
{
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
$order_id = I('post.order_id');
|
|
if (empty($uid) || empty($merchant_id) || empty($order_id)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '缺少参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$gift_order = $GCRModel->getFind(array($GCRModel::ID => $order_id, $GCRModel::XCX_VID => $uid));
|
|
if (!empty($gift_order)) {
|
|
if ($gift_order['state'] == 1) {
|
|
$result['status'] = 1;
|
|
$result['msg'] = '支付成功';
|
|
} else {
|
|
$result['status'] = 2;
|
|
$result['msg'] = '订单待支付';
|
|
}
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '参数有误';
|
|
}
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/*
|
|
* 获取我的礼品卡
|
|
*/
|
|
public function getMyCard()
|
|
{
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
if (empty($uid) || empty($merchant_id)) {
|
|
$result['state'] = 0;
|
|
$result['msg'] = '缺少参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$GCModel = D('RetailGiftCard');
|
|
$where['r.' . $GCRModel::XCX_VID] = $uid;
|
|
$where['r.' . $GCRModel::STATE] = 1;
|
|
$gift_order = $GCRModel->getGiftCardDetails($where, 'r.*,g.' . $GCModel::TITLE, 'r.' . $GCRModel::ID . ' desc');
|
|
if (!empty($gift_order)) {
|
|
$time = time();
|
|
foreach ($gift_order as &$val) {
|
|
$val[$GCModel::END_TIME] = date('Y-m-d H:i', $val[$GCModel::END_TIME]);
|
|
if ($val[$GCModel::END_TIME] > $time) {
|
|
$val['use'] = 1;
|
|
}
|
|
}
|
|
$result['gift_order'] = $gift_order;
|
|
$result['status'] = 1;
|
|
$result['msg'] = '查询成功';
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '暂无领取记录';
|
|
}
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/*
|
|
* 获取我的礼品卡详情
|
|
*/
|
|
public function getMyGiftDetail()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
$id = I('post.id');
|
|
$type = I('post.type');
|
|
$uid = self::$uid;
|
|
$xcx_vid = I('post.xcx_vid');
|
|
if (empty($merchant_id) || empty($id) || empty($type)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '缺少参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GCCModel = D('RetailGiftCardCover');
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$GCModel = D('RetailGiftCard');
|
|
$map['r.' . $GCRModel::STATE] = 1;
|
|
$map['r.' . $GCRModel::ID] = $id;
|
|
$field = array(
|
|
'r.*',
|
|
'c.' . $GCCModel::IMG_URL_ID,
|
|
'g.' . $GCModel::TITLE,
|
|
'g.' . $GCModel::NOTES
|
|
);
|
|
switch ($type) {
|
|
case 1:
|
|
$map['r.' . $GCRModel::XCX_VID] = $uid;
|
|
break;
|
|
case 2:
|
|
//领取礼品卡]
|
|
$map['r.' . $GCRModel::XCX_VID] = $xcx_vid;
|
|
break;
|
|
default:
|
|
$result['status'] = 0;
|
|
$result['msg'] = '参数有误';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$detail = $GCCModel->getGiftImg($map, $field);
|
|
if (!empty($detail)) {
|
|
$detail['notes'] = explode('<br/>', $detail[$GCModel::NOTES]);
|
|
$detail['img_url'] = $this->getGoodsImage($detail[$GCCModel::IMG_URL_ID]);
|
|
$detail['time'] = date('Y-m-d', $detail[$GCRModel::START_TIME]) . ' -- ' . date('Y-m-d', $detail[$GCRModel::END_TIME]);
|
|
$detail['id'] = $detail[$GCRModel::ID];
|
|
$result['data'] = $detail;
|
|
$result['status'] = 1;
|
|
$result['msg'] = '查询成功';
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '暂无数据';
|
|
}
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/*
|
|
* 礼品卡充值到余额
|
|
*/
|
|
public function giftSelfUse()
|
|
{
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
$id = I('post.id');
|
|
if (empty($uid) || empty($merchant_id) || empty($id)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '缺少参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$map[$GCRModel::ID] = $id;
|
|
$map[$GCRModel::XCX_VID] = $uid;
|
|
$map[$GCRModel::STATUS] = 0;
|
|
$map[$GCRModel::STATUS_TYPE] = 1;
|
|
$map[$GCRModel::STATE] = 1;
|
|
$order = $GCRModel->getFind($map);
|
|
if (empty($order)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '礼品卡记录不存在';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
if ($order[$GCRModel::START_TIME] > time() || $order[$GCRModel::END_TIME] < time()) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '不在该礼品卡可用时段';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$RSPUModel = D('RetailSmallProgramUser');
|
|
$VipModel = D('RetailVip');
|
|
$where = array(
|
|
'x.' . $RSPUModel::ID => $uid,
|
|
'v.' . $VipModel::STATUS => array('neq', 2)
|
|
);
|
|
$field = array(
|
|
'x.*',
|
|
'v.' . $VipModel::STATUS . ' as v_status'
|
|
);
|
|
$user = $RSPUModel->getVip($where, $field);
|
|
if ($user[$RSPUModel::VID]) {
|
|
if ($user['v_status'] == 1) {
|
|
$add = $VipModel->toSetInc(array($VipModel::ID => $user[$RSPUModel::VID]), $VipModel::BALANCE, $order[$GCRModel::MONEY]);
|
|
$RROModel = D('RetailRechargeOrder');
|
|
$item[$RROModel::MONEY] = $order[$GCRModel::PRICE];
|
|
$item[$RROModel::ALL_MONEY] = $order[$GCRModel::MONEY];
|
|
$item[$RROModel::TYPE] = 8;
|
|
$item[$RROModel::SCENE_TYPE] = 4;
|
|
$item[$RROModel::STATUS] = 1;
|
|
$item[$RROModel::MERCHANT_ID] = $user[$RSPUModel::MERCHANT_ID];
|
|
$item[$RROModel::ADD_TIME] = time();
|
|
$item[$RROModel::VID] = $user[$RSPUModel::VID];
|
|
$item[$RROModel::REMARK] = '礼品卡充值' . $item[$RROModel::ALL_MONEY] . '元';
|
|
$res = $RROModel->addOrder($item);
|
|
if ($res) {
|
|
$update = $GCRModel->updateOrder($map, array($GCRModel::STATUS => 1, $GCRModel::RECHARGE_VID => $user[$RSPUModel::VID]));
|
|
$result['status'] = 1;
|
|
$result['msg'] = '充值成功';
|
|
}
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '会员卡被冻结或挂失';
|
|
}
|
|
} else {
|
|
$result['status'] = 2;
|
|
$result['msg'] = '您需要绑定会员卡才能充值';
|
|
}
|
|
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/*
|
|
* 礼品卡领取
|
|
*/
|
|
public function toReceive()
|
|
{
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
$id = I('post.id');
|
|
$result['post'] = I('post.');
|
|
if (empty($uid)) {
|
|
$result['status'] = -1;
|
|
$result['msg'] = '用户未登录';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
if (empty($merchant_id) || empty($id)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '缺失参数';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$GCRModel = D('RetailGiftCardRecord');
|
|
$order = $GCRModel->getFind(array($GCRModel::ID => $id)); // 原礼品卡记录
|
|
if (empty($order)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '赠送的礼品卡不存在';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
if ($order[$GCRModel::STATUS] == 1 || $order[$GCRModel::STATUS_TYPE] == 2) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '该礼品卡已被领取';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$SPUModel = D('RetailSmallProgramUser');
|
|
$user = $SPUModel->getFind(array($SPUModel::ID => $uid));
|
|
if (empty($user)) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '用户不存在';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
$item[$GCRModel::XCX_VID] = $uid;
|
|
$item[$GCRModel::MERCHANT_ID] = $merchant_id;
|
|
$item[$GCRModel::GIFT_ID] = $order[$GCRModel::GIFT_ID];
|
|
$item[$GCRModel::GIFT_COVER_ID] = $order[$GCRModel::GIFT_COVER_ID];
|
|
$item[$GCRModel::GIFT_PRICE_ID] = $order[$GCRModel::GIFT_PRICE_ID];
|
|
$item[$GCRModel::ADD_TIME] = time();
|
|
$item[$GCRModel::STATUS] = 0;
|
|
$item[$GCRModel::STATUS_TYPE] = 1;
|
|
$item[$GCRModel::TYPE] = 2;
|
|
$item[$GCRModel::START_TIME] = $order[$GCRModel::START_TIME];
|
|
$item[$GCRModel::END_TIME] = $order[$GCRModel::END_TIME];
|
|
$item[$GCRModel::MONEY] = $order[$GCRModel::MONEY];
|
|
$item[$GCRModel::PRICE] = $order[$GCRModel::PRICE];
|
|
$item[$GCRModel::STATE] = 1;
|
|
$item[$GCRModel::P_XCX_VID] = $order[$GCRModel::XCX_VID];
|
|
$item[$GCRModel::GIFT_PID] = $id;
|
|
$add = $GCRModel->addOrder($item);
|
|
if (!empty($add)) {
|
|
$save[$GCRModel::STATUS_TYPE] = 2;
|
|
$save[$GCRModel::STATUS] = 1;
|
|
$update = $GCRModel->updateOrder(array($GCRModel::ID => $id), $save);
|
|
$result['id'] = $add;
|
|
$result['status'] = 1;
|
|
$result['msg'] = '领取成功';
|
|
} else {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '领取失败';
|
|
}
|
|
$this->ajaxReturn($result);
|
|
}
|
|
} |