Demo.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. public function yidong_sms(){
  22. $mobile = '18560505277';
  23. $code = '1234';
  24. $content = '您的短信验证码是'.$code.',本验证码五分钟内有效,请勿泄露';
  25. $url = 'https://112.35.10.201:28888/sms/submit';
  26. $data = [
  27. 'ecName' => '江苏达富居家养老健康管理有限公司',
  28. 'apId' => 'dafuhttps',
  29. 'secretKey' => '&YY4vrJb',
  30. 'mobiles' => $mobile,
  31. 'content' => $content,
  32. 'sign' => 'YV61594FE',
  33. 'addSerial' => '',
  34. ];
  35. $mac = md5(join('',$data));
  36. $data['mac'] = $mac;
  37. unset($data['secretKey']);
  38. $header = ["Content-Type:application/json;charset=UTF-8"];
  39. $rs = curl_post($url,base64_encode(json_encode($data)),$header);
  40. dump($rs);
  41. $rs = json_decode($rs,true);
  42. if(isset($rs['success']) && $rs['success'] == true){
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * 测试方法
  49. *
  50. * @ApiTitle (测试名称)
  51. * @ApiSummary (测试描述信息)
  52. * @ApiMethod (POST)
  53. * @ApiRoute (/api/demo/test/id/{id}/name/{name})
  54. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  55. * @ApiParams (name="id", type="integer", required=true, description="会员ID")
  56. * @ApiParams (name="name", type="string", required=true, description="用户名")
  57. * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
  58. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  59. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  60. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  61. * @ApiReturn ({
  62. 'code':'1',
  63. 'msg':'返回成功'
  64. })
  65. */
  66. public function test()
  67. {
  68. $easemob = new Easemob();
  69. $rs = $easemob->user_create('doctor1');
  70. }
  71. /**
  72. * 无需登录的接口
  73. *
  74. */
  75. public function test1()
  76. {
  77. $list = Db::name('user')->select();
  78. foreach($list as $key => $val){
  79. $tenim = new Tenim();
  80. $rs = $tenim->register('user'. $val['id'], $val['nickname'], '');
  81. dump($rs);
  82. }
  83. }
  84. public function test12()
  85. {
  86. $list = Db::name('doctor')->where('status',1)->select();
  87. foreach($list as $key => $val){
  88. $tenim = new Tenim();
  89. $rs = $tenim->register('doctor'. $val['id'], $val['nickname'], '');
  90. dump($rs);
  91. }
  92. }
  93. /**
  94. * 需要登录的接口
  95. *
  96. */
  97. public function test2()
  98. {
  99. $tenim = new Tenim();
  100. $order_id = 26;
  101. $message = [
  102. 'businessID' => 'order_status',
  103. 'name' => '待接单',
  104. 'status' => '10',
  105. 'id' => (string)$order_id,
  106. 'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
  107. ];
  108. $rs = $tenim->sendCustomMessageToUser('user7','doctor7',$message);
  109. dump($rs);
  110. }
  111. /**
  112. * 需要登录且需要验证有相应组的权限
  113. *
  114. */
  115. public function test3()
  116. {
  117. $tv_userid = input('tv_userid');
  118. $tv_signtime = input('tv_signtime');
  119. $salt = 'be7bcf1499b0fec801406f6aafbd04c4';
  120. $get_sign = md5(md5($tv_userid) . $tv_signtime . $salt);
  121. dump($get_sign);
  122. }
  123. public function test4(){
  124. $wenzhen_order['user_id'] = 13;
  125. //判断患者在线状态
  126. $tenim = new Tenim();
  127. $rs_online = $tenim->is_online('user'.$wenzhen_order['user_id']);
  128. if($rs_online != true){
  129. $this->error('患者现在不在线,请稍后拨打');
  130. }
  131. echo '在线';
  132. }
  133. public function test5(){
  134. $checkmap = [
  135. 'user_id' => 34,
  136. 'comefrom'=> 2,
  137. 'status' => ['IN','10,20,25,30'],//有效订单
  138. ];
  139. $check_order = Db::name('wenzhen_order')->where($checkmap)->whereTime('createtime','month')->find();
  140. echo db()->getLastSql();
  141. }
  142. //腾讯云拍照识别商品
  143. //{"Response":{"Products":[{"Name":"按摩椅","Parents":"家用电器-个护健康","Confidence":99,"XMin":107,"YMin":59,"XMax":447,"YMax":366}],"RequestId":"187745fc-0497-441e-88f5-f3f17d854016"}}
  144. public function search_by_image($image = 'https://jiankangyijia-1304634122.cos.ap-nanjing.myqcloud.com/uploads/20240504/5cbfbb7880323f05839d82afe1500569.png'){
  145. $tencent_yun = config('tencent_yun');
  146. $secret_id = $tencent_yun['secret_id'];
  147. $secret_key = $tencent_yun['secret_key'];
  148. $token = "";
  149. $service = "tiia";
  150. $host = "tiia.tencentcloudapi.com";
  151. $req_region = "ap-beijing";
  152. $version = "2019-05-29";
  153. $action = "DetectProduct";
  154. $payload = json_encode(['ImageUrl' => localpath_to_netpath($image)]);
  155. $endpoint = "https://tiia.tencentcloudapi.com";
  156. $algorithm = "TC3-HMAC-SHA256";
  157. $timestamp = time();
  158. $date = gmdate("Y-m-d", $timestamp);
  159. // ************* 步骤 1:拼接规范请求串 *************
  160. $http_request_method = "POST";
  161. $canonical_uri = "/";
  162. $canonical_querystring = "";
  163. $ct = "application/json; charset=utf-8";
  164. $canonical_headers = "content-type:".$ct."\nhost:".$host."\nx-tc-action:".strtolower($action)."\n";
  165. $signed_headers = "content-type;host;x-tc-action";
  166. $hashed_request_payload = hash("sha256", $payload);
  167. $canonical_request = "$http_request_method\n$canonical_uri\n$canonical_querystring\n$canonical_headers\n$signed_headers\n$hashed_request_payload";
  168. // ************* 步骤 2:拼接待签名字符串 *************
  169. $credential_scope = "$date/$service/tc3_request";
  170. $hashed_canonical_request = hash("sha256", $canonical_request);
  171. $string_to_sign = "$algorithm\n$timestamp\n$credential_scope\n$hashed_canonical_request";
  172. // ************* 步骤 3:计算签名 *************
  173. $secret_date = hash_hmac("sha256", $date, "TC3".$secret_key, true);
  174. $secret_service = hash_hmac("sha256", $service, $secret_date, true);
  175. $secret_signing = hash_hmac("sha256", "tc3_request", $secret_service, true);
  176. $signature = hash_hmac("sha256", $string_to_sign, $secret_signing);
  177. // ************* 步骤 4:拼接 Authorization *************
  178. $authorization = "$algorithm Credential=$secret_id/$credential_scope, SignedHeaders=$signed_headers, Signature=$signature";
  179. // ************* 步骤 5:构造并发起请求 *************
  180. $headers = [
  181. "Authorization" => $authorization,
  182. "Content-Type" => "application/json; charset=utf-8",
  183. "Host" => $host,
  184. "X-TC-Action" => $action,
  185. "X-TC-Timestamp" => $timestamp,
  186. "X-TC-Version" => $version
  187. ];
  188. if ($req_region) {
  189. $headers["X-TC-Region"] = $req_region;
  190. }
  191. if ($token) {
  192. $headers["X-TC-Token"] = $token;
  193. }
  194. $headers = array_map(function ($k, $v) { return "$k: $v"; }, array_keys($headers), $headers);
  195. try {
  196. $timeOut = 3;
  197. $ch = curl_init();
  198. curl_setopt($ch, CURLOPT_URL, $endpoint);
  199. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  200. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  201. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
  202. curl_setopt($ch, CURLOPT_HEADER, 0);
  203. curl_setopt($ch, CURLOPT_POST, 1);
  204. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  205. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  206. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  207. $response = curl_exec($ch);
  208. curl_close($ch);
  209. //return $response;
  210. $response = json_decode($response,true);
  211. if(is_array($response) && isset($response['Response']['Products'][0]['Name'])){
  212. $searchname = $response['Response']['Products'][0]['Name'];
  213. if(!empty($searchname)){
  214. return $searchname;
  215. }
  216. }
  217. return '';
  218. } catch (Exception $err) {
  219. //echo $err->getMessage();
  220. return '';
  221. }
  222. }
  223. }