Data.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Message\Lib;
  3. use Dever;
  4. //use Passport\Src\User;
  5. //use Passport\Src\Login;
  6. class Data
  7. {
  8. public function __construct()
  9. {
  10. # 获取用户信息
  11. //$user = new User();
  12. //$this->user = $user->data();
  13. }
  14. /**
  15. * 获取我的消息
  16. *
  17. * @return mixed
  18. */
  19. public function read($uid, $type = false, $update = false, $status = false, $project = 1)
  20. {
  21. if (!$uid) {
  22. Dever::alert('错误的用户信息');
  23. }
  24. $prefix = defined('DEVER_PROJECT') && DEVER_PROJECT != 'default' ? DEVER_PROJECT . '_' : '';
  25. $outbox = $prefix . 'message_outbox';
  26. $inbox = $prefix . 'message_inbox';
  27. $where = ' and a.state = 1 and a.project_id = ' . $project;
  28. # 去掉a.type判断
  29. $where .= ' and a.scope = 2';
  30. # 这个是兼容历史版本
  31. //$where .= ' (and a.type <= 10 || a.scope = 2)';
  32. # 读取outbox里的数据
  33. $sql = 'select a.name,a.content,a.id,a.type,a.uid,a.project_id,a.scope from '.$outbox.' as a where not exists(select oid from '.$inbox.' where a.id = oid and uid = '.$uid.')' . $where . ' ';
  34. $state = Dever::db('message/inbox')->query($sql);
  35. $outbox = $state->fetchAll();
  36. if ($outbox) {
  37. foreach ($outbox as $k => $v) {
  38. $insert['add_uid'] = $uid;
  39. $insert['add_oid'] = $v['id'];
  40. $insert['add_status'] = 1;
  41. $insert['add_project_id'] = $v['project_id'];
  42. $insert['add_type'] = $v['type'];
  43. //$insert['add_site'] = $v['site'];
  44. $insert['add_from_uid'] = $v['uid'];
  45. $insert['add_name'] = $v['name'];
  46. $insert['add_scope'] = $v['scope'];
  47. $insert['add_content'] = $v['content'];
  48. Dever::load('message/inbox-insert', $insert);
  49. }
  50. }
  51. if ($project) {
  52. $param['option_project_id'] = $project;
  53. }
  54. if ($type) {
  55. $param['option_type'] = $type;
  56. }
  57. $param['option_uid'] = $uid;
  58. if ($status > 0) {
  59. $param['option_status'] = $status;
  60. return Dever::load('message/inbox-total', $param);
  61. }
  62. $data = Dever::load('message/inbox-getAll', $param);
  63. if ($update) {
  64. foreach ($data as $k => $v) {
  65. Dever::load('message/inbox-update', array('where_id' => $v['id'], 'set_status' => 2));
  66. }
  67. }
  68. return $data;
  69. }
  70. /**
  71. * 查看我的新消息
  72. *
  73. * @return mixed
  74. */
  75. public function num($uid, $project = 1)
  76. {
  77. return $this->read($uid, false, false, 1, $project);
  78. }
  79. /**
  80. * 查看我的消息
  81. *
  82. * @return mixed
  83. */
  84. public function view($uid, $id)
  85. {
  86. if ($id > 0) {
  87. $info = Dever::load('message/inbox-one', array('option_uid' => $uid, 'option_id' => $id));
  88. if ($info) {
  89. Dever::load('message/inbox-update', array('where_id' => $id, 'set_status' => 2));
  90. $data = Dever::load('message/inbox-one', $id);
  91. return $data;
  92. }
  93. }
  94. Dever::alert('错误的消息信息');
  95. }
  96. public function test_api()
  97. {
  98. $type = Dever::input('type', 1);
  99. $id = Dever::input('id', 1);
  100. $to_uid = Dever::input('uid', 7);
  101. $name = Dever::input('name', 'test');
  102. $content = Dever::input('content', 'test');
  103. $link = Dever::input('link');
  104. $param = array('type' => $type, 'id' => $id, 'name' => $name, 'link' => $link);
  105. $push = array('jstyle://cn.jstyle.app/route?' . http_build_query($param), json_encode($param));
  106. //$push = json_encode($push);
  107. $uid = -1;
  108. $result = $this->push($uid, $to_uid, $name, $content, 11, 1, 1, $push);
  109. return $result;
  110. }
  111. /**
  112. * 推送消息
  113. *
  114. * @return mixed
  115. */
  116. public function push($uid, $to_uid, $name, $content, $type = 11, $project = 1, $scope = false, $push = false, $id = -1)
  117. {
  118. if (!is_numeric($type)) {
  119. Dever::alert('错误的消息类型');
  120. }
  121. $config = Dever::load('message/type-one', $type);
  122. if (!$config) {
  123. Dever::alert('错误的消息类型');
  124. }
  125. if (!$content) {
  126. Dever::alert('错误的消息内容');
  127. }
  128. if (!$scope) {
  129. $scope = $config['scope'];
  130. }
  131. if ($scope == 1 && !$uid) {
  132. Dever::alert('错误的发件人id');
  133. }
  134. if ($scope == 1 && !$to_uid) {
  135. Dever::alert('错误的收件人id');
  136. }
  137. if ($scope == 2) {
  138. $uid = -1;
  139. }
  140. if (is_numeric($uid)) {
  141. if ($id <= 0) {
  142. $data['add_uid'] = $uid;
  143. $data['add_name'] = $name;
  144. $data['add_content'] = $content;
  145. $data['add_type'] = $type;
  146. $data['add_scope'] = $scope;
  147. $data['add_project_id'] = $project;
  148. $id = Dever::load('message/outbox-insert', $data);
  149. }
  150. if ($id > 0) {
  151. if ($to_uid) {
  152. if (strstr($to_uid, ',')) {
  153. $to_uid = explode(',', $to_uid);
  154. } else {
  155. $to_uid = explode("\n", $to_uid);
  156. }
  157. foreach ($to_uid as $k => $v) {
  158. $insert['add_uid'] = $v;
  159. $insert['add_oid'] = $id;
  160. $insert['add_status'] = 1;
  161. $insert['add_type'] = $type;
  162. $insert['add_scope'] = $scope;
  163. $insert['add_project_id'] = $project;
  164. $insert['add_from_uid'] = $uid;
  165. $insert['add_name'] = $name;
  166. $insert['add_content'] = $content;
  167. //$insert['add_origin'] = $origin;
  168. Dever::load('message/inbox-insert', $insert);
  169. }
  170. }
  171. //这里可以设置发送push
  172. if (isset($config['push']) && $config['push'] > 0) {
  173. if ($scope == 2) {
  174. # 发送全部
  175. $to_uid = array();
  176. }
  177. return Dever::load('message/lib/push')->send($config['push'], $to_uid, $name, $content, $push);
  178. //Dever::daemon('lib/push.send?', 'message');
  179. }
  180. }
  181. }
  182. }
  183. }