123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- <?php
- namespace app\admin\controller;
- use app\admin\BaseController;
- use think\facade\Db;
- use think\facade\View;
- use think\facade\Session;
- use think\App;
- use app\wechat\controller\Officialaccount;
- class Pushmessage{
- protected $Officialaccount;
- public function initialize(){
- return new Officialaccount();
- }
- // 通知设置列表
- public function index(){
- if (request()->isAjax()){
- $user_type = get_login_admin('user_type');
- $where = array();
- if($user_type == -1 || $user_type == -2){
- $where = [
- 'user_type' => -1,
- ];
- }
-
- $list = Db::name('PushMessage')->where($where)->select();
- // dump($list);
- return to_assign(0, '', $list);
- } else {
- return view();
- }
- }
- // 添加通知设置
- public function add(){
- $param = get_params();
- if(request()->isAjax()){
-
- $data = [
- 'title' => $param['title'],
- 'is_who' => implode(',', $param['is_who']),
- 'is_wechat' => $param['is_wechat'],
- 'is_wechat_company' => $param['is_wechat_company'],
- 'is_wechat_proprietor' => $param['is_wechat_proprietor'],
-
- 'content' => $param['content'],
- 'is_project_name' => $param['is_project_name'],
- 'is_people' => $param['is_people'],
- 'content_company' => $param['content_company'],
- 'is_project_name_company' => $param['is_project_name_company'],
- 'is_people_company' => $param['is_people_company'],
- 'content_proprietor' => $param['content_proprietor'],
- 'is_project_name_proprietor' => $param['is_project_name_proprietor'],
- 'is_people_proprietor' => $param['is_people_proprietor'],
- // 'rule_id' => $param['rule_id'],
- // 'rule_url' => Db::name('AdminRule')->where('id', $param['rule_id'])->value('src'),
- 'rule_url' => $param['rule_url'],
- 'user_type' => -1,
- 'unit_name' => get_login_admin('unit_name'),
- 'pid' => $param['pid'],
- ];
-
- if(isset( $param['title_company'])){
- $data['title_company'] = $param['title_company'];
- }
- if(isset( $param['title_proprietor'])){
- $data['title_proprietor'] = $param['title_proprietor'];
- }
- // halt($data);
- if($data['rule_url'] != NULL){
- $rule_url = Db::name('PushMessage')->where('rule_url', $data['rule_url'])->find();
- if($rule_url != NULL){
- return to_assgin(1, "已存在该通知阶段");
- }
- }
-
- Db::startTrans();
- try {
- $uid = Db::name('PushMessage')->strict(false)->field(true)->insertGetId($data);
- add_log('add', $uid, $data);
- // 提交事务
- Db::commit();
- }catch(\Exception $e){
- // 回滚事务
- Db::rollback();
- return to_assign(1, '提交失败:' . $e->getMessage());
- }
- return to_assign(0,"操作成功");
- }else{
- $pid = isset($param['pid']) ? $param['pid'] : 0;
- $user_type = get_login_admin('user_type');
- $push_message = Db::name('PushMessage')->where('user_type', -1)->select()->toArray();
- $push_message = set_recursion($push_message);
- $rule = Db::name('adminRule')->order('sort asc,id asc')->select();
- $rule = set_recursion($rule);
- View::assign('push_message', $push_message);
- View::assign('rule', $rule);
- View::assign('pid', $pid);
- return view();
- }
-
- }
- //编辑通知设置
- public function edit(){
-
- $param = get_params();
- // halt($param);
- if(request()->isAjax()){
- $arr = array();
- if(isset($param['is_cost'])){
- $arr[0] = 0;
- }
- if(isset($param['is_owner'])){
- $arr[1] = 1;
- }
- if(isset($param['is_company'])){
- $arr[2] = 2;
- }
- $param['is_who'] = implode(',', $arr);
- // halt($param);
- $data = [
- 'id' => $param['id'],
- 'title' => $param['title'],
- 'title_company' => $param['title_company'],
- 'title_proprietor' => $param['title_proprietor'],
- 'is_who' => $param['is_who'],
- 'is_wechat' => $param['is_wechat'],
- 'is_wechat_company' => $param['is_wechat_company'],
- 'is_wechat_proprietor' => $param['is_wechat_proprietor'],
-
- 'content' => $param['content'],
- 'is_project_name' => $param['is_project_name'],
- 'is_people' => $param['is_people'],
- 'content_company' => $param['content_company'],
- 'is_project_name_company' => $param['is_project_name_company'],
- 'is_people_company' => $param['is_people_company'],
- 'content_proprietor' => $param['content_proprietor'],
- 'is_project_name_proprietor' => $param['is_project_name_proprietor'],
- 'is_people_proprietor' => $param['is_people_proprietor'],
- 'rule_url' => $param['rule_url'],
- 'user_type' => -1,
- 'unit_name' => get_login_admin('unit_name'),
- 'pid' => $param['pid'],
- ];
- if($data['rule_url'] != NULL){
- if($data['rule_url'] != Db::name('PushMessage')->where('id', $data['id'])->value('rule_url')){
- $rule_url = Db::name('PushMessage')->where('rule_url', $data['rule_url'])->find();
- if($rule_url != NULL){
- return to_assign(1, "已存在该通知阶段");
- }
- }
- }
-
- // halt($data);
- Db::startTrans();
- try {
- Db::name('PushMessage')->strict(false)->field(true)->update($data);
- add_log('add', $param['id'], $data);
- // 提交事务
- Db::commit();
- }catch(\Exception $e){
- // 回滚事务
- Db::rollback();
- return to_assign(1, '提交失败:' . $e->getMessage());
- }
- return to_assign(0,"操作成功");
- }else{
- $id = $param['id'];
- $data = Db::name('PushMessage')->where('id', $id)->find();
-
- $data['is_who'] = explode(',', $data['is_who']);
- // dump($data);
- if(in_array(0, $data['is_who'])){ // 财政局
- $data['is_cost'] = 1;
- }else{
- $data['is_cost'] = 0;
- }
- if(in_array(1, $data['is_who'])){ // 业主
- $data['is_owner'] = 1;
- }else{
- $data['is_owner'] = 0;
- }
-
- if(in_array(2, $data['is_who'])){ // 公司
- $data['is_company'] = 1;
- }else{
- $data['is_company'] = 0;
- }
-
- $push_message = Db::name('PushMessage')->where('user_type', -1)->select()->toArray();
- $push_message = set_recursion($push_message);
- $rule = Db::name('AdminRule')->order('sort asc,id asc')->select();
- $rule = set_recursion($rule);
-
- View::assign('data', $data);
- View::assign('push_message', $push_message);
- View::assign('rule', $rule);
- return view();
- }
- }
- // 删除通知设置
- public function delete(){
- $id = get_params('id');
- $count = Db::name('PushMessage')->where('pid', '=', $id)->count();
- if ($count > 0) {
- return to_assign(1, "该通知设置下还有子设置,无法删除");
- }
-
- if (Db::name('PushMessage')->delete($id) !== false) {
- add_log('delete', $id);
- return to_assign(0, "删除通知设置成功");
- } else {
- return to_assign(1, "删除失败");
- }
- // $is_who = [
- // 'cai','gongi',' yezhu'
- // ];
- // $is_who = [
- // 'cai','gongi'
- // ];
- // $is_who = [
- // 'cai'=> 0,
- // 'gongi'=> 1,
- // ];
- }
-
- /**
- * $url: 方法URL
- * $project_id:项目ID int
- * $financial:财政局人员 array
- * [1,2,3]
- * [
- * 0 => 1,
- * 1 => 2,
- * ];
- * $company:公司人员 [1,2,3] array
- * $proprietor:业主人员 [1,2,3] array
- * $new_url:用于新的内容设置
- */
- public function pushMessage($project_id, $financial = NULL, $company = NULL, $proprietor = NULL, $new_url = NULL){
- if($new_url == NULL){
- $url = (string) \think\facade\Route::buildUrl();
- $url = self::handleUrl($url);
- }else{
- $url = $new_url;
- }
-
-
- $push = Db::name('PushMessage')->where('rule_url', $url)->find();
- // 通知成员
- // $project_id
- $message_id = $push['id'];
- // 抄送成员
- $member = Db::name('PushMember')->where('message_id', $message_id)->where('unit_name', get_login_admin('unit_name'))->value('member_id');
-
- if(get_login_admin('user_type') == 0){
- if(!empty($financial)){
-
- // $financial = array_unique(array_merge(explode(',', $member), $financial));
- $member = self::inform_send($financial, $member);
- }
- }else if(get_login_admin('user_type') == 2){
- if(!empty($company)){
-
- // $company = array_unique(array_merge(explode(',', $member), $company));
- $member = self::inform_send($company, $member);
- }
- }else if(get_login_admin('user_type') == 1){
- $member = self::inform_send($proprietor, $member);
- }else{
- return 0;
- }
-
-
- if($financial != NULL){
- // halt($financial);
- if($push['content'] != NULL){ // 财政局
- $content = self::handleContent($push, $project_id);
-
- self::webPush($content, $project_id, $financial);
- // dump($financial, $content);
- if($push['is_wechat'] == 1){
- self::initialize()->sendText($financial, $content);
- }
- if($member != NULL){
- $str = '';
- for($i = 0; $i <count($financial); $i++){
- if($i < count($financial) - 1){
- $str = $str . Db::name('Admin')->where('id', $financial[$i])->value('nickname') . ',';
- }else{
- $str = $str . Db::name('Admin')->where('id', $financial[$i])->value('nickname');
- }
- }
- $content = str_replace('您', $str, $content);
- self::webPush($content, $project_id, $member);
- // dump($financial, $content);
- if($push['is_wechat'] == 1){
- self::initialize()->sendText($member, $content);
- }
- }
- }
- }
- if($proprietor != NULL){
- if($push['content_proprietor'] != NULL){ //公司
- $content_proprietor = self::handleContent_proprietor($push, $project_id);
- self::webPush($content_proprietor, $project_id, $proprietor);
- if($push['is_wechat_proprietor'] == 1){
- self::initialize()->sendText($proprietor, $content_proprietor);
- }
- if($member != NULL){
- $str = '';
- for($i = 0; $i <count($proprietor); $i++){
- if($i < count($proprietor) - 1){
- $str = $str . Db::name('Admin')->where('id', $proprietor[$i])->value('nickname') . ',';
- }else{
- $str = $str . Db::name('Admin')->where('id', $proprietor[$i])->value('nickname');
- }
- }
- $content = str_replace('您', $str, $content);
- self::webPush($content, $project_id, $member);
- // dump($financial, $content);
- if($push['is_wechat'] == 1){
- self::initialize()->sendText($member, $content);
- }
- }
- }
- }
-
- if($company != NULL){
- if($push['content_company'] != NULL){ //公司
- $content_company = self::handleContent_company($push, $project_id);
- self::webPush($content_company, $project_id, $company);
- if($push['is_wechat_company'] == 1){
- self::initialize()->sendText($company, $content_company);
- }
- if($member != NULL){
- $str = '';
- for($i = 0; $i <count($company); $i++){
- if($i < count($company) - 1){
- $str = $str . Db::name('Admin')->where('id', $company[$i])->value('nickname') . ',';
- }else{
- $str = $str . Db::name('Admin')->where('id', $company[$i])->value('nickname');
- }
- }
- $content = str_replace('您', $str, $content);
- self::webPush($content, $project_id, $member);
- // dump($financial, $content);
- if($push['is_wechat'] == 1){
- self::initialize()->sendText($member, $content);
- }
- }
- }
- }
- }
- // 网页通知
- public function webPush($content, $project_id, $uid){
- $uid = (array)$uid;
- for($i = 0; $i < count($uid); $i++){
- $data = [
- 'content' => $content,
- 'project_id' => $project_id,
- 'uid' => $uid[$i],
- 'create_time' => time(),
- ];
- $newId = Db::name('ProjectMsg')->strict(false)->field(true)->insertGetId($data);
- $unread = (string) Db::name('Admin')->where('id', $uid[$i])->value('unread');
- $unread = $unread . ',' . $newId;
- Db::name('Admin')->where('id', $uid[$i])->update(['unread' => $unread]);
- }
- }
- public function handleContent($push, $project_id){
- if(!is_array ($project_id)){
- $project_id = (array)$project_id;
- }
- $content = $push['content'];
- if($push['is_project_name'] == 0 && $push['is_people'] == NULL){
- $is_project_name = '';
- $people = '';
- }else if($push['is_project_name'] == 1 && $push['is_people'] == NULL){
- $is_project_name = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name = $is_project_name . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name = $is_project_name . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
-
- $people = '';
- }else if($push['is_project_name'] == 0 && $push['is_people'] != NULL){
- $is_project_name = '';
- $people = $push['is_people'] . ':' . get_login_admin('nickname') . '。';
- }else{
- $is_project_name = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name = $is_project_name . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name = $is_project_name . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
-
- $people = $push['is_people'] . ':' . get_login_admin('nickname') . '。';
- }
- if($push['is_people'] != NULL){
- $str = '“' . $push['is_people'] . '”';
- if(mb_strpos($content, $str) !== false){
- $content = str_replace($str, get_login_admin('nickname'), $content);
- }
- }
- $str = "“公司”";
- if(mb_strpos($content, $str) !== false){
- $company_name = Db::name('Department')->where('id', get_login_admin('unit_name'))->value('title');
- $content = str_replace($str, $company_name, $content);
- }
- $content = $content . "\n". $is_project_name . "\n". $people;
- return $content;
- }
- public function handleContent_proprietor($push, $project_id){
- if(!is_array ($project_id)){
- $project_id = (array)$project_id;
- }
- $content_proprietor = $push['content_proprietor'];
- if($push['is_project_name_proprietor'] == 0 && $push['is_people_proprietor'] == NULL){
- $is_project_name_proprietor = '';
- $people_proprietor = '';
- }else if($push['is_project_name_proprietor'] == 1 && $push['is_people_proprietor'] == NULL){
- $is_project_name_proprietor = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name_proprietor = $is_project_name_proprietor . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name_proprietor = $is_project_name_proprietor . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
- // $is_project_name_company = '项目名称' . ':' . Db::name('CostProject')->where('id', $project_id)->value('project_name') . '。';
- $people_proprietor = '';
- }else if($push['is_project_name_proprietor'] == 0 && $push['is_people_proprietor'] != NULL){
- $is_project_name_proprietor = '';
- $people_proprietor = $push['is_people_proprietor'] . ':' . get_login_admin('nickname') . '。';
- }else{
- $is_project_name_proprietor = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name_proprietor = $is_project_name_proprietor . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name_proprietor = $is_project_name_proprietor . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
- // $is_project_name_company = '项目名称' . ':' . Db::name('CostProject')->where('id', $project_id)->value('project_name') . '。';
- $people_proprietor = $push['is_people_proprietor'] . ':' . get_login_admin('nickname') . '。';
- }
- if($push['is_people_proprietor'] != NULL){
- $str = '“' . $push['is_people_proprietor'] . '”';
- if(mb_strpos($content_proprietor, $str) !== false){
- $content_proprietor = str_replace($str, get_login_admin('nickname'), $content_proprietor);
- }
- }
- $str = "“公司”";
- if(mb_strpos($content_proprietor, $str) !== false){
- $company_name = Db::name('Department')->where('id', get_login_admin('unit_name'))->value('title');
- $content_proprietor = str_replace($str, $company_name, $content_proprietor);
- }
- $content_proprietor = $content_proprietor . "\n". $is_project_name_proprietor . "\n". $people_proprietor;
- return $content_proprietor;
- }
- public function handleContent_company($push, $project_id){
- if(!is_array ($project_id)){
- $project_id = (array)$project_id;
- }
- $content_company = $push['content_company'];
- if($push['is_project_name_company'] == 0 && $push['is_people_company'] == NULL){
- $is_project_name_company = '';
- $people_company = '';
- }else if($push['is_project_name_company'] == 1 && $push['is_people_company'] == NULL){
- $is_project_name_company = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name_company = $is_project_name_company . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name_company = $is_project_name_company . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
- // $is_project_name_company = '项目名称' . ':' . Db::name('CostProject')->where('id', $project_id)->value('project_name') . '。';
- $people_company = '';
- }else if($push['is_project_name_company'] == 0 && $push['is_people_company'] != NULL){
- $is_project_name_company = '';
- $people_company = $push['is_people_company'] . ':' . get_login_admin('nickname') . '。';
- }else{
- $is_project_name_company = '项目名称' . ':';
- for($i = 0; $i < count($project_id); $i++){
- if($i < count($project_id) - 1){
- $is_project_name_company = $is_project_name_company . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . ',';
- continue;
- }
- $is_project_name_company = $is_project_name_company . Db::name('CostProject')->where('id', $project_id[$i])->value('project_name') . '。';
- }
- // $is_project_name_company = '项目名称' . ':' . Db::name('CostProject')->where('id', $project_id)->value('project_name') . '。';
- $people_company = $push['is_people_company'] . ':' . get_login_admin('nickname') . '。';
- }
- if($push['is_people_company'] != NULL){
- $str = '“' . $push['is_people_company'] . '”';
- if(mb_strpos($content_company, $str) !== false){
- $content_company = str_replace($str, get_login_admin('nickname'), $content_company);
- }
- }
- $str = "“公司”";
- if(mb_strpos($content_company, $str) !== false){
- $company_name = Db::name('Department')->where('id', get_login_admin('unit_name'))->value('title');
- $content_company = str_replace($str, $company_name, $content_company);
- }
- $content_company = $content_company . "\n". $is_project_name_company . "\n". $people_company;
- return $content_company;
- }
- public function handleUrl($url){
- $url = substr($url, 1);
- $url = strtolower($url);
- return $url;
- }
- public function handleId($notify){
- $idList = [];
- foreach($notify as $key => $value){
- $idList[$key] = $notify[$key]['id'];
- }
- // $idList = explode(',', $idList);
- return $idList;
- }
- public function handleParam($param){
- if (isset($param["financial"])) {
- $financial = $param['financial'];
- unset($param['financial']);
- $financial = self::handleId($financial);
- }else{
- $financial = NULL;
- }
- if(isset($param["company"])){
- $company = $param['company'];
- unset($param['company']);
- $company = self::handleId($company);
- }else{
- $company = NULL;
- }
- $data = [
- 'param' => $param,
- 'financial' => $financial,
- 'company' => $company,
- ];
-
- return $data;
- }
- // 抄送成员去除通知成员
- // $inform 通知成员
- // $member 抄送成员
- public function inform_send($inform, $member){
- // public function inform_send(){
- // $inform = [
- // 0 => 1,
- // 1 => 2,
- // ];
- // $member = [
- // // 0 => 3,
- // // 1 => 2,
- // // 2 => 6,
- // ];
- $a = 0;
- $b = 0;
- if(!empty($inform)){
- $a = count((array)$inform);
- $inform = (array)$inform;
- }
- if(!empty($member)){
- $b = count((array)$member);
- $member = (array)$member;
- }
-
- for($i = 0; $i < $a; $i++){
-
- for($j = 0; $j < $b; $j++){
- if($inform[$i] == $member[$j]){
- unset($member[$j]);
- }
- }
- }
- if(!empty($member)){
- $member = array_values($member);
- }
-
- // if($member ==null){
- // dump('111');
- // }else{
- // dump('222');
- // }
- // halt($member);
- return $member;
- }
- public function test(){
- dump(self::test1([]));
- }
- public function test1($a = NULL){
- if($a == NULL){
- return NULL;
- }else{
- return "!!!";
- }
- }
-
- }
|