| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\imageSearch;
- use app\models\Option;
- use app\utils\TiiaHelper;
- use yii\base\Model;
- class StoreImageSearchSettingForm extends Model
- {
- public $group_id;
- public $group_name;
- public $max_capacity;
- public $model;
- public $store_id;
- public function rules()
- {
- return [
- [['group_id','group_name','max_capacity','model'],'required'],
- [['group_id','group_name'],'string'],
- [['max_capacity', 'store_id'],'integer']
- ];
- }
- public function save(){
- if(!$this->validate()){
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- $store_id = 0;
- $status = Option::get('tencent_image_setting_status',0,'saas',0)['value'];
- if(!$status){
- // return ['code' => 1,'msg' => '平台未开启以图搜图功能'];
- $store_id = $this->store_id;
- $status = Option::get('tencent_image_setting_status',$store_id,'saas',0)['value'];
- if (!$status) {
- return ['code' => 1,'msg' => '平台未开启以图搜图功能'];
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- $tiia = new TiiaHelper(['store_id' => $store_id]);
- // 查询图库是否存在 返回code==0的时候标识图库已存在
- $checkRes = $tiia::describeGroup($this->group_id.'_'.get_store_id());
- if($checkRes['code'] == 0){
- // 图库已存在
- $t->rollBack();
- return ['code'=>100,'msg'=>$checkRes['data']];
- }
- // 在腾讯云创建图库
- $createRes = $tiia::createGroup($this->group_id.'_'.get_store_id(),$this->group_name,(int)$this->max_capacity);
- if($createRes['code'] == 1){
- // 图库创建失败
- $t->rollBack();
- return $createRes;
- }
- $this->model->store_id = get_store_id();
- $this->model->group_id = $this->group_id.'_'.get_store_id();
- $this->model->group_name = $this->group_name;
- $this->model->max_capacity = $this->max_capacity;
- $this->model->image_num = 0;
- $this->model->created_at = time();
- if(!$this->model->save()){
- $t->rollBack();
- // 腾讯云没有提供删除图库的接口
- return ['code'=>1,'msg'=>'保存失败'];
- }
- $t->commit();
- return ['code'=> 0 ,'msg' => '图库生成成功'];
- }catch (\Exception $e){
- $t->rollBack();
- return ['code'=>1,'msg' => $e->getMessage() ];
- }
- }
- /**
- * 更新图片库图片数量数据
- * @return array
- * User: hankaige
- * DATE TIME: 2023/1/12 16:23
- */
- public function refresh(){
- $tiia = new TiiaHelper();
- // 获取图片库信息 这里不需要再拼接store_id 数据表中的store_id与腾讯云的store_id相同
- $checkRes = $tiia::describeGroup($this->model->group_id);
- if($checkRes['code'] == 0){
- $this->model->image_num = $checkRes['data']['Groups'][0]['PicCount'];
- if($this->model->save()){
- return ['code'=>0,'msg'=>'更新成功','data'=>['pic_count'=>$checkRes['data']['Groups'][0]['PicCount']]];
- }else{
- return ['code'=>1,'msg'=>'更新失败'];
- }
- }else{
- return $checkRes;
- }
- }
- }
|