Files
chazhi_shop_backend/app/common/command/WechatLiveGoods.php
2026-04-14 17:46:22 +08:00

116 lines
4.2 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
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\command;
use app\common\enum\LiveGoodsEnum;
use app\common\model\live\LiveGoods;
use app\common\server\WxMnpLiveServer;
use think\console\Command;
use think\console\Output;
use think\console\Input;
use think\facade\Log;
class WechatLiveGoods extends Command
{
protected function configure()
{
$this->setName('wechat_live_goods')
->setDescription('微信小程序直播商品状态同步');
}
protected function execute(Input $input, Output $output)
{
try {
$liveGoodsModel = new LiveGoods();
// 系统待微信审核的商品
$localGoods = $liveGoodsModel->where([
'del' => 0,
'sys_audit_status' => LiveGoodsEnum::SYS_AUDIT_STATUS_WAIT_WECHAT
])
->select()->toArray();
if (empty($localGoods)) {
return true;
}
// 切分为20个一组获取每组的商品状态更新
$localGoods = array_chunk($localGoods, 20);
foreach ($localGoods as $localGoodsItem) {
$goodsIds = array_column($localGoodsItem, 'wx_goods_id');
$wxGoodsData = $this->getGoods($goodsIds);
$wxGoodsData = $wxGoodsData['goods'] ?? [];
$wxGoodsDataColumn = array_column($wxGoodsData, null,'goods_id');
$updateData = [];
foreach ($localGoodsItem as $goods) {
$wxGoodsId = $goods['wx_goods_id'];
if (!isset($wxGoodsDataColumn[$wxGoodsId])) {
continue;
}
$wxGoods = $wxGoodsDataColumn[$wxGoodsId];
$data = [
'id' => $goods['id'],
'wx_audit_status' => $wxGoods['audit_status'],
'audit_remark' => LiveGoodsEnum::getWxAuditStatusDesc($wxGoods['audit_status']),
];
if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_SUCCESS) {
$data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_SUCCESS;
}
if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_FAIL) {
$data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_FAIL;
}
$updateData[] = $data;
}
if (!empty($updateData)) {
$liveGoodsModel->saveAll($updateData);
}
}
return true;
} catch (\Exception $e) {
Log::write('更新直播商品状态失败:' . $e->getMessage());
return false;
}
}
/**
* @notes 获取商品
* @param $goodsIds
* @return array
* @throws \Exception
* @author 段誉
* @date 2023/2/17 19:24
*/
protected function getGoods($goodsIds)
{
$result = (new WxMnpLiveServer())->handle('getGoodsWarehouse', $goodsIds);
if (0 != $result['errcode']) {
return [];
}
return $result;
}
}