MessageTemplate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\message;
  11. use app\model\system\Site;
  12. use think\facade\Cache;
  13. use app\model\BaseModel;
  14. use addon\wechat\model\Wechat;
  15. /**
  16. * 消息管理类
  17. */
  18. class MessageTemplate extends BaseModel
  19. {
  20. /********************************************************************* 平台消息类型 start *********************************************************************************/
  21. /**
  22. * 编辑消息
  23. * @param $data
  24. * @param $condition
  25. */
  26. function editMessageTemplate($data, $condition)
  27. {
  28. $check_condition = array_column($condition, 2, 0);
  29. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  30. if ($keywords === '') {
  31. return $this->error('', 'REQUEST_KEYWORDS');
  32. }
  33. Cache::tag("message_template")->clear();
  34. $res = model('message_template')->update($data, $condition);
  35. if ($res === false) {
  36. return $this->error('', 'UNKNOW_ERROR');
  37. }
  38. return $this->success($res);
  39. }
  40. /**
  41. * 编辑邮箱是否启动
  42. * @param $is_open
  43. * @param $condition
  44. */
  45. public function modifyMessageTemplateEmailIsOpen($is_open, $condition)
  46. {
  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_template")->clear();
  53. $data = array(
  54. "email_is_open" => $is_open
  55. );
  56. $res = model('message_template')->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 modifyMessageTemplateSmsIsOpen($is_open, $condition)
  68. {
  69. $check_condition = array_column($condition, 2, 0);
  70. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  71. if ($keywords === '') {
  72. return $this->error('', 'REQUEST_KEYWORDS');
  73. }
  74. Cache::tag("message_template")->clear();
  75. $data = array(
  76. "sms_is_open" => $is_open
  77. );
  78. $res = model('message_template')->update($data, $condition);
  79. if ($res === false) {
  80. return $this->error('', 'UNKNOW_ERROR');
  81. }
  82. return $this->success($res);
  83. }
  84. /**
  85. * 编辑微信模板消息是否启动
  86. * @param $is_open
  87. * @param $condition
  88. */
  89. public function modifyMessageTemplateWechatIsOpen($is_open, $condition)
  90. {
  91. $check_condition = array_column($condition, 2, 0);
  92. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  93. if ($keywords === '') {
  94. return $this->error('', 'REQUEST_KEYWORDS');
  95. }
  96. Cache::tag("message_template")->clear();
  97. $data = array(
  98. "wechat_is_open" => $is_open
  99. );
  100. $res = model('message_template')->update($data, $condition);
  101. if ($res === false) {
  102. return $this->error('', 'UNKNOW_ERROR');
  103. }
  104. return $this->success($res);
  105. }
  106. /**
  107. * 编辑微信小程序消息是否启动
  108. * @param $is_open
  109. * @param $condition
  110. */
  111. public function modifyMessageTemplateWeappIsOpen($is_open, $condition)
  112. {
  113. $check_condition = array_column($condition, 2, 0);
  114. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  115. if ($keywords === '') {
  116. return $this->error('', 'REQUEST_KEYWORDS');
  117. }
  118. Cache::tag("message_template")->clear();
  119. $data = array(
  120. "weapp_is_open" => $is_open
  121. );
  122. $res = model('message_template')->update($data, $condition);
  123. if ($res === false) {
  124. return $this->error('', 'UNKNOW_ERROR');
  125. }
  126. return $this->success($res);
  127. }
  128. /**
  129. * 编辑阿里小程序消息是否启动
  130. * @param $is_open
  131. * @param $condition
  132. */
  133. public function modifyMessageTemplateAliappIsOpen($is_open, $condition)
  134. {
  135. $check_condition = array_column($condition, 2, 0);
  136. $keywords = isset($check_condition['keywords']) ? $check_condition['keywords'] : '';
  137. if ($keywords === '') {
  138. return $this->error('', 'REQUEST_KEYWORDS');
  139. }
  140. Cache::tag("message_template")->clear();
  141. $data = array(
  142. "aliapp_is_open" => $is_open
  143. );
  144. $res = model('message_template')->update($data, $condition);
  145. if ($res === false) {
  146. return $this->error('', 'UNKNOW_ERROR');
  147. }
  148. return $this->success($res);
  149. }
  150. /**
  151. * 消息详情
  152. * @param $condition
  153. * @param string $field
  154. * @return \multitype
  155. */
  156. public function getMessageTemplateInfo($condition, $field = "*", $ailas = 'a', $join = null)
  157. {
  158. $data = json_encode([$condition, $field, $ailas, $join]);
  159. $cache = Cache::get("message_template_getMessageTemplateInfo_" . $data);
  160. if (!empty($cache)) {
  161. return $this->success($cache);
  162. }
  163. $info = model("message_template")->getInfo($condition, $field, $ailas, $join);
  164. if (!empty($info)) {
  165. $info["message_json_array"] = empty($info["message_json"]) ? [] : json_decode($info["message_json"], true);//消息配置
  166. $info["sms_json_array"] = empty($info["sms_json"]) ? [] : json_decode($info["sms_json"], true);//短信配置
  167. $info["wechat_json_array"] = empty($info["wechat_json"]) ? [] : json_decode($info["wechat_json"], true);//微信公众号配置
  168. $info["weapp_json_array"] = empty($info["weapp_json"]) ? [] : json_decode($info["weapp_json"], true);//微信小程序配置
  169. $info["aliapp_json_array"] = empty($info["aliapp_json"]) ? [] : json_decode($info["aliapp_json"], true);//阿里配置
  170. }
  171. Cache::tag("message_template")->set("message_template_getMessageTemplateInfo_" . $data, $info);
  172. return $this->success($info);
  173. }
  174. /**
  175. * 消息列表
  176. * @param array $condition
  177. * @param bool $field
  178. * @param string $order
  179. * @param null $limit
  180. */
  181. public function getMessageTemplateList($condition = [], $field = true, $order = '', $limit = null)
  182. {
  183. $data = json_encode([$condition, $field, $order, $limit]);
  184. $cache = Cache::get("message_template_getMessageTemplateList_" . $data);
  185. if (!empty($cache)) {
  186. return $this->success($cache);
  187. }
  188. $list = model('message_template')->getList($condition, $field, $order, '', '', '', $limit);
  189. foreach ($list as $k => $v) {
  190. $list[$k]['support_type'] = explode(',', $v['support_type']);
  191. }
  192. Cache::tag("message_template")->set("message_template_getMessageTemplateList_" . $data, $list);
  193. return $this->success($list);
  194. }
  195. /**
  196. * 消息分页列表
  197. * @param array $condition
  198. * @param int $page
  199. * @param int $page_size
  200. * @param string $order
  201. * @param string $field
  202. * @return \multitype
  203. */
  204. public function getMessageTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  205. {
  206. $data = json_encode([$condition, $page, $page_size, $order, $field]);
  207. $cache = Cache::get("message_template_getMessageTemplatePageList_" . $data);
  208. if (!empty($cache)) {
  209. return $this->success($cache);
  210. }
  211. $list = model('message_template')->pageList($condition, $field, $order, $page, $page_size);
  212. Cache::tag("message_template")->set("message_template_getMessageTemplatePageList_" . $data, $list);
  213. return $this->success($list);
  214. }
  215. /**
  216. * 获取微信模板消息id
  217. * @param string $keywords
  218. */
  219. public function getWechatTemplateNo(string $keywords)
  220. {
  221. $keyword = explode(',', $keywords);
  222. $list = model('message_template')->getList([['keywords', 'in', $keyword], ['wechat_json', '<>', '']], 'keywords,wechat_json');
  223. if (!empty($list)) {
  224. $wechat = new Wechat();
  225. foreach ($list as $item) {
  226. $template_info = json_decode($item['wechat_json'], true);
  227. $res = $wechat->getTemplateId($template_info['template_id_short']);
  228. if (isset($res['errcode']) && $res['errcode'] == 0) {
  229. $template_info['template_id'] = $res['template_id'];
  230. model('message_template')->update(['wechat_json' => json_encode($template_info, JSON_UNESCAPED_UNICODE)], ['keywords' => $item['keywords']]);
  231. } else {
  232. return $this->error($res, $res['errmsg']);
  233. }
  234. }
  235. }
  236. Cache::clear('message_template');
  237. return $this->success();
  238. }
  239. /********************************************************************* 平台消息类型 end *********************************************************************************/
  240. /**
  241. * 消息发送
  242. * @param $param
  243. */
  244. public function sendMessageTemplate($param)
  245. {
  246. $keywords = $param["keywords"];
  247. $site_id = $param["site_id"];
  248. $condition = [
  249. ["mt.keywords", "=", $keywords],
  250. ["mt.site_id", "=", $site_id]
  251. ];
  252. $field = 'mt.*,m.addon, m.title, m.message_type, m.message_json, m.sms_addon, m.sms_json, m.sms_content, m.wechat_is_open, m.wechat_json, m.weapp_is_open, m.weapp_json, m.aliapp_is_open, m.aliapp_json, m.support_type';
  253. $join = [
  254. ['message_template m', 'm.keywords = mt.keywords', 'left']
  255. ];
  256. $message_template_info_result = $this->getMessageTemplateInfo($condition, $field, 'mt', $join);
  257. $message_template_info = $message_template_info_result["data"];
  258. if (empty($message_template_info)) return $this->error('', 'NOT_SET_SMS_TEMPLATE');
  259. $param["message_template_info"] = $message_template_info;
  260. //平台配置信息
  261. $site_model = new Site();
  262. $shop_info = $site_model->getSiteInfo([['site_id', '=', $site_id]], 'site_name,logo,seo_keywords,seo_description, create_time');
  263. $param["site_name"] = $shop_info["data"]["site_name"];
  264. $result = event("SendMessageTemplateTemplate", $param, true);//匹配消息模板 并发送
  265. return $result;
  266. }
  267. }