Data.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $push = array('type' => $type, 'id' => $id);
  101. $push = json_encode($push);
  102. $uid = -1;
  103. $to_uid = Dever::input('uid', 7);
  104. $name = Dever::input('name', 'test');
  105. $content = Dever::input('content', 'test');
  106. $result = $this->push($uid, $to_uid, $name, $content, 11, 1, 1, $push);
  107. return 'ok';
  108. }
  109. /**
  110. * 推送消息
  111. *
  112. * @return mixed
  113. */
  114. public function push($uid, $to_uid, $name, $content, $type = 11, $project = 1, $scope = false, $push = false, $id = -1)
  115. {
  116. if (!is_numeric($type)) {
  117. Dever::alert('错误的消息类型');
  118. }
  119. $config = Dever::load('message/type-one', $type);
  120. if (!$config) {
  121. Dever::alert('错误的消息类型');
  122. }
  123. if (!$content) {
  124. Dever::alert('错误的消息内容');
  125. }
  126. if (!$scope) {
  127. $scope = $config['scope'];
  128. }
  129. if ($scope == 1 && !$uid) {
  130. Dever::alert('错误的发件人id');
  131. }
  132. if ($scope == 1 && !$to_uid) {
  133. Dever::alert('错误的收件人id');
  134. }
  135. if ($scope == 2) {
  136. $uid = -1;
  137. }
  138. if (is_numeric($uid)) {
  139. if ($id <= 0) {
  140. $data['add_uid'] = $uid;
  141. $data['add_name'] = $name;
  142. $data['add_content'] = $content;
  143. $data['add_type'] = $type;
  144. $data['add_scope'] = $scope;
  145. $data['add_project_id'] = $project;
  146. $id = Dever::load('message/outbox-insert', $data);
  147. }
  148. if ($id > 0) {
  149. if ($to_uid) {
  150. if (strstr($to_uid, ',')) {
  151. $to_uid = explode(',', $to_uid);
  152. } else {
  153. $to_uid = explode("\n", $to_uid);
  154. }
  155. foreach ($to_uid as $k => $v) {
  156. $insert['add_uid'] = $v;
  157. $insert['add_oid'] = $id;
  158. $insert['add_status'] = 1;
  159. $insert['add_type'] = $type;
  160. $insert['add_scope'] = $scope;
  161. $insert['add_project_id'] = $project;
  162. $insert['add_from_uid'] = $uid;
  163. $insert['add_name'] = $name;
  164. $insert['add_content'] = $content;
  165. //$insert['add_origin'] = $origin;
  166. Dever::load('message/inbox-insert', $insert);
  167. }
  168. }
  169. //这里可以设置发送push
  170. if (isset($config['push']) && $config['push'] > 0) {
  171. if ($scope == 2) {
  172. # 发送全部
  173. $to_uid = array();
  174. }
  175. Dever::load('message/lib/push')->send($config['push'], $to_uid, $name, $content, $push);
  176. //Dever::daemon('lib/push.send?', 'message');
  177. }
  178. }
  179. }
  180. }
  181. }