Message.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\message;
  13. use app\model\web\WebSite;
  14. use think\facade\Cache;
  15. use app\model\BaseModel;
  16. use addon\wechat\model\Wechat;
  17. /**
  18. * 消息管理类
  19. */
  20. class Message extends BaseModel
  21. {
  22. /********************************************************************* 平台消息类型 start *********************************************************************************/
  23. /**
  24. * 编辑消息
  25. * @param $data
  26. * @param $condition
  27. */
  28. function editMessage($data, $condition){
  29. $check_condition = array_column($condition, 2, 0);
  30. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  31. if ($keywords === '') {
  32. return $this->error('', 'REQUEST_KEYWORDS');
  33. }
  34. Cache::tag("message")->clear();
  35. $res = model('message')->update($data, $condition);
  36. if ($res === false) {
  37. return $this->error('', 'UNKNOW_ERROR');
  38. }
  39. return $this->success($res);
  40. }
  41. /**
  42. * 编辑邮箱是否启动
  43. * @param $is_open
  44. * @param $condition
  45. */
  46. public function modifyMessageEmailIsOpen($is_open, $condition){
  47. $check_condition = array_column($condition, 2, 0);
  48. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  49. if ($keywords === '') {
  50. return $this->error('', 'REQUEST_KEYWORDS');
  51. }
  52. Cache::tag("message")->clear();
  53. $data = array(
  54. "email_is_open" => $is_open
  55. );
  56. $res = model('message')->update($data, $condition);
  57. if ($res === false) {
  58. return $this->error('', 'UNKNOW_ERROR');
  59. }
  60. return $this->success($res);
  61. }
  62. /**
  63. * 编辑短信消息是否启动
  64. * @param $is_open
  65. * @param $condition
  66. */
  67. public function modifyMessageSmsIsOpen($is_open, $condition){
  68. $check_condition = array_column($condition, 2, 0);
  69. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  70. if ($keywords === '') {
  71. return $this->error('', 'REQUEST_KEYWORDS');
  72. }
  73. Cache::tag("message")->clear();
  74. $data = array(
  75. "sms_is_open" => $is_open
  76. );
  77. $res = model('message')->update($data, $condition);
  78. if ($res === false) {
  79. return $this->error('', 'UNKNOW_ERROR');
  80. }
  81. return $this->success($res);
  82. }
  83. /**
  84. * 编辑微信模板消息是否启动
  85. * @param $is_open
  86. * @param $condition
  87. */
  88. public function modifyMessageWechatIsOpen($is_open, $condition){
  89. $check_condition = array_column($condition, 2, 0);
  90. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  91. if ($keywords === '') {
  92. return $this->error('', 'REQUEST_KEYWORDS');
  93. }
  94. Cache::tag("message")->clear();
  95. $data = array(
  96. "wechat_is_open" => $is_open
  97. );
  98. $res = model('message')->update($data, $condition);
  99. if ($res === false) {
  100. return $this->error('', 'UNKNOW_ERROR');
  101. }
  102. return $this->success($res);
  103. }
  104. /**
  105. * 编辑微信小程序消息是否启动
  106. * @param $is_open
  107. * @param $condition
  108. */
  109. public function modifyMessageWeappIsOpen($is_open, $condition){
  110. $check_condition = array_column($condition, 2, 0);
  111. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  112. if ($keywords === '') {
  113. return $this->error('', 'REQUEST_KEYWORDS');
  114. }
  115. Cache::tag("message")->clear();
  116. $data = array(
  117. "weapp_is_open" => $is_open
  118. );
  119. $res = model('message')->update($data, $condition);
  120. if ($res === false) {
  121. return $this->error('', 'UNKNOW_ERROR');
  122. }
  123. return $this->success($res);
  124. }
  125. /**
  126. * 编辑阿里小程序消息是否启动
  127. * @param $is_open
  128. * @param $condition
  129. */
  130. public function modifyMessageAliappIsOpen($is_open, $condition){
  131. $check_condition = array_column($condition, 2, 0);
  132. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  133. if ($keywords === '') {
  134. return $this->error('', 'REQUEST_KEYWORDS');
  135. }
  136. Cache::tag("message")->clear();
  137. $data = array(
  138. "aliapp_is_open" => $is_open
  139. );
  140. $res = model('message')->update($data, $condition);
  141. if ($res === false) {
  142. return $this->error('', 'UNKNOW_ERROR');
  143. }
  144. return $this->success($res);
  145. }
  146. /**
  147. * 消息详情
  148. * @param $condition
  149. * @param string $field
  150. * @return \multitype
  151. */
  152. public function getMessageInfo($condition, $field = "*"){
  153. $check_condition = array_column($condition, 2, 0);
  154. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  155. if ($keywords === '') {
  156. return $this->error('', 'REQUEST_KEYWORDS');
  157. }
  158. $data = json_encode([ $condition, $field ]);
  159. $cache = Cache::get("message_getMessageInfo_" . $data);
  160. if (!empty($cache)) {
  161. return $this->success($cache);
  162. }
  163. $info = model("message")->getInfo($condition, $field);
  164. $info["message_json_array"] = empty($info["message_json"]) ? [] : json_decode($info["message_json"], true);//消息配置
  165. $info["sms_json_array"] = empty($info["sms_json"]) ? [] : json_decode($info["sms_json"], true);//短信配置
  166. $info["wechat_json_array"] = empty($info["wechat_json"]) ? [] : json_decode($info["wechat_json"], true);//微信公众号配置
  167. $info["weapp_json_array"] = empty($info["weapp_json"]) ? [] : json_decode($info["weapp_json"], true);//微信小程序配置
  168. $info["aliapp_json_array"] = empty($info["aliapp_json"]) ? [] : json_decode($info["aliapp_json"], true);//阿里配置
  169. Cache::tag("message")->set("message_getMessageInfo_" . $data, $info);
  170. return $this->success($info);
  171. }
  172. /**
  173. * 消息列表
  174. * @param array $condition
  175. * @param bool $field
  176. * @param string $order
  177. * @param null $limit
  178. */
  179. public function getMessageList($condition = [], $field = true, $order = '', $limit = null){
  180. $data = json_encode([ $condition, $field, $order, $limit ]);
  181. $cache = Cache::get("message_getMessageList_" . $data);
  182. if (!empty($cache)) {
  183. return $this->success($cache);
  184. }
  185. $list = model('message')->getList($condition, $field, $order, '', '', '', $limit);
  186. Cache::tag("message")->set("message_getMessageList_" . $data, $list);
  187. return $this->success($list);
  188. }
  189. /**
  190. * 消息分页列表
  191. * @param array $condition
  192. * @param int $page
  193. * @param int $page_size
  194. * @param string $order
  195. * @param string $field
  196. * @return \multitype
  197. */
  198. public function getMessagePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  199. {
  200. $data = json_encode([ $condition, $page, $page_size, $order, $field ]);
  201. $cache = Cache::get("message_getMessagePageList_" . $data);
  202. if (!empty($cache)) {
  203. return $this->success($cache);
  204. }
  205. $list = model('message')->pageList($condition, $field, $order, $page, $page_size);
  206. Cache::tag("message")->set("message_getMessagePageList_" .$data, $list);
  207. return $this->success($list);
  208. }
  209. /**
  210. * 获取微信模板消息id
  211. * @param string $keywords
  212. */
  213. public function getWechatTemplateNo(string $keywords) {
  214. $keyword = explode(',', $keywords);
  215. $list = model('message')->getList([ ['keywords', 'in', $keyword], ['wechat_json', '<>', ''] ], 'keywords,wechat_json');
  216. if (!empty($list)) {
  217. $wechat = new Wechat();
  218. foreach ($list as $item) {
  219. $template_info = json_decode($item['wechat_json'], true);
  220. $res = $wechat->getTemplateId($template_info['template_id_short']);
  221. if (isset($res['errcode']) && $res['errcode'] == 0) {
  222. $template_info['template_id'] = $res['template_id'];
  223. model('message')->update(['wechat_json' => json_encode($template_info, JSON_UNESCAPED_UNICODE )], ['keywords' => $item['keywords']]);
  224. } else {
  225. return $this->error($res, $res['errmsg']);
  226. }
  227. }
  228. }
  229. Cache::clear('message');
  230. return $this->success();
  231. }
  232. /********************************************************************* 平台消息类型 end *********************************************************************************/
  233. /**
  234. * 消息发送
  235. * @param $param
  236. */
  237. public function sendMessage($param){
  238. $keywords = $param["keywords"];
  239. $condition = [
  240. ["keywords", "=", $keywords],
  241. ];
  242. $message_info_result = $this->getMessageInfo($condition);
  243. $message_info = $message_info_result["data"];
  244. $param["message_info"] = $message_info;
  245. //查询网站信息
  246. $website_model = new WebSite();
  247. $website_info_result = $website_model->getWebSite([["site_id", "=", 0]], "title");
  248. $param["site_name"] = $website_info_result["data"]["title"];
  249. $result = event("SendMessageTemplate", $param, true);//匹配消息模板 并发送
  250. return $result;
  251. }
  252. }