Data.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  97. * 推送消息
  98. *
  99. * @return mixed
  100. */
  101. public function push($uid, $to_uid, $name, $content, $type = 11, $project = 1, $scope = false, $id = -1)
  102. {
  103. if (!is_numeric($type)) {
  104. Dever::alert('错误的消息类型');
  105. }
  106. $config = Dever::load('message/type-one', $type);
  107. if (!$config) {
  108. Dever::alert('错误的消息类型');
  109. }
  110. if (!$content) {
  111. Dever::alert('错误的消息内容');
  112. }
  113. if (!$scope) {
  114. $scope = $config['scope'];
  115. }
  116. if ($scope == 1 && !$uid) {
  117. Dever::alert('错误的发件人id');
  118. }
  119. if ($scope == 1 && !$to_uid) {
  120. Dever::alert('错误的收件人id');
  121. }
  122. if ($scope == 2) {
  123. $uid = -1;
  124. }
  125. if (is_numeric($uid)) {
  126. if ($id <= 0) {
  127. $data['add_uid'] = $uid;
  128. $data['add_name'] = $name;
  129. $data['add_content'] = $content;
  130. $data['add_type'] = $type;
  131. $data['add_scope'] = $scope;
  132. $data['add_project_id'] = $project;
  133. $id = Dever::load('message/outbox-insert', $data);
  134. }
  135. if ($id > 0) {
  136. if ($to_uid) {
  137. $to_uid = explode(',', $to_uid);
  138. foreach ($to_uid as $k => $v) {
  139. $insert['add_uid'] = $v;
  140. $insert['add_oid'] = $id;
  141. $insert['add_status'] = 1;
  142. $insert['add_type'] = $type;
  143. $insert['add_scope'] = $scope;
  144. $insert['add_project_id'] = $project;
  145. $insert['add_from_uid'] = $uid;
  146. $insert['add_name'] = $name;
  147. $insert['add_content'] = $content;
  148. //$insert['add_origin'] = $origin;
  149. Dever::load('message/inbox-insert', $insert);
  150. }
  151. }
  152. //这里可以设置发送push
  153. if (isset($config['push']) && $config['push'] > 0) {
  154. if ($scope == 2) {
  155. # 发送全部
  156. $to_uid = -1;
  157. }
  158. $this->push($config['push'], $to_uid, $name, $content);
  159. }
  160. }
  161. }
  162. }
  163. private function push($id, $uid, $name, $content)
  164. {
  165. $config = Dever::db('message/push')->one($id);
  166. if ($config) {
  167. # 现在就是腾讯信鸽
  168. if ($config['type'] == 1) {
  169. $push = new \XingeApp($config['appid'], $config['appsecret']);
  170. $mess = new \Message();
  171. $mess->setExpireTime(86400);
  172. $mess->setTitle($name);
  173. $mess->setContent($content);
  174. $mess->setType(\Message::TYPE_MESSAGE);
  175. $uid = explode(',', $uid);
  176. $accountList = array_walk(
  177. $uid,
  178. function(&$value, $key, $prefix){$value = $prefix.$value;},
  179. 'dever_'
  180. );
  181. $ret = $push->PushAccountList(0, $accountList, $mess);
  182. return ($ret);
  183. }
  184. }
  185. }
  186. function DemoPushSingleAccountIOS()
  187. {
  188. $push = new XingeApp(000, 'secret_key');
  189. $mess = new MessageIOS();
  190. $mess->setExpireTime(86400);
  191. $mess->setAlert("ios test");
  192. //$mess->setAlert(array('key1'=>'value1'));
  193. $mess->setBadge(1);
  194. $mess->setSound("beep.wav");
  195. $custom = array('key1'=>'value1', 'key2'=>'value2');
  196. $mess->setCustom($custom);
  197. $acceptTime1 = new TimeInterval(0, 0, 23, 59);
  198. $mess->addAcceptTime($acceptTime1);
  199. $ret = $push->PushSingleAccount(0, 'joelliu', $mess, XingeApp::IOSENV_DEV);
  200. return $ret;
  201. }
  202. }