截流自动化的商城平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WechatLiveGoods.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\command;
  20. use app\common\enum\LiveGoodsEnum;
  21. use app\common\model\live\LiveGoods;
  22. use app\common\server\WxMnpLiveServer;
  23. use think\console\Command;
  24. use think\console\Output;
  25. use think\console\Input;
  26. use think\facade\Log;
  27. class WechatLiveGoods extends Command
  28. {
  29. protected function configure()
  30. {
  31. $this->setName('wechat_live_goods')
  32. ->setDescription('微信小程序直播商品状态同步');
  33. }
  34. protected function execute(Input $input, Output $output)
  35. {
  36. try {
  37. $liveGoodsModel = new LiveGoods();
  38. // 系统待微信审核的商品
  39. $localGoods = $liveGoodsModel->where([
  40. 'del' => 0,
  41. 'sys_audit_status' => LiveGoodsEnum::SYS_AUDIT_STATUS_WAIT_WECHAT
  42. ])
  43. ->select()->toArray();
  44. if (empty($localGoods)) {
  45. return true;
  46. }
  47. // 切分为20个一组,获取每组的商品状态更新
  48. $localGoods = array_chunk($localGoods, 20);
  49. foreach ($localGoods as $localGoodsItem) {
  50. $goodsIds = array_column($localGoodsItem, 'wx_goods_id');
  51. $wxGoodsData = $this->getGoods($goodsIds);
  52. $wxGoodsData = $wxGoodsData['goods'] ?? [];
  53. $wxGoodsDataColumn = array_column($wxGoodsData, null,'goods_id');
  54. $updateData = [];
  55. foreach ($localGoodsItem as $goods) {
  56. $wxGoodsId = $goods['wx_goods_id'];
  57. if (!isset($wxGoodsDataColumn[$wxGoodsId])) {
  58. continue;
  59. }
  60. $wxGoods = $wxGoodsDataColumn[$wxGoodsId];
  61. $data = [
  62. 'id' => $goods['id'],
  63. 'wx_audit_status' => $wxGoods['audit_status'],
  64. 'audit_remark' => LiveGoodsEnum::getWxAuditStatusDesc($wxGoods['audit_status']),
  65. ];
  66. if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_SUCCESS) {
  67. $data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_SUCCESS;
  68. }
  69. if ($wxGoods['audit_status'] == LiveGoodsEnum::WX_AUDIT_STATUS_FAIL) {
  70. $data['sys_audit_status'] = LiveGoodsEnum::SYS_AUDIT_STATUS_FAIL;
  71. }
  72. $updateData[] = $data;
  73. }
  74. if (!empty($updateData)) {
  75. $liveGoodsModel->saveAll($updateData);
  76. }
  77. }
  78. return true;
  79. } catch (\Exception $e) {
  80. Log::write('更新直播商品状态失败:' . $e->getMessage());
  81. return false;
  82. }
  83. }
  84. /**
  85. * @notes 获取商品
  86. * @param $goodsIds
  87. * @return array
  88. * @throws \Exception
  89. * @author 段誉
  90. * @date 2023/2/17 19:24
  91. */
  92. protected function getGoods($goodsIds)
  93. {
  94. $result = (new WxMnpLiveServer())->handle('getGoodsWarehouse', $goodsIds);
  95. if (0 != $result['errcode']) {
  96. return [];
  97. }
  98. return $result;
  99. }
  100. }