123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com.cn
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
- * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
- * =========================================================
- */
- namespace app\model\message;
- use app\model\web\WebSite;
- use think\facade\Cache;
- use app\model\BaseModel;
- use addon\wechat\model\Wechat;
- /**
- * 消息管理类
- */
- class Message extends BaseModel
- {
-
- /********************************************************************* 平台消息类型 start *********************************************************************************/
- /**
- * 编辑消息
- * @param $data
- * @param $condition
- */
- function editMessage($data, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 编辑邮箱是否启动
- * @param $is_open
- * @param $condition
- */
- public function modifyMessageEmailIsOpen($is_open, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $data = array(
- "email_is_open" => $is_open
- );
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 编辑短信消息是否启动
- * @param $is_open
- * @param $condition
- */
- public function modifyMessageSmsIsOpen($is_open, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $data = array(
- "sms_is_open" => $is_open
- );
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 编辑微信模板消息是否启动
- * @param $is_open
- * @param $condition
- */
- public function modifyMessageWechatIsOpen($is_open, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $data = array(
- "wechat_is_open" => $is_open
- );
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 编辑微信小程序消息是否启动
- * @param $is_open
- * @param $condition
- */
- public function modifyMessageWeappIsOpen($is_open, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $data = array(
- "weapp_is_open" => $is_open
- );
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 编辑阿里小程序消息是否启动
- * @param $is_open
- * @param $condition
- */
- public function modifyMessageAliappIsOpen($is_open, $condition){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- Cache::tag("message")->clear();
- $data = array(
- "aliapp_is_open" => $is_open
- );
- $res = model('message')->update($data, $condition);
- if ($res === false) {
- return $this->error('', 'UNKNOW_ERROR');
- }
- return $this->success($res);
- }
- /**
- * 消息详情
- * @param $condition
- * @param string $field
- * @return \multitype
- */
- public function getMessageInfo($condition, $field = "*"){
- $check_condition = array_column($condition, 2, 0);
- $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
- if ($keywords === '') {
- return $this->error('', 'REQUEST_KEYWORDS');
- }
- $data = json_encode([ $condition, $field ]);
- $cache = Cache::get("message_getMessageInfo_" . $data);
- if (!empty($cache)) {
- return $this->success($cache);
- }
- $info = model("message")->getInfo($condition, $field);
- $info["message_json_array"] = empty($info["message_json"]) ? [] : json_decode($info["message_json"], true);//消息配置
- $info["sms_json_array"] = empty($info["sms_json"]) ? [] : json_decode($info["sms_json"], true);//短信配置
- $info["wechat_json_array"] = empty($info["wechat_json"]) ? [] : json_decode($info["wechat_json"], true);//微信公众号配置
- $info["weapp_json_array"] = empty($info["weapp_json"]) ? [] : json_decode($info["weapp_json"], true);//微信小程序配置
- $info["aliapp_json_array"] = empty($info["aliapp_json"]) ? [] : json_decode($info["aliapp_json"], true);//阿里配置
- Cache::tag("message")->set("message_getMessageInfo_" . $data, $info);
- return $this->success($info);
- }
- /**
- * 消息列表
- * @param array $condition
- * @param bool $field
- * @param string $order
- * @param null $limit
- */
- public function getMessageList($condition = [], $field = true, $order = '', $limit = null){
- $data = json_encode([ $condition, $field, $order, $limit ]);
- $cache = Cache::get("message_getMessageList_" . $data);
- if (!empty($cache)) {
- return $this->success($cache);
- }
- $list = model('message')->getList($condition, $field, $order, '', '', '', $limit);
- Cache::tag("message")->set("message_getMessageList_" . $data, $list);
- return $this->success($list);
- }
- /**
- * 消息分页列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @return \multitype
- */
- public function getMessagePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
- {
- $data = json_encode([ $condition, $page, $page_size, $order, $field ]);
- $cache = Cache::get("message_getMessagePageList_" . $data);
- if (!empty($cache)) {
- return $this->success($cache);
- }
- $list = model('message')->pageList($condition, $field, $order, $page, $page_size);
- Cache::tag("message")->set("message_getMessagePageList_" .$data, $list);
- return $this->success($list);
- }
-
- /**
- * 获取微信模板消息id
- * @param string $keywords
- */
- public function getWechatTemplateNo(string $keywords) {
- $keyword = explode(',', $keywords);
- $list = model('message')->getList([ ['keywords', 'in', $keyword], ['wechat_json', '<>', ''] ], 'keywords,wechat_json');
- if (!empty($list)) {
- $wechat = new Wechat();
- foreach ($list as $item) {
- $template_info = json_decode($item['wechat_json'], true);
- $res = $wechat->getTemplateId($template_info['template_id_short']);
- if (isset($res['errcode']) && $res['errcode'] == 0) {
- $template_info['template_id'] = $res['template_id'];
- model('message')->update(['wechat_json' => json_encode($template_info, JSON_UNESCAPED_UNICODE )], ['keywords' => $item['keywords']]);
- } else {
- return $this->error($res, $res['errmsg']);
- }
- }
- }
- Cache::clear('message');
- return $this->success();
- }
- /********************************************************************* 平台消息类型 end *********************************************************************************/
- /**
- * 消息发送
- * @param $param
- */
- public function sendMessage($param){
- $keywords = $param["keywords"];
- $condition = [
- ["keywords", "=", $keywords],
- ];
- $message_info_result = $this->getMessageInfo($condition);
- $message_info = $message_info_result["data"];
- $param["message_info"] = $message_info;
- //查询网站信息
- $website_model = new WebSite();
- $website_info_result = $website_model->getWebSite([["site_id", "=", 0]], "title");
- $param["site_name"] = $website_info_result["data"]["title"];
- $result = event("SendMessageTemplate", $param, true);//匹配消息模板 并发送
- return $result;
- }
- }
|