AipImageSearchModel.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. {
  9. protected $name = 'ocrimage';
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = true;
  12. //显示
  13. public function index($map, $Nowpage, $limits, $od)
  14. {
  15. return $this->alias('o')
  16. //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')
  17. //->join('article_cate rc', 'r.cate_id = rc.id')
  18. ->where($map)
  19. ->page($Nowpage, $limits)
  20. ->order($od)
  21. ->select();
  22. }
  23. //图片添加
  24. public function add($data)
  25. {
  26. $add = $this->allowField(true)->save($data);
  27. //$add=1;
  28. $oid = $this->oid;
  29. //halt($data);
  30. if ($add) {
  31. $APP_ID = "17529719";
  32. $API_KEY = "xSNZEGbH8BmG84b4UBcOjIm5";
  33. $SRCRET_KEY = "0PEGcgmGQnTPWb7mqWSAZvBRqHcDEz9Z";
  34. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  35. //$image=$data["product_image"];
  36. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  37. //$image = ROOT_PATH . 'public' . DS . $data["product_image"];
  38. //echo $dir;die;
  39. // 如果有可选参数
  40. $options = [];
  41. $arr = ["name" => $data["pruduct_name"], "id" => $oid];
  42. $options["brief"] = json_encode($arr);
  43. $options["class_id1"] = 1;
  44. $options["class_id2"] = 1;
  45. //halt($options);
  46. // 带参数调用商品图检索—入库, 图片参数为本地图片
  47. $res = $client->productAdd($image, $options);
  48. //halt($res);
  49. if (isset($res["error_code"])) {
  50. return json(["code" => 100, "msg" => "上传失败"]);
  51. }
  52. if ($res) {
  53. return json(["code" => 200, "msg" => "上传成功"]);
  54. } else {
  55. return json(["code" => 100, "msg" => "上传失败"]);
  56. }
  57. } else {
  58. return json(["code" => 100, "msg" => "添加失败"]);
  59. }
  60. }
  61. //图库信息修改
  62. public function edit($data)
  63. {
  64. $edit = $this->allowField(true)->save($data, ["oid" => $data['oid']]);
  65. if ($edit) {
  66. return json(["code" => 200, "msg" => "修改成功"]);
  67. } else {
  68. return json(["code" => 100, "msg" => "修改失败"]);
  69. }
  70. }
  71. //图库删除
  72. public function del($oid)
  73. {
  74. $data = $this->where("oid", $oid)->find();
  75. $del = $this->where("oid", $oid)->delete();
  76. $APP_ID = "17529719";
  77. $API_KEY = "xSNZEGbH8BmG84b4UBcOjIm5";
  78. $SRCRET_KEY = "0PEGcgmGQnTPWb7mqWSAZvBRqHcDEz9Z";
  79. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  80. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  81. // 调用删除商品,传入参数为图片
  82. $res = $client->productDeleteByImage($image);
  83. if ($del && $res) {
  84. return json(["code" => 200, "data" => "删除成功"]);
  85. } else {
  86. return json(["code" => 100, "data" => "删除失败"]);
  87. }
  88. }
  89. }