OutsideController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/1/5
  8. * Time: 15:35
  9. */
  10. namespace app\outside\controllers;
  11. use app\common\exceptions\ApiException;
  12. use app\common\exceptions\AppException;
  13. use app\common\services\utils\EncryptUtil;
  14. use app\outside\modes\OutsideAppSetting;
  15. use app\outside\services\NotifyService;
  16. use app\outside\services\OutsideAppService;
  17. use Illuminate\Support\Facades\DB;
  18. use app\common\models\AccountWechats;
  19. use app\common\components\BaseController;
  20. class OutsideController extends BaseController
  21. {
  22. /**
  23. * @var OutsideAppSetting
  24. */
  25. public $outsideApp;
  26. protected $noAppMode = false; //无应用模式
  27. public $needVerifySign = true;
  28. /**
  29. * 前置action
  30. * @throws ApiException
  31. */
  32. public function preAction()
  33. {
  34. parent::preAction();
  35. $this->apiVerify();
  36. }
  37. /**
  38. * @throws ApiException
  39. */
  40. protected function apiVerify()
  41. {
  42. if ($this->noAppMode) {
  43. return;
  44. }
  45. $appID = trim(request()->input('app_id'));
  46. if (!is_numeric($appID)) {
  47. throw new ApiException('不符合标准的appID');
  48. }
  49. $appSet = OutsideAppSetting::where('app_id', $appID)->first();
  50. if (!$appSet) {
  51. throw new ApiException('应用不存在');
  52. }
  53. if (!$appSet->is_open) {
  54. throw new ApiException('应用已关闭');
  55. }
  56. $this->outsideApp = $appSet;
  57. //$this->setCurrentUniacid($appSet->uniacid);
  58. //开启签名验证,签名验证失败
  59. if(!$appSet->sign_required && !$this->appSignVerify()) {
  60. throw new ApiException('签名验证失败');
  61. }
  62. }
  63. protected function appSignVerify()
  64. {
  65. //过滤掉路由地址参数
  66. $requestData = request()->except(request()->getRequestUri());
  67. return (new NotifyService($requestData))->verifySign();
  68. }
  69. //设置公众号
  70. protected function setCurrentUniacid($uniacid)
  71. {
  72. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $uniacid;
  73. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  74. }
  75. }