Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

李洋 / 消息系统

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • CMS-Message
  • app
  • Http
  • Controllers
  • MessageApiController.php
MessageApiController.php 27.6 KB
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/12/7
 * Time: 13:04
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use RedisDB;
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
15
use Config;
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
16
use \Exception;
孙龙's avatar
批量上传 导入发送消息 邮件
88f7fb89
 
孙龙 committed 6 years ago
17 18 19 20
use Excel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
21 22 23 24 25 26 27 28 29 30 31 32 33

class MessageApiController extends Controller
{
    public function Entrance(Request $request, $id ){
        //统一入口
        $this->$id($request, $id);
    }

    private function Export($errcode=0,$errmsg='成功',$data=''){
        echo json_encode(['errcode'=>$errcode,'errmsg'=>$errmsg,'data'=>$data]);
        exit();
    }

孙龙's avatar
优化后台
1ad003fd
 
孙龙 committed 6 years ago
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    //删除消息模板
    public function deletetemplate($request){
        $data = $request->input();
        try{
            $tid = isset($data['id']) && $data['id'] ? intval($data['id']) : 0 ;
            $cnt = DB::connection('message');
            $bk = $cnt->table('lie_msg_tpl')->where("tpl_id",$tid)->update(['status'=>-1]);
            if($bk){
                $this->Export(0, '');
            }else{
                throw new \Exception('操作失败');
            }
        }catch(\Exception $e){
            $this->Export(1000, '操作失败');
        }

    }

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
52 53 54 55
    //新增/编辑 消息模板
    private function addTemplate($request)
    {
        $data = $request->input();
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
56
        $insert_tpl_id = $data['tpl_id'];
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
57

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
58
        //至少选择一个渠道
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
59 60
        if (empty($data['channels'])) {
            $this->Export(11001, '请勾选至少一个消息渠道');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
61
        }
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
62
        if (empty($data['description'])) {
李洋's avatar
1、消息描述加中文
ec3a2067
 
李洋 committed 7 years ago
63 64
            $this->Export(11002, '请输入消息key');
        }
李洋's avatar
添加消息模板类别
4260a4a7
 
李洋 committed 6 years ago
65 66 67
//        if (empty($data['tpl_key'])) {
//            $this->Export(11003, '请输入模板描述');
//        }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
68 69 70
//        if (empty($data['cls_id'])){
//            $this->Export(11024, '请输入模板分类');
//        }
李洋's avatar
添加消息模板类别
4260a4a7
 
李洋 committed 6 years ago
71 72
        if (empty($data['sub_cls_id'])){
            $this->Export(11025, '请输入模板二级分类');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
73
        }
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
74

李洋's avatar
1、添加重复的 模板描述 错误提示
253536a8
 
李洋 committed 7 years ago
75
        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
76 77 78 79
        if(empty($insert_tpl_id))
        {
            if($is_dumplicated !== NULL)
            {
李洋's avatar
1、消息描述加中文
ec3a2067
 
李洋 committed 7 years ago
80
                $this->Export(11022, '已存在相同模板key,请更换key内容');
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
81 82
            }
        }else
李洋's avatar
1、添加重复的 模板描述 错误提示
253536a8
 
李洋 committed 7 years ago
83
        {
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
84 85 86
            $is_edit_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->whereNotIn('tpl_id',[$insert_tpl_id])->first();
            if($is_edit_dumplicated !== NULL)
            {
李洋's avatar
1、消息描述加中文
ec3a2067
 
李洋 committed 7 years ago
87
                $this->Export(11023, '已存在相同模板key,请更换key内容');
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
88
            }
李洋's avatar
1、添加重复的 模板描述 错误提示
253536a8
 
李洋 committed 7 years ago
89
        }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
90

李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
91

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
92
        //添加事务 保证 数据库多条修改时 原子操作
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
93
        DB::connection('message')->transaction(function() use($data) {
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
94 95 96 97 98
            //消息模板模型
            $tpl_model = [];
            $tpl_model['tpl_id'] = $data['tpl_id'];
            $tpl_model['channels'] = $data['channels'];
            $tpl_model['description'] = $data['description'];
李洋's avatar
添加消息模板类别
4260a4a7
 
李洋 committed 6 years ago
99
//            $tpl_model['ex_str'] = $data['tpl_key'];
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
100 101
            $tpl_model['source_type'] = $data['source_type'];
            $tpl_model['creater'] = $data['creater'];
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
102
            $tpl_model['obj_user'] = $data['obj_user'];
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
103
            $tpl_model['msg_type'] = $data['msg_type'];
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
104
            $tpl_model['cls_id'] = $data['sub_cls_id'];
李洋's avatar
111
771db551
 
李洋 committed 7 years ago
105

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
106 107 108 109
            if (empty($tpl_model['tpl_id'])) {
                $tpl_model['create_time'] = time();
                $tpl_model['update_time'] = time();
                $insert_tpl_id = DB::connection('message')->table('lie_msg_tpl')->insertGetId($tpl_model);
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
110
//                var_dump($insert_tpl_id);die;
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
111 112
                if (!$insert_tpl_id) {
                    $this->Export(11020, '新增消息模板失败');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
113
                }
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
114
            } else {
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
115 116 117
                $tpl_model['update_time'] = time();
                $result = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id', $tpl_model['tpl_id'])->update($tpl_model);
                $insert_tpl_id = $tpl_model['tpl_id'];
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
118
                if (!$result) {
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
119
                    $this->Export(11021, '编辑消息模板失败');
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
120 121
                }
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
122 123 124 125 126 127

            //每一个渠道模板 的验证与写数据库
            //选了站内信渠道 就要选站内信类型
            if (strpos($data['channels'], '1') !== false) {
                if (empty($data['msg_type'])) {
                    $this->Export(11003, '请选择站内信类型');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
128
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
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
                if (empty($data['inner_title'])) {
                    $this->Export(11004, '请填写站内信标题');
                }
                if (empty($data['inner_content'])) {
                    $this->Export(11005, '请填写站内信内容');
                }
                if (empty($data['op_type'])) {
                    $this->Export(11006, '请选择点击消息操作');
                }
                if (empty($data['url']) && $data['op_type'] == 2) {
                    $this->Export(11007, '请填写要打开的url');
                }
                $chn_tpl_model_inner = [];
                $chn_tpl_model_inner['channel_tpl_id'] = $data['inner_channel_tpl_id'];
                $chn_tpl_model_inner['channel_type'] = $data['inner_channel_type'];
                $chn_tpl_model_inner['title'] = $data['inner_title'];
                $chn_tpl_model_inner['content'] = $data['inner_content'];
                $chn_tpl_model_inner['op_type'] = $data['op_type'];
                $chn_tpl_model_inner['tpl_id'] = $insert_tpl_id;
                if ($data['op_type'] == 2) {
                    $chn_tpl_model_inner['url'] = $data['url'];
                }
                if (empty($chn_tpl_model_inner['channel_tpl_id'])) {
                    $chn_tpl_model_inner['create_time'] = time();
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11008, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_inner['channel_tpl_id'])->update($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11009, '编辑消息模板失败');
                    }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
164
                }
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
165 166 167 168
            } else {
                if (!empty($data['inner_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['inner_channel_tpl_id'])->delete();
                    if (!$delete_result) {
李洋's avatar
1、删除不勾选的模板渠道
b151a00e
 
李洋 committed 7 years ago
169 170 171
                        $this->Export(11009, '编辑消息模板失败');
                    }
                }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
172
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
173 174 175
            if (strpos($data['channels'], '2') !== false) {
                if (empty($data['sms_content'])) {
                    $this->Export(11010, '请填写短信内容');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
176
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
177 178 179
                $chn_tpl_model_sms = [];
                $chn_tpl_model_sms['channel_tpl_id'] = $data['sms_channel_tpl_id'];
                $chn_tpl_model_sms['channel_type'] = $data['sms_channel_type'];
李洋's avatar
111
771db551
 
李洋 committed 7 years ago
180

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                $chn_tpl_model_sms['content'] = $data['sms_content'];
                $chn_tpl_model_sms['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_sms['channel_tpl_id'])) {
                    $chn_tpl_model_sms['create_time'] = time();
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11011, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_sms['channel_tpl_id'])->update($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11012, '编辑消息模板失败');
                    }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
196
                }
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
197 198 199 200
            } else {
                if (!empty($data['sms_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['sms_channel_tpl_id'])->delete();
                    if (!$delete_result) {
李洋's avatar
1、删除不勾选的模板渠道
b151a00e
 
李洋 committed 7 years ago
201 202 203
                        $this->Export(11012, '编辑消息模板失败');
                    }
                }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
204
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            if (strpos($data['channels'], '3') !== false) {
                if (empty($data['email_title'])) {
                    $this->Export(11013, '请填写邮件标题');
                }
                if (empty($data['email_content'])) {
                    $this->Export(11014, '请填写邮件内容');
                }
                $chn_tpl_model_email = [];
                $chn_tpl_model_email['channel_tpl_id'] = $data['email_channel_tpl_id'];
                $chn_tpl_model_email['channel_type'] = $data['email_channel_type'];
                $chn_tpl_model_email['title'] = $data['email_title'];
                $chn_tpl_model_email['content'] = $data['email_content'];
                $chn_tpl_model_email['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_email['channel_tpl_id'])) {
                    $chn_tpl_model_email['create_time'] = time();
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11015, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_email['channel_tpl_id'])->update($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11016, '编辑消息模板失败', $result);
                    }
                }
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
232 233 234 235
            } else {
                if (!empty($data['email_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['email_channel_tpl_id'])->delete();
                    if (!$delete_result) {
李洋's avatar
1、删除不勾选的模板渠道
b151a00e
 
李洋 committed 7 years ago
236 237 238
                        $this->Export(11016, '编辑消息模板失败');
                    }
                }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
239
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
240 241 242
            if (strpos($data['channels'], '4') !== false) {
                if (empty($data['wechat_notify_title'])) {
                    $this->Export(11017, '请填写微信模板编号');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
243
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
244 245 246 247 248
                $chn_tpl_model_wechatnotify = [];
                $chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
                $chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
                $chn_tpl_model_wechatnotify['title'] = $data['wechat_notify_title'];
                $chn_tpl_model_wechatnotify['tpl_id'] = $insert_tpl_id;
李洋's avatar
微信
86ebc545
 
李洋 committed 7 years ago
249 250 251
                $chn_tpl_model_wechatnotify['url'] = $data['wechat_notify_url'];
                $wechat_head = $data['wechat_notify_head'];
                $wechat_tail = $data['wechat_notify_tail'];
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
252
                $wechat_content = json_encode(array('wechat_tpl_head' => $wechat_head, 'wechat_tpl_tail' => $wechat_tail));
李洋's avatar
微信
86ebc545
 
李洋 committed 7 years ago
253
                $chn_tpl_model_wechatnotify['content'] = $wechat_content;
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
254 255 256 257 258 259 260 261 262 263 264 265 266
                if (empty($chn_tpl_model_wechatnotify['channel_tpl_id'])) {
                    $chn_tpl_model_wechatnotify['create_time'] = time();
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11018, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_wechatnotify['channel_tpl_id'])->update($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11019, '编辑消息模板失败');
                    }
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
267
                }
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
268 269 270 271
            } else {
                if (!empty($data['wechat_notify_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['wechat_notify_tpl_id'])->delete();
                    if (!$delete_result) {
李洋's avatar
1、删除不勾选的模板渠道
b151a00e
 
李洋 committed 7 years ago
272 273 274
                        $this->Export(11019, '编辑消息模板失败');
                    }
                }
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
275
            }
李洋's avatar
1111
964b032b
 
李洋 committed 7 years ago
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
            if (strpos($data['channels'], '5') !== false) {
                if (empty($data['ding_content'])) {
                    $this->Export(11020, '请填写钉钉消息内容');
                }
                $chn_tpl_model_ding = [];
                $chn_tpl_model_ding['channel_tpl_id'] = $data['ding_channel_tpl_id'];
                $chn_tpl_model_ding['channel_type'] = $data['ding_channel_type'];

                $chn_tpl_model_ding['content'] = $data['ding_content'];
                $chn_tpl_model_ding['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_ding['channel_tpl_id'])) {
                    $chn_tpl_model_ding['create_time'] = time();
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11021, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_ding['channel_tpl_id'])->update($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11022, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['ding_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['ding_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11023, '编辑消息模板失败');
                    }
                }
            }
        });
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
309
        $this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功');
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
310
    }
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
311

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
312 313 314 315
    //编辑手动消息
    //发送手动消息
    private function sendManualMessage($request)
    {
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
316
        $data = $request->input();
李洋's avatar
1、添加手动发送消息时 模板描述唯一性逻辑
ca8b07dd
 
李洋 committed 7 years ago
317 318 319
        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
        if($is_dumplicated !== NULL)
        {
李洋's avatar
1、消息描述加中文
ec3a2067
 
李洋 committed 7 years ago
320
            $this->Export(11024, '已存在相同模板key,请更换key内容');
李洋's avatar
1、添加手动发送消息时 模板描述唯一性逻辑
ca8b07dd
 
李洋 committed 7 years ago
321 322 323 324
        }

        $url = '';
        $current_domain = $_SERVER['HTTP_HOST'];
李洋's avatar
配置调整
d44c7c60
 
李洋 committed 7 years ago
325 326 327 328
        if($current_domain === Config('msgconfig.domain_local'))
        {
            $url = Config('msgconfig.api_domain_local');
        }elseif ($current_domain === Config('msgconfig.domain_sz'))
李洋's avatar
1、添加手动发送消息时 模板描述唯一性逻辑
ca8b07dd
 
李洋 committed 7 years ago
329
        {
李洋's avatar
配置调整
d44c7c60
 
李洋 committed 7 years ago
330 331
            $url = Config('msgconfig.api_domain_sz');
        }elseif ($current_domain === Config('msgconfig.domain_release'))
李洋's avatar
1、添加手动发送消息时 模板描述唯一性逻辑
ca8b07dd
 
李洋 committed 7 years ago
332
        {
李洋's avatar
配置调整
d44c7c60
 
李洋 committed 7 years ago
333
            $url = Config('msgconfig.api_domain_release');
李洋's avatar
1、添加手动发送消息时 模板描述唯一性逻辑
ca8b07dd
 
李洋 committed 7 years ago
334
        }
李洋's avatar
fix bug
e9771a85
 
李洋 committed 7 years ago
335

李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
336 337 338 339 340
        $params = [];
        $params['channels'] = $data['channels'];
        $params['msg_type'] = $data['msg_type'];
//        $params['creater'] = $data['creater'];
        $params['description'] = $data['description'];
李洋's avatar
1...
c5ef9078
 
李洋 committed 7 years ago
341
        $params['ex_str'] = $data['true_description'];
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
342 343 344 345 346 347
        $params['title_1'] = $data['title_1'];
        $params['title_3'] = $data['title_3'];
        $params['content_1'] = $data['content_1'];
        $params['content_2'] = $data['content_2'];
        $params['content_3'] = $data['content_3'];
        $params['op_type'] = $data['op_type'];
李洋's avatar
微信
86ebc545
 
李洋 committed 7 years ago
348
        $params['url_1'] = $data['url_1'];
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
349
        $params['obj_user'] = $data['obj_user'];
李洋's avatar
微信
86ebc545
 
李洋 committed 7 years ago
350 351 352 353 354 355 356
        $params['title_4'] = $data['title_4'];
        $params['url_4'] = $data['url_4'];

        $wechat_head = $data['wechat_notify_head'];
        $wechat_tail = $data['wechat_notify_tail'];
        $wechat_content = json_encode(array('wechat_tpl_head'=>$wechat_head,'wechat_tpl_tail'=>$wechat_tail));
        $params['content_4'] = $wechat_content;
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
357 358 359 360 361 362 363 364
        $params['expect_send_time'] = $data['expect_send_time'];
        //csrf
        $check['k1'] = time();
        $check['k2'] = pwdhash($check['k1'],'fh6y5t4rr351d2c3bryi');
        $params['pf'] = 1;
        $params['k1'] = $check['k1'];
        $params['k2'] = $check['k2'];
        $response = json_decode(curl($url,$params,1),true);
李洋's avatar
111
771db551
 
李洋 committed 7 years ago
365

李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
366
        $this->Export($response['err_code'], $response['err_msg']);
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
367 368

    }
李洋's avatar
添加消息模板类别
4260a4a7
 
李洋 committed 6 years ago
369 370 371 372 373 374

    /** 获取模板子分类
     * @param $request
     */
    private function getSubTplCls($request){
        $data = $request->input();
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
375 376
        $sub_tpl_info = DB::connection('message')->table('lie_msg_classify')->select('cls_id','name')
            ->where('parent_id',$data['sel_edit_cls'])->where('parent_id','!=',0)->get();
李洋's avatar
添加消息模板类别
4260a4a7
 
李洋 committed 6 years ago
377 378
        $this->Export(0, 'success',objectToArray($sub_tpl_info));
    }
李洋's avatar
消息模板改版 包括列表页 和 新增/编辑页
915e5bdd
 
李洋 committed 6 years ago
379

李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
380
    //删除消息模板(软删)
李洋's avatar
消息模板改版 包括列表页 和 新增/编辑页
915e5bdd
 
李洋 committed 6 years ago
381 382 383 384 385 386 387
    private function delTpl($request){
        $in_data = $request->input();
        $res = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id',$in_data['tpl_id'])->update(['status'=>1]);
        if ($res && $res !== false){
            $this->Export(0, 'success');
        }
    }
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
388

李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
389
    //新增/编辑分类
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
390 391 392
    private function editClassify($request){
        $in_data = $request->input('field');

李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
393 394
        //验证数据合理性 并去除字符串中的空格
        $in_data = $this->verifyClassifyData($in_data);
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
395 396 397 398

        //数据库操作
        $db = DB::connection('message');
        if (1 == $in_data['edit_type']){
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
            $classify_model = $this->constructClassifyModel($db, [], $in_data);
            $this->insertClassify($db, $classify_model);
        }else{
            $this->updateClassify($db, $in_data);
        }
        $this->Export(0, '操作成功');
    }

    //获取父级iD路径
    private function getParentIdPath($db, $parent_id) {
        if ($parent_id==0) {
            return strval(1);
        }
        $parent_id_path = $db->table('lie_msg_classify')->select('id_path')->where('cls_id',$parent_id)->first();
        if (!$parent_id_path) {
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
414
            $this->Export(12001, '未查找到根分类!');
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
415 416 417 418 419 420 421 422 423
        }
        return $parent_id_path->id_path;
    }

    //添加分类
    private function insertClassify($db, $classify_model) {
        if ($classify_model['depth']==1) {
            $root_id = $db->table('lie_msg_classify')->insertGetId($classify_model);
            if (!$root_id) {
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
424
                $this->Export(12002, '创建根分类失败!');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
425
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
426 427 428 429 430 431 432 433
            $this->Export(0, '创建根分类成功');
        } else {
            //获取父级id路径
            $parent_id_path = $this->getParentIdPath($db, $classify_model['parent_id']);
            $db->beginTransaction();
            try {//添加一级/二级分类
                $new_cls_id = $db->table('lie_msg_classify')->insertGetId($classify_model);
                if (!$new_cls_id) {
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
434
                    $this->Export(12003, '创建分类失败!');
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
435 436 437 438
                }else {//补充id路径
                    $id_path = $parent_id_path.','.strval($new_cls_id);
                    $res = $db->table('lie_msg_classify')->where('cls_id',$new_cls_id)->update(['id_path'=>$id_path]);
                    if (!$res) {
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
439
                        $this->Export(12004, '更新分类id路径失败!');
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
440
                    }
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
441
                }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
442 443 444
                $db->commit();
            } catch (Exception $e) {
                $db->rollback();
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
445
                $this->Export(12005, '新增分类失败');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
446
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
447 448 449 450 451 452 453 454 455
        }
    }

    //更新分类
    private function updateClassify($db, $in_data){
        if (!empty($in_data['sel_edit_sub_classify'])){
            $cls_id = $in_data['sel_edit_sub_classify'];
            $res = $db->table('lie_msg_classify')->where('cls_id',$cls_id)->update(['name'=>$in_data['edit_classify']]);
            if (!$res && $res !== 0){
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
456
                $this->Export(12006, '编辑子分类失败');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
457
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
458 459 460 461 462 463
        }else {
            $cls_id = $in_data['sel_edit_classify'];
            $db->beginTransaction();
            try {
                $res = $db->table('lie_msg_classify')->where('cls_id',$cls_id)->update(['name'=>$in_data['edit_classify']]);
                if (!$res && $res !== 0){
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
464
                    $this->Export(12007, '编辑子分类失败');
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
465 466 467
                }
                $res = $db->table('lie_msg_classify')->where('parent_id',$cls_id)->update(['parent_name'=>$in_data['edit_classify']]);
                if (!$res && $res !== 0){
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
468
                    $this->Export(12008, '编辑子分类失败');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
469
                }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
470 471 472
                $db->commit();
            } catch (Exception $e) {
                $db->rollback();
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
473
                $this->Export(12009, '编辑子分类失败');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
474 475
            }
        }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    }

    //构建消息分类模型
    private function constructClassifyModel($db, $classifyModel, $in_data) {
        $depth = 0;//分类深度
        //检测表中是否有数据,没有则为根节点
        $res = $db->table('lie_msg_classify')->select('cls_id')->first();
        if (!$res) {
            $depth = 1;
        } else {
            if (empty($in_data['sel_add_classify'])) {
                $depth = 2;
            } else {
                $depth = 3;
            }
        }
        $classify_model['depth']    = $depth;
        $classify_model['status']   = 1;
        $classify_model['name'] = $in_data['add_classify'];
        if ($depth==1) {
            $classify_model['parent_id'] = 0;
            $classify_model['parent_name'] = '';
            $classify_model['id_path']  = $this->getParentIdPath($db, $classify_model['parent_id']);
        }elseif($depth==2) {
            $classify_model['parent_id'] = 1;
            $parent_name = $db->table('lie_msg_classify')->select('name')->where('cls_id',$classify_model['parent_id'])->first();
            $classify_model['parent_name'] = $parent_name->name;
        }elseif($depth==3) {
            $classify_model['parent_id'] = $in_data['sel_add_classify'];
            $parent_name = $db->table('lie_msg_classify')->select('name')->where('cls_id',$classify_model['parent_id'])->first();
            $classify_model['parent_name'] = $parent_name->name;
        }else {
李洋's avatar
1、改返回的错误码
e599782b
 
李洋 committed 6 years ago
508
            $this->Export(12010, '参数错误');
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
509 510
        }
        return $classify_model;
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
511 512 513 514 515
    }

    //验证修改分类数据的合理性
    private function verifyClassifyData($in_data){
        if (1 == $in_data['edit_type']){
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
516 517
            if (empty($in_data['add_classify'])){
                $this->Export(11104, '请填写要添加的分类名称');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
518 519
            }
        }else{
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
520 521
            if (empty($in_data['sel_edit_classify']) && empty($in_data['sel_edit_sub_classify'])){
                $this->Export(11106, '请选择要编辑的分类或子分类');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
522
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
523 524
            if (empty($in_data['edit_classify'])){
                $this->Export(11107, '请填写要编辑的分类名称');
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
525
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
526 527 528 529 530 531
        }

        //去字符串空格
        if (!empty($in_data)){
            foreach ($in_data as &$d){
                $d = trim($d);
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
532
            }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
533
            unset($d);
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
534
        }
李洋's avatar
消息模板分类
26db6e8a
 
李洋 committed 6 years ago
535
        return $in_data;
李洋's avatar
添加 新增/编辑 消息模板分类功能
9438ddbd
 
李洋 committed 6 years ago
536
    }
孙龙's avatar
批量上传 导入发送消息 邮件
88f7fb89
 
孙龙 committed 6 years ago
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

    /**
     * 上传文件
     */
    public function updateMsgExcel(Request $request){

        $file = $request->file('file');
        $tp_id = $request->get('inner-choose-type');
        if(!$tp_id)  return back()->with("error_batch_msg","请选择对应消息模板");
        $info = DB::connection("message")->table("lie_msg_tpl")->where("tpl_id",intval($tp_id))->select("tpl_id","channels","description")->first();
        if(!$info) return back()->with("error_batch_msg","没找到对应消息模板");
        if(count(explode(",",$info->channels)) > 1) return back()->with("error_batch_msg","暂只支持一种渠道消息,请完善模板消息后再来");
        if($info->channels != 3  ) return back()->with("error_batch_msg","暂只支持邮件");
        if($file){
            try{
                $excel_file_path = $file->getRealPath();
                $res = [];
                $title = [];
                Excel::load($excel_file_path, function($reader) use( &$res,&$title ) {
                    $reader = $reader->getSheet(0);
                    $res = $reader->toArray();
                    $title = $res[0];
                    unset($res[0]);
                },'GBK');
孙龙's avatar
同下
d2fb461f
 
孙龙 committed 6 years ago
561 562 563 564
                $exchange = Config("website.msg_exchange"); // 交换器,在我理解,如果两个队列使用一个交换器就代表着两个队列是同步的,这个队列里存在的消息,在另一个队列里也会存在
                $queue = Config("website.msg_queue"); // 队列名称
                $connection = new AMQPStreamConnection(Config("website.rabbitmq.host"),
                    Config("website.rabbitmq.port"), Config("website.rabbitmq.login"), Config("website.rabbitmq.password"), Config("website.rabbitmq.vhost")); // 创建连接
孙龙's avatar
批量上传 导入发送消息 邮件
88f7fb89
 
孙龙 committed 6 years ago
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
                $channel = $connection->channel();
                $channel->queue_declare($queue, false, true, false, false);
                $channel->exchange_declare($exchange, 'direct', false, true, false);
                $channel->queue_bind($queue, $exchange); // 队列和交换器绑定
                $channel->tx_select();//开启事务
                foreach($res as $key=>$val){
                    $params = [];
                    $touser = $val[0];
                    unset($val[0]);
                    foreach($val as $kk=>$vv){
                        $params[array_get($title,$kk)] = $vv;
                    }
                    $msg_arr = [];
                    $msg_arr['job'] = 'msg.batch.send';
                    $_data['is_oversea'] = false;
                    $_data['template_id'] = $info->tpl_id;
                    $_data['keyword'] = $info->description;
                    $_data['channel_type'] = $info->channels;
                    $_data['touser'] = $touser;
                    $_data['data'] = json_encode($params);
                    $_data['url'] = '';
                    $_data['wechat_data'] = null;
                    $_data['is_ignore'] = 1;
                    $_data['ex_int'] = 0;
                    $_data['ex_str'] = "";
                    $_data['pf'] = "999999";
                    $_data['fromuser'] = "";
                    $_data['delay'] = 0;
                    $_data['rbmq'] = [
                        'expire_time'=>time()+3600*2
                    ];
孙龙's avatar
同下
d2fb461f
 
孙龙 committed 6 years ago
596
//                    dump($_data);
孙龙's avatar
批量上传 导入发送消息 邮件
88f7fb89
 
孙龙 committed 6 years ago
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
                    $msg_arr['data'] = $_data;
                    $msg = new AMQPMessage(json_encode($msg_arr));
                    $channel->basic_publish($msg, $exchange);
                }
                $channel->tx_commit();//提交事务
                $channel->close();
                $connection->close();
                return back()->with("success_batch_msg","上传成功");
            }catch(\Exception $e){
//                dump($e->getMessage());
                $channel->tx_rollback();
                return back()->with("error_batch_msg","请选择对应消息模板");
            }

        }
        return  back()->with("error_batch_msg","请务必上传文件");
    }
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
614
}