Ver Fonte

Merge branch 'shifuruzhu_notice-m'

liugc há 1 ano atrás
pai
commit
723fd2214d

+ 4 - 0
app/adminapi/controller/master_worker_register/MasterWorkerRegisterController.php

@@ -71,6 +71,10 @@ class MasterWorkerRegisterController extends BaseAdminController
         $params = (new MasterWorkerRegisterValidate())->post()->goCheck('edit');
         $result = MasterWorkerRegisterLogic::edit($params);
         if (true === $result) {
+            //审批通过 - 后续事件处理
+            if($params['status'] == 1){
+                MasterWorkerRegisterLogic::eventNotice($params);
+            }
             return $this->success('编辑成功', [], 1, 1);
         }
         return $this->fail(MasterWorkerRegisterLogic::getError());

+ 26 - 0
app/adminapi/logic/master_worker_register/MasterWorkerRegisterLogic.php

@@ -15,7 +15,9 @@
 namespace app\adminapi\logic\master_worker_register;
 
 
+use app\common\enum\notice\NoticeEnum;
 use app\common\model\master_worker\MasterWorker;
+use app\common\model\master_worker\MasterWorkerAuth;
 use app\common\model\master_worker_register\MasterWorkerRegister;
 use app\common\logic\BaseLogic;
 use app\common\service\ConfigService;
@@ -155,4 +157,28 @@ class MasterWorkerRegisterLogic extends BaseLogic
     {
         return MasterWorkerRegister::findOrEmpty($params['id'])->toArray();
     }
+
+    public static function eventNotice($params): bool
+    {
+        $registerInfo = MasterWorkerRegister::findOrEmpty($params['id'])->toArray();
+        if ($registerInfo && $registerInfo['worker_id']) {
+            $account = MasterWorker::where('worker_id',$registerInfo['worker_id'])->value('account');
+            $openid = MasterWorkerAuth::where('worker_id',$registerInfo['worker_id'])->value('openid');
+            event('Notice',  [
+                'scene_id' => NoticeEnum::ACCOUNT_PASSWORD,
+                'params' => [
+                    'user_id' => $registerInfo['worker_id'],
+                    'worker_id' => $registerInfo['worker_id'],
+                    'mobile' => $registerInfo['mobile'],
+                    'account' => $account,
+                    'password' => $registerInfo['mobile'],
+                    'openid' => $openid,
+                ]
+            ]);
+            return true;
+        }
+        return false;
+    }
+
+
 }

+ 2 - 0
app/common/enum/notice/NoticeEnum.php

@@ -38,6 +38,7 @@ class NoticeEnum
     const FIND_LOGIN_PASSWORD_CAPTCHA = 104;
     const OTHER_CAPTCHA = 105;
     const GCSSJHM_CAPTCHA =106;
+    const ACCOUNT_PASSWORD =107;
 
 
     /**
@@ -50,6 +51,7 @@ class NoticeEnum
         self::FIND_LOGIN_PASSWORD_CAPTCHA,
         self::OTHER_CAPTCHA,
         self::GCSSJHM_CAPTCHA,
+        self::ACCOUNT_PASSWORD,
     ];
 
 

+ 14 - 2
app/common/logic/NoticeLogic.php

@@ -20,6 +20,7 @@ use app\common\model\notice\NoticeRecord;
 use app\common\model\notice\NoticeSetting;
 use app\common\model\user\User;
 use app\common\service\sms\SmsMessageService;
+use app\common\service\wechat\WeChatOaService;
 
 
 /**
@@ -49,10 +50,21 @@ class NoticeLogic extends BaseLogic
             $res = false;
             self::setError('发送通知失败');
 
-            // 短信通知
-            if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES) {
+            // 短信验证码通知(具有时效性)
+            if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::VERIFICATION_CODE) {
                 $res = (new SmsMessageService())->send($params);
             }
+            // 短信业务通知
+            if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION) {
+                $res = (new SmsMessageService())->sendBusiness($params);
+            }
+            // 微信公众号消息模板业务通知
+            if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES
+                && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION
+                && isset($params['params']['openid']) && !empty($params['params']['openid'])
+            ) {
+                $res = (new WeChatOaService())->sendTemplateMessage($params);
+            }
 
             return $res;
         } catch (\Exception $e) {

+ 27 - 0
app/common/service/sms/SmsMessageService.php

@@ -64,6 +64,33 @@ class SmsMessageService
         }
     }
 
+    public function sendBusiness($params)
+    {
+        try {
+            // 通知设置
+            $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
+            // 替换通知模板参数
+            $content = $this->contentFormat($noticeSetting, $params);
+            // 添加通知记录
+            $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
+            // 发送短信
+            $smsDriver = new SmsDriver();
+            if(!is_null($smsDriver->getError())) {
+                throw new \Exception($smsDriver->getError());
+            }
+
+            $result =  $smsDriver->send($params['params']['mobile'], [
+                'template_id' => $noticeSetting['sms_notice']['template_id'],
+                'params' => $this->setSmsParams($noticeSetting, $params)
+            ]);
+            if ($result === false) {
+                throw new \Exception($smsDriver->getError());
+            }
+            return true;
+        } catch (\Exception $e) {
+            throw new \Exception($e->getMessage());
+        }
+    }
 
     /**
      * @notes 格式化消息内容

+ 72 - 0
app/common/service/wechat/WeChatOaService.php

@@ -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());
+        }
+    }
 }