Data.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.link,a.id,a.type,a.uid,a.project_id,a.scope,a.param from '.$outbox.' as a where not exists(select oid from '.$inbox.' where a.project_id = project_id and 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. $insert['add_link'] = $v['link'];
  49. $insert['add_param'] = $v['param'];
  50. Dever::load('message/inbox-insert', $insert);
  51. }
  52. }
  53. if ($project) {
  54. $param['option_project_id'] = $project;
  55. }
  56. if ($type) {
  57. $param['option_type'] = $type;
  58. }
  59. $param['option_uid'] = $uid;
  60. if ($status > 0) {
  61. $param['option_status'] = $status;
  62. return Dever::load('message/inbox-total', $param);
  63. }
  64. $data = Dever::load('message/inbox-getAll', $param);
  65. if ($update) {
  66. foreach ($data as $k => $v) {
  67. Dever::load('message/inbox-update', array('where_id' => $v['id'], 'set_status' => 2));
  68. }
  69. }
  70. return $data;
  71. }
  72. /**
  73. * 查看我的新消息
  74. *
  75. * @return mixed
  76. */
  77. public function num($uid, $project = 1, $type = false)
  78. {
  79. return $this->read($uid, $type, false, 1, $project);
  80. }
  81. /**
  82. * 查看我的消息
  83. *
  84. * @return mixed
  85. */
  86. public function view($uid, $id)
  87. {
  88. if ($id > 0) {
  89. $info = Dever::load('message/inbox-one', array('option_uid' => $uid, 'option_id' => $id));
  90. if ($info) {
  91. Dever::load('message/inbox-update', array('where_id' => $id, 'set_status' => 2));
  92. $data = Dever::load('message/inbox-one', $id);
  93. return $data;
  94. }
  95. }
  96. Dever::alert('错误的消息信息');
  97. }
  98. public function test_api()
  99. {
  100. $type = Dever::input('type', 1);
  101. $id = Dever::input('id', 1);
  102. $to_uid = Dever::input('uid', 7);
  103. $name = Dever::input('name', 'test');
  104. $content = Dever::input('content', 'test');
  105. $link = Dever::input('link');
  106. $param = array('type' => $type, 'id' => $id, 'name' => $name, 'link' => $link);
  107. $push = array(json_encode($param), 'jstyle://cn.jstyle.app/route?' . http_build_query($param));
  108. //$push = json_encode($push);
  109. $uid = -1;
  110. $result = $this->push($uid, $to_uid, $name, $content, 11, 1, 1, $push);
  111. return $result;
  112. }
  113. /**
  114. * 推送消息
  115. *
  116. * @return mixed
  117. */
  118. public function push($uid, $to_uid, $name, $content, $type = 1, $project = 1, $scope = false, $param = false, $id = -1)
  119. {
  120. if (!is_numeric($type)) {
  121. Dever::alert('错误的消息类型');
  122. }
  123. $config = Dever::load('message/type-one', $type);
  124. if (!$config) {
  125. Dever::alert('错误的消息类型');
  126. }
  127. if (!$content) {
  128. Dever::alert('错误的消息内容');
  129. }
  130. if (!$scope) {
  131. $scope = $config['scope'];
  132. }
  133. if ($scope == 1 && !$uid) {
  134. Dever::alert('错误的发件人id');
  135. }
  136. if ($scope == 1 && !$to_uid) {
  137. Dever::alert('错误的收件人id');
  138. }
  139. if ($scope == 2) {
  140. $uid = -1;
  141. }
  142. if ($param) {
  143. if (is_array($param) && isset($param[0])) {
  144. $add_param = $param[0];
  145. } elseif (is_string($param)) {
  146. $add_param = $param;
  147. }
  148. }
  149. if (is_numeric($uid)) {
  150. if ($id <= 0) {
  151. $data['add_uid'] = $uid;
  152. $data['add_name'] = $name;
  153. $data['add_content'] = $content;
  154. $data['add_type'] = $type;
  155. $data['add_scope'] = $scope;
  156. $data['add_project_id'] = $project;
  157. if (isset($add_param)) {
  158. $data['add_param'] = $add_param;
  159. }
  160. $id = Dever::load('message/outbox-insert', $data);
  161. }
  162. if ($id > 0) {
  163. if ($to_uid) {
  164. if (strstr($to_uid, ',')) {
  165. $to_uid = explode(',', $to_uid);
  166. } else {
  167. $to_uid = explode("\n", $to_uid);
  168. }
  169. foreach ($to_uid as $k => $v) {
  170. $insert['add_uid'] = $v;
  171. $insert['add_oid'] = $id;
  172. $insert['add_status'] = 1;
  173. $insert['add_type'] = $type;
  174. $insert['add_scope'] = $scope;
  175. $insert['add_project_id'] = $project;
  176. $insert['add_from_uid'] = $uid;
  177. $insert['add_name'] = $name;
  178. $insert['add_content'] = $content;
  179. //$insert['add_origin'] = $origin;
  180. if (isset($add_param)) {
  181. $insert['add_param'] = $add_param;
  182. }
  183. Dever::load('message/inbox-insert', $insert);
  184. }
  185. }
  186. //这里可以设置发送push
  187. if (isset($config['push']) && $config['push'] > 0) {
  188. if ($scope == 2) {
  189. # 发送全部
  190. $to_uid = array();
  191. }
  192. return Dever::load('message/lib/push')->send($config['push'], $to_uid, $name, $content, $param);
  193. //Dever::daemon('lib/push.send?', 'message');
  194. }
  195. }
  196. }
  197. }
  198. }