Inform.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 通用模板消息
  7. */
  8. namespace app\common\facade\library;
  9. use app\common\facade\WechatProgram;
  10. use app\common\model\SystemMemberWechatTpl;
  11. use app\common\model\SystemMemberMiniapp;
  12. use app\common\model\SystemMemberSmsQueue;
  13. use app\common\model\SystemUser;
  14. use Exception;
  15. class Inform{
  16. /**
  17. * 通用通知
  18. * @param int $uid 用户ID
  19. * @param int $miniapp_id 小程序ID
  20. * @param array $param
  21. * $param['title'] = 标题;
  22. * $param['content'] = 内容;
  23. * $param['type'] = 类型;
  24. * $param['state'] = 状态;
  25. * $param['remark'] = 备注;
  26. * @return void
  27. */
  28. public function sms(int $uid,int $miniapp_id,array $param){
  29. if(empty($uid) || empty($param['title']) ||empty($param['content'])){
  30. return;
  31. }
  32. return SystemMemberSmsQueue::create(['uid' => $uid,'member_miniapp_id' => $miniapp_id,'param' => json_encode($param),'is_send' => 0,'create_time' => time()]);
  33. }
  34. /**
  35. * 群发队列服务
  36. */
  37. public function smsQueue(){
  38. $info = SystemMemberSmsQueue::where(['is_send' => 0])->find();
  39. if(empty($info)){
  40. return;
  41. }
  42. $miniapp_id = $info->member_miniapp_id;
  43. $uid = $info->uid;
  44. $param = json_decode($info->param,true);
  45. $title = $param['title']; //通知标题
  46. $content = $param['content']; //业务内容
  47. $type = empty($param['type']) ? '申请' : $param['type']; //业务类型
  48. $state = empty($param['state']) ? '待审' : $param['state']; //状态
  49. $remark = empty($param['remark']) ? '如要疑问请咨询官方客服' : $param['remark']; //备注
  50. $url = empty($param['url']) ? 'pages/index/index' : $param['url']; //访问地址
  51. $miniapp = SystemMemberMiniapp::where(['id' => $miniapp_id])->field('id,mp_appid,miniapp_appid')->find();
  52. if(empty($miniapp)){
  53. return;
  54. }
  55. $user = SystemUser::where(['id' => $uid])->field('official_uid')->find();
  56. if (empty($user->official_uid)) {
  57. SystemMemberSmsQueue::where(['id' => $info->id])->update(['is_send' => 1]);
  58. return;
  59. }
  60. //用户类别
  61. $wechat = WechatProgram::isTypes($miniapp->id);
  62. if (empty($wechat)) {
  63. return;
  64. }
  65. $setting = SystemMemberWechatTpl::getConfig($miniapp->id);
  66. if (empty($setting)) {
  67. return;
  68. }
  69. //小程序模板消息
  70. $weapp_template_msg = [];
  71. //公众号消息
  72. if(!empty($miniapp->mp_appid) || !empty($setting->tplmsg_common_wechat)){
  73. try {
  74. $rel = $wechat->uniform_message->send([
  75. 'touser' => $user->official_uid,
  76. 'weapp_template_msg' => $weapp_template_msg,
  77. 'mp_template_msg' => [
  78. 'appid' => $miniapp->mp_appid,
  79. 'template_id' => $setting->tplmsg_common_wechat,
  80. 'url' => $url,
  81. 'miniprogram' => [
  82. 'pagepath' => $url,
  83. 'appid' => $miniapp->miniapp_appid
  84. ],
  85. 'data' => [
  86. 'first' => $title,
  87. 'keyword1' => $type, //业务类型
  88. 'keyword2' => $content, //业务内容
  89. 'keyword3' => $state, //处理结果
  90. 'keyword4' => date('Y-m-d H:i:s'), //时间
  91. 'keyword5' => $remark
  92. ],
  93. ],
  94. ]);
  95. if($rel['errcode'] == 0){
  96. SystemMemberSmsQueue::where(['id' => $info->id])->update(['is_send' => 1]);
  97. return true;
  98. }
  99. }catch (Exception $e) {
  100. return;
  101. }
  102. }
  103. }
  104. }