Controller.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Helper\ByteDance;
  4. use App\Helper\Kuaishou;
  5. use App\Helper\UniPlatform\Bytedance\ByteDanceAPI;
  6. use App\Helper\UniPlatform\Kuaishou\KuaishouAPI;
  7. use App\Helper\UniPlatform\Wechat\WechatAPI;
  8. use App\Helper\Wechat;
  9. use App\Models\PayConfig;
  10. use Dingo\Api\Routing\Helpers;
  11. use EasyWeChat\Factory;
  12. use EasyWeChat\MiniProgram\Application;
  13. use Illuminate\Foundation\Bus\DispatchesJobs;
  14. use Illuminate\Routing\Controller as BaseController;
  15. use Illuminate\Foundation\Validation\ValidatesRequests;
  16. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  17. use App\Services\Base\ErrorCode;
  18. use Request, Auth, Log;
  19. class Controller extends BaseController
  20. {
  21. use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Helpers;
  22. public function success($data = null, $code = 0, $msg = null)
  23. {
  24. if (!$msg) $msg = trans('api.SUCCESS');
  25. $result = [
  26. 'code' => $code,
  27. 'msg' => $msg,
  28. 'data' => $data
  29. ];
  30. return response()->json($result);
  31. }
  32. public function error($msg = null, $code = 1, $data = null)
  33. {
  34. $result = [
  35. 'code' => $code,
  36. 'msg' => $msg,
  37. 'data' => $data
  38. ];
  39. return response()->json($result);
  40. }
  41. public function validatorError($arr, $code = 1, $message = '')
  42. {
  43. foreach ($arr as $val) {
  44. if ($val && $message == '') {
  45. $message = $val;
  46. }
  47. }
  48. if ($code !== 0 && ErrorCode::CLIENT_WRONG_PARAMS && empty($msg)) {
  49. $msg = ErrorCode::message($code);
  50. }
  51. $result = [
  52. 'code' => $code,
  53. 'msg' => $msg,
  54. 'data' => $data
  55. ];
  56. return response()->json($result);
  57. }
  58. /**
  59. * @param int $platform
  60. * @return ByteDance|Kuaishou|Wechat
  61. */
  62. protected function getUniFactory(int $platform = 1)
  63. {
  64. if($platform == 1){
  65. return $this->getByteDanceFactory();
  66. }elseif($platform == 2){
  67. return $this->getKuishouFactory();
  68. }
  69. return $this->getWechatFactory();
  70. }
  71. protected function getByteDanceFactory(): ByteDance
  72. {
  73. $setting = PayConfig::first();
  74. return (new ByteDance(app(ByteDanceAPI::class)))->factory([
  75. 'app_id' => $setting->douyin_app_id,
  76. 'app_secret' => $setting->douyin_app_secret,
  77. 'slat' => $setting->douyin_salt,
  78. 'token' => $setting->douyin_token,
  79. ]);
  80. }
  81. protected function getKuishouFactory(): Kuaishou
  82. {
  83. $setting = PayConfig::first();
  84. return (new Kuaishou(app(KuaishouAPI::class)))->factory([
  85. 'app_id' => $setting->kuaishou_app_id,
  86. 'app_secret' => $setting->kuaishou_app_secret,
  87. ]);
  88. }
  89. protected function getWechatFactory(): Wechat
  90. {
  91. $setting = PayConfig::first();
  92. return (new Wechat(app(WechatAPI::class)))->factory([
  93. 'app_id' => $setting->mini_app_id,
  94. 'app_secret' => $setting->mini_app_key,
  95. 'mch_id' => $setting->wechat_mch_id,
  96. 'mch_key' => $setting->wechat_mch_key,
  97. 'response_type' => 'array'
  98. ]);
  99. }
  100. }