Voucher.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. use think\facade\Log;
  5. /**
  6. * 1.线下消费券关联使用
  7. * 2.不用于基础版的voucher相关功能 只是新加的功能用到
  8. *
  9. */
  10. class Voucher
  11. {
  12. /**
  13. * 购买商品 批量 获取线下抵用券
  14. */
  15. public function getVoucherListByGood($data,$memberId)
  16. {
  17. if (empty($data) || empty($data['virtual_code'])) {
  18. //不存在虚拟商品绑定的编号
  19. return true;
  20. }
  21. // 调试
  22. $checkHost = config('ds_config.do_net_card_url');
  23. $url = $checkHost.'/api/coupon.code/load';
  24. Log::info("绑定虚拟订单卡号:".\think\facade\Request::controller().'-'.\think\facade\Request::action());
  25. $checkCard = common_curl($url,
  26. [
  27. 'userId'=>$memberId,
  28. 'couponCode'=>$data['virtual_code'], //虚拟商品编号
  29. "quantity"=>$data['quantity'], //数量
  30. ]
  31. );
  32. if($checkCard['code']){
  33. ds_json_encode(10001,$checkCard['msg']);
  34. return false;
  35. }
  36. $resData = [];
  37. $time = time();
  38. foreach($checkCard['data'] as $k=>$v){
  39. $resInfo = [];
  40. $resInfo['voucher_code'] = '';
  41. $resInfo['voucher_pwd'] = '';
  42. $resInfo['content'] = '';
  43. if(!empty($v['couponCode'])){
  44. $resInfo['voucher_type'] = 1;
  45. $resInfo['voucher_code'] = $v['couponCode'];
  46. if(!empty($v['couponPwd'])){
  47. $resInfo['voucher_type'] = 2;
  48. $resInfo['voucher_pwd'] = $v['couponPwd'];
  49. }
  50. }else{
  51. $resInfo['voucher_code'] = $v['couponCode'];
  52. }
  53. if(!empty($v['qrCode'])){
  54. $resInfo['voucher_type'] = 3;
  55. $resInfo['content'] = $v['qrCode'];
  56. }
  57. $resInfo['desc'] = $v['couponName'];
  58. $resInfo['voucher_name'] = empty($v['couponName']) ? '' : $v['couponName'];
  59. $resInfo['create_time'] = $time;
  60. $resInfo['member_id'] = $memberId;
  61. $resInfo['voucher_status'] = $v['status'];
  62. $resInfo['start_time'] = strtotime($v['startDateTime']);
  63. $resInfo['expire_time'] = strtotime($v['endDateTime']);
  64. $resInfo['voucher_flag'] = $v['id'];
  65. $resInfo['tag_id'] = $data['order_id'];
  66. $resData[] = $resInfo;
  67. }
  68. if (empty($resData)) {
  69. return true; //为空直接返回true
  70. }
  71. $flagIds = array_column($resData, 'voucher_flag');
  72. $flagList = Db::name('sub_voucher')->where([
  73. ['voucher_flag', 'in', $flagIds]
  74. ])->column('voucher_flag');
  75. if (!empty($flagList)) {
  76. //已经存在的
  77. $str = "以下消费券:";
  78. $flagStr = implode('、', $flagList);
  79. $showStr = $str . $flagStr . "。已经绑定过,请刷新后查看。";
  80. // ds_json_encode(10001,$showStr);
  81. return true;
  82. }
  83. if (!empty($resData)) {
  84. $res = Db::name('sub_voucher')->insertAll($resData);
  85. if ($res) {
  86. return true;
  87. } else {
  88. ds_json_encode(10001, '消费券关联失败');
  89. }
  90. }
  91. }
  92. }