Demo.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Easemob;
  5. use app\common\library\Tenim;
  6. /**
  7. * 示例接口
  8. */
  9. class Demo extends Api
  10. {
  11. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  12. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  13. //如果接口已经设置无需登录,那也就无需鉴权了
  14. //
  15. // 无需登录的接口,*表示全部
  16. protected $noNeedLogin = ['*'];
  17. // 无需鉴权的接口,*表示全部
  18. protected $noNeedRight = ['test2'];
  19. /**
  20. * 测试方法
  21. *
  22. * @ApiTitle (测试名称)
  23. * @ApiSummary (测试描述信息)
  24. * @ApiMethod (POST)
  25. * @ApiRoute (/api/demo/test/id/{id}/name/{name})
  26. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  27. * @ApiParams (name="id", type="integer", required=true, description="会员ID")
  28. * @ApiParams (name="name", type="string", required=true, description="用户名")
  29. * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
  30. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  31. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  32. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  33. * @ApiReturn ({
  34. 'code':'1',
  35. 'msg':'返回成功'
  36. })
  37. */
  38. public function test()
  39. {
  40. $easemob = new Easemob();
  41. $rs = $easemob->user_create('doctor1');
  42. }
  43. /**
  44. * 无需登录的接口
  45. *
  46. */
  47. public function test1()
  48. {
  49. $tenim = new Tenim();
  50. $rs = $tenim->register('user'. 1, 'nickname', '');
  51. }
  52. /**
  53. * 需要登录的接口
  54. *
  55. */
  56. public function test2()
  57. {
  58. $tenim = new Tenim();
  59. $order_id = 26;
  60. $message = [
  61. 'businessID' => 'order_status',
  62. 'name' => '待接单',
  63. 'status' => '10',
  64. 'id' => (string)$order_id,
  65. 'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
  66. ];
  67. $rs = $tenim->sendCustomMessageToUser('user7','doctor7',$message);
  68. dump($rs);
  69. }
  70. /**
  71. * 需要登录且需要验证有相应组的权限
  72. *
  73. */
  74. public function test3()
  75. {
  76. $tv_userid = input('tv_userid');
  77. $tv_signtime = input('tv_signtime');
  78. $salt = 'be7bcf1499b0fec801406f6aafbd04c4';
  79. $get_sign = md5(md5($tv_userid) . $tv_signtime . $salt);
  80. dump($get_sign);
  81. }
  82. public function test4(){
  83. $a = time() - 20;
  84. dump($a);
  85. dump(get_last_time($a));
  86. }
  87. //腾讯云拍照识别商品
  88. //{"Response":{"Products":[{"Name":"按摩椅","Parents":"家用电器-个护健康","Confidence":99,"XMin":107,"YMin":59,"XMax":447,"YMax":366}],"RequestId":"187745fc-0497-441e-88f5-f3f17d854016"}}
  89. public function search_by_image($image = 'https://jiankangyijia-1304634122.cos.ap-nanjing.myqcloud.com/uploads/20240504/5cbfbb7880323f05839d82afe1500569.png'){
  90. $tencent_yun = config('tencent_yun');
  91. $secret_id = $tencent_yun['secret_id'];
  92. $secret_key = $tencent_yun['secret_key'];
  93. $token = "";
  94. $service = "tiia";
  95. $host = "tiia.tencentcloudapi.com";
  96. $req_region = "ap-beijing";
  97. $version = "2019-05-29";
  98. $action = "DetectProduct";
  99. $payload = json_encode(['ImageUrl' => localpath_to_netpath($image)]);
  100. $endpoint = "https://tiia.tencentcloudapi.com";
  101. $algorithm = "TC3-HMAC-SHA256";
  102. $timestamp = time();
  103. $date = gmdate("Y-m-d", $timestamp);
  104. // ************* 步骤 1:拼接规范请求串 *************
  105. $http_request_method = "POST";
  106. $canonical_uri = "/";
  107. $canonical_querystring = "";
  108. $ct = "application/json; charset=utf-8";
  109. $canonical_headers = "content-type:".$ct."\nhost:".$host."\nx-tc-action:".strtolower($action)."\n";
  110. $signed_headers = "content-type;host;x-tc-action";
  111. $hashed_request_payload = hash("sha256", $payload);
  112. $canonical_request = "$http_request_method\n$canonical_uri\n$canonical_querystring\n$canonical_headers\n$signed_headers\n$hashed_request_payload";
  113. // ************* 步骤 2:拼接待签名字符串 *************
  114. $credential_scope = "$date/$service/tc3_request";
  115. $hashed_canonical_request = hash("sha256", $canonical_request);
  116. $string_to_sign = "$algorithm\n$timestamp\n$credential_scope\n$hashed_canonical_request";
  117. // ************* 步骤 3:计算签名 *************
  118. $secret_date = hash_hmac("sha256", $date, "TC3".$secret_key, true);
  119. $secret_service = hash_hmac("sha256", $service, $secret_date, true);
  120. $secret_signing = hash_hmac("sha256", "tc3_request", $secret_service, true);
  121. $signature = hash_hmac("sha256", $string_to_sign, $secret_signing);
  122. // ************* 步骤 4:拼接 Authorization *************
  123. $authorization = "$algorithm Credential=$secret_id/$credential_scope, SignedHeaders=$signed_headers, Signature=$signature";
  124. // ************* 步骤 5:构造并发起请求 *************
  125. $headers = [
  126. "Authorization" => $authorization,
  127. "Content-Type" => "application/json; charset=utf-8",
  128. "Host" => $host,
  129. "X-TC-Action" => $action,
  130. "X-TC-Timestamp" => $timestamp,
  131. "X-TC-Version" => $version
  132. ];
  133. if ($req_region) {
  134. $headers["X-TC-Region"] = $req_region;
  135. }
  136. if ($token) {
  137. $headers["X-TC-Token"] = $token;
  138. }
  139. $headers = array_map(function ($k, $v) { return "$k: $v"; }, array_keys($headers), $headers);
  140. try {
  141. $timeOut = 3;
  142. $ch = curl_init();
  143. curl_setopt($ch, CURLOPT_URL, $endpoint);
  144. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  145. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  146. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
  147. curl_setopt($ch, CURLOPT_HEADER, 0);
  148. curl_setopt($ch, CURLOPT_POST, 1);
  149. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  150. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  151. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  152. $response = curl_exec($ch);
  153. curl_close($ch);
  154. //return $response;
  155. $response = json_decode($response,true);
  156. if(is_array($response) && isset($response['Response']['Products'][0]['Name'])){
  157. $searchname = $response['Response']['Products'][0]['Name'];
  158. if(!empty($searchname)){
  159. return $searchname;
  160. }
  161. }
  162. return '';
  163. } catch (Exception $err) {
  164. //echo $err->getMessage();
  165. return '';
  166. }
  167. }
  168. }