AppletsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\api\controller\applets;
  3. use app\api\controller\ZskkDefaultController;
  4. use app\api\servies\applets\AppletsService;
  5. use app\api\servies\Butt\ButtService;
  6. use app\api\utils\UUIDUtils;
  7. use app\common\library\uploadToCloud;
  8. use think\facade\Config;
  9. /*
  10. * 微信小程序接口
  11. */
  12. class AppletsController extends ZskkDefaultController
  13. {
  14. protected $needToken = true;
  15. protected $logName = "AppletsController";
  16. public function application_start(AppletsService $service)
  17. {
  18. try{
  19. $params = $this->getParams();
  20. $doctor = $this->getCache($this->getToken());
  21. $return = $service->application_start($params,$doctor);
  22. return $this->success($return);
  23. }catch(\Exception $e){
  24. $this->throwError($e->getMessage(),0001);
  25. }
  26. }
  27. public function myApplication(AppletsService $service)
  28. {
  29. try{
  30. $params = $this->getParams();
  31. $doctor = $this->getCache($this->getToken());
  32. $return = $service->getMyApplication($params,$doctor);
  33. return $this->success($return);
  34. }catch(\Exception $e){
  35. $this->throwError($e->getMessage(),0001);
  36. }
  37. }
  38. public function save_autograph(AppletsService $service)
  39. {
  40. $params = $this->getParams();
  41. if(empty($params['attachment']))
  42. {
  43. $this->throwError('找不到需要上传的文件','0009');
  44. }
  45. $attachment = explode('base64,',$params['attachment']);
  46. $image = $attachment[1];
  47. $file = base64_decode($image);
  48. if($file) {
  49. $name = UUIDUtils::uuid().'.png';
  50. $path = ROOT_PATH . 'public' . DS . 'uploads'. DS .$name;
  51. $url = '/' . 'autograph' . '/' .$name;
  52. $r = file_put_contents($path, $file);
  53. if (!$r) {
  54. $this->throwError('图片生成失败','0008');
  55. }else {
  56. $doctor = $this->getCache($this->getToken());
  57. $type = Config::get('upload');
  58. if($type == 1)
  59. {
  60. $r = $service->save_autograph($url,$doctor);
  61. $success = ['url'=>$url,'type'=>$type,'tokenUrl'=>$url];
  62. return $this->success($success);
  63. }
  64. $key = 'autograph/' . $name;
  65. $source_file = './' . 'uploads' . '/' . $name;
  66. $upload = new uploadToCloud();;
  67. $s = $upload->upload($key,$source_file);
  68. if(($s['@metadata']['statusCode'] ?? '') == '200')
  69. {
  70. $r = $service->save_autograph($url,$doctor);
  71. gc_collect_cycles();
  72. unset($info);
  73. unlink($source_file);
  74. $success = ['url'=>$key,'type'=>'2','tokenUrl'=>$upload->makeUrl($key)];
  75. return $this->success($success);
  76. }else{
  77. return '上传云失败';
  78. }
  79. // return $this->success($url);
  80. }
  81. }else{
  82. $this->throwError('无法获取上传的文件','0009');
  83. }
  84. }
  85. public function autograph_switch(AppletsService $service)
  86. {
  87. $params = $this->getParams();
  88. $doctor = $this->getCache($this->getToken());
  89. $info = $service->autograph_switch($doctor,$params['is_use_autograph']);
  90. return $this->success($info);
  91. }
  92. public function getAutograph(AppletsService $service)
  93. {
  94. $doctor = $this->getCache($this->getToken());
  95. if(empty($doctor))
  96. {
  97. $this->throwError('无效的token,请重新进行登陆','0099');
  98. }
  99. $data = $service->getAutograph($doctor['id']);
  100. $data['autograph_url'] = $data['autograph'];
  101. if($data['autograph_type'] == 2)
  102. {
  103. $data['autograph_url'] = $this->makeFileUrl($data['autograph'],$data['autograph_type']);
  104. }
  105. return $this->success($data);
  106. }
  107. }