作者 郭文星

'123'

... ... @@ -2,6 +2,7 @@
namespace app\api\controller\v1;
use app\admin\model\User;
use app\common\controller\Api;
use think\Db;
use think\Exception;
... ... @@ -505,4 +506,13 @@ class Client extends Base
}
}
public function SendMessage(){
$wxxcxpush=new WxxcxPush();
$res=$wxxcxpush->Message('o5OQw6NqWE8Xc34S9kQIzYeFPNUM');
if ($res !== false) {
$this->success('请求成功',$res);
} else {
$this->error(__('No rows were inserted'));
}
}
}
... ...
<?php
/**
* Created by PhpStorm.
* User: Kevin
* Date: 2022/06/13 05:55
* Time: 05:55
*/
namespace app\api\controller\v1;
use app\admin\model\tytcm\Reservation;
use app\api\controller\Common;
use think\Db;
use think\exception\PDOException;
use think\Request;
/**
* 微信小程序订阅消息推送
* @package app\api\controller\v1
*/
class WxxcxPush extends Base
{
/**
* 1.提醒社区管理员处理新的入州申请,发送订阅消息
*/
public function sendDealApplyMessage($openid_arr, $apply_info)
{
if (empty($openid_arr)) {
return array_callback(false, "未找到需要推送的人员");
}
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
if (!empty($openid_arr)) {
$sum_count = count($openid_arr);//总共需要推送的人数
//通知
$page = 'pages/admin_list/detail?apply_id=' . $apply_info['id'];
$value1 = "入州报备审核";
if ($apply_info['is_transfer'] == 1) {
//判断是否是转办
$value1 = "入州报备审核【转办】";
} elseif ($apply_info['user_reapply'] == 1) {
//判断是否是用户更新申报
$value1 = "入州报备审核【用户更新】";
}
$temp = array(
'value1' => $value1,
'value2' => "待处理",
'value3' => $apply_info['u_name'],
'value4' => date("Y年m月d日 H:i", $apply_info['createtime']),
'value5' => "点击立即处理",
);
// 启动事务
Db::startTrans();
try {
$createtime = $apply_info['createtime'];
foreach ($openid_arr as $uk => $uv) {
$user = [];
$insert_message_log_data = [];
$user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
$insert_message_log_data = [
"unionid" => $user['unionid'],
"wechat_xcx_openid" => $uv,
"corp_user_id" => 0,//发送人
"createtime" => $createtime,
];
if (!empty($uv)) {
$res = beingPushed(5, $uv, $page, $temp);
// file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
$res_arr = !empty($res) ? json_decode($res, true) : [];
if (!empty($res_arr) && $res_arr['errcode'] == "0") {
//推送成功
$react_count = $react_count + 1;
} else {
//推送失败,记录问题
$insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
}
} else {
$error = [];
$error = ["errcode" => "-1", "errmsg" => "社区管理员未授权小程序"];
$insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
}
//通知已读未读、推送日志记录起来
// Db::name("push_log")->insertGetId($insert_message_log_data);
}
// 执行提交操作
Db::commit();
return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);
} catch (PDOException $e) {
Db::rollback();
return array_callback(false, "请求异常" . $e->getMessage());
}
} else {
return array_callback(false, "未找到可通知的社区管理员!");
}
}
/**
* 2.提醒申请人员入州报备已处理完毕,发送订阅消息
*/
public function sendDealSuccessMessage($openid_arr, $deal_info)
{
if (empty($openid_arr)) {
return array_callback(false, "未找到需要推送的人员");
}
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
$apply_model = new \app\admin\model\AdmissionStateApply;
$type_stu = Db::name("admission_state_apply")->where("id", $deal_info['id'])->value("type_stu");
$type_stu_text_arr = $apply_model->getIncludeOldStatuTypeStuList();//处理结果['0'=> '正常','1'=> '健康监测','2'=> '已解除隔离','3'=> '高风险7天集中隔离5次核酸','4'=> '中风险7天居家3次核酸','5'=> '低风险3天2检']
$type_stu_text = $type_stu_text_arr[$type_stu];
$tips_text = $type_stu_text;//"结果:" .
if (!empty($openid_arr)) {
$sum_count = count($openid_arr);//总共需要推送的人数
//通知
$page = 'pages/declare/detail?apply_id=' . $deal_info['id'];
$temp = array(
'value1' => "已审核(" . date("m-d H:i", $deal_info['deal_time']) . ")",
'value2' => $deal_info['mng_user_name'],
'value3' => sub_str($tips_text, 20, false)//date("Y-m-d H:i",$deal_info['deal_time'])
);
// 启动事务
Db::startTrans();
try {
$createtime = $deal_info['deal_time'];
foreach ($openid_arr as $uk => $uv) {
$user = [];
$insert_message_log_data = [];
$user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
$insert_message_log_data = [
"unionid" => $user['unionid'],
"wechat_xcx_openid" => $uv,
"corp_user_id" => $deal_info['deal_mng_user_id'],//发送人
"createtime" => $createtime,
];
if (!empty($uv)) {
$res = beingPushed(2, $uv, $page, $temp);
// file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "给用户推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
$res_arr = !empty($res) ? json_decode($res, true) : [];
if (!empty($res_arr) && $res_arr['errcode'] == "0") {
//推送成功
$react_count = $react_count + 1;
} else {
//推送失败,记录问题
$insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
}
} else {
$error = [];
$error = ["errcode" => "-1", "errmsg" => "申报人未授权小程序"];
$insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
}
//通知已读未读、推送日志记录起来
// Db::name("push_log")->insertGetId($insert_message_log_data);
}
// 执行提交操作
Db::commit();
return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);
} catch (PDOException $e) {
Db::rollback();
return array_callback(false, "请求异常" . $e->getMessage());
}
} else {
return array_callback(false, "未找到可通知的申报用户!");
}
}
/**
* 3.提醒监测人员上传核酸报告,发送订阅消息
*/
public function sendUploadReportMessage($openid_arr, $deal_info,$report)
{
if (empty($openid_arr)) {
return array_callback(false, "未找到需要推送的人员");
}
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
$apply_model = new \app\admin\model\AdmissionStateApply;
$type_stu = Db::name("admission_state_apply")->where("id", $deal_info['id'])->value("type_stu");
$type_stu_text_arr = $apply_model->getIncludeOldStatuTypeStuList();//处理结果['0'=> '正常','1'=> '健康监测','2'=> '已解除隔离','3'=> '高风险7天集中隔离5次核酸','4'=> '中风险7天居家3次核酸','5'=> '低风险3天2检']
$type_stu_text = $type_stu_text_arr[$type_stu];
$tips_text = $type_stu_text;
if (!empty($openid_arr)) {
$sum_count = count($openid_arr);//总共需要推送的人数
//通知
$page = 'pages/declare/detail?apply_id=' . $deal_info['id'];
$temp = array(
'value1' => "核酸报告上传提醒(".$report['report_num']."/".$report['need_report_num'].")",
'value2' => $deal_info['mng_user_name'],
'value3' => sub_str($tips_text, 20, false)//date("Y-m-d H:i",$deal_info['deal_time'])
);
// 启动事务
Db::startTrans();
try {
foreach ($openid_arr as $uk => $uv) {
$user = [];
$insert_message_log_data = [];
$user = Db::name("user")->where("wx_xcx_openid", $uv)->order("id desc")->find();
$insert_message_log_data = [
"unionid" => $user['unionid'],
"wechat_xcx_openid" => $uv,
"corp_user_id" => $deal_info['deal_mng_user_id'],//发送人
"createtime" => time(),
];
if (!empty($uv)) {
$res = beingPushed(2, $uv, $page, $temp);
// file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . $uv. "给用户推送》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
$res_arr = !empty($res) ? json_decode($res, true) : [];
if (!empty($res_arr) && $res_arr['errcode'] == "0") {
//推送成功
$react_count = $react_count + 1;
} else {
//推送失败,记录问题
$insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
}
} else {
$error = [];
$error = ["errcode" => "-1", "errmsg" => "申报人未授权小程序"];
$insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
}
//通知已读未读、推送日志记录起来
// Db::name("push_log")->insertGetId($insert_message_log_data);
}
// 执行提交操作
Db::commit();
return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);
} catch (PDOException $e) {
Db::rollback();
return array_callback(false, "请求异常" . $e->getMessage());
}
} else {
return array_callback(false, "未找到可通知的监测人员!");
}
}
/**
* 1.问政小程序发送工单订阅消息
* $id 工单id
*/
public function sendWorkorderMessage($id,$temp)
{
if (empty($id)) {
return array_callback(false, "id不能为空");
}
$workorderInfo = Db::name("workorder_orders")->where("id", $id)->find();
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
if (!empty($workorderInfo)) {
if (!empty($workorderInfo['user_id'])) {
//找到用户
$where["id"] = ["=", $workorderInfo['user_id']];
$user = Db::name("user")->where($where)->order("id desc")->find();
if (!empty($user) && !empty($user['wx_xcx_openid'])) {
$sum_count = 1;//总共需要推送的人数
//通知
$page = 'pages/personal/speed/pading'."?id=".$id;
// 启动事务
Db::startTrans();
try {
$createtime = time();
$insert_message_log_data = [
"unionid" => $user['unionid'],
"wechat_xcx_openid" => $user['wx_xcx_openid'],
"message_id" => $id,
"status" => "0",//状态:0=未读,1=已读
"createtime" => $createtime,
"type"=>"2",//类型 1通知 2网络问政工单进度
];
if (!empty($user) && !empty($user['wx_xcx_openid'])) {
$res = beingPushed(1, $user['wx_xcx_openid'], $page, $temp);
file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . "《" . $id . "》" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
$res_arr = !empty($res) ? json_decode($res, true) : [];
if (!empty($res_arr) && $res_arr['errcode'] == "0") {
//推送成功
$react_count = $react_count + 1;
} else {
//推送失败,记录问题
$insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
}
} else {
$error = [];
$error = ["errcode" => "-1", "errmsg" => "提工单用户未授权小程序"];
$insert_message_log_data['error_tips'] = json_encode($error, JSON_UNESCAPED_UNICODE);
}
//推送日志记录起来
Db::name("workorder_push_log")->insertGetId($insert_message_log_data);
// 执行提交操作
Db::commit();
return array_callback(true, "推送成功", ["sum_count" => $sum_count, "react_count" => $react_count]);
} catch (PDOException $e) {
Db::rollback();
return array_callback(false, "请求异常" . $e->getMessage());
}
} else {
return array_callback(false, "未找到可通知的成员!");
}
} else {
return array_callback(false, "未找到推送人员");
}
} else {
return array_callback(false, "未查找到有效的工单数据");
}
}
public function sendMessage($data = [])
{
$res=[
'appid'=>$this->AppID,
'appsecret'=>$this->AppSecret,
];
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $res['appid'] . '&secret=' . $res['appsecret'];
//json_encode 对变量进行 JSON 编码 file_get_contents() 把整个文件读入一个字符串中。
$res = json_decode(file_get_contents($url),true);
$access_token = $res['access_token'] ;
//请求url
$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;
return self::curlPost($url,json_encode($data)) ;
}
//发送post请求 小程序发送一次性订阅消息
static function curlPost($url,$data)
{
$ch = curl_init();
$params[CURLOPT_URL] = $url; //请求url地址
$params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $data;
curl_setopt_array($ch, $params); //传入curl参数
$content = curl_exec($ch); //执行
curl_close($ch); //关闭连接
return $content;
}
//小程序发送一次性订阅消息
public function Message($openid){
if(!empty($openid)){
// 启动事务
$createtime = time();
//通知
$page = '/pageOne/moreRes/moreRes';
$temp='WO4COOXP_-_mWCO8ck6FABISehjMiPrJrtLNOVHImVY';
$insert_message_log_data = array(
'value1' => "消费卷名称",
'value2' => "待使用",
'value3' => date("Y-m-d H:i:s",time()),
'value4' => "123123412324"
);
$res = beingPushed(5, $openid, $page, $insert_message_log_data);
print_r($res);
return 213123123;
file_put_contents("kevin_wx_xcx_push.log", date("Y-m-d H:i:s") . "" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
$res_arr = !empty($res) ? json_decode($res, true) : [];
if (!empty($res_arr) && $res_arr['errcode'] == "0") {
} else {
//推送失败,记录问题
return array_callback(true, "推1送成功".json_encode($res_arr));
$insert_message_log_data['error_tips'] = json_encode($res_arr, JSON_UNESCAPED_UNICODE);
}
}
}
}
... ...
... ... @@ -5,13 +5,16 @@
use Symfony\Component\VarExporter\VarExporter;
use think\exception\HttpResponseException;
use think\Response;
use think\Db;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
if (!function_exists('__')) {
/**
* 获取语言变量值
* @param string $name 语言变量名
* @param array $vars 动态变量值
* @param array $vars 动态变量值
* @param string $lang 语言
* @return mixed
*/
... ... @@ -33,9 +36,9 @@ if (!function_exists('format_bytes')) {
/**
* 将字节转换为可读文本
* @param int $size 大小
* @param int $size 大小
* @param string $delimiter 分隔符
* @param int $precision 小数位数
* @param int $precision 小数位数
* @return string
*/
function format_bytes($size, $delimiter = '', $precision = 2)
... ... @@ -52,7 +55,7 @@ if (!function_exists('datetime')) {
/**
* 将时间戳转换为日期时间
* @param int $time 时间戳
* @param int $time 时间戳
* @param string $format 日期时间格式
* @return string
*/
... ... @@ -67,7 +70,7 @@ if (!function_exists('human_date')) {
/**
* 获取语义化时间
* @param int $time 时间
* @param int $time 时间
* @param int $local 本地时间
* @return string
*/
... ... @@ -81,7 +84,7 @@ if (!function_exists('cdnurl')) {
/**
* 获取上传资源的CDN的地址
* @param string $url 资源相对地址
* @param string $url 资源相对地址
* @param boolean $domain 是否显示域名 或者直接传入域名
* @return string
*/
... ... @@ -132,8 +135,8 @@ if (!function_exists('rmdirs')) {
/**
* 删除文件夹
* @param string $dirname 目录
* @param bool $withself 是否删除自身
* @param string $dirname 目录
* @param bool $withself 是否删除自身
* @return boolean
*/
function rmdirs($dirname, $withself = true)
... ... @@ -162,7 +165,7 @@ if (!function_exists('copydirs')) {
/**
* 复制文件夹
* @param string $source 源文件夹
* @param string $dest 目标文件夹
* @param string $dest 目标文件夹
*/
function copydirs($source, $dest)
{
... ... @@ -198,7 +201,7 @@ if (!function_exists('addtion')) {
/**
* 附加关联字段数据
* @param array $items 数据列表
* @param array $items 数据列表
* @param mixed $fields 渲染的来源字段
* @return array
*/
... ... @@ -271,7 +274,7 @@ if (!function_exists('var_export_short')) {
/**
* 使用短标签打印或返回数组结构
* @param mixed $data
* @param mixed $data
* @param boolean $return 是否返回数据
* @return string
*/
... ... @@ -487,7 +490,7 @@ if (!function_exists('build_suffix_image')) {
/**
* 生成文件后缀图片
* @param string $suffix 后缀
* @param null $background
* @param null $background
* @return string
*/
function build_suffix_image($suffix, $background = null)
... ... @@ -512,3 +515,1752 @@ EOT;
return $icon;
}
}
if (!function_exists('array_callback')) {
/**
* 20190729 kevin
* 规范数据返回函数
* @param unknown $state
* @param unknown $msg
* @param unknown $data
* @return multitype:unknown
*/
function array_callback($state = true, $msg = '', $data = array())
{
return array('state' => $state, 'msg' => $msg, 'data' => $data);
}
}
if (!function_exists('send_post')) {
//请求函数
function send_post($url, $post_data = [], $method = 'POST')
{
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $method, //or GET
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
}
if (!function_exists('corp_admin_id')) {
/**
* 获取企业所属admin_id
*/
function corp_admin_id($corp_id = null)
{
$admin_id = 1;
if (!empty($corp_id)) {
$admin_id = \think\Db::name("corp")->where("id", $corp_id)->value("admin_id");
}
$admin_id = $admin_id > 0 ? $admin_id : 1;
return $admin_id;
}
}
if (!function_exists('group_id_exchange_admin_id')) {
/**
* 根据角色组获取某个管理员id
*/
function group_id_exchange_admin_id($group_id = null)
{
$admin_id = 1;
if (!empty($group_id)) {
$admin_id = \think\Db::name("auth_group_access")->where("group_id", $group_id)->value("uid");
}
$admin_id = $admin_id > 0 ? $admin_id : 1;
return $admin_id;
}
}
if (!function_exists('get_groupinfo_by_admin_id')) {
/**
* 根据admin_id获取角色组信息
*/
function get_groupinfo_by_admin_id($admin_id = null)
{
$group_info = [
"id" => null,
"name" => ""
];
if (!empty($admin_id)) {
$group_info = \think\Db::name("auth_group_access")->alias("a")
->join("auth_group g", "g.id=a.group_id")
->field("g.id,g.name")
->where("a.uid", $admin_id)->find();
}
return $group_info;
}
}
if (!function_exists('verify_corp_admin_id')) {
/**
* 验证是否绑定过角色组(常规管理员)
*/
function verify_corp_admin_id($corp_id = null)
{
$check = false;
if (!empty($corp_id)) {
$admin_id = \think\Db::name("corp")->where("id", $corp_id)->value("admin_id");
if ($admin_id > 1) {
//绑定过其他管理员
$exsit = \think\Db::name("admin")->where("id", $admin_id)->find();
if ($exsit) {
$check = true;
}
}
}
return $check;
}
}
if (!function_exists('insert_openid_info')) {
/**
* 将授权用户信息保存起来
*/
function insert_openid_info($data)
{
$data['upt_time'] = time();
$res = \think\Db::name("openid_info")->where("openid", $data['openid'])->find();
// file_put_contents("ccc.txt", date("Y-m-d H:i:s") . json_encode($data) . PHP_EOL, FILE_APPEND);
if (!empty($res)) {
\think\Db::name("openid_info")->where("openid", $data['openid'])->update($data);
} else {
\think\Db::name("openid_info")->insertGetId($data);
}
return true;
}
}
if (!function_exists('http_request')) {
// curl请求
function http_request($url, $timeout = 30, $header = array())
{
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list($header, $data) = explode("\r\n\r\n", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
}
//加密解密函数
if (!function_exists('tyssl_decode')) {
/**
* 解密函数
* @param string $secretdata
* @return string
*/
function tyssl_decode($secretdata)
{
$cryptkey = hash('sha256', 'e09dor37csce2', true);
$password = "h78c4ea56z0sgw9e";
return openssl_decrypt($secretdata, 'aes-256-cbc', $cryptkey, false, $password);
}
}
if (!function_exists('tyssl_encode')) {
/**
* 加密函数
* @param string $secretdata
* @return string
*/
function tyssl_encode($secretdata)
{
$cryptkey = hash('sha256', 'e09dor37csce2', true);
$password = "h78c4ea56z0sgw9e";
return openssl_encrypt($secretdata, 'aes-256-cbc', $cryptkey, false, $password);
}
}
if (!function_exists('grid_level_pid_zero')) {
/**
* 无限级分类 获取顶级分类ID
*/
function grid_level_pid_zero($id)
{
$arr = \think\Db::name("grid_level")->order("pid asc,id asc")->column("id,pid");
while ($arr[$id]) {
$id = $arr[$id];
}
return $id;
}
}
if (!function_exists('uuid')) {
function uuid($num = 32)
{
if ($num == 16) {
return strtoupper(substr(md5(uniqid(mt_rand(), 1)), 10, 16));
}
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
mt_srand(( double )microtime() * 10000); //optional for php 4.2.0 and up.随便数播种,4.2.0以后不需要了。
$charid = strtoupper(md5(uniqid(rand(), true))); //根据当前时间(微秒计)生成唯一id.
$uuid = substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
return $uuid;
}
}
}
if (!function_exists('suiji_num')) {
/**
* 生成一个16位随机数(可以自定义前缀)
*/
function suiji_num($before)
{
return $before . substr(uuid(16), 0, 16 - strlen($before));
}
}
if (!function_exists('grid_level_has_child')) {
/**
* 查看当前网格层级是否还有子层级
*/
function grid_level_has_child($grid_leve_id)
{
$count = \think\Db::name("grid_level")->where("pid", $grid_leve_id)->count();
if ($count) {
return true;
}
return false;
}
}
if (!function_exists('grid_level_bottom_id')) {
/**
* 查看城市/农村的最底层分类id
*/
function grid_level_bottom_id($id)
{
$sid = \think\Db::name("grid_level")->where("pid", $id)->order("weigh desc,id desc")->value("id");
if (!empty($sid)) {
$id = grid_level_bottom_id($sid);
}
return $id;
}
}
if (!function_exists('grid_level_bottom_id_list')) {
/**
* 查看城市和农村的最底层id返回数组
*/
function grid_level_bottom_id_list()
{
$arr = [];
$c_id = grid_level_bottom_id(1);//1城市
$n_id = grid_level_bottom_id(2);//2农村
if ($c_id) {
$arr[] = $c_id;
}
if ($n_id) {
$arr[] = $n_id;
}
return $arr;
}
}
if (!function_exists('grid_level_last_two_id_list')) {
/**
* 查看城市和农村的最底层id返回数组
*/
function grid_level_last_two_id_list()
{
$arr = [];
$c_id = grid_level_bottom_id(1);//1城市
$n_id = grid_level_bottom_id(2);//2农村
if ($c_id) {
$arr[] = $c_id;
}
if ($n_id) {
$arr[] = $n_id;
}
$arr2 = [];
if (!empty($arr)) {
foreach ($arr as $k => $v) {
$arr2[] = $v;
$pid = \think\Db::name("grid_level")->where("id", $v)->value("pid");
if ($pid) {
$arr2[] = $pid;
}
}
}
return $arr2;
}
}
if (!function_exists('get_groupchat_ids_by_grid_infomation_id')) {
/**
* 查询某个网格及其子网格的客户群ids的一维数组
*/
function get_groupchat_ids_by_grid_infomation_id($grid_infomation_id)
{
$data = [];
$where = [];
if (!empty($grid_infomation_id)) {
if ($grid_infomation_id == -1) {
//返回全部客户群
} else {
//获取下级所有子网格ID
$tree = \fast\Tree::instance();
$gridInfomationModel = new \app\admin\model\GridInfomation();
$tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
$grid_ids = $tree->getChildrenIds($grid_infomation_id, true);
if (!empty($grid_ids)) {
//查询网格点对应客户群ids
if (!empty($grid_ids)) {
$where["id"] = ["IN", $grid_ids];
}
}
}
$gird_infomation_list = \think\Db::name("grid_infomation")->where($where)->field("corp_groupchat_ids,corp_user_ids")->select();
if (!empty($gird_infomation_list)) {
foreach ($gird_infomation_list as $k => $v) {
if (!empty($v['corp_groupchat_ids'])) {
$corp_groupchat_ids_arr = explode(",", $v['corp_groupchat_ids']);
$data = array_merge($data, $corp_groupchat_ids_arr);
}
}
}
}
if (!empty($data)) {
//过滤掉相同客户群id
$data = array_unique($data);
}
return $data;
}
}
if (!function_exists('get_all_next_grid_infomation_ids')) {
/***
* 获得下级所有网格点id
*/
function get_all_next_grid_infomation_ids($grid_infomation_id)
{
$grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
if (!empty($grid_infomation)) {
//获取下级所有子网格ID
$tree = \fast\Tree::instance();
$gridInfomationModel = new \app\admin\model\GridInfomation();
$tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
//$grid_ids包含网格所有等级的网格ids
$grid_ids = $tree->getChildrenIds($grid_infomation_id, true);
//获得网格点ids ,house_building_ids绑定了楼栋的
$where['id'] = ['in', $grid_ids];
$where['house_building_ids'] = ['>', '0'];
$list = \think\Db::name("grid_infomation")
->where($where)
->column('id');
return $list;
}
}
}
if (!function_exists('grid_infomation_relation')) {
/**
* 获取1.本网格、2.同级网格、3.本网格以及下级所有网格、4.同级网格以及下级所有网格 (多个用,隔开)
*/
function grid_infomation_relation($grid_infomation_id, $type = 1)
{
// 新版本根据网格点找人(user_grid) 2022.05.27 kevin更新
$ids = "";
$where = [];
if ($type == 1) {
//本网格
$where["a.id"] = ["=", $grid_infomation_id];
// $where["a.corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
$id_arr = \think\Db::name("grid_infomation")
->alias("a")
->join("user_grid b", "a.id=b.grid_id")
->where($where)
->group("a.id")
->column("a.id");
if (!empty($id_arr)) {
$ids = implode($id_arr, ",");
}
} elseif ($type == 2) {
//同级网格
$grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
if (!empty($grid_infomation)) {
$where["a.pid"] = ["=", $grid_infomation['pid']];
$where["a.corp_id"] = ["=", $grid_infomation['corp_id']];
$where["a.grid_level_id"] = ["=", $grid_infomation['grid_level_id']];
// $where["a.corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
$id_arr = \think\Db::name("grid_infomation")
->alias("a")
->join("user_grid b", "a.id=b.grid_id")
->where($where)
->group("a.id")
->column("a.id");
if (!empty($id_arr)) {
$ids = implode($id_arr, ",");
}
}
} elseif ($type == 3) {
//本网格以及下级所有网格
$grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
if (!empty($grid_infomation)) {
//获取下级所有子网格ID
$tree = \fast\Tree::instance();
$gridInfomationModel = new \app\admin\model\GridInfomation();
$tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
$grid_ids = $tree->getChildrenIds($grid_infomation_id, true);
if (!empty($grid_ids)) {
$where["a.id"] = ["in", $grid_ids];
// $where["a.corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
$id_arr = \think\Db::name("grid_infomation")
->alias("a")
->join("user_grid b", "a.id=b.grid_id")
->where($where)
->group("a.id")
->column("a.id");
if (!empty($id_arr)) {
$ids = implode($id_arr, ",");
}
}
}
} elseif ($type == 4) {
//同级网格以及下级所有网格
$grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
if (!empty($grid_infomation)) {
//第一步:先找到同级网格id
$where["pid"] = ["=", $grid_infomation['pid']];
$where["corp_id"] = ["=", $grid_infomation['corp_id']];
$where["grid_level_id"] = ["=", $grid_infomation['grid_level_id']];
$id_arr = \think\Db::name("grid_infomation")->where($where)->column("id");
if (!empty($id_arr)) {
$tree = \fast\Tree::instance();
$gridInfomationModel = new \app\admin\model\GridInfomation();
$tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
$grid_ids_arr = [];
foreach ($id_arr as $k => $v) {
$grid_ids = $tree->getChildrenIds($v, true);
$grid_ids_arr = array_merge($grid_ids_arr, $grid_ids);
$grid_ids_arr = array_unique($grid_ids_arr);
}
if (!empty($grid_ids_arr)) {
$where2 = [];
$where2["a.id"] = ["in", $grid_ids_arr];
// $where2["corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
$id_arr = \think\Db::name("grid_infomation")
->alias("a")
->join("user_grid b", "a.id=b.grid_id")
->where($where2)
->group("a.id")
->column("a.id");
if (!empty($id_arr)) {
$ids = implode($id_arr, ",");
}
}
}
}
}
return $ids;
// // 老版本根据客户群找人
// $ids = "";
// $where = [];
// if ($type == 1) {
// //本网格
// $where["id"] = ["=", $grid_infomation_id];
// $where["corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
// $id_arr = \think\Db::name("grid_infomation")->where($where)->column("id");
// if (!empty($id_arr)) {
// $ids = implode($id_arr, ",");
// }
// } elseif ($type == 2) {
// //同级网格
// $grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
// if (!empty($grid_infomation)) {
// $where["pid"] = ["=", $grid_infomation['pid']];
// $where["corp_id"] = ["=", $grid_infomation['corp_id']];
// $where["grid_level_id"] = ["=", $grid_infomation['grid_level_id']];
// $where["corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
// $id_arr = \think\Db::name("grid_infomation")->where($where)->column("id");
// if (!empty($id_arr)) {
// $ids = implode($id_arr, ",");
// }
// }
// } elseif ($type == 3) {
// //本网格以及下级所有网格
// $grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
// if (!empty($grid_infomation)) {
// //获取下级所有子网格ID
// $tree = \fast\Tree::instance();
// $gridInfomationModel = new \app\admin\model\GridInfomation();
// $tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
// $grid_ids = $tree->getChildrenIds($grid_infomation_id, true);
// if (!empty($grid_ids)) {
// $where["id"] = ["in", $grid_ids];
// $where["corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
// $id_arr = \think\Db::name("grid_infomation")->where($where)->column("id");
// if (!empty($id_arr)) {
// $ids = implode($id_arr, ",");
// }
// }
// }
// } elseif ($type == 4) {
// //同级网格以及下级所有网格
// $grid_infomation = \think\Db::name("grid_infomation")->where("id", $grid_infomation_id)->find();
// if (!empty($grid_infomation)) {
// //第一步:先找到同级网格id
// $where["pid"] = ["=", $grid_infomation['pid']];
// $where["corp_id"] = ["=", $grid_infomation['corp_id']];
// $where["grid_level_id"] = ["=", $grid_infomation['grid_level_id']];
// $id_arr = \think\Db::name("grid_infomation")->where($where)->column("id");
// if (!empty($id_arr)) {
// $tree = \fast\Tree::instance();
// $gridInfomationModel = new \app\admin\model\GridInfomation();
// $tree->init(collection($gridInfomationModel->order('pid asc,id asc')->select())->toArray(), 'pid');
// $grid_ids_arr = [];
// foreach ($id_arr as $k => $v) {
// $grid_ids = $tree->getChildrenIds($v, true);
// $grid_ids_arr = array_merge($grid_ids_arr, $grid_ids);
// $grid_ids_arr = array_unique($grid_ids_arr);
// }
// if (!empty($grid_ids_arr)) {
// $where2 = [];
// $where2["id"] = ["in", $grid_ids_arr];
// $where2["corp_groupchat_ids"] = ["neq", ""];//客户群不能为空
// $id_arr = \think\Db::name("grid_infomation")->where($where2)->column("id");
// if (!empty($id_arr)) {
// $ids = implode($id_arr, ",");
// }
// }
// }
// }
// }
// return $ids;
}
}
if (!function_exists('user_day_count')) {
/***
* 根据网格id更新当日网格中群众总数
* type =1 添加人员 type=2 删除人员
*/
function user_day_count($grid_id, $type = 1)
{
$nowday = date('Ymd', time());
//网格点信息
$info = \think\Db::name('grid_infomation')
->where(['id' => $grid_id])
->find();
//判断今日数据是否存在
$kcs = \think\Db::name('user_user_ctall')
->where(['nowday' => $nowday, 'grid_id' => $grid_id])
->find();
if ($type == 1) {
//添加人员
if (empty($kcs)) {
//保存数据
$gduall['admin_id'] = corp_admin_id($info['corp_id']);
$gduall['grid_id'] = $grid_id;
$gduall['nowday'] = $nowday;
$gduall['nums'] = 1;
$gduall['createtime'] = time();
$rs = \think\Db::name('user_user_ctall')->insertGetId($gduall);
} else {
//更新总数
$gdupd['nums'] = $kcs['nums'] + 1;
$gdupd['updatetime'] = time();
$rs = \think\Db::name('user_user_ctall')
->where(['grid_id' => $grid_id, 'nowday' => $nowday])
->update($gdupd);
}
}
if ($type == 2 && !empty($kcs)) {
//删除人员更新数据
//更新总数
$userAll = $kcs['nums'] - 1;
$gdupd['nums'] = $userAll > 0 ? $userAll : 0;
$gdupd['updatetime'] = time();
$rs = \think\Db::name('user_user_ctall')
->where(['grid_id' => $grid_id, 'nowday' => $nowday])
->update($gdupd);
}
}
}
if (!function_exists('sub_str')) {
/**
* 字符串截取
*
*/
function sub_str($str, $length = 0, $append = true)
{
$str = trim($str);
$strlength = strlen($str);
if ($length == 0 || $length >= $strlength) {
return $str; //截取长度等于0或大于等于本字符串的长度,返回字符串本身
} elseif ($length < 0) //如果截取长度为负数
{
$length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度
if ($length < 0) {
$length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度
}
}
if (function_exists('mb_substr')) {
$newstr = mb_substr($str, 0, $length, "utf-8");
} elseif (function_exists('iconv_substr')) {
$newstr = iconv_substr($str, 0, $length, "utf-8");
} else {
//$newstr = trim_right(substr($str, 0, $length));
$newstr = substr($str, 0, $length);
}
if ($append && $str != $newstr) {
$newstr .= '...';
}
return $newstr;
}
}
if (!function_exists('beingPushed')) {
/**
* 小程序模板推送选择类型
* @param $type @模板类型
* @param $openid @接收人ID
* @param $page @跳转页面
* @param $temp @具体提醒参数
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
function beingPushed($type, $openid, $page, $temp)
{
$data = [];
$data['touser'] = $openid;
//$data['page'] = $page;
//模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
switch ($type) {
case 1:
//待审批事项提醒(新版本的长期订阅)
$data['template_id'] = "ws6bz6-WyHHA-upU2oRzc_C5toDUexe9ZicQ-ITtImA";
$data['data'] = [
"thing10" => [
'value' =>1,//审核状态(待处理)
],
"thing12" => [
'value' => 2,//申请人(测试黄超)
],
"thing8" => [
'value' => 3,//原因(入州报备审核)
]
];
break;
case 2:
//审核结果通知(新版本的长期订阅)
$data['template_id'] = "Qt2xqfH8uQyTm1A0ReNxeybtAydAhfhi4HinnyB5f0s";
$data['data'] = [
"thing1" => [
'value' => $temp['value1'],//提醒内容(已审核)
],
// "name1" => [
// 'value' => $temp['value2'],//审核人
// ],
"thing4" => [
'value' => $temp['value3'],//提示说明
]
];
break;
case 3:
//待审批事项提醒(老版本的一次性订阅)
$data['template_id'] = "vVOonN76AFjxgz77iG-hcWrH6HJ-vPGKyIyAK0VzPIk";
$data['data'] = [
"thing1" => [
'value' => $temp['value1'],//待办事项
],
"thing4" => [
'value' => $temp['value2'],//流程状态
],
"thing2" => [
'value' => $temp['value3'],//申请人
],
"time3" => [
'value' => $temp['value4'],//申请时间
],
"thing5" => [
'value' => $temp['value5'],//备注描述
]
];
break;
case 4:
//审核结果通知(老版本的一次性订阅)
$data['template_id'] = "yJRK0DZFuEPqaWwyS0DegT0r2p5be0bm2eE59oroGME";
$data['data'] = [
"phrase2" => [
'value' => $temp['value1'],//审核结果
],
"name1" => [
'value' => $temp['value2'],//审核人
],
"date4" => [
'value' => $temp['value3'],//审批时间
]
];
break;
case 5:
//预约结果通知
$data['template_id'] = "WO4COOXP_-_mWCO8ck6FABISehjMiPrJrtLNOVHImVY";
$data['data'] = [
"thing1" => [
'value' => $temp['value1'],//消费代金券
],
"phrase4" => [
'value' =>$temp['value2'],//券状态
],
"time5" => [
'value' => $temp['value3'],//有效期
],
"character_string7" => [
'value' => $temp['value4'],//优惠券号
]
];
break;
}
return sendSubscribeMessage($data);
}
}
if (!function_exists('sendSubscribeMessage')) {
/**
* 小程序订阅消息推送
* @param $data
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
function sendSubscribeMessage($data)
{
//access_token
$access_token = getAccessToken();
//请求url
$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
//file_put_contents('notify.txt',$data.PHP_EOL,FILE_APPEND);
//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
$data['miniprogram_state'] = 'formal';
return curlPost($url, json_encode($data));
}
}
if (!function_exists('getAccessToken')) {
/**
* 获取access_token
* @return mixed
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
function getAccessToken()
{
$wx_xcx_token = \think\Db::name("wechat_token")->where("type", "wx_xcx")->find();
if (empty($wx_xcx_token) || empty($wx_xcx_token['access_token']) || $wx_xcx_token['over_time'] <= time()) {
//微信小程序失效
//当前时间戳
$now_time = strtotime(date('Y-m-d H:i:s', time()));
//获取新的access_token
$appid = "wx1c3cc5b4e2006be2";
$secret = "643f1b6fdba08a773a7761a1a194a1ad";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$res = json_decode(file_get_contents($url), true);
print_r($res);return $res;
$access_token = $res['access_token'];
if (!empty($access_token)) {
$time = time();
$update_data = [
"access_token" => $access_token,
"over_time" => $time + 7200,
"type" => "wx_xcx",
"updatetime" => $time
];
if (!empty($wx_xcx_token)) {
//更新
\think\Db::name("wechat_token")->where("id", $wx_xcx_token['id'])->update($update_data);
} else {
//新增
\think\Db::name("wechat_token")->insertGetId($update_data);
}
}
} else {
$access_token = $wx_xcx_token['access_token'];
}
return $access_token;
}
}
if (!function_exists('curlPost')) {
/**
* 发送post请求
*/
function curlPost($url, $data)
{
$ch = curl_init();
$params[CURLOPT_URL] = $url; //请求url地址
$params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $data;
curl_setopt_array($ch, $params); //传入curl参数
$content = curl_exec($ch); //执行
// file_put_contents('notify.txt', $content . date('Y-m-d H:i:s', time()) . PHP_EOL, FILE_APPEND);
curl_close($ch); //关闭连接
return $content;
}
}
if (!function_exists('get_groupchat_member_list_by_groupchat_ids')) {
/**
* 根据客户群id找到外部联系人群成员unionid一维数组
*/
function get_groupchat_member_list_by_groupchat_ids($ids)
{
$return_data = [];
if (!empty($ids)) {
$where = [];
$where["g.id"] = ["in", $ids];
$where["m.type"] = ["=", "2"];//成员类型 1企业成员 2外部联系人
$where['m.unionid'] = ["neq", ""];//unionid不能为空
$return_data = \think\Db::name("corp_groupchat")->alias("g")
->join("corp_groupchat_member m", "m.corp_groupchat_id=g.id")
->where($where)
->group("m.unionid")
->column("unionid");
}
return $return_data;
}
}
if (!function_exists('has_bind_grid_infomation_user_ids')) {
/**
* 查询哪些人员已经绑定过网格点了
*/
function has_bind_grid_infomation_user_ids()
{
$return_data = [];
//网格管理员
$where = [];
$where["corp_user_mng_ids"] = ["neq", ""];
$user_mng_ids = \think\Db::name("grid_infomation")->where($where)->where("corp_user_mng_ids", 'not null')->group("corp_user_mng_ids")->column("corp_user_mng_ids");
if (!empty($user_mng_ids)) {
foreach ($user_mng_ids as $k => $v) {
//由于网格点可能有多个网格管理员,这里需要implode,然后去重后返回
if (!empty($v)) {
$id_arr = explode(",", $v);
$return_data = array_merge($return_data, $id_arr);
}
}
if (!empty($return_data)) {
$return_data = array_unique($return_data);
}
}
//网格长
$where2 = [];
$where2["corp_user_ids"] = ["neq", ""];
$user_ids = \think\Db::name("grid_infomation")->where($where2)->where("corp_user_ids", 'not null')->group("corp_user_ids")->column("corp_user_ids");
$return_data = array_merge($return_data, $user_ids);
$return_data = array_unique($return_data);
// $where = [];
// $where["corp_user_ids"] = ["neq", ""];
// $user_ids = \think\Db::name("grid_infomation")->where($where)->where("corp_user_ids", 'not null')->group("corp_user_ids")->column("corp_user_ids");
// if (!empty($user_ids)) {
// foreach ($user_ids as $k => $v) {
// //由于网格点可能有多个网格管理员,这里需要implode,然后去重后返回
// if (!empty($v)) {
// $id_arr = explode(",", $v);
// $return_data = array_merge($return_data, $id_arr);
// }
// }
// if (!empty($return_data)) {
// $return_data = array_unique($return_data);
// }
// }
return $return_data;
}
}
if (!function_exists('has_bind_grid_infomation_groupchat_ids')) {
/**
* 查询哪些客户群已经绑定过网格点了
*/
function has_bind_grid_infomation_groupchat_ids()
{
$return_data = [];
//楼栋
$where = [];
$where["corp_groupchat_ids"] = ["neq", ""];
$corp_groupchat_ids = \think\Db::name("grid_infomation")->where($where)->where("corp_groupchat_ids", 'not null')->group("corp_groupchat_ids")->column("corp_groupchat_ids");
if (!empty($corp_groupchat_ids)) {
foreach ($corp_groupchat_ids as $k => $v) {
//由于网格点可能有多个客户群,这里需要implode,然后去重后返回
if (!empty($v)) {
$id_arr = explode(",", $v);
$return_data = array_merge($return_data, $id_arr);
}
}
if (!empty($return_data)) {
$return_data = array_unique($return_data);
}
}
return $return_data;
}
}
if (!function_exists('has_bind_grid_infomation_building_ids')) {
/**
* 查询哪些楼栋已经绑定过网格点了
*/
function has_bind_grid_infomation_building_ids()
{
$return_data = [];
//楼栋
$where = [];
$where["house_building_ids"] = ["neq", ""];
$house_building_ids = \think\Db::name("grid_infomation")->where($where)->where("house_building_ids", 'not null')->group("house_building_ids")->column("house_building_ids");
if (!empty($house_building_ids)) {
foreach ($house_building_ids as $k => $v) {
//由于网格点可能有多个楼栋,这里需要implode,然后去重后返回
if (!empty($v)) {
$id_arr = explode(",", $v);
$return_data = array_merge($return_data, $id_arr);
}
}
if (!empty($return_data)) {
$return_data = array_unique($return_data);
}
}
return $return_data;
}
}
if (!function_exists('getLatelyTime')) {
/**
* 获取最近一周,一个月,一年
* */
function getLatelyTime($type = '')
{
$now = time();
$result = [];
if ($type == 'week5') {
//最近一周
for ($i = 0; $i < 5; $i++) {
$result[] = date('Y-m-d ', strtotime('-' . $i . ' day', $now));
}
} elseif ($type == 'week') {
//最近一周
for ($i = 0; $i < 7; $i++) {
$result[] = date('Ymd', strtotime('-' . $i . ' day', $now));
}
} elseif ($type == 'month') {
//最近一个月
for ($i = 0; $i < 30; $i++) {
$result[] = date('Y-m-d', strtotime('-' . $i . ' day', $now));
}
} elseif ($type == 'year') {
//最近一年
for ($i = 0; $i < 12; $i++) {
$result[] = date('Y-m', strtotime('-' . $i . ' month', $now));
}
}
return $result;
}
}
if (!function_exists('grid_name_relation_by_id')) {
/**
* 根据某个网格点id返回所有上级的id(不含本身)
*/
function grid_name_relation_by_id($id, $arr = [])
{
$pid = \think\Db::name("grid_infomation")->where("id", $id)->value("pid");
if ($pid > 0) {
$arr = array_merge($arr, [$pid]);
}
return $pid > 0 ? grid_name_relation_by_id($pid, $arr) : $arr;
}
}
if (!function_exists('grid_name_relation_names')) {
/**
* 根据某个网格点id返回所有上级的名称 用"$str"拼接(不含本身)
*/
function grid_name_relation_names($id, $str = "/")
{
$pid_arr = grid_name_relation_by_id($id);
$grid_name_str = "";
$pid_arr = array_reverse($pid_arr);//数组倒序
if (!empty($pid_arr)) {
$w1 = [];
$w1["id"] = ["IN", $pid_arr];
$grid_name_arr = \think\Db::name("grid_infomation")->where($w1)->order("grid_level_id asc")->column("name");
$grid_name_str = implode($grid_name_arr, $str);
}
return $grid_name_str;
}
}
if (!function_exists('building_relation_names')) {
/**
* 根据某个楼栋id返回所有上级的名称 用"$str"拼接(不含本身)
*/
function building_relation_names($id, $str = "/")
{
$grid_name_str = "";
$house_building = \think\Db::name("house_building")->where("id", $id)->find();
$house_community = \think\Db::name("house_community")->where("id", $house_building['house_community_id'])->find();
$grid_name_str = $house_community['community_name'] . $str . $house_building['building_name'];
return $grid_name_str;
}
}
if (!function_exists('create_unit_to_chinese')) {
/**
* 批量创建单元转换中文拼接
* $start_num 起始单元
* $end_num 结束单元
*/
function create_unit_to_chinese($start_num, $end_num)
{
$arr = [];
for ($i = $start_num; $i <= $end_num; $i++) {
$arr[] = $i . "单元";
}
return $arr;
}
}
if (!function_exists('create_floor_to_chinese')) {
/**
* 批量创建楼层转换中文拼接
* $start_num 起始楼层号
* $end_num 结束楼层号
*/
function create_floor_to_chinese($start_num, $end_num)
{
$arr = [];
for ($i = $start_num; $i <= $end_num; $i++) {
$arr[] = $i;
}
return $arr;
}
}
if (!function_exists('create_room_to_chinese')) {
/**
* 批量创建房号转换中文拼接
* $start_num 起始房号
* $end_num 结束房号
*/
function create_room_to_chinese($start_num, $end_num)
{
$arr = [];
for ($i = $start_num; $i <= $end_num; $i++) {
$arr[] = str_pad($i, 2, "0", STR_PAD_LEFT);//两位数 左边补0
}
return $arr;
}
}
if (!function_exists('show_online_message_by_grid_id_arr')) {
/**
* 根据可查看的网格点数组去找进行中的敲门通知ids 一维数组
* $grid_ids 网格id一维数组
*/
function show_online_message_by_grid_id_arr($grid_ids)
{
$arr = [];
if (!empty($grid_ids)) {
$now_date = date("Y-m-d");
foreach ($grid_ids as $k => $v) {
$wh = [];
$wh["type"] = ["=", 3];//只显示网上敲门的数据
$wh["status"] = ["=", "1"];
$list = \think\Db::name("message")
->where($wh)
->where('FIND_IN_SET(:id,grid_infomation_ids)', ['id' => $v])
->field("id,start_time,end_time")
->group("id")
->select();
if (!empty($list)) {
foreach ($list as $lk => $lv) {
if (!empty($lv['start_time'])) {
if (strtotime($lv['start_time']) > strtotime($now_date)) {
unset($list[$lk]);
continue;//跳出本层循环
}
}
if (!empty($lv['end_time'])) {
if (strtotime($lv['end_time']) < strtotime($now_date)) {
unset($list[$lk]);
continue;//跳出本层循环
}
}
}
$ids_arr = array_column($list, "id");
$arr = array_merge($arr, $ids_arr);
}
}
}
$arr = array_unique($arr);
return $arr;
}
}
if (!function_exists('show_outdate_online_message_by_grid_id_arr')) {
/**
* 根据可查看的网格点数组去找已过期的敲门通知ids 一维数组
* $grid_ids 网格id一维数组
*/
function show_outdate_online_message_by_grid_id_arr($grid_ids)
{
$arr = [];
if (!empty($grid_ids)) {
$now_date = date("Y-m-d");
foreach ($grid_ids as $k => $v) {
$wh = [];
$wh["type"] = ["=", 3];//只显示网上敲门的数据
$wh["status"] = ["=", "1"];
$list = \think\Db::name("message")
->where($wh)
->where('FIND_IN_SET(:id,grid_infomation_ids)', ['id' => $v])
->field("id,start_time,end_time")
->group("id")
->select();
if (!empty($list)) {
foreach ($list as $lk => $lv) {
if (!empty($lv['end_time'])) {
if (strtotime($lv['end_time']) < strtotime($now_date)) {
$arr = array_merge($arr, [$lv['id']]);
}
}
}
}
}
}
$arr = array_unique($arr);
return $arr;
}
}
if (!function_exists('judge_bind_wx_xcx')) {
/**
* 判断是否绑定了小程序
* true已绑定 false未绑定
*/
function judge_bind_wx_xcx($unionid)
{
$flag = 0;
if (!empty($unionid)) {
//若授权小程序过小程序那么wx_xcx_openid肯定是有值的
$wx_xcx_openid = Db::name("user")->where("unionid", $unionid)->value("wx_xcx_openid");
if (!empty($wx_xcx_openid)) {
$flag = 1;
}
}
return $flag;
}
}
if (!function_exists('judge_bind_wgz_relation')) {
/**
* 判断是否与网格的网格长加了好友的
* true已加好友 false未加好友
*/
function judge_bind_wgz_relation($unionid, $grid_id)
{
$flag = 0;
if (!empty($unionid) && !empty($grid_id)) {
$grid_infomation = Db::name("grid_infomation")->where("id", $grid_id)->find();
if (!empty($grid_infomation['corp_user_ids'])) {
//网格长信息
$corp_user = Db::name("corp_user")->where("id", $grid_infomation['corp_user_ids'])->field("corp_id,userid")->find();
//找到外部联系人的userid
$userid = Db::name("corp_groupchat_member")->where("unionid", $unionid)->value("userid");
if (!empty($userid)) {
//先查询外部联系人详细信息【follow_user中包含已加的内部成员列表】
$externalcontact_Api = new \app\api\controller\wework\Externalcontact();
$rr = $externalcontact_Api->externalcontactInfo($corp_user['corp_id'], $userid, "");
//判断下网格长的userid是否在客户的资料详情的客户好友中
if ($rr['state'] == true) {
$follow_user = $rr['data']['follow_user'];
if (!empty($follow_user)) {
//找到了外部联系人加过的企业内部员工列表
$userid_arr = array_column($follow_user, "userid");
if (in_array($corp_user['userid'], $userid_arr)) {
$flag = 1;
}
}
}
}
}
}
return $flag;
}
}
if (!function_exists('judge_bind_groupchat_relation')) {
/**
* 判断是否加入过网格的客户群
* true已加客户群 false未加客户群
*/
function judge_bind_groupchat_relation($unionid, $grid_id)
{
$flag = 0;
if (!empty($unionid) && !empty($grid_id)) {
$grid_infomation = Db::name("grid_infomation")->where("id", $grid_id)->find();
//客户群id
if (!empty($grid_infomation['corp_groupchat_ids'])) {
$w = [];
$w["corp_groupchat_id"] = ["in", $grid_infomation['corp_groupchat_ids']];
$w['type'] = ["=", "2"];//外部联系人
$w['unionid'] = ["=", $unionid];
$count = Db::name("corp_groupchat_member")->where($w)->count();
if ($count > 0) {
$flag = 1;
}
}
}
return $flag;
}
}
if (!function_exists('user_id_get_admin_id')) {
/**
* 根据用户id获取归属admin_id
*/
function user_id_get_admin_id($id)
{
$admin_id = 1;
if (!empty($id)) {
$ad_id = Db::name("user")->alias("a")
->join("user_grid b", "a.unionid=b.unionid")
->where("a.id", $id)
->value("b.admin_id");
if (!empty($ad_id)) {
$admin_id = $ad_id;
}
}
return $admin_id;
}
}
if (!function_exists('str_add_xing')) {
/**
* 字符串中间加“*”号
* @param $str
* @return string
*/
function str_add_xing($str)
{
//判断是否包含中文字符
if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {
//按照中文字符计算长度
$len = mb_strlen($str, 'UTF-8');
//echo '中文';
if ($len >= 3) {
//三个字符或三个字符以上掐头取尾,中间用*代替
$str = mb_substr($str, 0, 1, 'UTF-8') . '*' . mb_substr($str, -1, 1, 'UTF-8');
} elseif ($len == 2) {
//两个字符
$str = mb_substr($str, 0, 1, 'UTF-8') . '*';
}
} else {
//按照英文字串计算长度
$len = strlen($str);
//echo 'English';
if ($len >= 3) {
//三个字符或三个字符以上掐头取尾,中间用*代替
$str = substr($str, 0, 1) . '*' . substr($str, -1);
} elseif ($len == 2) {
//两个字符
$str = substr($str, 0, 1) . '*';
}
}
return $str;
}
}
if (!function_exists('mobile_hide')) {
/**
* 手机号中间星号代替
*/
function mobile_hide($mobile)
{
if (strlen($mobile) == 11) {
$mobile = substr_replace($mobile, "****", 3, 4);
}
return $mobile;
}
}
if (!function_exists('idcard_hide')) {
/**
* 身份证中间星号代替
*/
function idcard_hide($idcard)
{
if (strlen($idcard) == 15) {
$idcard = substr_replace($idcard, "****", 8, 4);
} elseif (strlen($idcard) == 18) {
$idcard = substr_replace($idcard, "********", 6, 8);
}
return $idcard;
}
}
if (!function_exists('get_root_url')) {
/**
* 获取网站的根Url
* @return string
*/
function get_root_url()
{
$request = request();
$base = $request->root();
$root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
if ('' != $root) {
$root = '/' . ltrim($root, '/');
}
return ($request->isSsl() ? 'https' : 'http') . '://' . $request->host() . "{$root}";
}
}
if (!function_exists('del_place_qrcode')) {
/**
* 删除场所二维码文件(地址存在的是带域名的全路径 )
* @param [type] $pic [description]
* @return [type] [description]
*/
function del_place_qrcode($pic)
{
$pic_path_arr = parse_url($pic);//parse_url函数将URL转换为关联数组
$real_pic_path = $pic_path_arr['path'];
$path = __FILE__;
$paths = substr($path, 0, strpos($path, 'application'));
$pic1 = $paths . "public" . $real_pic_path;
if (file_exists($pic1)) {
@unlink($pic1);
return true;
}
return false;
}
}
if (!function_exists('is_idcard')) {
/**
* 验证身份证
* @param $id
* @return bool
* 公民身份证号码按照 GB11643—1999《公民身份证号码》国家标准编制,由18位数字组成。18位数字按照一定规则进行编写的:
* 前1-6位:行政区划代码,即归属地
* 第7-14位:出生年月日
* 第15-17位:顺序码,表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号
* 第17位:顺序码之一,代表性别,奇数表示男性,偶数表示女性
* 第18位:校验码,用于校验身份证号码是否合法。校检码可以是0~9的数字,有时也用x表示。
* 例如: ·510525197902217660
* 510525 四川省 泸州市 古蔺县
* 19790221 1979年02月21日出生
* 76 顺序码
* 6 顺序码,男
* 0 验证码
*/
function is_idcard($id)
{
$id = strtoupper($id);
$regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
$arr_split = array();
if (!preg_match($regx, $id)) {
return false;
}
if (15 == strlen($id)) //检查15位
{
$regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
@preg_match($regx, $id, $arr_split);
//检查生日日期是否正确
$dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
if (!strtotime($dtm_birth)) {
return false;
} else {
return true;
}
} else //检查18位
{
$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
@preg_match($regx, $id, $arr_split);
$dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
if (!strtotime($dtm_birth)) //检查生日日期是否正确
{
return false;
} else {
//检验18位身份证的校验码是否正确。
//校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
$arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$sign = 0;
for ($i = 0; $i < 17; $i++) {
$b = (int)$id{$i};
$w = $arr_int[$i];
$sign += $b * $w;
}
$n = $sign % 11;
$val_num = $arr_ch[$n];
if ($val_num != substr($id, 17, 1)) {
return false;
} else {
return true;
}
}
}
}
}
if (!function_exists('area_code_get_groupid_and_adminid')) {
/**
* 根据行政区域编码获取角色组和管理员id
*/
function area_code_get_groupid_and_adminid($area_code = null)
{
$group_id = 1;
$admin_id = 1;
if (!empty($area_code)) {
$group_id = Db::name("auth_group")->where("mng_area_code", $area_code)->value("id");
if ($group_id > 0) {
$admin_id = Db::name("auth_group_access")->where("group_id", $group_id)->value("uid");
}
}
$group_id = $group_id > 0 ? $group_id : 1;
$admin_id = $admin_id > 0 ? $admin_id : 1;
return ["group_id" => $group_id, "admin_id" => $admin_id];
}
}
if (!function_exists('full_image')) {
/**
* 获取图片全地址
*/
function full_image($img)
{
//七牛云插件(标识:qiniu)
$qiniu_config = get_addon_config("qiniu");
if (!empty($img)) {
//判断是不是数组
if (is_array($img)) {
foreach ($img as $k => $v) {
if (!empty($v)) {
if ('http' != substr($v, 0, 4)) {
$img[$k] = $qiniu_config['cdnurl'] . $v;
}
}
}
} else {
//判断是不是http开头的(若是就是就代表是本地上传。不是就是七牛上传的=>"/uploads")
if ('http' != substr($img, 0, 4)) {
$img = $qiniu_config['cdnurl'] . $img;
}
}
}
return $img;
}
}
if (!function_exists('get_tree_list')) {
/**
* 父子级树状结构 传入的参数为二维数组 parent是父级id
*/
function get_tree_list($list)
{
//将每条数据中的id值作为其下标
$temp = [];
foreach ($list as $v) {
$v['children'] = [];
$temp[$v['id']] = $v;
}
//获取分类树
foreach ($temp as $k => $v) {
$temp[$v['parent']]['children'][] = &$temp[$v['id']];
}
return isset($temp[0]['children']) ? $temp[0]['children'] : [];
}
}
if (!function_exists('monthlater')) {
/**
* @return array获取近12个月(月份从远到近)
*/
function monthlater()
{
$str = array();
for ($i = 0; $i <= 11; $i++) {
$str[] = date("Y-m", strtotime(date('Y-m-01') . " -$i months"));
}
return array_reverse($str);
}
}
if (!function_exists('apply_bind_admin_id')) {
/**
* 绑定管理员信息
*/
function apply_bind_admin_id($cun_code)
{
$admin_id = 4;
if ($cun_code) {
$admin_id = Db::name("hc_area_code")->where("code", $cun_code)->value("admin_id");
}
return $admin_id;
}
}
if (!function_exists('area_code_relation_names')) {
/**
* 根据某个区域code返回所有上级的名称 用"$str"拼接(不含本身)
*/
function area_code_relation_names($code, $str = "/")
{
$pid_arr = area_name_relation_by_id($code);
$grid_name_str = "";
$pid_arr = array_reverse($pid_arr);//数组倒序
if (!empty($pid_arr)) {
$w1 = [];
$w1["code"] = ["IN", $pid_arr];
$grid_name_arr = Db::name("hc_area_code")->where($w1)->order("level asc")->column("name");
$grid_name_str = implode($grid_name_arr, $str);
}
return $grid_name_str;
}
}
if (!function_exists('area_name_relation_by_id')) {
/**
* 根据某个区域code返回所有上级的id(不含本身)
*/
function area_name_relation_by_id($code, $arr = [])
{
$pcode = Db::name("hc_area_code")->where("code", $code)->value("pcode");
if ($pcode > 0) {
$arr = array_merge($arr, [$pcode]);
}
return $pcode > 0 ? area_name_relation_by_id($pcode, $arr) : $arr;
}
}
if (!function_exists('get_child_ids')) {
/**
* @Description: 获取当前分类下所有子类ID(包含自己)
* @pid:父类ID
*/
function get_child_ids($pid)
{
return get_ids($pid, '', 'code');
}
}
if (!function_exists('get_ids')) {
/**
* @Description: 获取类下所有父/子类ID
* @pid:多个父/子类ID集以,分隔
* @childids:找到的子/父分类列表
* @find_column:where查找的字段[id|pid:default]
*/
function get_ids($pid, $childids, $find_column = 'code')
{
if (!$pid || $pid <= 0 || strlen($pid) <= 0 || !in_array($find_column, array('code', 'pcode'))) return 0;
if (!$childids || strlen($childids) <= 0) $childids = $pid;
$column = ($find_column == 'code' ? "pcode" : "code");//id跟pid为互斥
$ids = Db::name("hc_area_code")->where("$column in ($pid)")->column("$find_column");
$ids = implode(",", $ids);
//未找到,返回已经找到的
if ($ids <= 0) return $childids;
//添加到集合中
$childids .= ',' . $ids;
//递归查找
return get_ids($ids, $childids, $find_column);
}
}
/**
* 发送curl请求获得数据
* @param $url string
* @param return array|mixed
*/
function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$AjaxReturn = curl_exec($curl);
//获取转换为数组
$data = json_decode($AjaxReturn, true);
curl_close($curl);
return $data;
}
function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
{
if (extension_loaded('mbstring') === true) {
$string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
if ($start < 0) {
$start = max(0, $string_length + $start);
} else if ($start > $string_length) {
$start = $string_length;
}
if ($length < 0) {
$length = max(0, $string_length - $start + $length);
} else if ((is_null($length) === true) || ($length > $string_length)) {
$length = $string_length;
}
if (($start + $length) > $string_length) {
$length = $string_length - $start;
}
if (is_null($encoding) === true) {
return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length);
}
return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
}
return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}
function send_post($url, $post_data, $method = 'POST')
{
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $method, //or GET
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function api_notice_increment($url, $data = '', $method = 'GET')
{
$curl = curl_init();
$header = array("application/x-www-form-urlencoded; charset=utf-8");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/**
*求两个已知经纬度之间的距离,单位为千米
* @param lng1,lng2 经度
* @param lat1,lat2 纬度
* @return float 距离,单位千米
**/
//根据经纬度计算距离
function distance($lng1, $lat1, $lng2, $lat2)
{
//将角度转为弧度
$radLat1 = deg2rad($lat1);
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2;//两纬度之差,纬度<90
$b = $radLng1 - $radLng2;//两经度之差纬度<180
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
return $s;
}
... ...