123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace app\common\logic;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * 1.线下消费券关联使用
- * 2.不用于基础版的voucher相关功能 只是新加的功能用到
- *
- */
- class Voucher
- {
- /**
- * 购买商品 批量 获取线下抵用券
- */
- public function getVoucherListByGood($data,$memberId)
- {
- if (empty($data) || empty($data['virtual_code'])) {
- //不存在虚拟商品绑定的编号
- return true;
- }
- // 调试
- $checkHost = config('ds_config.do_net_card_url');
- $url = $checkHost.'/api/coupon.code/load';
- Log::info("绑定虚拟订单卡号:".\think\facade\Request::controller().'-'.\think\facade\Request::action());
- $checkCard = common_curl($url,
- [
- 'userId'=>$memberId,
- 'couponCode'=>$data['virtual_code'], //虚拟商品编号
- "quantity"=>$data['quantity'], //数量
- ]
- );
- if($checkCard['code']){
- ds_json_encode(10001,$checkCard['msg']);
- return false;
- }
- $resData = [];
- $time = time();
- foreach($checkCard['data'] as $k=>$v){
- $resInfo = [];
- $resInfo['voucher_code'] = '';
- $resInfo['voucher_pwd'] = '';
- $resInfo['content'] = '';
- if(!empty($v['couponCode'])){
- $resInfo['voucher_type'] = 1;
- $resInfo['voucher_code'] = $v['couponCode'];
- if(!empty($v['couponPwd'])){
- $resInfo['voucher_type'] = 2;
- $resInfo['voucher_pwd'] = $v['couponPwd'];
- }
- }else{
- $resInfo['voucher_code'] = $v['couponCode'];
- }
- if(!empty($v['qrCode'])){
- $resInfo['voucher_type'] = 3;
- $resInfo['content'] = $v['qrCode'];
- }
- $resInfo['desc'] = $v['couponName'];
- $resInfo['voucher_name'] = empty($v['couponName']) ? '' : $v['couponName'];
- $resInfo['create_time'] = $time;
- $resInfo['member_id'] = $memberId;
- $resInfo['voucher_status'] = $v['status'];
- $resInfo['start_time'] = strtotime($v['startDateTime']);
- $resInfo['expire_time'] = strtotime($v['endDateTime']);
- $resInfo['voucher_flag'] = $v['id'];
- $resInfo['tag_id'] = $data['order_id'];
- $resData[] = $resInfo;
- }
- if (empty($resData)) {
- return true; //为空直接返回true
- }
- $flagIds = array_column($resData, 'voucher_flag');
- $flagList = Db::name('sub_voucher')->where([
- ['voucher_flag', 'in', $flagIds]
- ])->column('voucher_flag');
- if (!empty($flagList)) {
- //已经存在的
- $str = "以下消费券:";
- $flagStr = implode('、', $flagList);
- $showStr = $str . $flagStr . "。已经绑定过,请刷新后查看。";
- // ds_json_encode(10001,$showStr);
- return true;
- }
- if (!empty($resData)) {
- $res = Db::name('sub_voucher')->insertAll($resData);
- if ($res) {
- return true;
- } else {
- ds_json_encode(10001, '消费券关联失败');
- }
- }
- }
- }
|