Goods.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\live\model;
  11. use app\model\BaseModel;
  12. use app\model\system\Cron;
  13. use app\model\upload\Upload;
  14. class Goods extends BaseModel
  15. {
  16. private $liveStatus = [
  17. 0 => '未审核',
  18. 1 => '审核中',
  19. 2 => '审核通过',
  20. 3 => '审核驳回'
  21. ];
  22. /**
  23. * 获取直播间列表
  24. * @param array $condition
  25. * @param bool $field
  26. * @param string $order
  27. * @param int $page
  28. * @param int $list_rows
  29. * @param string $alias
  30. * @param array $join
  31. * @return array
  32. */
  33. public function getGoodsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
  34. {
  35. $data = model('weapp_goods')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
  36. if (!empty($data[ 'list' ])) {
  37. foreach ($data[ 'list' ] as $k => $item) {
  38. $data[ 'list' ][ $k ][ 'status_name' ] = $this->liveStatus[ $item[ 'status' ] ] ?? '';
  39. }
  40. }
  41. return $this->success($data);
  42. }
  43. /**
  44. * 同步商品库商品
  45. */
  46. public function syncGoods($start, $limit, $site_id, $status = 2)
  47. {
  48. $live = new Live($site_id);
  49. $result = $live->getGoodsList($start, $limit, $status);
  50. if ($result[ 'code' ] < 0) return $result;
  51. if (!empty($result[ 'data' ][ 'goods' ])) {
  52. $upload = new Upload($site_id, 'shop');
  53. $upload->setPath('upload/live/goods/');
  54. $goodsID_arr = [];
  55. foreach ($result[ 'data' ][ 'goods' ] as $item) {
  56. $goodsID_arr[] = $item[ 'goodsId' ];
  57. preg_match("/(pages\/goods\/detail\?sku_id=)(\d*)$/", $item[ 'url' ], $matches);
  58. $data = [
  59. 'site_id' => $site_id,
  60. 'goods_id' => $item[ 'goodsId' ],
  61. 'name' => $item[ 'name' ],
  62. 'price' => $item[ 'price' ],
  63. 'status' => $status,
  64. 'url' => $item[ 'url' ],
  65. 'sku_id' => $matches[ 2 ] ?? 0,
  66. 'third_party_tag' => $item[ 'thirdPartyTag' ]
  67. ];
  68. $room_info = model('weapp_goods')->getInfo([ [ 'goods_id', '=', $item[ 'goodsId' ] ], [ 'site_id', '=', $site_id ] ], 'id');
  69. if (empty($room_info)) {
  70. if (is_url($item[ 'coverImgUrl' ])) {
  71. $pull_result = $upload->remotePull($item[ 'coverImgUrl' ]);
  72. $pull_result = $pull_result[ 'data' ];
  73. if (isset($pull_result[ 'pic_path' ]) && !empty($pull_result[ 'pic_path' ])) {
  74. $data[ 'cover_img' ] = $pull_result[ 'pic_path' ];
  75. } else {
  76. $data[ 'cover_img' ] = $item[ 'coverImgUrl' ];
  77. }
  78. }
  79. model('weapp_goods')->add($data);
  80. } else {
  81. model('weapp_goods')->update($data, [ [ 'id', '=', $room_info[ 'id' ] ] ]);
  82. }
  83. }
  84. $list_id_arr = model('weapp_goods')->getList([ [ 'site_id', '=', $site_id ] ]);
  85. foreach ($list_id_arr as $key => $val) {
  86. if (!in_array($val[ 'goods_id' ], $goodsID_arr)) {
  87. model('weapp_goods')->delete([ 'goods_id' => $val[ 'goods_id' ] ]);
  88. }
  89. }
  90. $total_page = ceil($result[ 'data' ][ 'total' ] / $limit);
  91. return $this->success([ 'page' => $start, 'total_page' => $total_page ]);
  92. } else {
  93. return $this->success([ 'page' => $start, 'total_page' => 1 ]);
  94. }
  95. }
  96. /**
  97. * 添加商品
  98. * @param $param
  99. */
  100. public function addGoods($param)
  101. {
  102. if (!preg_match("/(pages\/goods\/detail\?sku_id=)(\d*)$/", $param[ 'url' ], $matches)) {
  103. return $this->error('', '商品链接格式不正确');
  104. }
  105. $live = new Live($param[ 'site_id' ]);
  106. if (is_url($param[ 'goods_pic' ])) {
  107. $upload = new Upload($param[ 'site_id' ]);
  108. $goods_pic = $upload->setPath("common/temp/" . date("Ymd") . '/')->remotePullToLocal($param[ 'goods_pic' ])[ 'data' ][ 'path' ] ?? '';
  109. $result = $live->addImageMedia($goods_pic);
  110. } else {
  111. $result = $live->addImageMedia($param[ 'goods_pic' ]);
  112. }
  113. if ($result[ 'code' ] < 0) return $result;
  114. $audit = [
  115. 'goodsInfo' => [
  116. 'coverImgUrl' => $result[ 'data' ][ 'media_id' ],
  117. 'name' => $param[ 'name' ],
  118. 'priceType' => $param[ 'price_type' ],
  119. 'price' => $param[ 'price' ],
  120. 'price2' => $param[ 'price2' ],
  121. 'url' => $param[ 'url' ]
  122. ]
  123. ];
  124. if ($param[ 'price_type' ] == 1) unset($audit[ 'goodsInfo' ][ 'price2' ]);
  125. $result = $live->addGoodsAudit($audit);
  126. if ($result[ 'code' ] < 0) return $result;
  127. $data = [
  128. 'site_id' => $param[ 'site_id' ],
  129. 'goods_id' => $result[ 'data' ][ 'goodsId' ],
  130. 'name' => $param[ 'name' ],
  131. 'cover_img' => $param[ 'goods_pic' ],
  132. 'price' => $param[ 'price' ],
  133. 'status' => 1,
  134. 'url' => $param[ 'url' ],
  135. 'audit_id' => $result[ 'data' ][ 'auditId' ],
  136. 'sku_id' => $matches[ 2 ],
  137. 'third_party_tag' => 2
  138. ];
  139. $result = model('weapp_goods')->add($data);
  140. $cron_info = model("cron")->getInfo([ [ 'event', '=', 'LiveGoodsStatus' ], [ 'relate_id', '=', $param[ 'site_id' ] ] ]);
  141. if (empty($cron_info)) {
  142. $cron = new Cron();
  143. $cron->addCron(2, 10, '小程序商品获取审核状态', 'LiveGoodsStatus', time(), $param[ 'site_id' ]);
  144. }
  145. return $this->success($result);
  146. }
  147. /**
  148. * 删除商品
  149. * @param $id
  150. * @param $site_id
  151. */
  152. public function deleteGoods($id, $site_id)
  153. {
  154. $info = model('weapp_goods')->getInfo([ [ 'site_id', '=', $site_id ], [ 'id', '=', $id ] ], 'goods_id');
  155. if (empty($info)) return $this->error('', '未获取到商品信息');
  156. $live = new Live($site_id);
  157. $result = $live->deleteGoods($info[ 'goods_id' ]);
  158. if ($result[ 'code' ] < 0) return $result;
  159. $res = model('weapp_goods')->delete([ [ 'site_id', '=', $site_id ], [ 'id', '=', $id ] ]);
  160. return $this->success($res);
  161. }
  162. /**
  163. * 获取直播商品审核状态
  164. * @param $id
  165. */
  166. public function getGoodsAuditStatus($site_id)
  167. {
  168. $prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
  169. $data = model('weapp_goods')->query("SELECT GROUP_CONCAT(goods_id) as goods_id FROM {$prefix}weapp_goods WHERE site_id = {$site_id} AND status = 1");
  170. if (isset($data[ 0 ]) && isset($data[ 0 ][ 'goods_id' ]) && !empty($data[ 0 ][ 'goods_id' ])) {
  171. $live = new Live($site_id);
  172. $result = $live->getGoodsStatus(explode(',', $data[ 0 ][ 'goods_id' ]));
  173. if ($result[ 'code' ] < 0) return $result;
  174. foreach ($result[ 'data' ] as $item) {
  175. if ($item[ 'audit_status' ] != 1) {
  176. model('weapp_goods')->update([ 'status' => $item[ 'audit_status' ] ], [ [ 'site_id', '=', $site_id ], [ 'goods_id', '=', $item[ 'goods_id' ] ] ]);
  177. }
  178. }
  179. }
  180. }
  181. }