ReceiveMessageController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\WechatMini\api\controllers;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\web\NotFoundHttpException;
  6. use common\helpers\WechatHelper;
  7. use api\controllers\OnAuthController;
  8. use addons\WechatMini\common\enums\video\EventEnum;
  9. /**
  10. * Class ReceiveMessageController
  11. * @package addons\WechatMini\api\controllers
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class ReceiveMessageController extends OnAuthController
  15. {
  16. public $modelClass = '';
  17. /**
  18. * 不用进行登录验证的方法
  19. *
  20. * 例如: ['index', 'update', 'create', 'view', 'delete']
  21. * 默认全部需要验证
  22. *
  23. * @var array
  24. */
  25. protected $authOptional = ['index'];
  26. /**
  27. * @param $action
  28. * @return bool
  29. * @throws \yii\base\InvalidConfigException
  30. * @throws \yii\web\BadRequestHttpException
  31. * @throws \yii\web\ForbiddenHttpException
  32. */
  33. public function beforeAction($action)
  34. {
  35. // 非格式化返回
  36. Yii::$app->params['triggerBeforeSend'] = false;
  37. return parent::beforeAction($action);
  38. }
  39. /**
  40. * @return array|mixed|string
  41. * @throws NotFoundHttpException
  42. */
  43. public function actionIndex()
  44. {
  45. $request = Yii::$app->request;
  46. switch ($request->getMethod()) {
  47. // 激活小程序
  48. case 'GET':
  49. if (WechatHelper::verifyTokenMiniProgram($request->get('signature'), $request->get('timestamp'), $request->get('nonce'))) {
  50. return $request->get('echostr');
  51. }
  52. throw new NotFoundHttpException('签名验证失败.');
  53. // 接收数据
  54. case 'POST':
  55. Yii::$app->services->log->push(500, 'wechatMiniMsg', file_get_contents('php://input'));
  56. $message = file_get_contents('php://input');
  57. !is_array($message) && $message = Json::decode($message);
  58. // $message = Yii::$app->request->post();
  59. try {
  60. switch ($message['MsgType']) {
  61. case 'event' : // '收到事件消息';
  62. $reply = $this->event($message);
  63. break;
  64. }
  65. return $reply;
  66. } catch (\Exception $e) {
  67. // 记录行为日志
  68. Yii::$app->services->log->push(500, 'wechatMiniApiReply', Yii::$app->debris->getSysError($e));
  69. if (YII_DEBUG) {
  70. return $e->getMessage();
  71. }
  72. return '系统出错,请联系管理员';
  73. }
  74. break;
  75. default:
  76. throw new NotFoundHttpException('所请求的页面不存在.');
  77. }
  78. }
  79. /**
  80. * @param $message
  81. */
  82. public function event($message)
  83. {
  84. switch ($message['Event']) {
  85. // 商家取消开通自定义组件
  86. case EventEnum::OPEN_PRODUCT_ACCOUNT_REGISTER :
  87. break;
  88. // 商品审核结果/商品系统下架通知
  89. case EventEnum::OPEN_PRODUCT_SPU_AUDIT :
  90. $baseProduct = Yii::$app->wechatMiniService->extendVideoSpu->view($message['out_product_id']);
  91. $spu = $baseProduct['spu'];
  92. if ($product = Yii::$app->wechatMiniService->videoSpu->findByOutProductId($message['out_product_id'])) {
  93. $product->status = $message['status'];
  94. $product->product_id = $spu['product_id'];
  95. $product->edit_status = $spu['edit_status'];
  96. $product->info_version = $spu['info_version'];
  97. $product->reject_reason = $message['reject_reason'];
  98. $product->save();
  99. }
  100. break;
  101. // 类目审核结果
  102. case EventEnum::OPEN_PRODUCT_CATEGORY_AUDIT :
  103. Yii::$app->wechatMiniService->videoAudit->callbackCate($message['audit_id'], $message['status'], $message['reject_reason']);
  104. break;
  105. // 品牌审核结果
  106. case EventEnum::OPEN_PRODUCT_BRAND_AUDIT :
  107. Yii::$app->wechatMiniService->videoAudit->callbackBrand($message['audit_id'], $message['status'], $message['reject_reason'], $message['brand_id']);
  108. break;
  109. // 场景审核结果
  110. case EventEnum::OPEN_PRODUCT_SCENE_GROUP_AUDIT :
  111. break;
  112. // 分享员绑定解绑通知
  113. case EventEnum::MINIPROGRAM_SHARER_BIND_STATUS_CHANGE :
  114. break;
  115. // 用户领券通知
  116. case EventEnum::OPEN_PRODUCT_RECEIVE_COUPON :
  117. break;
  118. }
  119. return 'success';
  120. }
  121. }
粤ICP备19079148号