123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace app\api\controller\xmwechat;
- use app\common\controller\Frontend;
- use app\common\library\xmwechat\offiaccount\MessageService;
- use app\common\library\xmwechat\offiaccount\OaService;
- use think\db\exception\PDOException;
- use think\exception\ValidateException;
- use think\Request;
- use think\facade\Db;
- use EasyWeChat\Factory;
- /**
- * 公众号
- */
- class Offiaccount extends Frontend
- {
- protected array $noNeedLogin = ['messageServe', 'oauth', 'sendAuditMessage','isBind','sendResultMessage'];
- public function initialize(): void
- {
- parent::initialize();
- }
- //判断是否绑定
- public function isBind(Request $request)
- {
- if ($request->isPost()) {
- $data = $this->request->post();
- $isBind= Db::name('oauth_log')->where('user_id',$data['id'])->value('opid_status');
- if ($isBind) {
- $this->success(('succeeded!'), ['isBind' => 'ture',]);
- }else {
- $this->success(('succeeded!'), ['isBind' => 'falase',]);
- }
- }
- }
- /**
- * 公众号服务器推送
- * @param Request $request
- * @author jsjxsz
- */
- public function messageServe(Request $request)
- {
-
- try {
- $params = $request->param(['signature', 'timestamp', 'nonce', 'echostr']);
-
- if (isset($params['echostr']) && !empty($params['echostr'])) {
- $res = MessageService::getInstance()->checkSignature($params);
- if ($res !== false) {
- echo $params['echostr'];
- } else {
- echo '验证失败';
- }
- exit();
- }
- return MessageService::getInstance()->messageServe();
- } catch (ValidateException|PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 微信公众号网页授权绑定uuid
- * @throws \Exception
- */
- public function oauth(Request $request)
- {
- $code = $request->param('code');
- if (!isset($code)) {
- try {
- $redirectUrl = OaService::getInstance()->getRedirectUrl();
- header("Location: {$redirectUrl}");
- exit();
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- $result = OaService::getInstance()->oauthCallback($code);
- // header("Content-Type: application/json");
- if (!isset($result['openid'])) {
- $this->error('获取openid失败');
- throw new \Exception('获取openid失败');
- }
-
- }
- /**
- * 仪器预约待审核通知
- * @param Request $request
- * @return void
- * @throws \Exception
- */
- public function sendAuditMessage(Request $request): void
- {
- if ($request->isPost()) {
- $res = $this->request->post();
- $openid = $res["openid"];
- $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
- $data = [
- 'character_string7' => ['value' => $res["data"]["character_string7"]],
- 'thing4' => ['value' => $res["data"]["thing4"]],
- 'const6' => ['value' => $res["data"]["const6"]],
-
- 'time9' => ['value' => date('Y-m-d H:i')],
- ];
- $url = 'http://weixin.qq.com/download';
- $miniprogram = [
- 'appid' => 'wx58633590ab59b7a1',
- 'pagepath' => ''
- ];
- $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data,$url, $miniprogram);
- if ($result !== false) {
- $this->success('发送成功');
- } else {
- $this->error('发送失败');
- }
- }
- }
- /**
- * 仪器预约待审核通知
- * @param
- * @return void
- * @throws \Exception
- */
- public function wxAuditMessage($res = null): void
- {
- $openid = $res["openid"];
- $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
- $data = [
- 'character_string7' => ['value' => $res["data"]["character_string7"]],
- 'thing4' => ['value' => $res["data"]["thing4"]],
- 'const6' => ['value' => $res["data"]["const6"]],
- 'time9' => ['value' => date('Y-m-d H:i')],
- ];
- $url = 'http://weixin.qq.com/download';
- $miniprogram = [
- 'appid' => 'wx58633590ab59b7a1',
- 'pagepath' => ''
- ];
- $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data,$url, $miniprogram);
- if ($result !== false) {
- $this->success('发送成功');
- } else {
- $this->error('发送失败');
- }
- }
- /**
- * 仪器预约审核结果通知
- * @param
- * @return void
- * @throws \Exception
- */
- public function wxResultMessage($res): void
- {
- $openid = $res["openid"];
- // halt($res);
- $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
- $data = [
- 'character_string1' => ['value' => $res["data"]["character_string1"]],
- 'const3' => ['value' => $res["data"]["const3"]],
- 'const4' => ['value' => $res["data"]["const4"]],
- 'thing5' => ['value' => $res["data"]["thing5"]],
- // 'time9' => ['value' => date('Y-m-d H:i')],
- ];
- $url = 'http://weixin.qq.com/download';
- $miniprogram = [
- 'appid' => 'wx58633590ab59b7a1',
- 'pagepath' => ''
- ];
- $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
- if ($result !== false) {
- $this->success('发送成功');
- } else {
- $this->error('发送失败');
- }
- }
- /**
- * 仪器预约审核结果通知
- * @param Request $request
- * @return void
- * @throws \Exception
- */
- public function sendResultMessage(Request $request): void
- {
- if ($request->isPost()) {
- $res = $this->request->post();
- $openid = $res["openid"];
- // halt($res);
- $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
- $data = [
- 'character_string1' => ['value' => $res["data"]["character_string1"]],
- 'const3' => ['value' => $res["data"]["const3"]],
- 'const4' => ['value' => $res["data"]["const4"]],
- 'thing5' => ['value' => $res["data"]["thing5"]],
- // 'time9' => ['value' => date('Y-m-d H:i')],
- ];
- $url = 'http://weixin.qq.com/download';
- $miniprogram = [
- 'appid' => 'wx58633590ab59b7a1',
- 'pagepath' => ''
- ];
- $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
- if ($result !== false) {
- $this->success('发送成功');
- } else {
- $this->error('发送失败');
- }
- }
- }
- }
|