$mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]] ]; if (!empty($params['status'])) { $where[] = ['audit_status' => $params['status']]; } if (!empty($params['shop_id'])) { $where[] = ['shop_id' => $params['shop_id']]; } $ret = WechatVideoShopCateAudit::listPage([ 'where' => $where, 'order' => 'id desc', 'limit' => $params['limit'] ?? 10, 'with' => ['catOne', 'catTwo', 'catThree', 'shop'] ]); if (!$ret) return WechatVideoShopCateAudit::getStaticError(); $ret['status_map'] = WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_REMARKS; return $ret; } /** * 同步类目 * @return void * @throws \Exception */ public function async() { $config = \Yii::$app->services->setting->addons->get(1, WechatVideoShopEnums::ADDONS_NAME, 'basic'); $resp = WechatRequestHelper::accessTokenAndToArray(new CategoryAllRequest($config)); $cats = []; try { foreach ($resp['cats'] as $item) { foreach ($item['cat_and_qua'] as $cat) { $cats[$cat['cat']['cat_id']] = [ 'id' => $cat['cat']['cat_id'], 'name' => $cat['cat']['name'], 'pid' => $cat['cat']['f_cat_id'], 'level' => $cat['cat']['level'], 'product' => Json::encode($cat['product'] ?? []), 'qua' => Json::encode($cat['qua'] ?? []), 'product_qua' => Json::encode($cat['product_qua'] ?? []), ]; } } $cats = array_values($cats); WechatVideoShopCate::find()->createCommand()->batchInsert(WechatVideoShopCate::tableName(), array_keys($cats[0]), $cats)->execute(); } catch (\Exception $e) { var_dump($e->getMessage(), $e->getFile(), $e->getLine()); } } /** * @param int $mall_id * @param array $params * @return string|true * @throws \Exception */ public function create(int $mall_id, array $params) { try { $config = (new ShopService())->getShop($params['shop_id']); $cateModel = new CategoryUploadQualificationModel(); $cateInfoModel = new CategoryUploadQualificationInfoModel(); $certificate = []; if (!empty($params['certificate'])) { foreach ($params['certificate'] as $param) { // 上传 $qualificationModel = new UploadQualificationModel(); $qualificationModel->media = $param; $uploadRequest = new UploadQualificationRequest($config); $resp = WechatRequestHelper::accessTokenAndToArray($uploadRequest, $qualificationModel); if (!empty($resp['data']['file_id'])) { $certificate[] = $resp['data']['file_id']; } } } $cateInfoModel->level1 = $params['cats'][0]; $cateInfoModel->level2 = $params['cats'][1]; $cateInfoModel->level3 = $params['cats'][2]; $cateInfoModel->certificate = $certificate; $cateModel->category_info = $cateInfoModel; $resp = WechatRequestHelper::accessTokenAndToArray(new CategoryUploadQualificationRequest($config), $cateModel); // 提交数据 if (!empty($resp['audit_id'])) { $model = WechatVideoShopCateAudit::getOne([ ['mall_id' => $mall_id], ['cat_three' => $params['cats'][2]], ['shop_id' => $params['shop_id']], ]); if (empty($model)) { $model = new WechatVideoShopCateAudit(); $model->mall_id = $mall_id; $model->cat_one = $params['cats'][0]; $model->cat_two = $params['cats'][1]; $model->cat_three = $params['cats'][2]; $model->shop_id = $params['shop_id']; } $model->audit_id = $resp['audit_id']; $model->req = $cateModel->toArray(); if (!$model->save()) throw new \Exception($model->getErrorMessage()); } return true; } catch (\Exception $e) { return $e->getMessage(); } } /** * 获取类目详情 * @param int $cat_id * @return array * @throws \Exception */ public function detail(int $cat_id) { $config = \Yii::$app->services->setting->addons->get(1, WechatVideoShopEnums::ADDONS_NAME, 'basic'); $model = new CategoryInfoModel(); $model->cat_id = $cat_id; return WechatRequestHelper::accessTokenAndToArray(new CategoryInfoRequest($config), $model); } /** * @param array $event * @return void * @throws \Exception */ public function auditResult(array $event) { $audit = WechatVideoShopCateAudit::getOne([ ['audit_id' => $event['ProductCategoryAudit']['audit_id']] ]); if (empty($audit)) throw new \Exception("记录不存在"); $audit->audit_status = $event['ProductCategoryAudit']['status']; $audit->audit_reason = $event['ProductCategoryAudit']['reason'] ?? ''; if (!$audit->save()) { throw new \Exception($audit->getErrorMessage()); } } /** * @param int $mall_id * @param int $shop_id * @return array|false|mixed|string */ public function enableList(int $mall_id, int $shop_id) { $cats = WechatVideoShopCateAudit::lists([ 'where' => [ ['mall_id' => $mall_id], ['shop_id' => $shop_id], ['audit_status' => WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_AGREE] ], 'select'=> 'cat_one, cat_two, cat_three' ]); $ret = []; if (!empty($cats)) { $one_ids = array_column($cats, "cat_one"); $two_ids = array_column($cats, "cat_two"); $three_ids = array_column($cats, "cat_three"); $ret = WechatVideoShopCate::lists([ 'where' => [ ['pid' => 0], ['status' => StatusEnum::ENABLED], ['id' => $one_ids] ], 'order' => 'id asc', 'with' => ['children' => function($query) use($two_ids, $three_ids) { $query->with(['children' => function($query1) use($three_ids) { $query1->andWhere(['id' => $three_ids]); }])->andWhere(['id' => $two_ids]); }] ]); if (!empty(WechatVideoShopCate::getStaticError())) return WechatVideoShopCate::getStaticError(); } return $ret; } /** * @param int $cat_id * @return array|string */ public function wxCatInfo(int $cat_id) { $cateModel = new CategoryInfoModel(); $cateModel->cat_id = $cat_id; try { return WechatRequestHelper::accessTokenAndToArray(new CategoryInfoRequest($this->config), $cateModel); } catch (\Exception $e) { return $e->getMessage(); } } /** * 本地微信类目列表 - 仅限未审核通过的 * @return mixed|string */ public function wxList(int $mall_id, int $shop_id) { $three_ids = WechatVideoShopCateAudit::find()->where(['mall_id' => $mall_id]) ->andWhere(['shop_id' => $shop_id]) ->andWhere(['audit_status' => WechatVideoShopCateAuditStatusEnums::AUDIT_STATUS_AGREE]) ->select('cat_three')->column(); $ret = WechatVideoShopCate::lists([ 'where' => [ ['pid' => 0], ['status' => StatusEnum::ENABLED], ], 'order' => 'id asc', 'with' => ['children' => function($query) use($three_ids) { $query->with(['children' => function($query1) use($three_ids) { $query1->andWhere(['not in', 'id', $three_ids]); }]); }] ]); if (!$ret) return WechatVideoShopCate::getStaticError(); if (!empty($ret)) { foreach ($ret as $key => $item) { if (!empty($item['children'])) { foreach ($item['children'] as $key1 => $item1) { if (empty($item1['children'])) { unset($ret[$key][$key1]); } } } else { unset($ret[$key]); } } } return $ret; } }