Demo.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Overtrue\Pinyin\Pinyin;
  5. use think\Db;
  6. /**
  7. * 示例接口
  8. */
  9. class Demo extends Api
  10. {
  11. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  12. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  13. //如果接口已经设置无需登录,那也就无需鉴权了
  14. //
  15. // 无需登录的接口,*表示全部
  16. protected $noNeedLogin = ['test', 'test1'];
  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. private function shouzimu($string)
  39. {
  40. // 小内存型
  41. $pinyin = new Pinyin(); // 默认
  42. $pinyin_arr = $pinyin->convert($string);
  43. $shouzimu = '';
  44. if(!empty($pinyin_arr)){
  45. foreach($pinyin_arr as $key => $val){
  46. $shouzimu .= strtoupper(substr($val,0,1));
  47. }
  48. }
  49. return $shouzimu;
  50. }
  51. /**
  52. * 无需登录的接口
  53. *
  54. */
  55. public function test1()
  56. {
  57. $list = Db::name('video')->where('search_title','')->select();
  58. foreach($list as $key => $video){
  59. $shouzimu = $this->shouzimu($video['title']);
  60. Db::name('video')->where('id',$video['id'])->update(['search_title'=>$shouzimu]);
  61. }
  62. }
  63. /**
  64. * 需要登录的接口
  65. *
  66. */
  67. public function test2()
  68. {
  69. $this->success('返回成功', ['action' => 'test2']);
  70. }
  71. /**
  72. * 需要登录且需要验证有相应组的权限
  73. *
  74. */
  75. public function test3()
  76. {
  77. $this->success('返回成功', ['action' => 'test3']);
  78. }
  79. }