Controller.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AdminMerchant;
  4. use Dingo\Api\Routing\Helpers;
  5. use Illuminate\Support\Facades\Log;
  6. use Laravel\Lumen\Routing\Controller as BaseController;
  7. class Controller extends BaseController
  8. {
  9. use Helpers;
  10. protected static $MERCHANT_ID = 0;
  11. protected static $MERCHANT = [];
  12. protected static $ORDER_TAG = null;
  13. const SOURCE_TYPE_WECHAT = 'wechat';
  14. const SOURCE_TYPE_ALIPAY = 'alipay';
  15. protected static $SOURCE_TYPE = 'wechat';
  16. const PATMENT_TYPE_MINIAPP = 1;
  17. const PATMENT_TYPE_ACCOUNT = 2;
  18. const PATMENT_TYPE_ALIPAY_CARD = 3;
  19. public static $paymentTypeMaps = [
  20. self::PATMENT_TYPE_MINIAPP => '微信/支付宝',
  21. self::PATMENT_TYPE_ACCOUNT => '平台余额',
  22. self::PATMENT_TYPE_ALIPAY_CARD => '支付宝预支付(缴纳押金)',
  23. ];
  24. public static $URLS = [
  25. 'api/relay/order/auto-close',
  26. 'api/common/clear-cache',
  27. 'api/t',
  28. ];
  29. public function __construct()
  30. {
  31. $url = request()->path();
  32. if (!in_array($url, self::$URLS)) {
  33. $merchant_id = merchant_id();
  34. if (!$merchant_id) {
  35. return_json('商户id错误');
  36. }
  37. self::$MERCHANT_ID = $merchant_id;
  38. self::$MERCHANT = AdminMerchant::byId($merchant_id);
  39. if (!self::$MERCHANT) {
  40. return_json('平台暂停服务,如有不便敬请谅解');
  41. }
  42. self::$ORDER_TAG = self::$MERCHANT['order_key'];
  43. }
  44. self::$SOURCE_TYPE = source_type();
  45. if (!self::$SOURCE_TYPE) {
  46. return_json('没有获取到平台信息');
  47. }
  48. }
  49. // 返回错误的请求
  50. protected function errorBadRequest($msg)
  51. {
  52. return $this->response->array([
  53. 'message' => $msg
  54. ])->statusCode(400);
  55. }
  56. /**
  57. * 验证错误
  58. * @param $msg
  59. * User: Mead
  60. */
  61. protected function errorNoValidation($msg, $code = 422)
  62. {
  63. return $this->response->array([
  64. 'message' => $msg
  65. ])->statusCode($code);
  66. }
  67. /**
  68. * 验证错误
  69. * @param $msg
  70. * User: Mead
  71. */
  72. protected function errorNoYundong($msg)
  73. {
  74. return $this->response->array([
  75. 'message' => $msg
  76. ])->statusCode(450);
  77. }
  78. /**
  79. * 异常错误
  80. * @param $msg
  81. * User: Mead
  82. */
  83. protected function errorException($msg)
  84. {
  85. return $this->response->array([
  86. 'message' => $msg
  87. ])->statusCode(423);
  88. }
  89. /**
  90. * 获取当前登录的用户的openid
  91. * @return mixed
  92. * User: Mead
  93. */
  94. protected function getOpenId()
  95. {
  96. return $this->user->auth->credential;
  97. }
  98. /**
  99. * 处理异常信息
  100. * @param $exception
  101. * @return string
  102. * Author: Mead
  103. */
  104. public function exception($exception)
  105. {
  106. Log::error($exception);
  107. return $this->errorNoValidation('异常错误');
  108. }
  109. }