Product.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\controller\merchant\alibaba;
  3. use think\facade\Db;
  4. use app\services\ThirdParty\AlibabaAgent\ProductService;
  5. use crmeb\basic\BaseController;
  6. class Product extends BaseController
  7. {
  8. /**
  9. * 已入库的1688商品列表
  10. *
  11. * 展示 rrx_alibaba_import_goods 表中的商品数据
  12. * GET /merchant/alibaba/product/lst?page=1&limit=20&keyword=&status=
  13. *
  14. * @return \think\Response
  15. */
  16. public function lst()
  17. {
  18. $request = app('request');
  19. $page = (int)($request->param('page', 1));
  20. $limit = (int)($request->param('limit', 20));
  21. $keyword = $request->param('keyword', '');
  22. $status = $request->param('status', '');
  23. $query = Db::name('alibaba_import_goods');
  24. // 关键词搜索(商品名称)
  25. if (!empty($keyword)) {
  26. $query->where('product_name', 'like', '%' . $keyword . '%');
  27. }
  28. // 状态筛选
  29. if ($status !== '') {
  30. $query->where('status', (int)$status);
  31. }
  32. $count = $query->count('id');
  33. $list = $query
  34. ->order('create_time', 'DESC')
  35. ->page($page, $limit)
  36. ->select()
  37. ->toArray();
  38. // 格式化返回数据
  39. foreach ($list as &$item) {
  40. $item['id'] = (int)$item['id'];
  41. $item['product_id'] = (int)$item['product_id'];
  42. $item['status'] = (int)$item['status'];
  43. $item['create_time'] = (string)$item['create_time'];
  44. // 解析 product_info JSON
  45. $productInfo = [];
  46. if (!empty($item['product_info'])) {
  47. $productInfo = json_decode($item['product_info'], true) ?: [];
  48. }
  49. $item['product_info'] = $productInfo;
  50. // 提取关键展示字段
  51. $item['price'] = $productInfo['price'] ?? 0;
  52. $item['minPrice'] = $productInfo['minPrice'] ?? 0;
  53. $item['maxPrice'] = $productInfo['maxPrice'] ?? 0;
  54. $item['stock'] = $productInfo['stock'] ?? 0;
  55. $item['unit'] = $productInfo['unit'] ?? '件';
  56. $item['sku_count'] = isset($productInfo['skuList']) ? count($productInfo['skuList']) : 0;
  57. $item['image_list'] = $productInfo['imageList'] ?? [];
  58. $item['shop_name'] = $productInfo['shopName'] ?? '';
  59. $item['category_name'] = $productInfo['categoryName'] ?? '';
  60. // 加价标记
  61. $item['markup_rate'] = $productInfo['markupRate'] ?? 0;
  62. $item['original_min_price'] = $productInfo['originalMinPrice'] ?? 0;
  63. $item['original_max_price'] = $productInfo['originalMaxPrice'] ?? 0;
  64. }
  65. unset($item);
  66. return json([
  67. 'code' => 200,
  68. 'message' => 'success',
  69. 'data' => [
  70. 'list' => $list,
  71. 'count' => $count,
  72. 'page' => $page,
  73. 'limit' => $limit,
  74. ],
  75. ]);
  76. }
  77. /**
  78. * 获取单个入库商品的详细信息
  79. *
  80. * GET /merchant/alibaba/product/detail?id=123
  81. *
  82. * @return \think\Response
  83. */
  84. public function detail()
  85. {
  86. $request = app('request');
  87. $id = (int)$request->param('id', 0);
  88. if ($id <= 0) {
  89. return json(['code' => 400, 'message' => '参数错误:id不能为空']);
  90. }
  91. $item = Db::name('alibaba_import_goods')
  92. ->where('id', $id)
  93. ->find();
  94. if (!$item) {
  95. return json(['code' => 404, 'message' => '商品不存在']);
  96. }
  97. $item['id'] = (int)$item['id'];
  98. $item['product_id'] = (int)$item['product_id'];
  99. $item['status'] = (int)$item['status'];
  100. // 解析 product_info JSON
  101. if (!empty($item['product_info'])) {
  102. $item['product_info'] = json_decode($item['product_info'], true) ?: [];
  103. }
  104. return json([
  105. 'code' => 200,
  106. 'message' => 'success',
  107. 'data' => $item,
  108. ]);
  109. }
  110. /**
  111. * 删除入库商品(软删除:修改status为0)
  112. *
  113. * POST /merchant/alibaba/product/delete
  114. * Body: id=123
  115. *
  116. * @return \think\Response
  117. */
  118. public function delete()
  119. {
  120. $request = app('request');
  121. $id = (int)$request->param('id', 0);
  122. if ($id <= 0) {
  123. return json(['code' => 400, 'message' => '参数错误:id不能为空']);
  124. }
  125. $exists = Db::name('alibaba_import_goods')->where('id', $id)->find();
  126. if (!$exists) {
  127. return json(['code' => 404, 'message' => '商品不存在']);
  128. }
  129. Db::name('alibaba_import_goods')->where('id', $id)->update(['status' => 0]);
  130. return json(['code' => 200, 'message' => '删除成功']);
  131. }
  132. /**
  133. * 正式入库:将 alibaba_import_goods 表中的商品写入 store_product 系列表
  134. *
  135. * 业务场景:从 alibaba_import_goods 表中选择已导入的1688商品,
  136. * 正式写入到 rrx_store_product、rrx_store_product_attr、rrx_store_product_attr_value 表中
  137. *
  138. * POST /merchant/alibaba/product/importStore
  139. * Body: id=123 (alibaba_import_goods 表的主键ID)
  140. * 或 Body: ids=123,456 (批量入库,逗号分隔)
  141. *
  142. * @param ProductService $productService
  143. * @return \think\Response
  144. */
  145. public function importStore(ProductService $productService)
  146. {
  147. $request = app('request');
  148. $idStr = $request->param('id', '');
  149. if (empty($idStr)) {
  150. return json(['code' => 400, 'message' => '参数错误:请传入id']);
  151. }
  152. // 解析要入库的ID列表(支持单个id或逗号分隔的多个id)
  153. $importIds = [];
  154. $parts = explode(',', $idStr);
  155. foreach ($parts as $part) {
  156. $part = (int)trim($part);
  157. if ($part > 0) $importIds[] = $part;
  158. }
  159. if (empty($importIds)) {
  160. return json(['code' => 400, 'message' => '参数错误:id格式不正确']);
  161. }
  162. $merId = $this->request->merId();;
  163. $successList = [];
  164. $failList = [];
  165. foreach ($importIds as $importId) {
  166. $result = $productService->importToStoreProduct($importId, $merId);
  167. if ($result['code'] === 200) {
  168. $successList[] = [
  169. 'import_goods_id' => $importId,
  170. 'product_id' => $result['product_id'],
  171. 'msg' => $result['msg'],
  172. ];
  173. } else {
  174. $failList[] = [
  175. 'import_goods_id' => $importId,
  176. 'msg' => $result['msg'],
  177. ];
  178. }
  179. }
  180. $message = count($successList) . '个商品入库成功';
  181. if (!empty($failList)) {
  182. $message .= ',' . count($failList) . '个商品入库失败';
  183. }
  184. return json([
  185. 'code' => 200,
  186. 'message' => $message,
  187. 'data' => [
  188. 'success' => $successList,
  189. 'fail' => $failList,
  190. ],
  191. ]);
  192. }
  193. }