初始上传
This commit is contained in:
136
addon/wechat/model/Config.php
Executable file
136
addon/wechat/model/Config.php
Executable file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
/**
|
||||
* 微信公众号配置
|
||||
*/
|
||||
class Config extends BaseModel
|
||||
{
|
||||
/******************************************************************** 微信公众号配置 start ****************************************************************************/
|
||||
/**
|
||||
* 设置微信公众号配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setWechatConfig($data, $is_use, $site_id)
|
||||
{
|
||||
$config_info = $this->getWechatConfig($site_id);
|
||||
if (!empty($config_info[ 'data' ][ 'value' ][ 'qrcode' ]) && !empty($data[ 'qrcode' ]) && $config_info[ 'data' ][ 'value' ][ 'qrcode' ] != $data[ 'qrcode' ]) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($config_info[ 'data' ][ 'value' ][ 'qrcode' ], $site_id);
|
||||
}
|
||||
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '微信公众号设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信公众号配置信息
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'wechat_name' => '',
|
||||
'wechat_original' => '',
|
||||
'appid' => '',
|
||||
'appsecret' => '',
|
||||
'token' => '',
|
||||
'encodingaeskey' => '',
|
||||
'qrcode' => '',
|
||||
'headimg' => ''
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
/******************************************************************** 微信公众号配置 end ****************************************************************************/
|
||||
|
||||
|
||||
/******************************************************************** 分享内容配置 start ****************************************************************************/
|
||||
/**
|
||||
* 设置分享内容
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setShareConfig($data, $is_use, $site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '分享内容设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_SHARE_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分享内容
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getShareConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_SHARE_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'goods_param_1' => '优惠价',
|
||||
'goods_param_2' => '全场正品',
|
||||
'qrcode_param_1' => '向您推荐',
|
||||
'qrcode_param_2' => '注册有优惠',
|
||||
'shop_param_1' => '欢迎打开',
|
||||
'shop_param_2' => '向您推荐',
|
||||
'shop_param_3' => '注册有优惠'
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
/******************************************************************** 分享内容配置 end ****************************************************************************/
|
||||
|
||||
/**
|
||||
* 设置模板消息配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setTemplateMessageConfig($data, $is_use, $site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '微信模板消息设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_TEMPLATE_MESSAGE_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板消息配置
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateMessageConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_TEMPLATE_MESSAGE_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [ 'is_jump_weapp' => 0 ];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
275
addon/wechat/model/Fans.php
Executable file
275
addon/wechat/model/Fans.php
Executable file
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信粉丝
|
||||
*/
|
||||
class Fans extends BaseModel
|
||||
{
|
||||
/***************************************************************** 微信粉丝 start ***************************************************************************/
|
||||
|
||||
/**
|
||||
* 获取粉丝列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getFansList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('wechat_fans')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取粉丝分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getFansPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'subscribe_time desc', $field = '*')
|
||||
{
|
||||
$list = model('wechat_fans')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为微信粉丝批量打标签
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function fansBatchTagging($data)
|
||||
{
|
||||
//调用微信接口服务器配置 修改微信端粉丝标签
|
||||
$res = model('wechat_fans')->update([ 'tagid_list' => implode(',', $data[ 'tag_id_list' ]) ], [ 'openid' => [ 'in', $data[ 'openid_list' ] ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为微信粉丝批量取消标签
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function batchUnTagging($data)
|
||||
{
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步粉丝标签
|
||||
* @param $openid_list
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function syncFans($openid_list)
|
||||
{
|
||||
$wechat_model = new Wechat();
|
||||
$select_result = $wechat_model->selectUser($openid_list);//获取多个微信粉丝信息
|
||||
if ($select_result[ "code" ] != 0)
|
||||
return $select_result;
|
||||
|
||||
$user_list = $select_result[ "data" ][ "user_info_list" ];
|
||||
$subscribe_scene_array = array (
|
||||
"ADD_SCENE_SEARCH" => "公众号搜索",
|
||||
"ADD_SCENE_ACCOUNT_MIGRATION" => "公众号迁移",
|
||||
"ADD_SCENE_PROFILE_CARD" => "名片分享",
|
||||
"ADD_SCENE_QR_CODE" => "扫描二维码",
|
||||
"ADD_SCENE_PROFILE_ LINK" => "图文页内名称点击",
|
||||
"ADD_SCENE_PROFILE_ITEM" => "图文页右上角菜单",
|
||||
"ADD_SCENE_PAID" => "支付后关注",
|
||||
"ADD_SCENE_OTHERS" => "其他",
|
||||
);
|
||||
foreach ($user_list as $user_k => $user_item) {
|
||||
$unionid = empty($user_item[ "unionid" ]) ? '' : $user_item[ "unionid" ];
|
||||
$province = base64_encode($user_item[ "province" ]);
|
||||
$city = base64_encode($user_item[ "city" ]);
|
||||
$nickname = base64_encode($user_item[ 'nickname' ]);
|
||||
$nickname_decode = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $user_item[ 'nickname' ]);
|
||||
$data = array (
|
||||
'nickname' => $nickname,
|
||||
'nickname_decode' => $nickname_decode,
|
||||
'headimgurl' => $user_item[ 'headimgurl' ],
|
||||
'sex' => $user_item[ "sex" ],
|
||||
'language' => $user_item[ "language" ],
|
||||
'country' => $user_item[ "country" ],
|
||||
'province' => $province,
|
||||
'city' => $city,
|
||||
'district' => '',
|
||||
'openid' => $user_item[ "openid" ],
|
||||
'unionid' => $unionid,
|
||||
'groupid' => $user_item[ "groupid" ],
|
||||
'is_subscribe' => $user_item[ "subscribe" ],
|
||||
'remark' => $user_item[ "remark" ],
|
||||
'update_date' => time(),
|
||||
'tagid_list' => empty($user_item[ "tagid_list" ]) ? '' : implode(',', $user_item[ "tagid_list" ]),
|
||||
'subscribe_scene' => $user_item[ "subscribe_scene" ],
|
||||
'subscribe_scene_name' => $subscribe_scene_array[ $user_item[ "subscribe_scene" ] ],
|
||||
'qr_scene' => $user_item[ "qr_scene" ],
|
||||
'qr_scene_str' => $user_item[ "qr_scene_str" ],
|
||||
'subscribe_time' => $user_item[ "subscribe_time" ],
|
||||
);
|
||||
$res = model("wechat_fans")->add($data);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/***************************************************************** 微信粉丝 end ***************************************************************************/
|
||||
|
||||
/***************************************************************** 微信粉丝标签 start ***************************************************************************/
|
||||
|
||||
/**
|
||||
* 增加粉丝标签
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addFansTag($data)
|
||||
{
|
||||
$res = model('wechat_fans_tag')->add($data);
|
||||
if ($res === false)
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
|
||||
//同步修改微信端粉丝标签
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑粉丝标签
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editFansTag($data, $condition)
|
||||
{
|
||||
$res = model('wechat_fans_tag')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
|
||||
//同步修改微信端粉丝标签
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteFansTag($condition)
|
||||
{
|
||||
$res = model('wechat_fans_tag')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取粉丝列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getFansTagList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('wechat_fans_tag')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信粉丝标签列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $order
|
||||
* @param bool $field
|
||||
* @return array
|
||||
*/
|
||||
public function getFansTagPageList($condition = [], $page = 1, $list_rows = PAGE_LIST_ROWS, $order = '', $field = true)
|
||||
{
|
||||
$list = model('wechat_fans_tag')->pageList($condition, $field, $order, $page, $list_rows);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步粉丝标签
|
||||
* @return array
|
||||
*/
|
||||
public function syncFansTag()
|
||||
{
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/***************************************************************** 微信粉丝标签 end ***************************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* 增加粉丝
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addFans($data)
|
||||
{
|
||||
$res = model('wechat_fans')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消关注
|
||||
* @param $open_id
|
||||
* @return array
|
||||
*/
|
||||
public function unfollowWechat($open_id)
|
||||
{
|
||||
$data = array (
|
||||
'is_subscribe' => 0,
|
||||
'update_date' => time(),
|
||||
'unsubscribe_time' => time()
|
||||
);
|
||||
$wechat_fans_model = model('wechat_fans');
|
||||
$res = $wechat_fans_model->update($data, [ 'openid' => $open_id ]);
|
||||
return success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 粉丝详情
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getFansInfo($condition, $field = "*")
|
||||
{
|
||||
if (!empty($cache)) {
|
||||
return $this->success($cache);
|
||||
}
|
||||
$info = model('wechat_fans')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑粉丝
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editFans($data, $condition)
|
||||
{
|
||||
$data[ "update_date" ] = time();
|
||||
$result = model("wechat_fans")->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
}
|
||||
96
addon/wechat/model/Material.php
Executable file
96
addon/wechat/model/Material.php
Executable file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信素材管理
|
||||
*/
|
||||
class Material extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加微信素材
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addMaterial($data)
|
||||
{
|
||||
$res = model('wechat_media')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改微信素材
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editMaterial($data, $condition)
|
||||
{
|
||||
$res = model('wechat_media')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除微信素材
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteMaterial($condition)
|
||||
{
|
||||
$res = model('wechat_media')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信素材信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMaterialInfo($condition, $field = '*')
|
||||
{
|
||||
$res = model('wechat_media')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信素材列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMaterialList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$res = model('wechat_media')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信素材分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMaterialPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'update_time desc', $field = '*')
|
||||
{
|
||||
$list = model('wechat_media')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
48
addon/wechat/model/Menu.php
Executable file
48
addon/wechat/model/Menu.php
Executable file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信公众号配置
|
||||
*/
|
||||
class Menu extends BaseModel
|
||||
{
|
||||
|
||||
/******************************************************************** 微信公众号菜单配置 start ****************************************************************************/
|
||||
/**
|
||||
* 设置微信公众号配置
|
||||
* @param $data
|
||||
* @param int $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setWechatMenuConfig($data, $site_id = 0)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '微信公众号设置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_MENU_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信公众号菜单配置
|
||||
* @param int $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatMenuConfig($site_id = 0)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_MENU_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
/******************************************************************** 微信公众号菜单配置 end ****************************************************************************/
|
||||
}
|
||||
105
addon/wechat/model/Message.php
Executable file
105
addon/wechat/model/Message.php
Executable file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use addon\weapp\model\Config as WeappConfig;
|
||||
use addon\mobileshop\model\Config as MobileShopConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 微信消息模板
|
||||
*/
|
||||
class Message extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param array $param
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function sendMessage(array $param)
|
||||
{
|
||||
try {
|
||||
$site_id = $param[ 'site_id' ];
|
||||
$support_type = $param[ "support_type" ] ?? [];
|
||||
|
||||
if (!empty($support_type) && !in_array("wechat", $support_type)) return $this->success();
|
||||
|
||||
if (empty($param[ 'openid' ])) return $this->success('缺少必需参数openid');
|
||||
|
||||
$message_info = $param[ 'message_info' ];
|
||||
if ($message_info[ 'wechat_is_open' ] == 0) return $this->error('未启用模板消息');
|
||||
|
||||
$wechat_info = json_decode($message_info[ 'wechat_json' ], true);
|
||||
if (empty($message_info[ 'wechat_template_id' ])) return $this->error('未配置模板消息');
|
||||
|
||||
$template_data = [
|
||||
// 'first' => [
|
||||
// 'value' => $wechat_info[ 'headtext' ],
|
||||
// 'color' => !empty($wechat_info[ 'headtextcolor' ]) ? $wechat_info[ 'headtextcolor' ] : '#f00'
|
||||
// ],
|
||||
// 'remark' => [
|
||||
// 'value' => $wechat_info[ 'bottomtext' ],
|
||||
// 'color' => !empty($wechat_info[ 'bottomtextcolor' ]) ? $wechat_info[ 'bottomtextcolor' ] : '#333'
|
||||
// ]
|
||||
];
|
||||
if (!empty($param[ 'template_data' ])) $template_data = array_merge($template_data, $param[ 'template_data' ]);
|
||||
|
||||
|
||||
$data = [
|
||||
'openid' => $param[ 'openid' ],
|
||||
'template_id' => $message_info[ 'wechat_template_id' ],
|
||||
'data' => $template_data,
|
||||
'miniprogram' => [],
|
||||
'url' => ""
|
||||
];
|
||||
|
||||
if (!empty($param[ 'page' ])) {
|
||||
// 商家消息
|
||||
if ($message_info[ 'message_type' ] == 2 && addon_is_exit('mobileshop', $site_id)) {
|
||||
$config = new MobileShopConfig();
|
||||
$weapp_config = $config->getWeappConfig($site_id)[ 'data' ][ "value" ];
|
||||
if (!empty($weapp_config[ 'appid' ])) {
|
||||
$data[ 'miniprogram' ] = [
|
||||
'appid' => $weapp_config[ 'appid' ],
|
||||
'pagepath' => $param[ 'page' ]
|
||||
];
|
||||
}
|
||||
$mshop_config = $config->getMShopDomainName($site_id)[ 'data' ][ 'value' ];
|
||||
$data[ 'url' ] = $mshop_config[ 'domain_name_mobileshop' ] . '/' . $param[ 'page' ];
|
||||
} else {
|
||||
$template_config_model = new Config();
|
||||
$template_config = $template_config_model->getTemplateMessageConfig($site_id)[ 'data' ][ 'value' ];
|
||||
|
||||
if ($template_config[ 'is_jump_weapp' ]) {
|
||||
// 小程序配置
|
||||
$weapp_config = new WeappConfig();
|
||||
$weapp_config = $weapp_config->getWeappConfig($site_id)[ 'data' ][ "value" ];
|
||||
|
||||
if (!empty($weapp_config[ 'appid' ])) {
|
||||
$data[ 'miniprogram' ] = [
|
||||
'appid' => $weapp_config[ 'appid' ],
|
||||
'pagepath' => $param[ 'page' ]
|
||||
];
|
||||
}
|
||||
}
|
||||
$data[ 'url' ] = getH5Domain() . '/' . $param[ 'page' ];
|
||||
}
|
||||
}
|
||||
$wechat = new Wechat($site_id);
|
||||
$res = $wechat->sendTemplateMessage($data);
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', "模板消息发送失败:" . $e->getMessage() . ",File:" . $e->getFile() . ",line:" . $e->getLine());
|
||||
}
|
||||
}
|
||||
}
|
||||
129
addon/wechat/model/MessageRecords.php
Executable file
129
addon/wechat/model/MessageRecords.php
Executable file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\system\Wechat\common\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信模板消息消息发送记录
|
||||
*/
|
||||
class MessageRecords extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 新建微信模板消息发送记录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addWechatMessageRecords($data)
|
||||
{
|
||||
$id = model("nc_site_message_wechat_list")->add($data);
|
||||
if ($id === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加微信模板消息发送记录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addWechatMessageRecordsList($data)
|
||||
{
|
||||
$id = model("nc_site_message_wechat_list")->addList($data);
|
||||
if ($id === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑微信模板消息发送记录
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editWechatMessageRecords($data, $condition)
|
||||
{
|
||||
$res = model("nc_site_message_wechat_list")->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除微信模板消息发送记录
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteWechatMessageRecords($condition)
|
||||
{
|
||||
$res = model("nc_site_message_wechat_list")->delete($condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询微信模板消息发送记录
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatMessageRecordsInfo($condition, $field = "*")
|
||||
{
|
||||
$res = model("nc_site_message_wechat_list")->getInfo($condition, $field);
|
||||
if (empty($res)) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询微信模板消息发送记录
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatMessageRecordsList($condition, $field = "*")
|
||||
{
|
||||
$res = model("nc_site_message_wechat_list")->getList($condition, $field);
|
||||
if (empty($res)) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询微信模板消息发送记录分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatMessageRecordsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$res = model("nc_site_message_wechat_list")->pageList($condition, $field, $order, $page, $page_size);
|
||||
if (empty($res)) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
}
|
||||
103
addon/wechat/model/Qrcode.php
Executable file
103
addon/wechat/model/Qrcode.php
Executable file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 微信二维码
|
||||
*/
|
||||
class Qrcode extends BaseModel
|
||||
{
|
||||
/***************************************************************** 微信二维码 start ***************************************************************************/
|
||||
/**
|
||||
* 添加微信二维码
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addQrcode($data)
|
||||
{
|
||||
$data[ "update_time" ] = time();
|
||||
Cache::tag("wechat_qrcode")->clear();
|
||||
$result = model("wechat_qrcode")->add($data);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑微信二维码
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editQrcode($data, $condition)
|
||||
{
|
||||
$data[ "update_time" ] = time();
|
||||
Cache::tag("wechat_qrcode")->clear();
|
||||
$result = model("wechat_qrcode")->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除微信二维码
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteQrcode($condition)
|
||||
{
|
||||
Cache::tag("wechat_qrcode")->clear();
|
||||
$result = model("wechat_qrcode")->delete($condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认二维码模板
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyQrcodeDefault($condition)
|
||||
{
|
||||
//将全部模板设置为非默认
|
||||
Cache::tag("wechat_qrcode")->clear();
|
||||
model("wechat_qrcode")->update([ "is_default" => 0 ], [ 'is_default' => 1 ]);
|
||||
$res = model("wechat_qrcode")->update([ "is_default" => 1 ], $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取二维码模板详情
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getQrcodeInfo($condition, $field = "*")
|
||||
{
|
||||
$info = model('wechat_qrcode')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信二维码列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getQrcodePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('wechat_qrcode')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/***************************************************************** 微信粉丝 end ***************************************************************************/
|
||||
}
|
||||
161
addon/wechat/model/Replay.php
Executable file
161
addon/wechat/model/Replay.php
Executable file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信回复
|
||||
*/
|
||||
class Replay extends BaseModel
|
||||
{
|
||||
|
||||
/*******************************************************************************微信回复开始*****************************************************/
|
||||
/**
|
||||
* 添加微信关键词回复
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addRule($data)
|
||||
{
|
||||
$res = model('wechat_replay_rule')->add($data);
|
||||
if ($res === false) {
|
||||
return $this->error($res, 'SAVE_FAIL');
|
||||
}
|
||||
return $this->success($res, 'SAVE_SUCCESS');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改微信关键词回复
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editRule($data, $condition)
|
||||
{
|
||||
$res = model('wechat_replay_rule')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error($res, 'SAVE_FAIL');
|
||||
}
|
||||
return $this->success($res, 'SAVE_SUCCESS');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除微信关键词回复
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteRule($condition)
|
||||
{
|
||||
$res = model('wechat_replay_rule')->delete($condition);
|
||||
if ($res === false) {
|
||||
return $this->error($res, 'DELETE_FAIL');
|
||||
}
|
||||
return $this->success($res, 'DELETE_SUCCESS');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信关键词回复信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getRuleInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model('wechat_replay_rule')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取回复列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getReplayPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('wechat_replay_rule')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取站点关键字回复
|
||||
* @param $keywords
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getSiteWechatKeywordsReplay($keywords, $site_id)
|
||||
{
|
||||
|
||||
$list = model('wechat_replay_rule')->getList([['rule_type', '=', 'KEYWORDS'], ['site_id', '=', $site_id]]);
|
||||
$default_list = model('wechat_replay_rule')->getList([['rule_type', '=', 'DEFAULT'], ['site_id', '=', $site_id]]);
|
||||
$rule_list = array();
|
||||
foreach ($list as $k => $v) {
|
||||
$kewords_array = json_decode($v['keywords_json'], true);
|
||||
$replay_array = json_decode($v['replay_json'], true);
|
||||
if (!empty($kewords_array) && !empty($replay_array)) {
|
||||
foreach ($kewords_array as $k_key => $v_key) {
|
||||
if (($v_key['keywords_type'] == 0 && $v_key['keywords_name'] == $keywords) || ($v_key['keywords_type'] == 1 && (strpos($keywords, $v_key['keywords_name']) !== false))) {
|
||||
$num = rand(0, count($replay_array) - 1);
|
||||
$rule_list[] = $replay_array[$num];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($rule_list)) {
|
||||
$default_rule_list = array();
|
||||
foreach ($default_list as $kk => $vv) {
|
||||
$default_replay_array = json_decode($vv['replay_json'], true);
|
||||
if (!empty($default_replay_array)) {
|
||||
$default_rule_list[] = $default_replay_array[0];
|
||||
}
|
||||
}
|
||||
if (!empty($default_rule_list)) {
|
||||
$rule = $default_rule_list[0];
|
||||
} else {
|
||||
$rule = [];
|
||||
}
|
||||
} else {
|
||||
$rule = $rule_list[0];
|
||||
}
|
||||
return $this->success($rule);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取微信关注回复
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
function getWechatFollowReplay($site_id)
|
||||
{
|
||||
$follow_info = model('wechat_replay_rule')->getInfo([['rule_type', '=', 'AFTER'], ['site_id', '=', $site_id]]);
|
||||
$replay_content = '';
|
||||
if (!empty($follow_info['replay_json'])) {
|
||||
$replay_info = json_decode($follow_info['replay_json'], true);
|
||||
switch ($replay_info[0]['type']) {
|
||||
case 'text' :
|
||||
$replay_content = $replay_info[0]['reply_content'];
|
||||
break;
|
||||
case 'articles' :
|
||||
$replay_content = '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $this->success($replay_content);
|
||||
}
|
||||
|
||||
/*******************************************************************************微信回复结束*****************************************************/
|
||||
}
|
||||
281
addon/wechat/model/Stat.php
Executable file
281
addon/wechat/model/Stat.php
Executable file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 数据统计与分析
|
||||
*/
|
||||
class Stat extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取用户增减数据, 最大时间跨度:7
|
||||
* @param $begin_date
|
||||
* @param $end_date
|
||||
* @return array
|
||||
*/
|
||||
public function userSummary($begin_date, $end_date)
|
||||
{
|
||||
$cache = Cache::get("wechat_user_summary_" . "_" . $begin_date . "_" . $end_date);
|
||||
if (!empty($cache))
|
||||
return $this->success($cache);
|
||||
|
||||
$wechat_model = new Wechat();
|
||||
$result = $wechat_model->userSummary($begin_date, $end_date);
|
||||
if ($result[ "code" ] < 0)
|
||||
return $result;
|
||||
|
||||
Cache::tag("wechat_visit")->set("wechat_user_summary_" . "_" . $begin_date . "_" . $end_date, $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取累计用户数据, 最大时间跨度:7;
|
||||
* @param $begin_date
|
||||
* @param $end_date
|
||||
* @return array
|
||||
*/
|
||||
public function userCumulate($begin_date, $end_date)
|
||||
{
|
||||
$cache = Cache::get("wechat_user_cumulate_" . "_" . $begin_date . "_" . $end_date);
|
||||
if (!empty($cache))
|
||||
return $this->success($cache);
|
||||
|
||||
$wechat_model = new Wechat();
|
||||
$result = $wechat_model->userCumulate($begin_date, $end_date);
|
||||
if ($result[ "code" ] < 0)
|
||||
return $result;
|
||||
|
||||
Cache::tag("wechat_visit")->set("wechat_user_cumulate_" . "_" . $begin_date . "_" . $end_date, $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口分析分时数据, 最大时间跨度:1;
|
||||
* @param $begin_date
|
||||
* @param $end_date
|
||||
* @return array
|
||||
*/
|
||||
public function interfaceSummaryHourly($begin_date, $end_date)
|
||||
{
|
||||
$cache = Cache::get("wechat_interface_summary_hourly_" . "_" . $begin_date . "_" . $end_date);
|
||||
if (!empty($cache))
|
||||
return $this->success($cache);
|
||||
|
||||
$wechat_model = new Wechat();
|
||||
$result = $wechat_model->interfaceSummaryHourly($begin_date, $end_date);
|
||||
if ($result[ "code" ] < 0)
|
||||
return $result;
|
||||
|
||||
Cache::tag("wechat_visit")->set("wechat_interface_summary_hourly_" . "_" . $begin_date . "_" . $end_date, $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口分析数据, 最大时间跨度:30;
|
||||
* @param $begin_date
|
||||
* @param $end_date
|
||||
* @return array
|
||||
*/
|
||||
public function interfaceSummary($begin_date, $end_date)
|
||||
{
|
||||
// $cache = Cache::get("wechat_interface_summary_" . "_" . $begin_date."_" . $end_date);
|
||||
// if (!empty($cache))
|
||||
// return $this->success($cache);
|
||||
|
||||
$wechat_model = new Wechat();
|
||||
$result = $wechat_model->interfaceSummary($begin_date, $end_date);
|
||||
if ($result[ "code" ] < 0)
|
||||
return $result;
|
||||
|
||||
Cache::tag("wechat_visit")->set("wechat_interface_summary_" . "_" . $begin_date . "_" . $end_date, $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信粉丝统计(以天为单位)
|
||||
* @param $begin_date
|
||||
* @param $end_date
|
||||
* @return array
|
||||
*/
|
||||
public function fans($begin_date, $end_date)
|
||||
{
|
||||
// $cache = Cache::get("wechat_fans_statistics_" . "_" . $begin_date."_" . $end_date);
|
||||
// if (!empty($cache)) {
|
||||
// return $this->success($cache);
|
||||
// }
|
||||
// $data = [ 'begin_date' => $param['begin_date'], 'end_date' => $param['end_date'] ];
|
||||
$user_summary_result = $this->userSummary($begin_date, $end_date);
|
||||
if ($user_summary_result[ "code" ] < 0) {
|
||||
return $user_summary_result;
|
||||
}
|
||||
$user_cumulate_result = $this->userCumulate($begin_date, $end_date);
|
||||
if ($user_cumulate_result[ "code" ] < 0) {
|
||||
return $user_cumulate_result;
|
||||
}
|
||||
$list = [];
|
||||
foreach ($user_cumulate_result[ "data" ] as $cumulate_k => $cumulate_v) {
|
||||
$temp_item = $cumulate_v;
|
||||
$temp_item[ 'cumulate_user' ] = empty($cumulate_v[ 'cumulate_user' ]) ? 0 : $cumulate_v[ 'cumulate_user' ];
|
||||
$new_user = 0;
|
||||
$cancel_user = 0;
|
||||
$net_growth_user = 0;
|
||||
foreach ($user_summary_result[ 'data' ] as $key => $item) {
|
||||
if ($item[ "ref_date" ] == $cumulate_v[ "ref_date" ]) {
|
||||
$new_user += $item[ 'new_user' ];
|
||||
$cancel_user += $item[ 'cancel_user' ];
|
||||
$net_growth_user += $item[ "new_user" ] - $item[ "cancel_user" ];
|
||||
}
|
||||
}
|
||||
$temp_item[ 'new_user' ] = $new_user;
|
||||
$temp_item[ 'cancel_user' ] = $cancel_user;
|
||||
$temp_item[ 'net_growth_user' ] = $net_growth_user;
|
||||
$list[] = $temp_item;
|
||||
}
|
||||
Cache::tag("wechat_visit")->set("wechat_fans_statistics_" . "_" . $begin_date . "_" . $end_date, $list);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口访问数据图
|
||||
* @param $date_type
|
||||
* @return array
|
||||
*/
|
||||
public function interfaceSummaryStatistics($date_type)
|
||||
{
|
||||
$date_data = $this->getDaterange($date_type);
|
||||
$is_error = true;
|
||||
$callback_count_data = [];
|
||||
$fail_count_data = [];
|
||||
$average_time_cost_data = [];
|
||||
$max_time_cost_data = [];
|
||||
foreach ($date_data[ "date_list" ] as $k => $v) {
|
||||
$callback_count = 0;
|
||||
$fail_count = 0;
|
||||
$average_time_cost = 0;
|
||||
$max_time_cost = 0;
|
||||
if ($is_error) {
|
||||
$temp_data = $this->interfaceSummary($v, $v);
|
||||
if (!empty($temp_data[ "data" ])) {
|
||||
$temp_date_item = $temp_data[ "data" ];
|
||||
$callback_count = $temp_date_item[ 0 ][ "callback_count" ];
|
||||
$fail_count = $temp_date_item[ 0 ][ "fail_count" ];
|
||||
$average_time_cost = $temp_date_item[ 0 ][ "total_time_cost" ] / $temp_date_item[ 0 ][ "callback_count" ];
|
||||
$max_time_cost = $temp_date_item[ 0 ][ "max_time_cost" ];
|
||||
} else {
|
||||
$is_error = false;
|
||||
}
|
||||
}
|
||||
$callback_count_data[] = $callback_count;
|
||||
$fail_count_data[] = $fail_count;
|
||||
$average_time_cost_data[] = $average_time_cost;
|
||||
$max_time_cost_data[] = $max_time_cost;
|
||||
}
|
||||
$return_data = array (
|
||||
"date" => $date_data[ "date_list" ],
|
||||
"data" => array (
|
||||
"callback_count_data" => $callback_count_data,
|
||||
"fail_count_data" => $fail_count_data,
|
||||
"average_time_cost_data" => $average_time_cost_data,
|
||||
"max_time_cost_data" => $max_time_cost_data
|
||||
)
|
||||
);
|
||||
return $this->success($return_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户访问统计图
|
||||
* @param $date_type
|
||||
* @return array
|
||||
*/
|
||||
public function userSummaryStatistics($date_type)
|
||||
{
|
||||
$is_error = true;
|
||||
$date_data = $this->getDaterange($date_type);
|
||||
$new_user_data = [];
|
||||
$cancel_user_data = [];
|
||||
$net_growth_user_data = [];
|
||||
$cumulate_user_data = [];
|
||||
foreach ($date_data[ "date_list" ] as $k => $v) {
|
||||
$new_user = 0;
|
||||
$cancel_user = 0;
|
||||
$net_growth_user = 0;
|
||||
$cumulate_user = 0;
|
||||
if ($is_error) {
|
||||
$temp_data = $this->fans($v, $v);
|
||||
if (!empty($temp_data[ "data" ])) {
|
||||
$temp_date_item = $temp_data[ "data" ];
|
||||
$new_user = $temp_date_item[ 0 ][ "new_user" ];
|
||||
$cancel_user = $temp_date_item[ 0 ][ "cancel_user" ];
|
||||
$net_growth_user = $temp_date_item[ 0 ][ "net_growth_user" ];
|
||||
$cumulate_user = $temp_date_item[ 0 ][ "cumulate_user" ];
|
||||
} else {
|
||||
$is_error = false;
|
||||
}
|
||||
}
|
||||
|
||||
$new_user_data[] = $new_user;
|
||||
$cancel_user_data[] = $cancel_user;
|
||||
$net_growth_user_data[] = $net_growth_user;
|
||||
$cumulate_user_data[] = $cumulate_user;
|
||||
}
|
||||
$return_data = array (
|
||||
"date" => $date_data[ "date_list" ],
|
||||
"data" => array (
|
||||
"new_user_data" => $new_user_data,
|
||||
"cancel_user_data" => $cancel_user_data,
|
||||
"net_growth_user_data" => $net_growth_user_data,
|
||||
"cumulate_user_data" => $cumulate_user_data
|
||||
)
|
||||
);
|
||||
return $this->success($return_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间间隔
|
||||
* @param $date_type
|
||||
* @return array
|
||||
*/
|
||||
public function getDaterange($date_type)
|
||||
{
|
||||
$yesterday = strtotime('-1 days');
|
||||
switch ( $date_type ) {
|
||||
case 'yesterday':
|
||||
$yesterday = strtotime('-1 days');
|
||||
$begin_time = $yesterday;
|
||||
$end_time = $yesterday;
|
||||
break;
|
||||
case 'week':
|
||||
$week = strtotime('-7 days');
|
||||
$begin_time = $week;
|
||||
$end_time = $yesterday;
|
||||
break;
|
||||
case 'month':
|
||||
$month = strtotime('-30 days');
|
||||
$begin_time = $month;
|
||||
$end_time = $yesterday;
|
||||
break;
|
||||
}
|
||||
$date_x = periodGroup($begin_time, $end_time, "Y-m-d");
|
||||
$begin_date = date("Ymd", $begin_time);
|
||||
$end_date = date("Ymd", $end_time);
|
||||
$data = array (
|
||||
"begin_date" => $begin_date,
|
||||
"end_date" => $end_date,
|
||||
"date_list" => $date_x,
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
999
addon/wechat/model/Wechat.php
Executable file
999
addon/wechat/model/Wechat.php
Executable file
@@ -0,0 +1,999 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\model;
|
||||
|
||||
use addon\wxoplatform\model\OpenPlatform;
|
||||
use addon\wxoplatform\model\Oplatform;
|
||||
use EasyWeChat\Factory;
|
||||
use EasyWeChat\Kernel\Messages\Article;
|
||||
use EasyWeChat\Kernel\Messages\Text;
|
||||
use EasyWeChat\Kernel\Messages\News;
|
||||
use EasyWeChat\Kernel\Messages\NewsItem;
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 微信公众号
|
||||
*/
|
||||
class Wechat extends BaseModel
|
||||
{
|
||||
|
||||
public $app;//微信公众对象
|
||||
private $site_id;//站点
|
||||
//公众号类型
|
||||
public $service_type = array (
|
||||
0 => "订阅号",
|
||||
1 => "由历史老帐号升级后的订阅号",
|
||||
2 => "服务号",
|
||||
);
|
||||
|
||||
//公众号认证类型
|
||||
public $verify_type = array (
|
||||
-1 => "未认证",
|
||||
0 => "微信认证",
|
||||
1 => "新浪微博认证",
|
||||
2 => "腾讯微博认证",
|
||||
3 => "已资质认证通过但还未通过名称认证",
|
||||
4 => "已资质认证通过、还未通过名称认证,但通过了新浪微博认证",
|
||||
5 => "已资质认证通过、还未通过名称认证,但通过了腾讯微博认证",
|
||||
);
|
||||
|
||||
//business_info 说明
|
||||
public $business_type = array (
|
||||
'open_store' => "是否开通微信门店功能",
|
||||
'open_scan' => "是否开通微信扫商品功能",
|
||||
'open_pay' => "是否开通微信支付功能",
|
||||
'open_card' => "是否开通微信卡券功能",
|
||||
'open_shake' => "是否开通微信摇一摇功能",
|
||||
);
|
||||
|
||||
public function __construct($site_id = 0)
|
||||
{
|
||||
$this->site_id = $site_id;
|
||||
// $response = $this->app->server->serve();
|
||||
// 将响应输出
|
||||
// $response->send();exit; // Laravel 里请使用:return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权实例
|
||||
*/
|
||||
public function app()
|
||||
{
|
||||
$config_model = new Config();
|
||||
$config_result = $config_model->getWechatConfig($this->site_id);
|
||||
$config = $config_result[ "data" ];
|
||||
if (!empty($config)) {
|
||||
$config_info = $config[ "value" ];
|
||||
}
|
||||
//授权方式 手动授权 或 自动授权(通过开放平台)
|
||||
$auth_type = $config_info[ "is_authopen" ] ?? '';
|
||||
|
||||
if ($auth_type == "1") {
|
||||
$oplatform_model = new Oplatform();
|
||||
$config_info[ 'type' ] = 'wechat';
|
||||
|
||||
$this->app = $oplatform_model->app($config_info);
|
||||
} else {
|
||||
$config = [
|
||||
'app_id' => $config_info[ "appid" ] ?? '',
|
||||
'secret' => $config_info[ "appsecret" ] ?? '',
|
||||
'token' => $config_info[ "token" ] ?? '', // Token
|
||||
'aes_key' => $config_info[ 'encodingaeskey' ] ?? '', // EncodingAESKey,兼容与安全模式下请一定要填写!!!
|
||||
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
|
||||
'response_type' => 'array',
|
||||
/**
|
||||
* 日志配置
|
||||
*
|
||||
* level: 日志级别, 可选为:debug/info/notice/warning/error/critical/alert/emergency
|
||||
* permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644)
|
||||
* file:日志文件位置(绝对路径!!!),要求可写权限
|
||||
*/
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'permission' => 0777,
|
||||
'file' => 'runtime/log/wechat/easywechat.logs',
|
||||
],
|
||||
];
|
||||
if (empty($config[ 'app_id' ]) || empty($config[ 'secret' ])) {
|
||||
throw new \Exception('商家公众号配置有误,请联系平台管理员');
|
||||
}
|
||||
|
||||
$this->app = Factory::officialAccount($config);
|
||||
}
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建微信菜单
|
||||
* @param $buttons
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function menu($buttons)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->menu->create($buttons);
|
||||
if ($result[ 'errcode' ] == 0) {
|
||||
return $this->success();
|
||||
} else {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取粉丝列表
|
||||
* @param null $nextOpenId
|
||||
* @return array
|
||||
*/
|
||||
public function user($nextOpenId = null)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->user->list($nextOpenId); // $nextOpenId 可选
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询多个粉丝信息
|
||||
* @param $user_list
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function selectUser($user_list)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->user->select($user_list); // $nextOpenId 可选
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到粉丝信息
|
||||
* @param $openId
|
||||
* @return array
|
||||
*/
|
||||
public function getUser($openId)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->user->get($openId); // $nextOpenId 可选
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Code 获取用户 session 信息
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public function authCodeToOpenid($params = [])
|
||||
{
|
||||
try {
|
||||
if (empty($_REQUEST[ "code" ])) {
|
||||
$auth_result = $this->getAuthCode(request()->url(true));
|
||||
if ($auth_result[ "code" ] >= 0) {
|
||||
header("Location: " . $auth_result[ "data" ]);
|
||||
} else {
|
||||
return $auth_result;
|
||||
}
|
||||
|
||||
}
|
||||
$user = $this->app()->oauth->user();//获取授权用户
|
||||
$data = [
|
||||
'openid' => $user->getId(),
|
||||
'userinfo' => [
|
||||
'avatarUrl' => $user->getAvatar(),
|
||||
'nickName' => $user->getNickname()
|
||||
]
|
||||
];
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
if (property_exists($e, 'body')) {
|
||||
return $this->handleError($e->body[ 'errcode' ], $e->body[ 'errmsg' ]);
|
||||
}
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取授权信息
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function getAuthByCode($params)
|
||||
{
|
||||
try {
|
||||
$user = $this->app()->oauth->userByCode($params[ 'code' ]);//获取授权用户
|
||||
$original = $user->getOriginal();
|
||||
$data = [
|
||||
'openid' => $original[ 'openid' ],
|
||||
'userinfo' => [
|
||||
'avatarUrl' => $original[ 'headimgurl' ] ?? '',
|
||||
'nickName' => $original[ 'nickname' ] ?? ''
|
||||
]
|
||||
];
|
||||
if (isset($original[ 'unionid' ])) {
|
||||
$data[ 'unionid' ] = $original[ 'unionid' ];
|
||||
}
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
if (property_exists($e, 'body')) {
|
||||
return $this->handleError($e->body[ 'errcode' ], $e->body[ 'errmsg' ]);
|
||||
}
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公众号网页授权code
|
||||
* @param $redirect
|
||||
* @param string $scopes snsapi_base:静默授权 snsapi_userinfo:用户授权
|
||||
* @return array
|
||||
*/
|
||||
public function getAuthCode($redirect, $scopes = 'snsapi_base')
|
||||
{
|
||||
try {
|
||||
$response = $this->app()->oauth->scopes([ $scopes ])->redirect($redirect);
|
||||
return $this->success($response->getTargetUrl());
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取永久二维码
|
||||
* @param $uid
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getQrcode($uid)
|
||||
{
|
||||
$result = $this->app()->qrcode->forever($uid);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
|
||||
$url = $this->app()->qrcode->url($result[ 'data' ][ 'ticket' ]);
|
||||
if (isset($url[ 'errcode' ]) && $url[ 'errcode' ] != 0) {
|
||||
return $this->error($url, $url[ "errmsg" ]);
|
||||
}
|
||||
|
||||
return $this->success($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取临时二维码
|
||||
* @param $scene
|
||||
* @param $seconds
|
||||
* @return array
|
||||
*/
|
||||
public function getTempQrcode($scene, $seconds)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->qrcode->temporary($scene, $seconds);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
|
||||
$url = $this->app()->qrcode->url($result[ 'ticket' ]);
|
||||
if (isset($url[ 'errcode' ]) && $url[ 'errcode' ] != 0) {
|
||||
return $this->handleError($url[ 'errcode' ], $url[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($url);
|
||||
} catch (\Exception $e) {
|
||||
if (property_exists($e, 'body')) {
|
||||
return $this->handleError($e->body[ 'errcode' ], $e->body[ 'errmsg' ]);
|
||||
}
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取jssdk配置
|
||||
* @param $url
|
||||
* @return array
|
||||
*/
|
||||
public function getJssdkConfig($url)
|
||||
{
|
||||
try {
|
||||
$this->app()->jssdk->setUrl($url);
|
||||
$res = $this->app->jssdk->buildConfig([], false, false, false);
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
if (property_exists($e, 'body')) {
|
||||
return $this->handleError($e->body[ 'errcode' ], $e->body[ 'errmsg' ]);
|
||||
}
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************** 数据统计与分析start *******************************************************************/
|
||||
|
||||
|
||||
/********************************************************** 数据统计与分析end *******************************************************************/
|
||||
|
||||
|
||||
/*******************************************************************************微信接口连接开始*****************************************************/
|
||||
|
||||
|
||||
/*******************************************************************************微信接口连接结束*****************************************************/
|
||||
|
||||
|
||||
/***************************************************************** 微信公众号 统计 start *****************************************************************************************/
|
||||
|
||||
|
||||
/***************************************************************** 微信公众号 统计 end *****************************************************************************************/
|
||||
|
||||
|
||||
/****************************************************************************** 数据统计与分析***********************************************************/
|
||||
|
||||
/**
|
||||
* 获取用户增减数据, 最大时间跨度:7;
|
||||
* @param $from
|
||||
* @param $to
|
||||
* @return array
|
||||
*/
|
||||
public function userSummary($from, $to)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->data_cube->userSummary($from, $to);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error([], $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result[ "list" ]);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取累计用户数据, 最大时间跨度:7
|
||||
* @param $from
|
||||
* @param $to
|
||||
* @return array
|
||||
*/
|
||||
public function userCumulate($from, $to)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->data_cube->userCumulate($from, $to);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error([], $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result[ "list" ]);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口分析分时数据, 最大时间跨度:1;
|
||||
* @param $from
|
||||
* @param $to
|
||||
* @return array
|
||||
*/
|
||||
public function interfaceSummaryHourly($from, $to)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->data_cube->interfaceSummaryHourly($from, $to);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error([], $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result[ "list" ]);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口分析数据, 最大时间跨度:30;
|
||||
* @param $from
|
||||
* @param $to
|
||||
* @return array
|
||||
*/
|
||||
public function interfaceSummary($from, $to)
|
||||
{
|
||||
try {
|
||||
$result = $this->app()->data_cube->interfaceSummary($from, $to);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error([], $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result[ "list" ]);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage());
|
||||
}
|
||||
}
|
||||
/****************************************************************************** 数据统计与分析***********************************************************/
|
||||
|
||||
/****************************************************************************** 素材start***********************************************************/
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
function uploadImage($path)
|
||||
{
|
||||
$result = $this->app()->material->uploadImage($path);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传语音
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
function uploadVoice($path)
|
||||
{
|
||||
$result = $this->app()->material->uploadVoice($path);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传视频
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
function uploadVideo($path)
|
||||
{
|
||||
$result = $this->app()->material->uploadVideo($path);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传缩略图
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
function uploadThumb($path)
|
||||
{
|
||||
$result = $this->app()->material->uploadThumb($path);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图文
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadArticle($data)
|
||||
{
|
||||
$article_data = [];
|
||||
foreach ($data as $k => $v) {
|
||||
$article_data[] = new Article($v);
|
||||
}
|
||||
$result = $this->app()->material->uploadArticle($article_data);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改图文
|
||||
* @param $mediaId
|
||||
* @param string $data 文章详情
|
||||
* @param int $index 多图文中的第几篇
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function updateArticle($mediaId, $data, $index = 0)
|
||||
{
|
||||
$result = $this->app()->material->updateArticle($mediaId, $data, $index);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图文消息图片
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
*/
|
||||
public function uploadArticleImage($path)
|
||||
{
|
||||
$result = $this->app()->material->uploadArticleImage($path);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取永久素材
|
||||
* @param $mediaId
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getMaterial($mediaId)
|
||||
{
|
||||
$result = $this->app()->material->get($mediaId);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除永久素材
|
||||
* @param $mediaId
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function deleteMaterial($mediaId)
|
||||
{
|
||||
$result = $this->app()->material->delete($mediaId);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/****************************************************************************** 素材end***********************************************************/
|
||||
|
||||
|
||||
/****************************************************************************** 回复start***********************************************************/
|
||||
|
||||
/**
|
||||
* 用户事件
|
||||
* @return array
|
||||
*/
|
||||
public function relateWeixin()
|
||||
{
|
||||
$server = $this->app->server;
|
||||
$message = $server->getMessage();
|
||||
if (isset($message[ 'MsgType' ])) {
|
||||
switch ( $message[ 'MsgType' ] ) {
|
||||
case 'event':
|
||||
$this->app->server->push(function($res) {
|
||||
if ($res[ 'Event' ] == 'subscribe') {
|
||||
// 关注公众号
|
||||
$Userstr = $this->getUser($res[ 'FromUserName' ]);
|
||||
//获取用户信息
|
||||
$wechat_user = $this->app->user->get($res[ 'FromUserName' ]);
|
||||
if (preg_match("/^qrscene_/", $res[ 'EventKey' ])) {
|
||||
$source_uid = substr($res[ 'EventKey' ], 8);
|
||||
$_SESSION[ 'source_uid' ] = $source_uid;
|
||||
} elseif (!empty($_SESSION[ 'source_uid' ])) {
|
||||
$source_uid = $_SESSION[ 'source_uid' ];
|
||||
} else {
|
||||
$source_uid = 0;
|
||||
}
|
||||
|
||||
if (preg_match("/^qrscene_key_/", $res[ 'EventKey' ])) {
|
||||
//新增2021.06.02
|
||||
$key = substr($res[ 'EventKey' ], 12);
|
||||
$cache = Cache::get('wechat_' . $key);
|
||||
if (!empty($cache)) {
|
||||
Cache::set('wechat_' . $key, $wechat_user);
|
||||
}
|
||||
}
|
||||
if (preg_match("/^verify_/", $res[ 'EventKey' ])) {
|
||||
$cache = Cache::get($res[ 'EventKey' ]);
|
||||
if (empty($cache)) {
|
||||
Cache::set($res[ 'EventKey' ], $wechat_user, 7200);
|
||||
}
|
||||
}
|
||||
|
||||
$nickname_decode = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $wechat_user[ 'nickname' ]);
|
||||
$headimgurl = $wechat_user[ 'headimgurl' ];
|
||||
$sex = $wechat_user[ 'sex' ];
|
||||
$language = $wechat_user[ 'language' ];
|
||||
$country = $wechat_user[ 'country' ];
|
||||
$province = $wechat_user[ 'province' ];
|
||||
$city = $wechat_user[ 'city' ];
|
||||
$district = "无";
|
||||
$openid = $wechat_user[ 'openid' ];
|
||||
$nickname = $wechat_user[ 'nickname' ];
|
||||
if (!empty($wechat_user[ 'unionid' ])) {
|
||||
$unionid = $wechat_user[ 'unionid' ];
|
||||
} else {
|
||||
$unionid = '';
|
||||
}
|
||||
$memo = $wechat_user[ 'remark' ];
|
||||
$data = array (
|
||||
'site_id' => $this->site_id,
|
||||
'nickname' => $nickname,
|
||||
'nickname_decode' => $nickname_decode,
|
||||
'headimgurl' => $headimgurl,
|
||||
'sex' => $sex,
|
||||
'language' => $language,
|
||||
'country' => $country,
|
||||
'province' => $province,
|
||||
'city' => $city,
|
||||
'district' => $district,
|
||||
'openid' => $openid,
|
||||
'unionid' => $unionid,
|
||||
'groupid' => '',
|
||||
'is_subscribe' => 1,
|
||||
'remark' => $memo,
|
||||
'subscribe_time' => $wechat_user[ 'subscribe_time' ] ?? 0,
|
||||
'subscribe_scene' => $wechat_user[ 'subscribe_scene' ] ?? 0,
|
||||
'unsubscribe_time' => $wechat_user[ 'unsubscribe_time' ] ?? 0,
|
||||
'update_date' => time()
|
||||
);
|
||||
|
||||
$fans = new Fans();
|
||||
$fans_info = $fans->getFansInfo([ [ 'openid', '=', $openid ], [ 'site_id', '=', $this->site_id ] ]);
|
||||
if (empty($fans_info[ 'data' ])) {
|
||||
$fans->addFans($data);
|
||||
} else {
|
||||
$fans->editFans($data, [ [ 'openid', '=', $openid ], [ 'site_id', '=', $this->site_id ] ]);
|
||||
}
|
||||
|
||||
//获取关注发送消息内容
|
||||
$replay = new Replay();
|
||||
$replay_content = $replay->getWechatFollowReplay($this->site_id);
|
||||
return new Text($replay_content[ 'data' ]);
|
||||
} else if ($res[ 'Event' ] == 'unsubscribe') {
|
||||
//取消关注
|
||||
$fans = new Fans();
|
||||
$openid = $res[ 'FromUserName' ];
|
||||
$fans->unfollowWechat((string) $openid);
|
||||
} else if ($res[ 'Event' ] == 'unsubscribe') {
|
||||
//取消关注
|
||||
$fans = new Fans();
|
||||
$openid = $res[ 'FromUserName' ];
|
||||
$fans->unfollowWechat((string) $openid);
|
||||
} else if ($res[ 'Event' ] == 'SCAN') {
|
||||
// SCAN事件 - 用户已关注时的事件推送 - 扫描带参数二维码事件
|
||||
$openid = $res[ 'FromUserName' ];
|
||||
$data = $res[ 'EventKey' ];
|
||||
|
||||
if (preg_match("/^key_/", $data)) {
|
||||
$key = substr($data, 4);
|
||||
$cache = Cache::get('wechat_' . $key);
|
||||
if (!empty($cache)) {
|
||||
$wechat_user = $this->app->user->get($res[ 'FromUserName' ]);
|
||||
Cache::set('wechat_' . $key, $wechat_user);
|
||||
}
|
||||
}
|
||||
if (preg_match("/^verify_/", $res[ 'EventKey' ])) {
|
||||
$cache = Cache::get($res[ 'EventKey' ]);
|
||||
if (empty($cache)) {
|
||||
$wechat_user = $this->app->user->get($res[ 'FromUserName' ]);
|
||||
Cache::set($res[ 'EventKey' ], $wechat_user, 7200);
|
||||
}
|
||||
}
|
||||
return new Text('扫码成功。');
|
||||
} else if ($res[ 'Event' ] == 'CLICK') {
|
||||
// CLICK事件 - 自定义菜单事件
|
||||
$openid = $res[ 'FromUserName' ];
|
||||
$data = $res[ 'EventKey' ];
|
||||
|
||||
if (strpos($res[ 'EventKey' ], 'MATERIAL_GRAPHIC_MESSAGE_') === 0) {
|
||||
$material_id = substr($res[ 'EventKey' ], 25);
|
||||
$material_type = 1;
|
||||
} else if (strpos($res[ 'EventKey' ], 'MATERIAL_PICTURE_') === 0) {
|
||||
$material_id = substr($res[ 'EventKey' ], 17);
|
||||
$material_type = 2;
|
||||
} else if (strpos($res[ 'EventKey' ], 'MATERIAL_TEXT_MESSAGE_') === 0) {
|
||||
$material_id = substr($res[ 'EventKey' ], 22);
|
||||
$material_type = 5;
|
||||
}
|
||||
$material = new Material();
|
||||
$media_info = $material->getMaterialInfo([ [ 'id', '=', $material_id, 'type', '=', $material_type ] ]);
|
||||
$media_info = $media_info[ 'data' ];
|
||||
if ($media_info) {
|
||||
$value = json_decode($media_info[ 'value' ], true);
|
||||
if ($material_type == 1) {
|
||||
//图文
|
||||
$url = __ROOT__;
|
||||
$url = $url . '/index.php/wechat/api/auth/wechatArticle?id=' . $media_info[ 'id' ];
|
||||
$items = [
|
||||
new NewsItem([
|
||||
'title' => $value[ 0 ][ 'title' ],
|
||||
'description' => strip_tags($value[ 0 ][ 'content' ]),
|
||||
'url' => $url,
|
||||
'image' => $value[ 0 ][ 'cover' ][ 'path' ],
|
||||
]),
|
||||
];
|
||||
return new News($items);
|
||||
|
||||
} else if ($material_type == 2) {
|
||||
//图片
|
||||
|
||||
} else if ($material_type == 5) {
|
||||
//文字
|
||||
return new Text($value[ 'content' ]);
|
||||
}
|
||||
}
|
||||
} else if ($res[ 'Event' ] == 'weapp_audit_success') {
|
||||
// 小程序审核通过 自动发布
|
||||
if (addon_is_exit('wxoplatform')) {
|
||||
$weapp_original = $res[ 'ToUserName' ];
|
||||
$site_info = model('config')->getInfo([ [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ], [ 'value', 'like', '%is_authopen%' ], [ 'value', 'like', '%' . $weapp_original . '%' ] ], 'site_id');
|
||||
if (!empty($site_info)) {
|
||||
// 先将审核中的变更为审核成功
|
||||
model('weapp_audit_record')->update([ 'status' => 1, 'audit_time' => time() ], [ [ 'status', '=', 0 ], [ 'site_id', '=', $site_info[ 'site_id' ] ] ]);
|
||||
$platform = new OpenPlatform($site_info[ 'site_id' ]);
|
||||
$result = $platform->release();
|
||||
if ($result[ 'code' ] >= 0) {
|
||||
model('weapp_audit_record')->update([ 'status' => 5, 'release_time' => time() ], [ [ 'status', '=', 1 ], [ 'site_id', '=', $site_info[ 'site_id' ] ] ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
$response = $this->app->server->serve();
|
||||
// 将响应输出
|
||||
return $response->send();
|
||||
|
||||
break;
|
||||
case 'text':
|
||||
$this->app->server->push(function($res) {
|
||||
$replay = new Replay();
|
||||
$rule = $replay->getSiteWechatKeywordsReplay($res[ 'Content' ], $this->site_id);
|
||||
if ($rule[ 'data' ]) {
|
||||
if ($rule[ 'data' ][ 'type' ] == 'text') {
|
||||
//文字
|
||||
return new Text($rule[ 'data' ][ 'reply_content' ]);
|
||||
} else {
|
||||
|
||||
$material = new Material();
|
||||
$media_info = $material->getMaterialInfo([ [ 'media_id', '=', $rule[ 'data' ][ 'media_id' ] ] ]);
|
||||
$media_info = $media_info[ 'data' ];
|
||||
|
||||
if ($media_info) {
|
||||
$material_type = $media_info[ 'type' ];
|
||||
$value = json_decode($media_info[ 'value' ], true);
|
||||
|
||||
if ($material_type == 1) {
|
||||
$url = __ROOT__;
|
||||
$url = $url . '/index.php/wechat/api/auth/wechatArticle?id=' . $media_info[ 'id' ];
|
||||
$items = [
|
||||
new NewsItem([
|
||||
'title' => $value[ 0 ][ 'title' ],
|
||||
'description' => strip_tags($value[ 0 ][ 'content' ]),
|
||||
'url' => $url,
|
||||
'image' => $value[ 0 ][ 'cover' ][ 'path' ],
|
||||
]),
|
||||
];
|
||||
return new News($items);
|
||||
} else if ($material_type == 2) {
|
||||
//图片
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
$response = $this->app->server->serve();
|
||||
// 将响应输出
|
||||
return $response->send();
|
||||
|
||||
break;
|
||||
case 'image':
|
||||
// return '收到图片消息';
|
||||
break;
|
||||
case 'voice':
|
||||
// return '收到语音消息';
|
||||
break;
|
||||
case 'video':
|
||||
// return '收到视频消息';
|
||||
break;
|
||||
case 'location':
|
||||
//return '收到坐标消息';
|
||||
break;
|
||||
case 'link':
|
||||
//return '收到链接消息';
|
||||
break;
|
||||
case 'file':
|
||||
///return '收到文件消息';
|
||||
// ... 其它消息
|
||||
default:
|
||||
//return '收到其它消息';
|
||||
break;
|
||||
}
|
||||
}
|
||||
$response = $this->app->server->serve();
|
||||
return $response->send();
|
||||
}
|
||||
|
||||
/****************************************************************************** 回复end***********************************************************/
|
||||
|
||||
/****************************************************************************** 模板消息start ***********************************************************/
|
||||
|
||||
/**
|
||||
* 添加模板消息
|
||||
* @param $shortId
|
||||
* @param $keyword_name_list
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getTemplateId($shortId, $keyword_name_list)
|
||||
{
|
||||
try {
|
||||
$res = $this->app()->template_message->addTemplate($shortId, $keyword_name_list);
|
||||
if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) return $res;
|
||||
$errormsg = [
|
||||
'45026' => '模板数量超出限制'
|
||||
];
|
||||
return [ 'errcode' => $res[ 'errcode' ], 'errmsg' => $errormsg[ $res[ 'errcode' ] ] ?? $res[ 'errmsg' ] ];
|
||||
} catch (\Exception $e) {
|
||||
return [ 'errcode' => -1, 'errmsg' => $e->getMessage() ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除模板消息
|
||||
* @param $templateId
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function deleteTemplate($templateId)
|
||||
{
|
||||
try {
|
||||
$res = $this->app()->template_message->deletePrivateTemplate($templateId);
|
||||
if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) return $res;
|
||||
$errormsg = [
|
||||
'45026' => '模板数量超出限制'
|
||||
];
|
||||
return [ 'errcode' => $res[ 'errcode' ], 'errmsg' => $errormsg[ $res[ 'errcode' ] ] ?? $res[ 'errmsg' ] ];
|
||||
} catch (\Exception $e) {
|
||||
return [ 'errcode' => -1, 'errmsg' => $e->getMessage() ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param array $param
|
||||
* @return array
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function sendTemplateMessage(array $param)
|
||||
{
|
||||
$result = $this->app()->template_message->send([
|
||||
'touser' => $param[ 'openid' ], // openid
|
||||
'template_id' => $param[ 'template_id' ],// 模板id
|
||||
'url' => $param[ 'url' ],// 跳转链接
|
||||
'miniprogram' => $param[ 'miniprogram' ], // 跳转小程序 ['appid' => 'xxxxxxx','page' => 'pages/xxx',]
|
||||
'data' => $param[ 'data' ] // 模板变量
|
||||
]);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] != 0) {
|
||||
return $this->error($result, $result[ "errmsg" ]);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/****************************************************************************** 模板消息end***********************************************************/
|
||||
|
||||
|
||||
/******************************************************************************* 开放平台start *************************************************************************/
|
||||
/**
|
||||
* 将公众号或小程序绑定到指定开放平台帐号下
|
||||
*/
|
||||
public function bindTo($open_appid)
|
||||
{
|
||||
try {
|
||||
$res = $this->app()->account->bindTo($open_appid);
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return [ 'errcode' => -1, 'errmsg' => $e->getMessage() ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将公众号/小程序从开放平台帐号下解绑
|
||||
*/
|
||||
public function unbindFrom($open_appid)
|
||||
{
|
||||
try {
|
||||
$res = $this->app()->account->unbindFrom($open_appid);
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return [ 'errcode' => -1, 'errmsg' => $e->getMessage() ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公众号/小程序所绑定的开放平台帐号
|
||||
*/
|
||||
public function getBinding()
|
||||
{
|
||||
try {
|
||||
$res = $this->app()->account->getBinding();
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return [ 'errcode' => -1, 'errmsg' => $e->getMessage() ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取关注二维码
|
||||
* @param $fans_id
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getFollowQrcode($fans_id)
|
||||
{
|
||||
$cache = Cache::get('wechat_follow_qrcode_' . $fans_id);
|
||||
if ($cache) return $cache;
|
||||
|
||||
$res = $this->getTempQrcode('follow_key_' . $fans_id, 1728000);
|
||||
if ($res[ 'code' ] != 0) return $res;
|
||||
|
||||
$data = $this->success([
|
||||
'qrcode' => $res[ 'data' ],
|
||||
'site_id' => $this->site_id,
|
||||
'fans_id' => $fans_id
|
||||
]);
|
||||
Cache::set('wechat_follow_qrcode_' . $fans_id, $data, 1728000);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/******************************************************************************* 开放平台end *************************************************************************/
|
||||
|
||||
/**
|
||||
* 处理错误信息
|
||||
* @param $errcode
|
||||
* @param string $message
|
||||
* @return array
|
||||
*/
|
||||
public function handleError($errcode, $message = '')
|
||||
{
|
||||
$error = require 'addon/wechat/config/wechat_error.php';
|
||||
return $this->error([], $error[ $errcode ] ?? $message);
|
||||
}
|
||||
}
|
||||
256
addon/wechat/model/WechatMessage.php
Executable file
256
addon/wechat/model/WechatMessage.php
Executable file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\system\Wechat\common\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 微信公众号模板消息配置
|
||||
*/
|
||||
class WechatMessage extends BaseModel
|
||||
{
|
||||
|
||||
public $site_model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->site_model = new Site();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param $data
|
||||
*/
|
||||
public function send($param)
|
||||
{
|
||||
$message_model = new Wechat();
|
||||
$config_result = $message_model->getWechatConfigInfo($param[ "site_id" ]);
|
||||
$config_data = $config_result[ "data" ][ "value" ];
|
||||
$wechat_api = new Weixin('public');
|
||||
$wechat_api->initWechatPublicAccount($config_data[ "appid" ], $config_data[ "appsecret" ]);
|
||||
$data = array (
|
||||
"open_id" => $param[ "open_id" ],
|
||||
"template_id" => $param[ "template_id" ],
|
||||
"url" => $param[ "url" ],
|
||||
"first" => $param[ "first" ],
|
||||
"remark" => $param[ "remark" ],
|
||||
"keyword" => $param[ "keyword" ]
|
||||
);
|
||||
$res = $wechat_api->tmplmsgSend($data);
|
||||
if ($res->errcode == 0) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error($res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有当前绑定微信公众号的模板消息
|
||||
* @param $param
|
||||
* @return mixed
|
||||
*/
|
||||
public function getWechatTemplateList($param)
|
||||
{
|
||||
$config_result = $this->getWechatConfigInfo($param[ "site_id" ]);
|
||||
$config_data = $config_result[ "data" ][ "value" ];
|
||||
$wechat_api = new Weixin('public');
|
||||
$wechat_api->initWechatPublicAccount($config_data[ "appid" ], $config_data[ "appsecret" ]);
|
||||
|
||||
$res = $wechat_api->getWechatTemplateList();//获取微信消息模板
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信模板消息id
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageTemplateId($param)
|
||||
{
|
||||
$wechat_model = new Wechat();
|
||||
$config_result = $wechat_model->getWechatConfigInfo($param[ "site_id" ]);
|
||||
$config_data = $config_result[ "data" ][ "value" ];
|
||||
$wechat_api = new Weixin('public');
|
||||
$wechat_api->initWechatPublicAccount($config_data[ "appid" ], $config_data[ "appsecret" ]);
|
||||
$keyword = $param[ "keyword" ];//消息管理关键标识
|
||||
|
||||
//微信消息模板
|
||||
$message_model = new Message();
|
||||
$message_info_result = $message_model->getSiteWechatMessageInfo([ "keyword" => $keyword, "site_id" => $param[ "site_id" ] ]);
|
||||
$message_info = $message_info_result[ "data" ];
|
||||
if (empty($message_info))
|
||||
return $this->error([], "当前微信消息模板不存在!");
|
||||
|
||||
$message_type_model = new MessageModel();
|
||||
$message_type_info_result = $message_type_model->getMessageTypeInfo([ "keyword" => $keyword ]);
|
||||
$message_type_info = $message_type_info_result[ "data" ];
|
||||
if (empty($message_type_info[ "wechat_json" ]))
|
||||
return $this->error([], "当前微信消息模板不存在!");
|
||||
|
||||
$template_res = $wechat_api->getWechatTemplateId($message_type_info[ "wechat_json" ][ "template_no" ]);
|
||||
if ($template_res->errcode == 0) {
|
||||
//设置模板编号
|
||||
$res = $message_model->editSiteWechatMessage([ "template_id" => $template_res->template_id ], [ "keyword" => $keyword, "site_id" => $param[ "site_id" ] ]);
|
||||
return $res;
|
||||
} else {
|
||||
return $this->error($template_res, $template_res->errmsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置模板消息
|
||||
* @param $param
|
||||
* @return mixed
|
||||
*/
|
||||
public function resetMessage($param)
|
||||
{
|
||||
$wechat_model = new Wechat();
|
||||
$config_result = $wechat_model->getWechatConfigInfo($param[ "site_id" ]);
|
||||
$config_data = $config_result[ "data" ][ "value" ];
|
||||
$wechat_api = new Weixin('public');
|
||||
$wechat_api->initWechatPublicAccount($config_data[ "appid" ], $config_data[ "appsecret" ]);
|
||||
|
||||
$res = $wechat_api->getWechatTemplateList();//获取微信消息模板
|
||||
|
||||
//申請微信模板消息
|
||||
$message_model = new Message();
|
||||
$list = $message_model->getSiteWechatMessageList([ "site_id" => $param[ "site_id" ] ]);
|
||||
$res = error();
|
||||
if (!empty($list[ "data" ])) {
|
||||
foreach ($list[ "data" ] as $k => $v) {
|
||||
if (!empty($v[ "wechat_json" ])) {
|
||||
$temp_json = json_decode($v[ "wechat_json" ], true);
|
||||
if (!empty($temp_json[ "template_no" ])) {
|
||||
$res = $wechat_api->getWechatTemplateId($temp_json[ "template_no" ]);
|
||||
if ($res->errcode == 0) {
|
||||
//设置模板编号
|
||||
$message_model->editSiteWechatMessage([ "template_id" => $res->template_id ], [ "keyword" => $v[ "keyword" ], "site_id" => $param[ "site_id" ] ]);
|
||||
} else {
|
||||
return $this->error($res, $res->errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function sendMessage($param)
|
||||
{
|
||||
$message_model = new SiteMessage();
|
||||
$type_info = $message_model->getSiteMessageTypeViewInfo([ "keyword" => $param[ "keyword" ], "site_id" => $param[ "site_id" ] ]);
|
||||
if (( ( !empty($param[ "support_type" ]) && stripos($param[ "support_type" ], "Wechat") !== false ) || empty($param[ "support_type" ]) ) && stripos($type_info[ "data" ][ "port" ], "Wechat") !== false && $type_info[ "data" ][ "wechat_is_open" ] == 1) {
|
||||
$message_model = new Message();
|
||||
$message_info = $message_model->getSiteWechatMessageInfo([ "keyword" => $param[ "keyword" ], "site_id" => $param[ "site_id" ] ]);
|
||||
if (empty($message_info[ "data" ][ "template_id" ]))
|
||||
return $this->error();
|
||||
|
||||
if (empty($message_info[ "data" ][ "headtext" ]))
|
||||
return $this->error();
|
||||
|
||||
if (empty($message_info[ "data" ][ "bottomtext" ]))
|
||||
return $this->error();
|
||||
|
||||
//模板消息字段
|
||||
if (empty($param[ "keyword_json" ]))
|
||||
return $this->error();
|
||||
|
||||
//发送的接收用户
|
||||
if (empty($param[ "open_id" ]))
|
||||
return $this->error();
|
||||
|
||||
$url = empty($param[ "url" ]) ? '' : $param[ "url" ];
|
||||
$data = array (
|
||||
"open_id" => $param[ "open_id" ],
|
||||
"template_id" => $message_info[ "data" ][ "template_id" ],
|
||||
"url" => $url,
|
||||
"first" => $message_info[ "data" ][ "headtext" ],
|
||||
"remark" => $message_info[ "data" ][ "bottomtext" ],
|
||||
"keyword" => $param[ "keyword_json" ],
|
||||
"site_id" => $param[ "site_id" ],
|
||||
);
|
||||
//添加发送记录(未即时发送的记录 发送状态为为发送)
|
||||
$message_records_model = new MessageRecords();
|
||||
$records_data = array (
|
||||
"site_id" => $param[ "site_id" ],
|
||||
"open_id" => $param[ "open_id" ],
|
||||
"keyword_json" => json_encode($param[ "keyword_json" ]),
|
||||
"keyword" => $param[ "keyword" ],
|
||||
"url" => $url,
|
||||
"create_time" => time()
|
||||
);
|
||||
//是否即时发送
|
||||
if ($type_info[ "data" ][ "is_instant" ] == 1) {
|
||||
$result = $this->send($data);
|
||||
if ($result[ "code" ] == 0) {
|
||||
$status = 1;
|
||||
$records_data[ "send_time" ] = time();
|
||||
} else {
|
||||
$status = -1;
|
||||
$records_data[ "result" ] = $result[ "data" ]->errmsg;
|
||||
}
|
||||
} else {
|
||||
$status = 0;
|
||||
}
|
||||
$records_data[ "status" ] = $status;
|
||||
$result = $message_records_model->addWechatMessageRecords($records_data);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱消息延时发送
|
||||
* @param array $param
|
||||
*/
|
||||
public function cronMessageSend($param = [])
|
||||
{
|
||||
$message_records_model = new MessageRecords();
|
||||
$message_records_list = $message_records_model->getWechatMessageRecordsList([ "status" => 0 ]);
|
||||
if (!empty($message_records_list[ "data" ])) {
|
||||
foreach ($message_records_list[ "data" ] as $k => $v) {
|
||||
$message_model = new Message();
|
||||
$message_info = $message_model->getSiteWechatMessageInfo([ "keyword" => $param[ "keyword" ], "site_id" => $param[ "site_id" ] ]);
|
||||
|
||||
$params = array (
|
||||
"site_id" => $v[ "site_id" ],
|
||||
"open_id" => $v[ "open_id" ],
|
||||
"keyword" => json_decode($v[ "keyword_json" ], true),
|
||||
"url" => $v[ "url" ],
|
||||
"template_id" => $message_info[ "data" ][ "template_id" ],
|
||||
"first" => $message_info[ "data" ][ "headtext" ],
|
||||
"remark" => $message_info[ "data" ][ "bottomtext" ],
|
||||
);
|
||||
$result = $this->send($params);
|
||||
|
||||
$data = array ();
|
||||
$condition = array (
|
||||
"id" => $v[ "id" ],
|
||||
"site_id" => $v[ "site_id" ]
|
||||
);
|
||||
if ($result[ "code" ] == 0) {
|
||||
$data[ "send_time" ] = time();
|
||||
$data[ "status" ] = 1;
|
||||
} else {
|
||||
$data[ "status" ] = -1;
|
||||
$data[ "result" ] = $result[ "message" ];
|
||||
}
|
||||
$message_records_model->editWechatMessageRecords($data, $condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user