AipImageSearchModel.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. use ocr\AipImageSearch;
  6. use org\Qiniu;
  7. class AipImageSearchModel extends Model{
  8. protected $name = 'ocrimage';
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = true;
  11. //显示
  12. public function index($map, $Nowpage, $limits,$od)
  13. {
  14. return $this->alias ('o')
  15. //field('r.id,r.title,r.cate_id,r.photo,r.remark,r.keyword,r.content,r.views,r.type,r.is_tui,r.from,r.writer,r.ip,r.create_time,r.update_time,r.status,rc.name,r.music')
  16. //->join('article_cate rc', 'r.cate_id = rc.id')
  17. ->where($map)
  18. ->page($Nowpage, $limits)
  19. ->order($od)
  20. ->select();
  21. }
  22. //图片添加
  23. public function add($data)
  24. {
  25. $add=$this->allowField(true)->save($data);
  26. //$add=1;
  27. $oid=$this->oid;
  28. //halt($data);
  29. if($add){
  30. $APP_ID = "19011542";
  31. $API_KEY = "8IhdHiBUdLRgKzxbt4Lh5FTq";
  32. $SRCRET_KEY = "Ku1lOeQjNYmG5nabOjq3t1xu5yaLINv8";
  33. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  34. //$image=$data["product_image"];
  35. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  36. //$image = ROOT_PATH . 'public' . DS . $data["product_image"];
  37. //echo $dir;die;
  38. // 如果有可选参数
  39. $options = array();
  40. $arr=array("name"=>$data["pruduct_name"],"id"=>$oid);
  41. $options["brief"] =json_encode($arr);
  42. $options["class_id1"] = 1;
  43. $options["class_id2"] = 1;
  44. //halt($options);
  45. // 带参数调用商品图检索—入库, 图片参数为本地图片
  46. $res=$client->sameHqAdd($image, $options);
  47. //halt($res);
  48. //halt($res);
  49. if(isset($res["error_code"])){
  50. $del = Db::name('ocrimage')->where('oid',$oid)->delete();
  51. return json(["code"=>100,"msg"=>"上传失败"]);
  52. }
  53. if($res){
  54. return json(["code"=>200,"msg"=>"上传成功"]);
  55. }else{
  56. $del = Db::name('ocrimage')->where('oid',$oid)->delete();
  57. return json(["code"=>100,"msg"=>"上传失败"]);
  58. }
  59. }
  60. else{
  61. return json(["code"=>100,"msg"=>"添加失败"]);
  62. }
  63. }
  64. //图库信息修改
  65. public function edit($data){
  66. $data['type'] = 1;
  67. $edit=$this->allowField(true)->save($data,["oid"=>$data['oid']]);
  68. if($edit){
  69. return json(["code"=>200,"msg"=>"修改成功"]);
  70. }else{
  71. return json(["code"=>100,"msg"=>"修改失败"]);
  72. }
  73. }
  74. //图库删除
  75. public function del($oid)
  76. {
  77. $data=$this->where("oid",$oid)->find();
  78. $del=$this->where("oid",$oid)->delete();
  79. $APP_ID = "17529719";
  80. $API_KEY = "xSNZEGbH8BmG84b4UBcOjIm5";
  81. $SRCRET_KEY = "0PEGcgmGQnTPWb7mqWSAZvBRqHcDEz9Z";
  82. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  83. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  84. // 调用删除商品,传入参数为图片
  85. $res=$client->sameHqDeleteByImage($image);
  86. if($del&&$res){
  87. return json(["code"=>200,"data"=>"删除成功"]);
  88. }else{
  89. return json(["code"=>100,"data"=>"删除失败"]);
  90. }
  91. }
  92. }