Miniprogram.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\api\controller\xmwechat;
  3. use app\common\controller\Frontend;
  4. use app\common\library\xmwechat\miniprogram\MpPayService;
  5. use app\common\library\xmwechat\miniprogram\MpService;
  6. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  7. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  8. use think\db\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Request;
  11. /**
  12. * 小程序
  13. */
  14. class Miniprogram extends Frontend
  15. {
  16. protected array $noNeedLogin = ['getOpenidAndUnionid', 'getPhoneNumber', 'checkText', 'checkMedia', 'messageServe'];
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. }
  21. /**
  22. * 微信小程序获取Openid和Unionid(若绑定微信开放平台,获取不到Unionid,只能获取Openid)
  23. * @param Request $request
  24. * @return void
  25. * @author jsjxsz
  26. */
  27. public function getOpenidAndUnionid(Request $request): void
  28. {
  29. if ($request->isPost()) {
  30. // code 前端获取,传递给接口
  31. $params = $request->only(['code']);
  32. $result = MpService::getInstance()->getOpenidAndUnionid($params);
  33. if (!empty($result)) {
  34. // 这里获取到Openid、Unionid
  35. $this->success('success', $result);
  36. } else {
  37. $this->error('error');
  38. }
  39. }
  40. }
  41. /**
  42. * 微信小程序获取手机号示例
  43. * @param Request $request
  44. * @return void
  45. * @author jsjxsz
  46. */
  47. public function getPhoneNumber(Request $request): void
  48. {
  49. if ($request->isPost()) {
  50. // code 前端获取,传递给接口
  51. $params = $request->only(['code']);
  52. $result = MpService::getInstance()->getPhoneNumber($params);
  53. if (!empty($result)) {
  54. // 这里获取到手机号
  55. $this->success('success', $result);
  56. } else {
  57. $this->error('error');
  58. }
  59. }
  60. }
  61. /**
  62. * 微信小程序文本内容安全识别示例
  63. * 单个appid调用上限为2000次/分钟,1,000,000次/天
  64. * openid 用户openid,需在近两小时访问过小程序
  65. * scene 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
  66. * @param Request $request
  67. * @return void
  68. * @author jsjxsz
  69. */
  70. public function checkText(Request $request): void
  71. {
  72. if ($request->isPost()) {
  73. // openid 用户需在近两小时访问过小程序
  74. $params = $request->only(['openid', 'scene', 'content']);
  75. $result = MpService::getInstance()->checkText($params);
  76. if ($result !== false) {
  77. $this->success('文本内容-检测通过');
  78. } else {
  79. $this->error('文本内容-检测未通过');
  80. }
  81. }
  82. }
  83. /**
  84. * 微信小程序音视频内容安全识别示例
  85. * openid 用户openid,需在近两小时访问过小程序
  86. * media_type 1:音频; 2:图片
  87. * scene 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
  88. * media_url 要检测的图片或音频的url,支持图片格式包括jpg, jepg, png, bmp, gif(取首帧),支持的音频格式包括mp3, aac, ac3, wma, flac, vorbis, opus, wav
  89. * @param Request $request
  90. * @return void
  91. * @throws \Exception
  92. * @author jsjxsz
  93. */
  94. public function checkMedia(Request $request): void
  95. {
  96. if ($request->isPost()) {
  97. $params = $request->only(['openid', 'media_type', 'scene', 'media_url']);
  98. $result = MpService::getInstance()->checkMedia($params);
  99. if ($result !== false) {
  100. // 这里写你自己的逻辑
  101. $this->success('音频/图片内容-检测请求成功,检测结果微信会异步推送。接收微信异步推送的方法(messageServe): app/api/controller/xmwechat/Miniprogram.php');
  102. } else {
  103. $this->error('音频/图片内容-检测请求未成功');
  104. }
  105. }
  106. }
  107. /**
  108. * 微信小程序 音视频内容安全识别结果推送示例
  109. * 需要在后台【微信管理】【小程序】【配置管理】中配置消息推送的相关参数
  110. * @param Request $request
  111. * @author jsjxsz
  112. */
  113. public function messageServe(Request $request)
  114. {
  115. $jsonData = file_get_contents("php://input");
  116. $params = $request->param(['signature', 'timestamp', 'nonce', 'echostr']);
  117. try {
  118. if (isset($params['echostr']) && !empty($params['echostr'])) {
  119. $res = MpService::getInstance()->checkSignature($params);
  120. if ($res !== false) {
  121. echo $params['echostr'];
  122. } else {
  123. echo '验证失败';
  124. }
  125. exit();
  126. }
  127. $data = json_decode($jsonData, true);
  128. if (!empty($data)) {
  129. if (isset($params['errmsg']) && isset($params['errcode']) && isset($params['result'])) {
  130. //result 为综合结果,包含的属性有
  131. //suggest 建议,有risky、pass、review三种值
  132. //label 命中标签枚举值,100 正常;20001 时政;20002 色情;20006 违法犯罪;21000 其他
  133. $trace_id = $params['trace_id'];
  134. if ($params['result']['label'] !== 100) {
  135. /**
  136. * 此处根据命中标签枚举值和trace_id处理自己的相关逻辑
  137. */
  138. }
  139. }
  140. }
  141. } catch (ValidateException|PDOException $e) {
  142. $this->error($e->getMessage());
  143. }
  144. }
  145. }