MSystemPushMessage.Class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * 系统模板消息
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2021/05/07
  7. * Time: 15:22
  8. */
  9. namespace JinDouYun\Model\System;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Cache\SystemCache;
  14. use JinDouYun\Dao\System\DSystemPushMessage;
  15. use JinDouYun\Dao\System\DSystemPushMessageSetting;
  16. use Util\WeiXin\Miniprogram;
  17. use Util\WeiXin\Offiaccount;
  18. class MSystemPushMessage
  19. {
  20. private $objDSystemPushMessage;
  21. private $onlineUserId;
  22. private $onlineEnterpriseId;
  23. public function __construct($onlineUserId, $enterpriseId)
  24. {
  25. $this->onlineUserId = $onlineUserId;
  26. $this->onlineEnterpriseId = $enterpriseId;
  27. $this->objDSystemPushMessage = new DSystemPushMessage('default');
  28. }
  29. /**
  30. * 添加系统模版
  31. * @param $params
  32. * @return ResultWrapper
  33. */
  34. public function add($params)
  35. {
  36. $dbResult = $this->objDSystemPushMessage->insert($params);
  37. if ($dbResult === false) {
  38. return ResultWrapper::fail($this->objDSystemPushMessage->error(), ErrorCode::$dberror);
  39. }
  40. return ResultWrapper::success($dbResult);
  41. }
  42. /**
  43. * 推送渠道启用/停用
  44. */
  45. public function updatePushEnableStatus($params)
  46. {
  47. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  48. $id = $params['id'];
  49. unset($params['id']);
  50. $dbResult = $objDSystemPushMessageSetting->update($params, $id);
  51. if ($dbResult === false) {
  52. return ResultWrapper::fail($this->objDSystemPushMessage->error(), ErrorCode::$dberror);
  53. }
  54. return ResultWrapper::success($dbResult);
  55. }
  56. /**
  57. * 推送消息启用/停用
  58. * @param $params
  59. * @return ResultWrapper
  60. */
  61. public function updateEnableStatus($params)
  62. {
  63. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  64. $settingData = $objDSystemPushMessageSetting->get(['enterpriseId'=>$this->onlineEnterpriseId,'messageId'=>$params['id']]);
  65. if ($settingData === false) {
  66. return ResultWrapper::fail($this->$objDSystemPushMessageSetting->error(), ErrorCode::$dberror);
  67. }
  68. // 设置表如果为空,新增一条设置项
  69. if( empty($settingData) ){
  70. $settingData = [
  71. 'messageId' => $params['id'],
  72. 'enterpriseId' => $this->onlineEnterpriseId,
  73. 'enabledStatus' => StatusCode::$standard,
  74. 'createTime' => time(),
  75. ];
  76. $dbResult = $objDSystemPushMessageSetting->insert($settingData);
  77. }else{
  78. $conditon = [
  79. 'messageId'=>$params['id'],
  80. 'enterpriseId' => $this->onlineEnterpriseId,
  81. ];
  82. $dbResult = $objDSystemPushMessageSetting->update(['enabledStatus'=>$params['enabledStatus']], $conditon);
  83. }
  84. if ($dbResult === false) {
  85. return ResultWrapper::fail($this->objDSystemPushMessage->error(), ErrorCode::$dberror);
  86. }
  87. return ResultWrapper::success($dbResult);
  88. }
  89. /**
  90. * 获取系统内置推送消息模板列表
  91. * @param $selectParams
  92. * @return ResultWrapper
  93. */
  94. public function getAll($selectParams)
  95. {
  96. $limit = $selectParams['limit'];
  97. unset($selectParams['limit']);
  98. $offset = $selectParams['offset'];
  99. unset($selectParams['offset']);
  100. $where = ' where m.deleteStatus = '.StatusCode::$standard;
  101. // 消息类型
  102. if( isset($selectParams['type']) && !empty($selectParams['type']) ){
  103. $where .= ' and m.type = '.$selectParams['type'];
  104. }
  105. // 关键字
  106. if( isset($selectParams['keyword']) && !empty($selectParams['keyword']) ){
  107. $where .= ' and m.name like "%'.$selectParams['keyword'].'%"';
  108. }
  109. // 启用状态
  110. if( isset($selectParams['enabledStatus']) && !empty($selectParams['enabledStatus']) ){
  111. $where .= ' and a.enabledStatus = '.$selectParams['enabledStatus'];
  112. }
  113. $basesql = 'from qianniao_system_push_message m LEFT JOIN (select enabledStatus,messageId,weixinEnabledStatus,smsEnabledStatus,id from qianniao_system_push_message_setting where enterpriseId = '.$this->onlineEnterpriseId.') a on m.id = a.messageId';
  114. $sql = 'select m.id,m.name,IFNULL(a.enabledStatus, 4) as enabledStatus,a.weixinEnabledStatus,a.smsEnabledStatus,a.id as settingId '.$basesql.$where.' order by id desc limit '.$offset.','.$limit;
  115. $dbResult = $this->objDSystemPushMessage->query($sql);
  116. if ($dbResult === false) {
  117. return ResultWrapper::fail($this->objDSystemPushMessage->error(), ErrorCode::$dberror);
  118. }
  119. $sql = 'select count(*) as total '.$basesql.$where;
  120. $total = $this->objDSystemPushMessage->query($sql);
  121. $return = [
  122. 'data' => $dbResult,
  123. 'total' => ($total) ? intval($total[0]['total']) : 0,
  124. ];
  125. return ResultWrapper::success($return);
  126. }
  127. /**
  128. * 配置详情
  129. */
  130. public function settingDetail($id)
  131. {
  132. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  133. $sql = 'select s.*,m.weixinTemplate,m.name,m.tid,m.kidList from qianniao_system_push_message_setting s left join qianniao_system_push_message m on m.id = s.messageId where s.id = '.$id;
  134. $settingData = $objDSystemPushMessageSetting->query($sql);
  135. if ($settingData === false) {
  136. return ResultWrapper::fail($this->$objDSystemPushMessageSetting->error(), ErrorCode::$dberror);
  137. }
  138. if(empty($settingData)){
  139. return ResultWrapper::success([]);
  140. }
  141. return ResultWrapper::success($settingData);
  142. }
  143. /**
  144. * 获取对应推送模板的推送信息
  145. */
  146. public function getSettingDataByMessageIds($selectParams)
  147. {
  148. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  149. $sql = 'select s.*,m.weixinTemplate,m.name from qianniao_system_push_message_setting s left join qianniao_system_push_message m on m.id = s.messageId where s.messageId in('.implode(',', $selectParams['id']).') and s.enterpriseId = '.$this->onlineEnterpriseId.' and s.weixinEnabledStatus = '.StatusCode::$standard.' and s.enabledStatus ='.StatusCode::$standard;
  150. $settingData = $objDSystemPushMessageSetting->query($sql);
  151. if ($settingData === false) {
  152. return ResultWrapper::fail($objDSystemPushMessageSetting->error(), ErrorCode::$dberror);
  153. }
  154. if(empty($settingData)){
  155. return ResultWrapper::success([]);
  156. }
  157. return ResultWrapper::success($settingData);
  158. }
  159. /**
  160. * 编辑推送设置表
  161. * @param $params
  162. * @return ResultWrapper
  163. */
  164. public function edit($params)
  165. {
  166. $id = $params['id'];
  167. unset($params['id']);
  168. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  169. $dbResult = $objDSystemPushMessageSetting->update($params, $id);
  170. if ($dbResult === false) {
  171. return ResultWrapper::fail($objDSystemPushMessageSetting->error(), ErrorCode::$dberror);
  172. }
  173. return ResultWrapper::success($dbResult);
  174. }
  175. /**
  176. * 删除系统模版
  177. * @param $ids
  178. * @return ResultWrapper
  179. */
  180. public function del($ids)
  181. {
  182. $dbResult = $this->objDSystemPushMessage->update(['deleteStatus' => StatusCode::$delete],$ids);
  183. if ($dbResult === false) {
  184. return ResultWrapper::fail($this->objDSystemPushMessage->error(), ErrorCode::$dberror);
  185. }
  186. return ResultWrapper::success($dbResult);
  187. }
  188. /**
  189. * 自动创建微信模板消息
  190. */
  191. public function autoCreateWeiXinTemplateId($id)
  192. {
  193. $objDSystemPushMessageSetting = new DSystemPushMessageSetting();
  194. // 1.获取模板信息
  195. $pushMessageData = self::settingDetail($id);
  196. if(!$pushMessageData->isSuccess()){
  197. return ResultWrapper::fail($pushMessageData->getData(), $pushMessageData->getErrorCode());
  198. }
  199. $pushMessageData = $pushMessageData->getData();
  200. if(empty($pushMessageData)){
  201. return ResultWrapper::fail('小程序内置模板的数据为空', ErrorCode::$contentNotExists);
  202. }
  203. $pushMessageData = array_pop($pushMessageData);
  204. // 2.获取当前登录企业的小程序配置
  205. $objSystemCache = new SystemCache();
  206. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  207. if (empty($miniprogramSetting)) {
  208. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  209. }
  210. $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid
  211. // 3.获取access_token
  212. $objOffiaccount = new Offiaccount($miniprogramSetting['appid'], $miniprogramSetting['APPscrect']);
  213. $result = $objOffiaccount->token();
  214. if (!$result->isSuccess()) {
  215. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  216. }
  217. $access_token = $result->getData();
  218. // 4.创建模板id
  219. $objMiniprogram = new Miniprogram($miniprogramSetting['appid'], $miniprogramSetting['APPscrect']);
  220. $pushMessageData['kidList'] = json_decode($pushMessageData['kidList'], true);
  221. $result = $objMiniprogram->addtemplate($access_token, $pushMessageData['tid'], $pushMessageData['kidList'], $pushMessageData['name']);
  222. if(!$result->isSuccess()){
  223. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  224. }
  225. // 5.修改设置表中的模板id字段值
  226. $update = [
  227. 'weixinTemplateId' => $result->getData(),
  228. 'updateTime' => time(),
  229. ];
  230. $dbResult = $objDSystemPushMessageSetting->update($update, ['id' => $pushMessageData['id']]);
  231. if($dbResult === false){
  232. return ResultWrapper::fail($objDSystemPushMessageSetting->error(), ErrorCode::$dberror);
  233. }
  234. return ResultWrapper::success($update['weixinTemplateId']);
  235. }
  236. /**
  237. * 发送订阅消息
  238. * @param $templateMessageId
  239. * @param $openId
  240. * @param $data
  241. * @return ResultWrapper
  242. */
  243. public function sendWeiXinPushMessage($pushMessageId, $openId, $data, $page = '/pages/index/index')
  244. {
  245. // 1.获取当前登录企业的小程序配置
  246. $objSystemCache = new SystemCache();
  247. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  248. if (empty($miniprogramSetting)) {
  249. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  250. }
  251. $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid
  252. // 2.获取access_token
  253. $objOffiaccount = new Offiaccount($miniprogramSetting['appid'], $miniprogramSetting['APPscrect']);
  254. $result = $objOffiaccount->token();
  255. if (!$result->isSuccess()) {
  256. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  257. }
  258. $access_token = $result->getData();
  259. // 3.查询对应模板id
  260. $pushMessageSettingData = self::getSettingDataByMessageIds(['id'=>[$pushMessageId]]);
  261. if(!$pushMessageSettingData->isSuccess()){
  262. return ResultWrapper::fail($pushMessageSettingData->getData(), $pushMessageSettingData->getErrorCode());
  263. }
  264. $pushMessageSettingData = $pushMessageSettingData->getData();
  265. if(empty($pushMessageSettingData)){
  266. return ResultWrapper::fail('未查询到对应的推销小程序模板设置数据', ErrorCode::$configEroor);
  267. }
  268. $pushMessageSettingData = array_pop($pushMessageSettingData);
  269. if($pushMessageSettingData['enabledStatus'] == StatusCode::$delete){
  270. return ResultWrapper::fail('推送消息未启用,不推送', ErrorCode::$notAllowAccess);
  271. }
  272. $objMiniprogram = new Miniprogram($miniprogramSetting['appid'], $miniprogramSetting['APPscrect']);
  273. $result = $objMiniprogram->subscribeMessageSend($access_token, $pushMessageSettingData['weixinTemplateId'], $openId, $page, $data);
  274. if(!$result->isSuccess()){
  275. return ResultWrapper::fail('发送订阅消息报错:'.$result->getData(), $result->getErrorCode());
  276. }
  277. return ResultWrapper::success('订阅消息发送成功');
  278. }
  279. }