CateService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopCateAuditStatusEnums;
  4. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  5. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\basic\UploadQualificationModel;
  6. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryAuditModel;
  7. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryInfoModel;
  8. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryUploadQualificationInfoModel;
  9. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryUploadQualificationModel;
  10. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\basic\UploadQualificationRequest;
  11. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryAllRequest;
  12. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryInfoRequest;
  13. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryUploadQualificationRequest;
  14. use addons\WechatVideoShop\common\helper\WechatRequestHelper;
  15. use addons\WechatVideoShop\common\models\WechatVideoShopCate;
  16. use addons\WechatVideoShop\common\models\WechatVideoShopCateAudit;
  17. use common\enums\StatusEnum;
  18. use GuzzleHttp\Psr7\UploadedFile;
  19. use yii\helpers\Json;
  20. class CateService extends AbstractService
  21. {
  22. /**
  23. * @param int $mall_id
  24. * @param array $params
  25. * @return array|mixed|string
  26. */
  27. public function index(int $mall_id, array $params)
  28. {
  29. $where = [
  30. ['mall_id' => $mall_id],
  31. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  32. ];
  33. if (!empty($params['status'])) {
  34. $where[] = ['audit_status' => $params['status']];
  35. }
  36. if (!empty($params['shop_id'])) {
  37. $where[] = ['shop_id' => $params['shop_id']];
  38. }
  39. $ret = WechatVideoShopCateAudit::listPage([
  40. 'where' => $where,
  41. 'order' => 'id desc',
  42. 'limit' => $params['limit'] ?? 10,
  43. 'with' => ['catOne', 'catTwo', 'catThree', 'shop']
  44. ]);
  45. if (!$ret) return WechatVideoShopCateAudit::getStaticError();
  46. $ret['status_map'] = WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_REMARKS;
  47. return $ret;
  48. }
  49. /**
  50. * 同步类目
  51. * @return void
  52. * @throws \Exception
  53. */
  54. public function async()
  55. {
  56. $config = \Yii::$app->services->setting->addons->get(1, WechatVideoShopEnums::ADDONS_NAME, 'basic');
  57. $resp = WechatRequestHelper::accessTokenAndToArray(new CategoryAllRequest($config));
  58. $cats = [];
  59. try {
  60. foreach ($resp['cats'] as $item) {
  61. foreach ($item['cat_and_qua'] as $cat) {
  62. $cats[$cat['cat']['cat_id']] = [
  63. 'id' => $cat['cat']['cat_id'],
  64. 'name' => $cat['cat']['name'],
  65. 'pid' => $cat['cat']['f_cat_id'],
  66. 'level' => $cat['cat']['level'],
  67. 'product' => Json::encode($cat['product'] ?? []),
  68. 'qua' => Json::encode($cat['qua'] ?? []),
  69. 'product_qua' => Json::encode($cat['product_qua'] ?? []),
  70. ];
  71. }
  72. }
  73. $cats = array_values($cats);
  74. WechatVideoShopCate::find()->createCommand()->batchInsert(WechatVideoShopCate::tableName(), array_keys($cats[0]), $cats)->execute();
  75. } catch (\Exception $e) {
  76. var_dump($e->getMessage(), $e->getFile(), $e->getLine());
  77. }
  78. }
  79. /**
  80. * @param int $mall_id
  81. * @param array $params
  82. * @return string|true
  83. * @throws \Exception
  84. */
  85. public function create(int $mall_id, array $params)
  86. {
  87. try {
  88. $config = (new ShopService())->getShop($params['shop_id']);
  89. $cateModel = new CategoryUploadQualificationModel();
  90. $cateInfoModel = new CategoryUploadQualificationInfoModel();
  91. $certificate = [];
  92. if (!empty($params['certificate'])) {
  93. foreach ($params['certificate'] as $param) {
  94. // 上传
  95. $qualificationModel = new UploadQualificationModel();
  96. $qualificationModel->media = $param;
  97. $uploadRequest = new UploadQualificationRequest($config);
  98. $resp = WechatRequestHelper::accessTokenAndToArray($uploadRequest, $qualificationModel);
  99. if (!empty($resp['data']['file_id'])) {
  100. $certificate[] = $resp['data']['file_id'];
  101. }
  102. }
  103. }
  104. $cateInfoModel->level1 = $params['cats'][0];
  105. $cateInfoModel->level2 = $params['cats'][1];
  106. $cateInfoModel->level3 = $params['cats'][2];
  107. $cateInfoModel->certificate = $certificate;
  108. $cateModel->category_info = $cateInfoModel;
  109. $resp = WechatRequestHelper::accessTokenAndToArray(new CategoryUploadQualificationRequest($config), $cateModel);
  110. // 提交数据
  111. if (!empty($resp['audit_id'])) {
  112. $model = WechatVideoShopCateAudit::getOne([
  113. ['mall_id' => $mall_id],
  114. ['cat_three' => $params['cats'][2]],
  115. ['shop_id' => $params['shop_id']],
  116. ]);
  117. if (empty($model)) {
  118. $model = new WechatVideoShopCateAudit();
  119. $model->mall_id = $mall_id;
  120. $model->cat_one = $params['cats'][0];
  121. $model->cat_two = $params['cats'][1];
  122. $model->cat_three = $params['cats'][2];
  123. $model->shop_id = $params['shop_id'];
  124. }
  125. $model->audit_id = $resp['audit_id'];
  126. $model->req = $cateModel->toArray();
  127. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  128. }
  129. return true;
  130. } catch (\Exception $e) {
  131. return $e->getMessage();
  132. }
  133. }
  134. /**
  135. * 获取类目详情
  136. * @param int $cat_id
  137. * @return array
  138. * @throws \Exception
  139. */
  140. public function detail(int $cat_id)
  141. {
  142. $config = \Yii::$app->services->setting->addons->get(1, WechatVideoShopEnums::ADDONS_NAME, 'basic');
  143. $model = new CategoryInfoModel();
  144. $model->cat_id = $cat_id;
  145. return WechatRequestHelper::accessTokenAndToArray(new CategoryInfoRequest($config), $model);
  146. }
  147. /**
  148. * @param array $event
  149. * @return void
  150. * @throws \Exception
  151. */
  152. public function auditResult(array $event)
  153. {
  154. $audit = WechatVideoShopCateAudit::getOne([
  155. ['audit_id' => $event['ProductCategoryAudit']['audit_id']]
  156. ]);
  157. if (empty($audit)) throw new \Exception("记录不存在");
  158. $audit->audit_status = $event['ProductCategoryAudit']['status'];
  159. $audit->audit_reason = $event['ProductCategoryAudit']['reason'] ?? '';
  160. if (!$audit->save()) {
  161. throw new \Exception($audit->getErrorMessage());
  162. }
  163. }
  164. /**
  165. * @param int $mall_id
  166. * @param int $shop_id
  167. * @return array|false|mixed|string
  168. */
  169. public function enableList(int $mall_id, int $shop_id)
  170. {
  171. $cats = WechatVideoShopCateAudit::lists([
  172. 'where' => [
  173. ['mall_id' => $mall_id],
  174. ['shop_id' => $shop_id],
  175. ['audit_status' => WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_AGREE]
  176. ],
  177. 'select'=> 'cat_one, cat_two, cat_three'
  178. ]);
  179. $ret = [];
  180. if (!empty($cats)) {
  181. $one_ids = array_column($cats, "cat_one");
  182. $two_ids = array_column($cats, "cat_two");
  183. $three_ids = array_column($cats, "cat_three");
  184. $ret = WechatVideoShopCate::lists([
  185. 'where' => [
  186. ['pid' => 0],
  187. ['status' => StatusEnum::ENABLED],
  188. ['id' => $one_ids]
  189. ],
  190. 'order' => 'id asc',
  191. 'with' => ['children' => function($query) use($two_ids, $three_ids) {
  192. $query->with(['children' => function($query1) use($three_ids) {
  193. $query1->andWhere(['id' => $three_ids]);
  194. }])->andWhere(['id' => $two_ids]);
  195. }]
  196. ]);
  197. if (!empty(WechatVideoShopCate::getStaticError())) return WechatVideoShopCate::getStaticError();
  198. }
  199. return $ret;
  200. }
  201. /**
  202. * @param int $cat_id
  203. * @return array|string
  204. */
  205. public function wxCatInfo(int $cat_id)
  206. {
  207. $cateModel = new CategoryInfoModel();
  208. $cateModel->cat_id = $cat_id;
  209. try {
  210. return WechatRequestHelper::accessTokenAndToArray(new CategoryInfoRequest($this->config), $cateModel);
  211. } catch (\Exception $e) {
  212. return $e->getMessage();
  213. }
  214. }
  215. /**
  216. * 本地微信类目列表 - 仅限未审核通过的
  217. * @return mixed|string
  218. */
  219. public function wxList(int $mall_id, int $shop_id)
  220. {
  221. $three_ids = WechatVideoShopCateAudit::find()->where(['mall_id' => $mall_id])
  222. ->andWhere(['shop_id' => $shop_id])
  223. ->andWhere(['audit_status' => WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_AGREE])
  224. ->select('cat_three')->column();
  225. $ret = WechatVideoShopCate::lists([
  226. 'where' => [
  227. ['pid' => 0],
  228. ['status' => StatusEnum::ENABLED],
  229. ],
  230. 'order' => 'id asc',
  231. 'with' => ['children' => function($query) use($three_ids) {
  232. $query->with(['children' => function($query1) use($three_ids) {
  233. $query1->andWhere(['not in', 'id', $three_ids]);
  234. }]);
  235. }]
  236. ]);
  237. if (!$ret) return WechatVideoShopCate::getStaticError();
  238. if (!empty($ret)) {
  239. foreach ($ret as $key => $item) {
  240. if (!empty($item['children'])) {
  241. foreach ($item['children'] as $key1 => $item1) {
  242. if (empty($item1['children'])) {
  243. unset($ret[$key][$key1]);
  244. }
  245. }
  246. } else {
  247. unset($ret[$key]);
  248. }
  249. }
  250. }
  251. return $ret;
  252. }
  253. }