StoreImageSearchSettingForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\imageSearch;
  8. use app\models\Option;
  9. use app\utils\TiiaHelper;
  10. use yii\base\Model;
  11. class StoreImageSearchSettingForm extends Model
  12. {
  13. public $group_id;
  14. public $group_name;
  15. public $max_capacity;
  16. public $model;
  17. public $store_id;
  18. public function rules()
  19. {
  20. return [
  21. [['group_id','group_name','max_capacity','model'],'required'],
  22. [['group_id','group_name'],'string'],
  23. [['max_capacity', 'store_id'],'integer']
  24. ];
  25. }
  26. public function save(){
  27. if(!$this->validate()){
  28. return [
  29. 'code' => 1,
  30. "msg" => $this->getErrorSummary(false)[0]
  31. ];
  32. }
  33. $store_id = 0;
  34. $status = Option::get('tencent_image_setting_status',0,'saas',0)['value'];
  35. if(!$status){
  36. // return ['code' => 1,'msg' => '平台未开启以图搜图功能'];
  37. $store_id = $this->store_id;
  38. $status = Option::get('tencent_image_setting_status',$store_id,'saas',0)['value'];
  39. if (!$status) {
  40. return ['code' => 1,'msg' => '平台未开启以图搜图功能'];
  41. }
  42. }
  43. $t = \Yii::$app->db->beginTransaction();
  44. try {
  45. $tiia = new TiiaHelper(['store_id' => $store_id]);
  46. // 查询图库是否存在 返回code==0的时候标识图库已存在
  47. $checkRes = $tiia::describeGroup($this->group_id.'_'.get_store_id());
  48. if($checkRes['code'] == 0){
  49. // 图库已存在
  50. $t->rollBack();
  51. return ['code'=>100,'msg'=>$checkRes['data']];
  52. }
  53. // 在腾讯云创建图库
  54. $createRes = $tiia::createGroup($this->group_id.'_'.get_store_id(),$this->group_name,(int)$this->max_capacity);
  55. if($createRes['code'] == 1){
  56. // 图库创建失败
  57. $t->rollBack();
  58. return $createRes;
  59. }
  60. $this->model->store_id = get_store_id();
  61. $this->model->group_id = $this->group_id.'_'.get_store_id();
  62. $this->model->group_name = $this->group_name;
  63. $this->model->max_capacity = $this->max_capacity;
  64. $this->model->image_num = 0;
  65. $this->model->created_at = time();
  66. if(!$this->model->save()){
  67. $t->rollBack();
  68. // 腾讯云没有提供删除图库的接口
  69. return ['code'=>1,'msg'=>'保存失败'];
  70. }
  71. $t->commit();
  72. return ['code'=> 0 ,'msg' => '图库生成成功'];
  73. }catch (\Exception $e){
  74. $t->rollBack();
  75. return ['code'=>1,'msg' => $e->getMessage() ];
  76. }
  77. }
  78. /**
  79. * 更新图片库图片数量数据
  80. * @return array
  81. * User: hankaige
  82. * DATE TIME: 2023/1/12 16:23
  83. */
  84. public function refresh(){
  85. $tiia = new TiiaHelper();
  86. // 获取图片库信息 这里不需要再拼接store_id 数据表中的store_id与腾讯云的store_id相同
  87. $checkRes = $tiia::describeGroup($this->model->group_id);
  88. if($checkRes['code'] == 0){
  89. $this->model->image_num = $checkRes['data']['Groups'][0]['PicCount'];
  90. if($this->model->save()){
  91. return ['code'=>0,'msg'=>'更新成功','data'=>['pic_count'=>$checkRes['data']['Groups'][0]['PicCount']]];
  92. }else{
  93. return ['code'=>1,'msg'=>'更新失败'];
  94. }
  95. }else{
  96. return $checkRes;
  97. }
  98. }
  99. }