Data.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, $push = 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. if (strstr($to_uid, ',')) {
  138. $to_uid = explode(',', $to_uid);
  139. } else {
  140. $to_uid = explode("\n", $to_uid);
  141. }
  142. foreach ($to_uid as $k => $v) {
  143. $insert['add_uid'] = $v;
  144. $insert['add_oid'] = $id;
  145. $insert['add_status'] = 1;
  146. $insert['add_type'] = $type;
  147. $insert['add_scope'] = $scope;
  148. $insert['add_project_id'] = $project;
  149. $insert['add_from_uid'] = $uid;
  150. $insert['add_name'] = $name;
  151. $insert['add_content'] = $content;
  152. //$insert['add_origin'] = $origin;
  153. Dever::load('message/inbox-insert', $insert);
  154. }
  155. }
  156. //这里可以设置发送push
  157. if (isset($config['push']) && $config['push'] > 0) {
  158. if ($scope == 2) {
  159. # 发送全部
  160. $to_uid = array();
  161. }
  162. $this->push($config['push'], $to_uid, $name, $content, $push);
  163. }
  164. }
  165. }
  166. }
  167. private function push($id, $uid, $name, $content, $push)
  168. {
  169. $config = Dever::db('message/push')->one($id);
  170. if ($config) {
  171. # 现在就是腾讯信鸽
  172. if ($config['type'] == 1) {
  173. Dever::apply('sdk/sg', 'message');
  174. $push = new \XingeApp($config['appid'], $config['appsecret']);
  175. $mess = new \Message();
  176. $mess->setExpireTime(86400);
  177. $mess->setTitle($name);
  178. $mess->setContent($content);
  179. //$mess->setType(\Message::TYPE_MESSAGE);
  180. $mess->setType(\Message::TYPE_NOTIFICATION);
  181. $action = new \ClickAction();
  182. $action->setActionType(\ClickAction::TYPE_ACTIVITY);
  183. $action->setActivity($push);
  184. $mess->setAction($action);
  185. $total = count($uid);
  186. $prefix = 'dever_';
  187. $num = 10;
  188. if ($total <= 0) {
  189. # 发全部
  190. $ret = $push->PushAllDevices(0, $mess);
  191. } elseif ($total == 1) {
  192. # 发单条
  193. $ret = $push->PushSingleAccount(0, $prefix . $uid, $mess);
  194. } elseif ($total <= $num) {
  195. # 发列表
  196. $accountList = array_walk(
  197. $uid,
  198. function(&$value, $key, $prefix){$value = $prefix.$value;},
  199. $prefix
  200. );
  201. $ret = $push->PushAccountList(0, $accountList, $mess);
  202. } else {
  203. # 大批量发送
  204. $ret = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
  205. if (!($ret['ret_code'] === 0)) {
  206. } else {
  207. $page = intval($total/$num)+1;
  208. for ($i = 0; $i <= $page; $i++) {
  209. $array = array();
  210. for($j = 0; $j <= $i+$num; $j++) {
  211. $k = $i + $j;
  212. if (isset($uid[$k])) {
  213. $array[$k] = $prefix . $uid[$k];
  214. }
  215. }
  216. array_push($ret, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
  217. }
  218. }
  219. }
  220. return ($ret);
  221. }
  222. }
  223. }
  224. function DemoPushSingleAccountIOS()
  225. {
  226. $push = new XingeApp(000, 'secret_key');
  227. $mess = new MessageIOS();
  228. $mess->setExpireTime(86400);
  229. $mess->setAlert("ios test");
  230. //$mess->setAlert(array('key1'=>'value1'));
  231. $mess->setBadge(1);
  232. $mess->setSound("beep.wav");
  233. $custom = array('key1'=>'value1', 'key2'=>'value2');
  234. $mess->setCustom($custom);
  235. $acceptTime1 = new TimeInterval(0, 0, 23, 59);
  236. $mess->addAcceptTime($acceptTime1);
  237. $ret = $push->PushSingleAccount(0, 'joelliu', $mess, XingeApp::IOSENV_DEV);
  238. return $ret;
  239. }
  240. }