Demo.php 7.7 KB

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