jh-admin/addon/coupon/shop/controller/Coupon.php

558 lines
22 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
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\coupon\shop\controller;
use addon\aliapp\model\Config;
use addon\coupon\model\alipay\AlipaydiscountCouponType;
use addon\coupon\model\alipay\AlistoreCouponType;
use addon\coupon\model\CouponWeChat;
use addon\coupon\model\MemberCoupon;
use app\shop\controller\BaseShop;
use addon\coupon\model\CouponType as CouponTypeModel;
use addon\coupon\model\Coupon as CouponModel;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;
/**
* 优惠券
* @author Administrator
*
*/
class Coupon extends BaseShop
{
public function __construct()
{
$this->app_module = input('app_module', SHOP_MODULE);
if ($this->app_module == 'store') {
$this->initConstructInfo(); // 加载构造函数重要信息
} else {
parent::__construct();
}
}
/**
* Common: 优惠券编辑 - 进入优惠券添加页面
* Author: wu-hui
* Time: 2023/07/05 9:47
* @return mixed
*/
public function add()
{
// 代金券使用回调地址同步
try {
$this->assign('isWeCard',0);
$this->assign('isAliCard',0);
$config_model = new \addon\wechatpay\model\Config();
$app_type_array = ['h5', 'wechat', 'weapp', 'pc'];
$weapp = $config_model->getPayConfig($this->site_id)['data']['value'];
$config = new Config();
$aliapp = $config->getAppConfig($this->site_id)['data']['value'];
if($aliapp){
$this->assign('isAliCard',1);
}
if(isset($weapp['v3_pay_signkey'])&&$weapp['v3_pay_signkey']){
// 判断:是否已经同步 未同步则同步一下
$name = "voucher_notify_synchronous" . $this->site_id;
$isSynchronous = (int)Cache::get($name);
try {
if ($isSynchronous <= 0) {
$couponWeChatModel = new CouponWeChat($this->site_id);
$couponWeChatModel->voucherSetNotify();
Cache::set($name, 1);
}
$this->assign('isWeCard',1);
}catch (Exception $E){
}
}
} catch (Exception $e) {
// 通知地址同步为静默处理 错误后不做任何操作
}
return $this->fetch("coupon/editInfo");
}
/**
* Common: 优惠券编辑 - 进入优惠券编辑页面
* Author: wu-hui
* Time: 2023/07/05 15:48
* @return mixed
*/
public function edit()
{
// 基本参数
$couponTypeModel = new CouponTypeModel();
$couponTypeId = input('coupon_type_id', 0);
// 获取优惠券信息
$couponTypeInfo = $couponTypeModel->getCouponTypeInfo($couponTypeId, $this->site_id);
if (empty($couponTypeInfo['data'])) $this->error('未获取到优惠券数据', addon_url('coupon://shop/coupon/lists'));
$config_model = new \addon\wechatpay\model\Config();
$this->assign('isWeCard',0);
$this->assign('isAliCard',0);
$weapp = $config_model->getPayConfig($this->site_id)['data']['value'];
$config = new Config();
$aliapp = $config->getAppConfig($this->site_id)['data']['value'];
if($aliapp){
$this->assign('isAliCard',1);
}
if(isset($weapp['v3_pay_signkey'])&&$weapp['v3_pay_signkey']){
// 判断:是否已经同步 未同步则同步一下
$name = "voucher_notify_synchronous" . $this->site_id;
$isSynchronous = (int)Cache::get($name);
if ($isSynchronous <= 0) {
$couponWeChatModel = new CouponWeChat($this->site_id);
try {
$couponWeChatModel->voucherSetNotify();
Cache::set($name, 1);
$this->assign('isWeCard',1);
}catch (Exception $E){
}
}
}
return $this->fetch("coupon/editInfo", [
'coupon_type_id' => $couponTypeId,
'info' => $couponTypeInfo['data'][0]
]);
}
/**
* Common: 优惠券编辑 - 优惠券添加或者编辑统一处理
* Author: wu-hui
* Time: 2023/07/05 11:20
* @return array
*/
public function addOrEdit()
{
try {
// 参数获取
$id = (int)input('id');// 卡卷id =0添加>0修改
$info = (array)input('info');// 优惠券信息数组
$info['site_id'] = $this->site_id;
$info['end_time'] = strtotime($info['end_time']);
$couponTypeModel = new CouponTypeModel();
// 根据归属平台处理信息 weapp=微信小程序aliapp=支付宝小程序
if ($info['app_type'] == 'weapp') {
// 微信平台
$info['channel'] = '';// 支付宝平台渠道信息置空
$info = $this->weAppCoupon($info, $id);
} else if ($info['app_type'] == 'aliapp') {
// 支付宝小程序
$info['weapp_channel'] = '';// 微信平台渠道信息置空
// TODO由于逻辑流程修改等待开发者重新调试中...
$info = $this->aliPayCoupon($info, $id);
} else {
// 无归属平台
$info['channel'] = '';// 支付宝平台渠道信息置空
$info['weapp_channel'] = '';// 微信平台渠道信息置空
}
// 根据是否存在id 进行对应的添加||编辑操作
if ($id > 0) return array_merge($couponTypeModel->editCouponType($info, $id), $info);
else return $couponTypeModel->addCouponType($info);
} catch (\Exception $e) {
return error('FAIL', $e->getMessage());
}
}
/**
* Common: 优惠券编辑 - 支付宝平台优惠券相关处理
* Time: 2023/07/05 10:16
* @param array $info
* @param int $id
* @return array|mixed
*/
private function aliPayCoupon(array $info, int $id)
{
$couponTypeModel = new CouponTypeModel();
if ($id > 0) {
// 编辑信息
$couponInfo = model("promotion_coupon_type")->getInfo([['coupon_type_id', '=', $id]]);
switch ($info['channel']) {
case 'alicard':
$info['alipay_pass_template_id'] = $couponInfo['alipay_pass_template_id'];
$template = $couponTypeModel->alipayPassTemplateUpdate($this->site_id, $info);
if ($template['code'] < 0) return $template;
break;
case 'alistore':
if ($info['count'] < $couponInfo['count']) return error(-1, '修改失败,支付宝商家券限制:发放数量不能小于已设定数量');
if ($info['type'] != $couponInfo['type']) return error(-1, '修改失败,支付宝商家券限制:优惠券类型不可修改');
if ($info['money'] != $couponInfo['money']) return error(-1, '修改失败,支付宝商家券限制:优惠券面额不可修改');
if ($info['at_least'] != $couponInfo['at_least']) return error(-1, '修改失败,支付宝商家券限制:满多少元可以使用不可修改');
$info['alipay_pass_template_id'] = $couponInfo['alipay_pass_template_id'];
$template = (new AlistoreCouponType())->alipayMarketingActivityOrdervoucherCreateAndModify($this->site_id, $info, $info['alipay_pass_template_id']);
if ($template['code'] < 0) return $template;
break;
case 'alipaydiscount':
$alipaydiscountModel = new AlipaydiscountCouponType();
$ali_coupon_type = $alipaydiscountModel->alipayMarketingActivityVoucherQuery($this->site_id, $info['alipay_pass_template_id']);
// 计算修改数量
$num = $info['count'] - $ali_coupon_type['data']['voucher_send_mode_info']['voucher_send_rule_info']['quantity'];
if ($num < 0) return error(-1, '修改失败,支付宝支付券限制:发放数量不能小于已设定数量');
if ($info['type'] != $couponInfo['type']) return error(-1, '修改失败,支付宝支付券限制:"优惠券类型"不可修改');
if ($info['money'] != $couponInfo['money']) return error(-1, '修改失败,支付宝支付券限制:"优惠券面额"不可修改');
if ($info['at_least'] != $couponInfo['at_least']) return error(-1, '修改失败,支付宝支付券限制:"满多少元可以使用"不可修改');
$info['alipay_pass_template_id'] = $couponInfo['alipay_pass_template_id'];
// 修改商家券活动发券数量上限
if ($num > 0) $info['data']['voucher_append'] = $alipaydiscountModel->alipayMarketingActivityVoucherAppend($this->site_id, $info['alipay_pass_template_id'], $info['count']);
$template = $alipaydiscountModel->alipayMarketingActivityVoucherCreateAndModify($this->site_id, $info, $info['alipay_pass_template_id']);
if ($template['code'] < 0) return $template;
break;
}
} else {
// 添加信息
switch ($info['channel']) {
case 'alicard':
$template = $couponTypeModel->alipayPassTemplateAdd($this->site_id, $info);
if ($template['code'] < 0) return $template;
$info['alipay_pass_template_id'] = json_decode($template['data']['result'], TRUE)['tpl_id'];
break;
case 'alistore':
$template = (new AlistoreCouponType())->alipayMarketingActivityOrdervoucherCreateAndModify($this->site_id, $info);
if ($template['code'] < 0) return $template;
$info['alipay_pass_template_id'] = $template['data']['activity_id'];
break;
case 'alipaydiscount':
$template = (new AlipaydiscountCouponType())->alipayMarketingActivityVoucherCreateAndModify($this->site_id, $info);
if ($template['code'] < 0) return $template;
$info['alipay_pass_template_id'] = $template['data']['activity_id'];
break;
}
}
return $info;
}
/**
* Common: 优惠券编辑 - 微信平台优惠券相关处理
* Author: wu-hui
* Time: 2023/07/05 11:15
* @param array $info
* @param int $id
* @return array
* @throws Exception
* @throws \app\exception\ApiException
*/
private function weAppCoupon(array $info, int $id)
{
$couponWeChatModel = new CouponWeChat($info['site_id']);
$weAppChannel = (string)$info['weapp_channel'];
if ($id > 0) {
// 编辑信息
throw new Exception('微信优惠券发布后暂不支持编辑!');
// if($weAppChannel == 'store') $couponWeChatModel->storeUpdateBaseInfo($info,$id);// 编辑商家券
// else if($weAppChannel == 'voucher') throw new Exception('微信代金券发布后不可编辑!');// 编辑代金券
} else {
// 添加信息
if ($weAppChannel == 'store') {
// 发布商家券
$addResult = $couponWeChatModel->storeAdd($info);
$info['weapp_stock_id'] = (int)$addResult['stock_id'];
$info['weapp_status'] = 1;// 商家券不需要激活 直接为可以使用状态
} else if ($weAppChannel == 'voucher') {
// 发布代金券
$addResult = $couponWeChatModel->voucherAdd($info);
$info['weapp_stock_id'] = (int)$addResult['stock_id'];
}
}
return $info;
}
/**
* Common: 优惠券编辑 - 微信代金券激活
* Author: wu-hui
* Time: 2023/07/05 14:33
* @return array
*/
public function voucherActivation()
{
try {
// 参数获取
$couponTypeId = input('coupon_type_id');
$isDelayed = (int)input('is_delayed', 0);
// 判断:是否需要延时操作 如果为发布后立即激活则必须进行延时操作 否则激活失败
if ($isDelayed == 1) sleep(5);
// 激活操作
$coupon = model("promotion_coupon_type")->getInfo([
['coupon_type_id', '=', $couponTypeId]
], 'site_id,coupon_type_id,weapp_stock_id');
(new CouponWeChat($coupon['site_id']))->voucherActivation($coupon['weapp_stock_id']);
// 激活成功 修改激活状态
model("promotion_coupon_type")->update([
'weapp_status' => 1
], [['coupon_type_id', '=', $couponTypeId]]);
return success(0, '激活成功');
} catch (\Exception $e) {
return error('FAIL', $e->getMessage());
}
}
/**
* 活动详情
*/
public function detail()
{
$coupon_type_id = input('coupon_type_id', 0);
$coupon_type_model = new CouponTypeModel();
$coupon_type_info = $coupon_type_model->getCouponTypeInfo($coupon_type_id, $this->site_id)['data'] ?? [];
if (empty($coupon_type_info)) $this->error('未获取到优惠券数据', addon_url('coupon://shop/coupon/lists'));
$this->assign('info', $coupon_type_info[0]);
$this->assign('get_type', (new CouponModel())->getCouponGetType());
return $this->fetch("coupon/detail");
}
/**
* 活动列表
*/
public function lists()
{
$coupon_type_model = new CouponTypeModel();
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$coupon_name = input('coupon_name', '');
$status = input('status', '');
$inventory_count = input('inventory_count', '');#剩余数量
$condition = [];
if ($status !== "") {
$condition[] = ['status', '=', $status];
}
$type = input('type');
if ($type) {
$condition[] = ['type', '=', $type];
}
//类型
$validity_type = input('validity_type', '');
if ($validity_type !== '') {
$start_time = input('start_time', '');
$end_time = input('end_time', '');
switch ($validity_type) {
case 0: //固定
$condition[] = ['end_time', 'between', [$start_time, $end_time]];
break;
case 1:
$condition[] = ['fixed_term', 'between', [$start_time, $end_time]];
break;
case 2:
$condition[] = ['validity_type', '=', 2];
break;
}
}
if ($inventory_count) {
$condition[] = ['', "exp", Db::raw("(lead_count < count && count != -1) OR count = -1")];
}
$condition[] = ['site_id', '=', $this->site_id];
$condition[] = ['coupon_name', 'like', '%' . $coupon_name . '%'];
$field = '*';
//排序
$link_sort = input('order', 'create_time');
$sort = input('sort', 'desc');
if ($link_sort == 'sort') {
$order_by = $link_sort . ' ' . $sort;
} else {
$order_by = $link_sort . ' ' . $sort . ',sort desc';
}
$res = $coupon_type_model->getCouponTypePageList($condition, $page, $page_size, $order_by, $field);
//获取优惠券状态
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
foreach ($res['data']['list'] as $key => $val) {
$res['data']['list'][$key]['status_name'] = $coupon_type_status_arr[$val['status']];
}
return $res;
} else {
$this->forthMenu();
//优惠券状态
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
$this->assign('coupon_type_status_arr', $coupon_type_status_arr);
return $this->fetch("coupon/lists");
}
}
/**
* 排序
* @return mixed
*/
public function couponSort()
{
$sort = input('sort', 0);
$coupon_type_id = input('coupon_type_id', 0);
$coupon_type_model = new CouponTypeModel();
return $coupon_type_model->couponSort($coupon_type_id, $sort);
}
/**
* 优惠券推广
*/
public function couponUrl()
{
$coupon_type_id = input('coupon_type_id', '');
$coupon_model = new couponTypeModel();
// $coupon_info = $coupon_model->getInfo([['coupon_type_id','=',$coupon_type_id]],'coupon_type_id,coupon_name,site_id');
// $coupon_info = $coupon_info[ 'data' ];
// $res = $coupon_model->spread($coupon_info[ 'coupon_type_id' ], $coupon_info[ 'coupon_name' ], $coupon_info[ 'site_id' ]);
$res = $coupon_model->urlQrcode('/pages_tool/goods/coupon_receive', ['coupon_type_id' => $coupon_type_id], 'coupon', $this->site_id);
return $res;
}
/**
* 发送优惠券
*/
public function send()
{
$member_id = input('member_id');
$coupon_type_model = new CouponTypeModel();
if (request()->isAjax()) {
$coupon_data = json_decode(input('coupon_data', '[]'), true);
$get_type = input('get_type', 4);
if (empty($coupon_data)) {
return error('', 'REQUEST_COUPON_TYPE_ID');
}
$res = (new CouponModel())->giveCoupon($coupon_data, $this->site_id, $member_id, $get_type);
return $res;
} else {
//优惠券状态
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
$this->assign('coupon_type_status_arr', $coupon_type_status_arr);
$this->assign('member_id', $member_id);
return $this->fetch("coupon/send");
}
}
/**
* 活动列表
*/
public function couponSelect()
{
$coupon_type_model = new CouponTypeModel();
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$coupon_name = input('coupon_name', '');
$condition[] = ['site_id', '=', $this->site_id];
$condition[] = ['status', '=', 1];
$condition[] = ['coupon_name', 'like', '%' . $coupon_name . '%'];
$order = 'create_time desc';
$field = '*';
$res = $coupon_type_model->getCouponTypePageList($condition, $page, $page_size, $order, $field);
//获取优惠券状态
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
foreach ($res['data']['list'] as $key => $val) {
$res['data']['list'][$key]['status_name'] = $coupon_type_status_arr[$val['status']];
}
return $res;
} else {
//优惠券状态
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
$this->assign('coupon_type_status_arr', $coupon_type_status_arr);
$select_id = input('select_id', '');
$this->assign('select_id', $select_id);
return $this->fetch("coupon/coupon_select");
}
}
/**
* 关闭活动
*/
public function close()
{
if (request()->isAjax()) {
$coupon_type_id = input('coupon_type_id', 0);
$coupon_type_model = new CouponTypeModel();
return $coupon_type_model->closeCouponType($coupon_type_id, $this->site_id);
}
}
/**
* 删除活动
*/
public function delete()
{
if (request()->isAjax()) {
$coupon_type_id = input('coupon_type_id', 0);
$coupon_type_model = new CouponTypeModel();
return $coupon_type_model->deleteCouponType($coupon_type_id, $this->site_id);
}
}
/**
* 优惠券领取记录
* */
public function receive()
{
$coupon_model = new CouponModel();
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$coupon_type_id = input('coupon_type_id', 0);
$state = input('state', '');
$condition = [];
$condition[] = ['npc.coupon_type_id', '=', $coupon_type_id];
$condition[] = ['npc.site_id', '=', $this->site_id];
if ($state !== '') {
$condition[] = ['npc.state', '=', $state];
}
$res = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
return $res;
} else {
$coupon_type_id = input('coupon_type_id', 0);
$this->assign('coupon_type_id', $coupon_type_id);
$this->assign('get_type', $coupon_model->getCouponGetType());
return $this->fetch("coupon/receive");
}
}
/**
* 优惠券回收
*/
public function recoveryCoupon()
{
if (request()->isAjax()) {
$conpon_list = json_decode(input('coupon_list', '[]'), true);
return (new MemberCoupon())->recoveryCoupon($conpon_list, $this->site_id);
}
}
/**
* 激活支付券
*/
public function alipayMarketingActivityVoucherPublish()
{
if (request()->isAjax()) {
$coupon_type_id = input('coupon_type_id', 0);
$coupon_type = model("promotion_coupon_type")->getInfo([['coupon_type_id', '=', $coupon_type_id]]);
return (new AlipaydiscountCouponType())->alipayMarketingActivityVoucherPublish($this->site_id, $coupon_type['alipay_pass_template_id']);
}
}
}