Wechat_applet.php 1.4 KB

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