359 lines
17 KiB
PHP
359 lines
17 KiB
PHP
<?php
|
||
|
||
namespace addon\cardservice\model;
|
||
|
||
use addon\aliapp\model\MinCode;
|
||
use addon\aliapp\model\CloudPay;
|
||
use app\model\BaseModel;
|
||
use app\model\image\ImageService;
|
||
|
||
class AliGoodsTemplate extends BaseModel
|
||
{
|
||
|
||
private $mime = [
|
||
'image/jpeg' => '.jpg',
|
||
'image/png' => '.png',
|
||
'image/gif' => '.gif',
|
||
'image/bmp' => '.bmp',
|
||
'image/webp' => '.webp',
|
||
];
|
||
public $image_service;
|
||
public $driver;
|
||
|
||
public function getCardType($cloud_card_type)
|
||
{
|
||
switch ($cloud_card_type) {
|
||
case 'week':
|
||
case 'discount_week':
|
||
$card_type = 'PERIOD_PAY';
|
||
$cycle_type = 'WEEK';
|
||
break;
|
||
case 'discount_month':
|
||
case 'month':
|
||
$card_type = 'PERIOD_PAY';
|
||
$cycle_type = 'MONTH';
|
||
break;
|
||
case 'discount_num':
|
||
case 'num':
|
||
$card_type = 'TIMES_CARD';
|
||
$cycle_type = '';
|
||
break;
|
||
}
|
||
$typearr = [
|
||
'card_type' => $card_type,
|
||
'cycle_type' => $cycle_type,
|
||
];
|
||
return $typearr;
|
||
}
|
||
|
||
/***
|
||
* 添加产品模版
|
||
* @param $site_id
|
||
* @param $goods_id
|
||
* @param $data
|
||
* @return array|void
|
||
*/
|
||
public function CreateAndModifyTemplate($site_id, $goods_id, $data, $type = 'modify')
|
||
{
|
||
$card_template_model = new MinCode($site_id);
|
||
$appid = $card_template_model->getApp()->uconfig['appid'];//APPid
|
||
if (empty($appid)) return $this->error('您还为开通支付宝小程序,请先开通');
|
||
if (mb_strlen($data['goods_name']) < 3) return $this->error('商品名称长度需大于等于最小长度3');//官方接口要求商品名字字符串长度必须大于等于3
|
||
$mystore = [
|
||
['site_id', '=', $site_id],
|
||
['is_mystore', '=', 1],
|
||
// ['my_status', '>', 0],
|
||
];
|
||
if ($data['sale_store'] != 'all') {
|
||
$mystore[] = ['store_id', 'in', explode(',', $data['sale_store'])];
|
||
}
|
||
$store_store_info = model('store')->getColumn($mystore, 'store_id,storeinfo', 'store_id');
|
||
if (empty($store_store_info)) return $this->error('请先添加蚂蚁门店');
|
||
$usable_shop_list = [];
|
||
foreach ($store_store_info as $k => $v) {
|
||
$store_info = json_decode($v['storeinfo'], true);
|
||
if (isset($store_info['shop_id']) && $store_info['shop_id']) {
|
||
$usable_shop_list[] = $store_info['shop_id'];
|
||
}
|
||
}
|
||
|
||
if (empty($usable_shop_list)) return $this->error('请先添加蚂蚁门店');
|
||
$endTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y') + 1);
|
||
$template_data = [
|
||
'card_template_name' => $data['goods_name'], //商品名称
|
||
'settle_type' => '1',//结算方式 1统一收款,2统一结算到门店
|
||
'out_card_id' => (string)$goods_id,
|
||
'card_template_app_id' => (string)$appid,//APPid
|
||
'card_type' => $this->getCardType($data['cloud_card_type'])['card_type'], // 次卡: TIMES_CARD周期付: PERIOD_PAY
|
||
'category_id' => $data['alipay_cat_id'],
|
||
'sale_info' => [ //售卖信息 开始时间 结束时间
|
||
'salable_start_time' => date('Y-m-d H:i:s'), //Y-m-d H:i:s
|
||
'salable_end_time' => date('Y-m-d H:i:s', $endTime), //
|
||
],
|
||
'use_info' => [
|
||
'usable_count' => (int)$data['periods'], //可用次数
|
||
'use_instruction' => $data['use_rule'],//使用规则
|
||
'usable_shop_list' => $usable_shop_list,//蚂蚁门店
|
||
'period_price_list' => [],//阶梯价格
|
||
'expire_period' => '', //有效期 次卡有效
|
||
'cycle_info' => [
|
||
'cycle_type' => $this->getCardType($data['cloud_card_type'])['cycle_type'], //周: WEEK月: MONTH
|
||
'cycle_charge_type' => 'FIXED_DATE', //用户可指定固定日期开始扣费: USER_DESIGN指定固定日期开始扣费: FIXED_DATE指定固定天数后开始扣费: FIXED_DAYS
|
||
'cycle_value' => '1', //【描述】根据周期计费类型来传递参数值 1.固定日期开始扣费,需要传入周几(1-7)或每月几号(1-28)开始扣费 2.指定固定天数后开始扣费,需要传入具体多少天(0-30)
|
||
'user_select_range_start' => '0'//【描述】周期计费类型为USER_DESIGN,即用户可指定固定日期开始扣费的场景下,商家可以控制用户下单可选择时间范围,如下单时用户只能选0-30天范围内的时间作为首期计费时间,则该参数传0
|
||
],
|
||
'discount_recover_rule_info' => [ //退款规则
|
||
'least_period' => (int)$data['least_period'] ?? 0, //用户使用满多少期后才允许用户主动解约,不传默认不允许用户主动解约
|
||
'recover_rule' => 'NEED'//需要追回优惠: NEED不需要追回优惠: UNNEED
|
||
],
|
||
'reservation_url' => 'alipays://platformapi/startapp?appId=' . $appid . '&page=pages_promotion/cardservice/service_goods/reserve_list',//周期付场景下预约链接,填写小程序地址。当card_type= PERIOD_PAY时生效。
|
||
// 'use_method' => [
|
||
// [//用户可选的使用方式。当card_type= TIMES_CARD时生效。不传默认为到店亮码使用。
|
||
// 'use_method_type' => '2', //到店亮码使用: 1商家小程序内部使用: 2
|
||
// 'use_path' => 'alipays://platformapi/startapp?appId=' . $appid //小程序使用链接
|
||
// ]
|
||
// ],
|
||
],
|
||
'image_id_list' => [],//图片地址
|
||
];
|
||
if (!$data['least_period']) {
|
||
unset($template_data['use_info']['discount_recover_rule_info']['least_period']);
|
||
}
|
||
if ($data['cloud_card_type'] == 'discount_num' || $data['cloud_card_type'] == 'num') {
|
||
if ($data['validity_type'] == 0) {
|
||
$template_data['use_info']['expire_period'] = 9999;
|
||
} else if ($data['validity_type'] == 1) {
|
||
$template_data['use_info']['expire_period'] = $data['validity_day'];
|
||
} else if ($data['validity_type'] == 2) {
|
||
$template_data['use_info']['expire_period'] = ($data['validity_time'] - time()) / 86400;
|
||
}
|
||
} else {
|
||
unset($template_data['use_info']['expire_period']);
|
||
}
|
||
$goods_image = $data['goods_image'];
|
||
$this->driver = config('upload')['driver'] ?? 'gd';
|
||
$this->image_service = new ImageService($this->driver);
|
||
$image_id_list = [];
|
||
$goods_image_arr = $this->handleImg($goods_image, 'BIG');
|
||
foreach ($goods_image_arr as $img_k => $img_y) {
|
||
if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) {
|
||
$image = $this->image_service->open(img($img_y))->thumb(750, 750);
|
||
$file = root_path() . 'runtime/temp/' . $goods_id . $this->mime[$image->image->mime];
|
||
$image->save($file, 100);
|
||
$image = $card_template_model->imageUpload($img_k . $img_y, $file, true);
|
||
$res = $image['alipay_offline_material_image_upload_response'];
|
||
if ($res['code'] == 10000) {
|
||
if (count($image_id_list) < 3) {
|
||
$image_id_list[] = $res['image_id'];
|
||
} else {
|
||
continue;
|
||
}
|
||
}
|
||
} else {
|
||
if (count($image_id_list) < 3) {
|
||
$image_id_list[] = $img_y;
|
||
}
|
||
}
|
||
}
|
||
$template_data['image_id_list'] = $image_id_list;
|
||
$period_price_list = [];
|
||
if ($data['discount_type'] == 1) {
|
||
$downPaymentPeriods = $data['downPaymentPeriods'];
|
||
if ($downPaymentPeriods) {
|
||
for ($i = 1; $i <= $downPaymentPeriods; $i++) {
|
||
$period_price_list[] = [
|
||
'period' => $i, //期数
|
||
'sale_price' => (int)$data['downPayment'] * 100,//优惠价
|
||
'original_price' => (int)$data['average_original_price'] * 100 //原价
|
||
];
|
||
}
|
||
}
|
||
for ($i = $downPaymentPeriods + 1; $i <= $data['periods']; $i++) {
|
||
$period_price_list[] = [
|
||
'period' => $i, //期数
|
||
'sale_price' => (int)$data['average_discount_price'] * 100,//优惠价
|
||
'original_price' => (int)$data['average_original_price'] * 100 //原价
|
||
];
|
||
}
|
||
} else {
|
||
$downPaymentPeriods = str_replace(',', ',', $data['downPaymentPeriods']);
|
||
$PaymentPeriods = explode(',', $downPaymentPeriods);
|
||
$periods = $data['periods'];
|
||
$remain = ($periods - count($PaymentPeriods));
|
||
for ($i = 1; $i <= $remain; $i++) {
|
||
$period_price_list[] = [
|
||
'period' => $i, //期数
|
||
'sale_price' => (int)$data['average_discount_price'] * 100,//优惠价
|
||
'original_price' => (int)$data['average_original_price'] * 100 //原价
|
||
];
|
||
}
|
||
if (count($PaymentPeriods)) {
|
||
foreach ($PaymentPeriods as $k => $v) {
|
||
$period_price_list[] = [
|
||
'period' => $k + $remain + 1, //期数
|
||
'sale_price' => (int)$v * 100,//优惠价
|
||
'original_price' => (int)$data['average_original_price'] * 100 //原价
|
||
];
|
||
}
|
||
}
|
||
}
|
||
if (count($period_price_list) > 0) {
|
||
foreach ($period_price_list as $key => $val) {
|
||
//优惠价格最多优惠30%
|
||
if ($val['sale_price'] < bcsub($val['original_price'], bcmul($val['original_price'], 0.3))) {
|
||
return $this->error(-1, '开卡优惠价格最多优惠30%');
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
$template_data['use_info']['period_price_list'] = $period_price_list;
|
||
if ($type == 'modify') {
|
||
$template_data['card_template_id'] = $data['card_template_id'];//卡项模版ID
|
||
$template_data['card_template_status'] = 'PUBLISHED'; //卡项状态
|
||
if ($template_data['card_type'] == 'PERIOD_PAY') {
|
||
//当商品类型是周期付的时候,需要删掉用户使用方式,不然接口会报错参数非法,官方文档指明当card_type= TIMES_CARD时生效
|
||
unset($template_data['use_info']['use_method']);
|
||
} else if ($template_data['card_type'] == 'TIMES_CARD') {
|
||
//预约链接与上面同理,当card_type= PERIOD_PAY时生效
|
||
unset($template_data['use_info']['reservation_url']);
|
||
}
|
||
$api = 'alipay.commerce.merchantcard.template.modify';
|
||
$key = 'alipay_commerce_merchantcard_template_modify_response';
|
||
} else {
|
||
$api = 'alipay.commerce.merchantcard.template.create';
|
||
$key = 'alipay_commerce_merchantcard_template_create_response';
|
||
}
|
||
$template = [
|
||
'card_template' => $template_data
|
||
];
|
||
$res = $card_template_model->requestApi($api, $template)[$key];
|
||
if ($res['code'] != '10000') {
|
||
return $this->error(-1, '暂时不支持公域推广' . $res['sub_msg']);
|
||
} else {
|
||
return $this->success($res);
|
||
}
|
||
}
|
||
|
||
|
||
/***
|
||
* 创建1.5商品版本
|
||
* @param $site_id
|
||
* @param $goods_id
|
||
* @param $data
|
||
* @return array
|
||
*/
|
||
public function CreateUpdatePublic($site_id, $goods_id, $data)
|
||
{
|
||
$card_template_model = new MinCode($site_id);
|
||
$appid = $card_template_model->getApp()->uconfig['appid'];//APPid
|
||
if (empty($appid)) return $this->error('您还为开通支付宝小程序,请先开通');
|
||
if (mb_strlen($data['goods_name']) < 3) return $this->error('商品名称长度需大于等于最小长度3');//官方接口要求商品名字字符串长度必须大于等于3
|
||
$mystore = [
|
||
['site_id', '=', $site_id],
|
||
['is_mystore', '=', 1],
|
||
];
|
||
if ($data['sale_store'] != 'all') {
|
||
$mystore[] = ['store_id', 'in', explode(',', $data['sale_store'])];
|
||
}
|
||
$store_store_info = model('store')->getColumn($mystore, 'store_id,storeinfo', 'store_id');
|
||
if (empty($store_store_info)) return $this->error('请先添加蚂蚁门店');
|
||
$usable_shop_list = [];
|
||
foreach ($store_store_info as $k => $v) {
|
||
$store_info = json_decode($v['storeinfo'], true);
|
||
if (isset($store_info['shop_id']) && $store_info['shop_id']) {
|
||
$usable_shop_list[] = $store_info['shop_id'];
|
||
}
|
||
}
|
||
if (empty($usable_shop_list)) return $this->error('请先添加蚂蚁门店');
|
||
$pay = new \addon\alipay\model\Config();
|
||
$payConfig = $pay->getPayConfig($site_id)['data']['value'];
|
||
$goods_image = $data['goods_image'];
|
||
$PicUrl = $this->PicUrl($goods_image);
|
||
$pushData=[
|
||
'merchantPid'=>$payConfig['merchant_smid']??'2088730454482465',
|
||
'productNo'=>$goods_id,
|
||
'productName'=>$data['goods_name'],
|
||
'subscriptionType'=>strtoupper($data['cloud_card_type']),//月卡 MONTH周卡 WEEK 优惠月卡 DISCOUNT_MONTH 优惠周卡 DISCOUNT_WEEK
|
||
'periods'=>$data['periods'],
|
||
'deductionAmount'=>$data['average_discount_price'], //优惠价格
|
||
'downPaymentPeriods'=>$data['downPaymentPeriods'],
|
||
'downPayment'=>$data['downPayment'], //优惠在优惠
|
||
'applicableShops'=>implode(',',$usable_shop_list),//适用门店
|
||
'description'=>$data['introduction']?:$data['goods_content'],//商品描述
|
||
'productCoverPicUrl'=>$PicUrl, //商品封面图URL
|
||
'originalPrice'=>$data['average_original_price'], //优惠月卡/优惠周卡
|
||
// 'purchaseNotesList'=>'',//使用规则
|
||
// 'moreUseMoreDiscount'=>'',
|
||
// 'perPeriodPriceArray'=>'',
|
||
// 'allowSurrenderPeriods'=>'', //小于总周期数可解约
|
||
// 'deductionDays'=>'', //指定扣款天数
|
||
];
|
||
$least_periods = $data['least_period'];
|
||
if($least_periods>0){
|
||
$pushData['deductionDays']=$least_periods;
|
||
}
|
||
$use_rule=explode("\n",$data['use_rule']);
|
||
$use_rule=array_map(function ($v) {
|
||
return ['rule'=>$v];
|
||
},$use_rule);
|
||
$pushData['purchaseNotesList'] =$use_rule;
|
||
$CloudPay=new CloudPay();
|
||
$info=$CloudPay->saveOrUpdate($pushData)['response'];
|
||
if($info['code']!=10000){
|
||
return $this->error(-1,$info['sub_msg']);
|
||
}else{
|
||
return $this->success($info);
|
||
}
|
||
}
|
||
/**
|
||
* 处理分割图片
|
||
* @param $images
|
||
* @return false|string[]
|
||
*/
|
||
private function handleImg($images, $size = '')
|
||
{
|
||
$img_arr = explode(',', $images);
|
||
if ($size) {
|
||
$thumb_value = model('album_pic')->getColumn(['pic_path' => $img_arr], 'thumb_value', 'pic_path');
|
||
$img_arr = array_map(function ($v) use ($thumb_value, $size) {
|
||
if (array_key_exists($v, $thumb_value)) {
|
||
$value = json_decode($thumb_value[$v], true);
|
||
if (array_key_exists($size, $value)) {
|
||
$img_path = $value[$size];
|
||
$thumb_name = $img_path['thumb_name'];
|
||
parse_str($thumb_name, $srt);
|
||
$v = array_values($srt);
|
||
if ($v) {
|
||
return $v[0];
|
||
} else {
|
||
return img($v);
|
||
}
|
||
}
|
||
}
|
||
return img($v);
|
||
}, $img_arr);
|
||
} else {
|
||
$img_arr = array_map('img', $img_arr);
|
||
}
|
||
return $img_arr;
|
||
}
|
||
|
||
|
||
/***
|
||
* 上传图片
|
||
* @param $images
|
||
* @return mixed
|
||
*/
|
||
private function PicUrl($images){
|
||
$driver = config('upload')['driver'] ?? 'gd';
|
||
$image_service = new ImageService($driver);
|
||
$image = $image_service->open($images)->thumb(400, 600);
|
||
$file = root_path() . 'runtime/temp/' . md5($images) . $this->mime[$image->image->mime];
|
||
$image->save($file, 100);
|
||
$img = UrlimgBase64($file);
|
||
$pay = new CloudPay();
|
||
$picture = $pay->logoUpload($img);
|
||
return $picture['pictureUrl'];
|
||
}
|
||
}
|