Blame view

application/admin/controller/fastflow/flow/CustomMessage.php 5.94 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  <?php

  

  

  namespace app\admin\controller\fastflow\flow;

  

  

  use app\admin\model\Admin;

  use app\admin\model\AuthGroupAccess;

  use app\common\controller\Backend;

  use fastflow\api;

  use Tx\Mailer;

  

  class CustomMessage extends Backend

  {

      //flowName          流程名称

      //flowDescription   流程描述

      //scope             域 1=人员,2=分组

      //scopeName         域名称

      //workerIds         审批人id

      //workerNames       审批人名称

      //checkmode         审批模式

      //stepName          步骤名称

      //preStepName       上一步骤名称

      //bill              单据表名

      //billId            单据id

      //billName          单据注释

      //agency            是否为代理

      //agentIds          代理人ids

      //agentName         代理人名称

      //principalIds      被代理人id

      //principalName     被代理人名称

      //receivers         接收人

  

      protected $noNeedLogin = ['*'];

      //protected $noNeedRight = ['sendMessageAsync'];

  

      public function __construct()

      {

  

      }

  

      public function sendMessage($params)

      {

          $messageModel = new \app\admin\model\fastflow\flow\Message();

          $enabledWays = $messageModel->getEnabledWays();

          if (count($enabledWays) == 0) {

              return;

          }

          $fo = fsockopen('localhost', $_SERVER['SERVER_PORT'], $error, $errstr, 3);

          if (!$fo) {

              return ['code' => 0, 'msg' => '消息发送失败'];

          }

          $head = "GET " . url('/fastflow/flow/custom_message/sendmessageasync', $params) . " HTTP/1.0\r\n";

          $head .= "Host: " . $_SERVER['HTTP_HOST'] . "\r\n";

          $head .= "\r\n";

          fputs($fo, $head);

          sleep(1);

          fclose($fo);

      }

  

      public function sendMessageAsync()

      {

          $builtParams = (new api())->buidMessageParams(input(''));

  

          $messageModel = new \app\admin\model\fastflow\flow\Message();

          $enabledWays = $messageModel->getEnabledWays();

          $options = $messageModel->getMessageOptions();

  

          foreach ($enabledWays as $way) {

              $body = self::parseTemplet($options[$way]['templet'], $builtParams);

              $receivers = self::enumReceiver($builtParams['scope'], $builtParams['receivers']);

              $method = 'send' . ucfirst($way);

              call_user_func([$this, $method], $receivers, $options[$way]['config'], $body);

          }

          return;

      }

  

      public function parseTemplet($templet, $params)

      {

          $body = $templet;

          foreach ($params as $key => $value) {

              if ($key !== 'receivers') {

                  $keywords = '{' . $key . '}';

                  $body = str_replace($keywords, $value, $body);

              }

          }

          foreach ($this->setCustomVariate($params) as $key => $value) {

              $keywords = '{' . $key . '}';

              $body = str_replace($keywords, $value, $body);

          }

          return $body;

      }

  

      /*

       * 自定义模板变量

       */

      private function setCustomVariate($params)

      {

          $variates = [];

          $variates['time'] = date('Y-m-d H:i:s', time());

  

          return $variates;

      }

  

      /**

       * 枚举消息接收用户

       * @param $scope

       * @param $receivers

       */

      public function enumReceiver($scope, $receivers)

      {

          $receiverList = [];

          if ($scope == 1) {//人员

              foreach ($receivers as $receiver) {

                  $admin = (new Admin())->find($receiver['id']);

                  if ($admin) {

                      $receiverList[] = $admin;

                  }

              }

          } elseif ($scope == 2) {//分组

              foreach ($receivers as $receiver) {

                  $groupAccess = (new AuthGroupAccess())->where('group_id', $receiver['id'])->select();

                  foreach ($groupAccess as $row) {

                      $admin = (new Admin())->find($row['uid']);

                      if ($admin) {

                          $receiverList[] = $admin;

                      }

                  }

              }

          }

          return $receiverList;

      }

  

      /*

       * $scope:1=人员,2=分组

       * $receivers = [['id' => 1, 'name' => '张三'], ['id' => 2, 'name' => '李四']] 或

       * $receivers = [['id' => 1, 'name' => '销售部'], ['id' => 2, 'name' => '生产部']]

       * $config为用户自定义配置,使用键名获取值,如:$config['host']

       * $body为消息主体

       */

  

      /*

       * 自定义邮件发送

       */

      public function sendEmail($receivers, $config, $body)

      {

          foreach ($receivers as $receiver)

          {

              $email = $receiver['email'];

              $ok = (new Mailer())

                  ->setServer($config['host'], $config['port'], $config['verify_type'])

                  ->setAuth($config['user'], $config['password'])

                  ->setFrom($config['user'], $config['user'])

                  ->addTo($email, $email)

                  ->setSubject('您有待审批流程需要处理')

                  ->setBody($body)

                  ->send();

          }

          return ['code' => 1, 'msg' => '邮件发送成功'];

      }

  

      /*

       * 自定义阿里云短消息发送

       */

      public function sendAliyunSms($receivers, $config, $body)

      {

          $aliyunSms = new \addons\fastflow\library\aliyunsms\AliyunSms($config['accessKeyId'], $config['accessKeySecret'], $config['signName'], $config['templateCode'], $body, $config['endpoint']);

          $phoneNumbers = '';

          foreach ($receivers as $receiver)

          {

              if (isset($admin['mobile'])) {

                  $phoneNumbers .= ',' . $admin['mobile'];

              }

          }

          $phoneNumbers = trim($phoneNumbers, ',');

          $result = $aliyunSms->sendSms($phoneNumbers);

          if ($result['Code'] == 'OK') {

              $this->success('短信发送成功!');

          } else {

              $this->error($result['Message']);

          }

      }

  

  }