|
|
@@ -14,6 +14,9 @@
|
|
|
namespace app\common\service\wechat;
|
|
|
|
|
|
|
|
|
+use app\common\enum\notice\NoticeEnum;
|
|
|
+use app\common\logic\NoticeLogic;
|
|
|
+use app\common\model\notice\NoticeSetting;
|
|
|
use EasyWeChat\Kernel\Exceptions\Exception;
|
|
|
use EasyWeChat\OfficialAccount\Application;
|
|
|
|
|
|
@@ -154,4 +157,73 @@ class WeChatOaService
|
|
|
return $this->app->getUtils()->buildJsSdkConfig($url, $jsApiList, $openTagList, $debug);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public function contentFormat($noticeSetting, $params)
|
|
|
+ {
|
|
|
+ $content = $noticeSetting['oa_notice']['content'];
|
|
|
+ foreach($params['params'] as $k => $v) {
|
|
|
+ $search = '${' . $k . '}';
|
|
|
+ $content = str_replace($search, $v, $content);
|
|
|
+ }
|
|
|
+ return $content;
|
|
|
+ }
|
|
|
+ public function getTemplateMessageParams($noticeSetting, $params)
|
|
|
+ {
|
|
|
+ $arr = [];
|
|
|
+ $content = $noticeSetting['oa_notice']['content'];
|
|
|
+ foreach ($params['params'] as $item => $val) {
|
|
|
+ $search = '${' . $item . '}';
|
|
|
+ if(strpos($content, $search) !== false && !in_array($item, $arr)) {
|
|
|
+ //arr => 获的数组[nickname, order_sn] //顺序可能是乱的
|
|
|
+ $arr[] = $item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
|
|
|
+ $arr2 = [];
|
|
|
+ if (!empty($arr)) {
|
|
|
+ foreach ($arr as $v) {
|
|
|
+ $key = strpos($content, $v);
|
|
|
+ $arr2[$key] = $v;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //格式化 arr2 => 以小到大的排序的数组
|
|
|
+ ksort($arr2);
|
|
|
+ $arr3 = array_values($arr2);
|
|
|
+
|
|
|
+ //arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
|
|
|
+ $arr4 = [];
|
|
|
+ foreach ($arr3 as $v2) {
|
|
|
+ if(isset($params['params'][$v2])) {
|
|
|
+ $arr4[] = $params['params'][$v2] . "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $arr4;
|
|
|
+ }
|
|
|
+ public function sendTemplateMessage(array $params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 通知设置
|
|
|
+ $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
|
|
|
+ // 替换通知模板参数
|
|
|
+ $content = $this->contentFormat($noticeSetting, $params);
|
|
|
+ // 添加通知记录
|
|
|
+ $params['user_id'] = $params['worker_id'];
|
|
|
+ $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::OA, $content);
|
|
|
+ // 发送
|
|
|
+ $result = $this->app->getClient()->postJson('cgi-bin/message/template/send', [
|
|
|
+ 'touser' => $params['params']['openid'],
|
|
|
+ 'template_id' => $noticeSetting['oa_notice']['template_id'],
|
|
|
+ 'url' => '',
|
|
|
+ 'data' => $this->getTemplateMessageParams($noticeSetting, $params)
|
|
|
+ ]);
|
|
|
+ if (intval($result["errcode"]) > 0) {
|
|
|
+ throw new \Exception('微信通知发送失败:'.$result["errmsg"]);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new \Exception($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|