WechatApplet.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php namespace Msg\Lib\Method;
  2. use Dever;
  3. use Api\Lib\Account;
  4. class Wechat_applet
  5. {
  6. private $config;
  7. # 初始化
  8. public function init($template_id, $config_id, $account, $project = 'api')
  9. {
  10. if (empty($param['openid'])) {
  11. Dever::error('openid不存在');
  12. }
  13. $msg = 'msg';
  14. if ($project != 'api') {
  15. $msg = $project;
  16. }
  17. $this->config = Dever::db($msg . '/account_wechat')->find(['account_id' => $config_id, 'template_id' => $template_id]);
  18. if (!$this->config) {
  19. Dever::error('小程序配置不存在');
  20. }
  21. $this->config['project'] = $project;
  22. $this->config['account'] = $param['openid'];
  23. return $this->config['account'];
  24. }
  25. # 发送
  26. public function send($api, $content, $param)
  27. {
  28. if (!$this->config) {
  29. Dever::error('小程序配置不存在');
  30. }
  31. $data = [];
  32. foreach ($param as $k => $v) {
  33. $data[$k]['value'] = $v;
  34. }
  35. $send['touser'] = $this->config['account'];
  36. $send['template_id'] = $this->config['code'];
  37. $send['page'] = $param['page'] ?? '';
  38. $send['data'] = $data;
  39. $send['log'] = true;
  40. $result = Dever::load(Account::class)->run($api, 'applet_send', $send, 1, 'run', $this->config['project']);
  41. return Dever::json_encode($result);
  42. }
  43. }