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 10.6 KB
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
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
<?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;

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

    //新增/编辑 消息模板
    private function addTemplate($request)
    {
        $data = $request->input();
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
32
        $insert_tpl_id = $data['tpl_id'];
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
33

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
34
        //至少选择一个渠道
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
35 36
        if (empty($data['channels'])) {
            $this->Export(11001, '请勾选至少一个消息渠道');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
37
        }
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
38 39
        if (empty($data['description'])) {
            $this->Export(11002, '请输入模板描述');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
40 41
        }

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
42 43 44
        //添加事务 保证 数据库多条修改时 原子操作
        DB::beginTransaction();
        try {
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
45

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
46 47 48 49 50 51 52 53 54
            //消息模板模型
            $tpl_model = [];
            $tpl_model['tpl_id'] = $data['tpl_id'];
            $tpl_model['channels'] = $data['channels'];
            $tpl_model['description'] = $data['description'];
            $tpl_model['source_type'] = $data['source_type'];
            $tpl_model['creater'] = $data['creater'];
            //        $tpl_model['obj_user'] = $data['obj_user'];
            $tpl_model['msg_type'] = $data['msg_type'];
李洋's avatar
111
771db551
 
李洋 committed 7 years ago
55

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
56 57 58 59 60 61
            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);
                if (!$insert_tpl_id) {
                    $this->Export(11020, '新增消息模板失败');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
62
                }
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
63
            } else {
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
64 65 66
                $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
67
                if (!$result) {
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
68
                    $this->Export(11021, '编辑消息模板失败');
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
69 70
                }
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
71 72 73 74 75 76

            //每一个渠道模板 的验证与写数据库
            //选了站内信渠道 就要选站内信类型
            if (strpos($data['channels'], '1') !== false) {
                if (empty($data['msg_type'])) {
                    $this->Export(11003, '请选择站内信类型');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
77
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
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
                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
113 114
                }
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
115 116 117
            if (strpos($data['channels'], '2') !== false) {
                if (empty($data['sms_content'])) {
                    $this->Export(11010, '请填写短信内容');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
118
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
119 120 121
                $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
122

李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                $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
138 139
                }
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
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
            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
update api
fc0928a2
 
李洋 committed 7 years ago
167
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
168 169 170
            if (strpos($data['channels'], '4') !== false) {
                if (empty($data['wechat_notify_title'])) {
                    $this->Export(11017, '请填写微信模板编号');
李洋's avatar
update api
fc0928a2
 
李洋 committed 7 years ago
171
                }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                $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;
                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
190
                }
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
191
            }
李洋's avatar
添加事务 保证 数据库多条修改时 原子操作
294256d2
 
李洋 committed 7 years ago
192 193 194
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
195
        }
李洋's avatar
修复更新模板问题
ff1bf2cd
 
李洋 committed 7 years ago
196
        $this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功');
李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
197
    }
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
198

李洋's avatar
添加修改数据库api
d624e727
 
李洋 committed 7 years ago
199 200 201 202
    //编辑手动消息
    //发送手动消息
    private function sendManualMessage($request)
    {
李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
203 204 205
//        Csrf($request);
        $data = $request->input();
        $url = 'http://api.liexin.com/msg/sendMessageByHandle';
李洋's avatar
111
771db551
 
李洋 committed 7 years ago
206 207


李洋's avatar
手动发消息接口联调
cfe21d14
 
李洋 committed 7 years ago
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        $params = [];
        $params['channels'] = $data['channels'];
        $params['msg_type'] = $data['msg_type'];
//        $params['creater'] = $data['creater'];
        $params['description'] = $data['description'];
        $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'];
        $params['url'] = $data['url'];
        $params['obj_user'] = $data['obj_user'];
        $params['wechat_tpl_id'] = $data['wechat_tpl_id'];
        $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
230

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

    }
}