Offiaccount.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\api\controller\xmwechat;
  3. use app\common\controller\Frontend;
  4. use app\common\library\xmwechat\offiaccount\MessageService;
  5. use app\common\library\xmwechat\offiaccount\OaService;
  6. use think\db\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Request;
  9. use think\facade\Db;
  10. use EasyWeChat\Factory;
  11. /**
  12. * 公众号
  13. */
  14. class Offiaccount extends Frontend
  15. {
  16. protected array $noNeedLogin = ['messageServe', 'oauth', 'sendAuditMessage','isBind','sendResultMessage'];
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. }
  21. //判断是否绑定
  22. public function isBind(Request $request)
  23. {
  24. if ($request->isPost()) {
  25. $data = $this->request->post();
  26. $isBind= Db::name('oauth_log')->where('user_id',$data['id'])->value('opid_status');
  27. if ($isBind) {
  28. $this->success(('succeeded!'), ['isBind' => 'ture',]);
  29. }else {
  30. $this->success(('succeeded!'), ['isBind' => 'falase',]);
  31. }
  32. }
  33. }
  34. /**
  35. * 公众号服务器推送
  36. * @param Request $request
  37. * @author jsjxsz
  38. */
  39. public function messageServe(Request $request)
  40. {
  41. try {
  42. $params = $request->param(['signature', 'timestamp', 'nonce', 'echostr']);
  43. if (isset($params['echostr']) && !empty($params['echostr'])) {
  44. $res = MessageService::getInstance()->checkSignature($params);
  45. if ($res !== false) {
  46. echo $params['echostr'];
  47. } else {
  48. echo '验证失败';
  49. }
  50. exit();
  51. }
  52. return MessageService::getInstance()->messageServe();
  53. } catch (ValidateException|PDOException $e) {
  54. $this->error($e->getMessage());
  55. }
  56. }
  57. /**
  58. * 微信公众号网页授权绑定uuid
  59. * @throws \Exception
  60. */
  61. public function oauth(Request $request)
  62. {
  63. $code = $request->param('code');
  64. if (!isset($code)) {
  65. try {
  66. $redirectUrl = OaService::getInstance()->getRedirectUrl();
  67. header("Location: {$redirectUrl}");
  68. exit();
  69. } catch (\Exception $e) {
  70. $this->error($e->getMessage());
  71. }
  72. }
  73. $result = OaService::getInstance()->oauthCallback($code);
  74. // header("Content-Type: application/json");
  75. if (!isset($result['openid'])) {
  76. $this->error('获取openid失败');
  77. throw new \Exception('获取openid失败');
  78. }
  79. }
  80. /**
  81. * 仪器预约待审核通知
  82. * @param Request $request
  83. * @return void
  84. * @throws \Exception
  85. */
  86. public function sendAuditMessage(Request $request): void
  87. {
  88. if ($request->isPost()) {
  89. $res = $this->request->post();
  90. $openid = $res["openid"];
  91. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  92. $data = [
  93. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  94. 'thing4' => ['value' => $res["data"]["thing4"]],
  95. 'const6' => ['value' => $res["data"]["const6"]],
  96. 'time9' => ['value' => date('Y-m-d H:i')],
  97. ];
  98. $url = 'http://weixin.qq.com/download';
  99. $miniprogram = [
  100. 'appid' => 'wx58633590ab59b7a1',
  101. 'pagepath' => ''
  102. ];
  103. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data,$url, $miniprogram);
  104. if ($result !== false) {
  105. $this->success('发送成功');
  106. } else {
  107. $this->error('发送失败');
  108. }
  109. }
  110. }
  111. /**
  112. * 仪器预约待审核通知
  113. * @param
  114. * @return void
  115. * @throws \Exception
  116. */
  117. public function wxAuditMessage($res = null): void
  118. {
  119. $openid = $res["openid"];
  120. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  121. $data = [
  122. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  123. 'thing4' => ['value' => $res["data"]["thing4"]],
  124. 'const6' => ['value' => $res["data"]["const6"]],
  125. 'time9' => ['value' => date('Y-m-d H:i')],
  126. ];
  127. $url = 'http://weixin.qq.com/download';
  128. $miniprogram = [
  129. 'appid' => 'wx58633590ab59b7a1',
  130. 'pagepath' => ''
  131. ];
  132. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data,$url, $miniprogram);
  133. if ($result !== false) {
  134. $this->success('发送成功');
  135. } else {
  136. $this->error('发送失败');
  137. }
  138. }
  139. /**
  140. * 仪器预约审核结果通知
  141. * @param
  142. * @return void
  143. * @throws \Exception
  144. */
  145. public function wxResultMessage($res): void
  146. {
  147. $openid = $res["openid"];
  148. // halt($res);
  149. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  150. $data = [
  151. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  152. 'const3' => ['value' => $res["data"]["const3"]],
  153. 'const4' => ['value' => $res["data"]["const4"]],
  154. 'thing5' => ['value' => $res["data"]["thing5"]],
  155. // 'time9' => ['value' => date('Y-m-d H:i')],
  156. ];
  157. $url = 'http://weixin.qq.com/download';
  158. $miniprogram = [
  159. 'appid' => 'wx58633590ab59b7a1',
  160. 'pagepath' => ''
  161. ];
  162. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  163. if ($result !== false) {
  164. $this->success('发送成功');
  165. } else {
  166. $this->error('发送失败');
  167. }
  168. }
  169. /**
  170. * 仪器预约审核结果通知
  171. * @param Request $request
  172. * @return void
  173. * @throws \Exception
  174. */
  175. public function sendResultMessage(Request $request): void
  176. {
  177. if ($request->isPost()) {
  178. $res = $this->request->post();
  179. $openid = $res["openid"];
  180. // halt($res);
  181. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  182. $data = [
  183. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  184. 'const3' => ['value' => $res["data"]["const3"]],
  185. 'const4' => ['value' => $res["data"]["const4"]],
  186. 'thing5' => ['value' => $res["data"]["thing5"]],
  187. // 'time9' => ['value' => date('Y-m-d H:i')],
  188. ];
  189. $url = 'http://weixin.qq.com/download';
  190. $miniprogram = [
  191. 'appid' => 'wx58633590ab59b7a1',
  192. 'pagepath' => ''
  193. ];
  194. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  195. if ($result !== false) {
  196. $this->success('发送成功');
  197. } else {
  198. $this->error('发送失败');
  199. }
  200. }
  201. }
  202. }