| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace addons\WechatVideoShop\common\service;
- use addons\WechatVideoShop\common\enums\WechatVideoShopCateAuditStatusEnums;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\basic\UploadQualificationModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryAuditModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryInfoModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryUploadQualificationInfoModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\cate\CategoryUploadQualificationModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\basic\UploadQualificationRequest;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryAllRequest;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryInfoRequest;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\cate\CategoryUploadQualificationRequest;
- use addons\WechatVideoShop\common\helper\WechatRequestHelper;
- use addons\WechatVideoShop\common\models\WechatVideoShopCate;
- use addons\WechatVideoShop\common\models\WechatVideoShopCateAudit;
- use common\enums\StatusEnum;
- use GuzzleHttp\Psr7\UploadedFile;
- use yii\helpers\Json;
- class CateService extends AbstractService
- {
- /**
- * @param int $mall_id
- * @param array $params
- * @return array|mixed|string
- */
- public function index(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $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;
- }
- }
|