message.mod.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. /**
  8. * 更改消息提醒状态
  9. * @param $id
  10. * @return bool
  11. */
  12. function message_notice_read($id) {
  13. $id = intval($id);
  14. if (empty($id)) {
  15. return true;
  16. }
  17. table('core_message_notice_log')->fillIsRead(MESSAGE_READ)->whereId($id)->save();
  18. return true;
  19. }
  20. function message_notice_all_read($type = '') {
  21. global $_W;
  22. $message_table = table('core_message_notice_log');
  23. if (!empty($type)) {
  24. $message_table->whereType($type);
  25. }
  26. if (user_is_founder($_W['uid']) && !user_is_vice_founder($_W['uid'])) {
  27. $message_table->fillIsRead(MESSAGE_READ)->whereIsRead(MESSAGE_NOREAD)->save();
  28. return true;
  29. }
  30. $message_table->fillIsRead(MESSAGE_READ)->whereIsRead(MESSAGE_NOREAD)->whereUid($_W['uid'])->save();
  31. return true;
  32. }
  33. function message_setting($uid, $type = 0, $params = array()) {
  34. global $_W;
  35. $data = array(
  36. 'order_message' => array(
  37. 'title' => '订单消息',
  38. 'msg' => '用户购买模块,服务等,提交订单或付款后,将会有消息提醒,建议打开',
  39. 'permission' => array('founder'),
  40. 'types' => array(
  41. MESSAGE_ORDER_TYPE => array(
  42. 'title' => '提交订单',
  43. 'msg' => '用户购买模块,服务等,提交订单后,将会有消息提醒,建议打开',
  44. 'permission' => array('founder'),
  45. 'notice_data' => array(
  46. 'sign' => $params['orderid'],
  47. 'message' => sprintf(
  48. '%s ' . date('Y-m-d H:i:s') . ' 在商城订购了%s, 商品金额 %.2f元',
  49. $params['username'],
  50. $params['goods_name'],
  51. $params['money']
  52. )
  53. ),
  54. ),
  55. MESSAGE_ORDER_WISH_TYPE => array(
  56. 'title' => '提交星愿订单',
  57. 'msg' => '用户购买星愿应用,提交订单后,将会有消息提醒,建议打开',
  58. 'permission' => array(),
  59. 'notice_data' => array(
  60. 'sign' => $params['orderid'],
  61. 'message' => sprintf(
  62. '您在商城为%s订购了%s星愿应用, 商品金额 %.2f元',
  63. $params['account_name'],
  64. $params['goods_name'],
  65. $params['money']
  66. )
  67. ),
  68. ),
  69. MESSAGE_ORDER_PAY_TYPE => array(
  70. 'title' => '支付成功',
  71. 'msg' => '用户购买模块,服务等,付款后,将会有消息提醒,建议打开',
  72. 'permission' => array('founder'),
  73. 'notice_data' => array(
  74. 'sign' => $params['orderid'],
  75. 'message' => sprintf(
  76. '%s ' . date('Y-m-d H:i:s') . ' 在商城成功支付 %.2f元',
  77. $params['username'],
  78. $params['money']
  79. )
  80. ),
  81. ),
  82. MESSAGE_ORDER_APPLY_REFUND_TYPE => array(
  83. 'title' => '申请退款',
  84. 'msg' => '用户购买模块,服务等,付款后,将会有消息提醒,建议打开',
  85. 'permission' => array('founder'),
  86. 'notice_data' => array(
  87. 'sign' => $params['orderid'],
  88. 'message' => sprintf(
  89. '%s ' . date('Y-m-d H:i:s') . ' 在商城申请退款 %.2f元',
  90. $params['username'],
  91. $params['money']
  92. )
  93. ),
  94. ),
  95. )
  96. ),
  97. 'expire_message' => array(
  98. 'title' => '到期消息',
  99. 'msg' => '用户公众号,小程序到期,平台类型到期,将会有消息提醒,建议打开',
  100. 'permission' => array(),
  101. 'types' => array(
  102. MESSAGE_ACCOUNT_EXPIRE_TYPE => array(
  103. 'title' => '公众号到期',
  104. 'msg' => '用户公众号到期后,将会有消息提醒,建议打开',
  105. 'permission' => array(),
  106. 'notice_data' => array(
  107. 'sign' => $params['uniacid'],
  108. 'end_time' => $params['end_time'],
  109. 'message' => sprintf('%s-%s已过期', $params['account_name'], $params['type_name'])
  110. ),
  111. ),
  112. MESSAGE_WECHAT_EXPIRE_TYPE => array(
  113. 'title' => '小程序到期',
  114. 'msg' => '用户小程序到期后,将会有消息提醒,建议打开',
  115. 'permission' => array(),
  116. 'notice_data' => array(
  117. 'sign' => $params['uniacid'],
  118. 'end_time' => $params['end_time'],
  119. 'message' => sprintf('%s-%s已过期', $params['account_name'], $params['type_name'])
  120. ),
  121. ),
  122. MESSAGE_WEBAPP_EXPIRE_TYPE => array(
  123. 'title' => 'pc过期',
  124. 'msg' => '用户pc类型到期后,将会有消息提醒,建议打开',
  125. 'permission' => array(),
  126. 'notice_data' => array(
  127. 'sign' => $params['uniacid'],
  128. 'end_time' => $params['end_time'],
  129. 'message' => sprintf('%s-%s已过期', $params['account_name'], $params['type_name'])
  130. ),
  131. ),
  132. MESSAGE_USER_EXPIRE_TYPE => array(
  133. 'title' => '用户账号到期',
  134. 'msg' => '用户账号到期后,将会有消息提醒,建议打开',
  135. 'permission' => array(),
  136. 'notice_data' => array(
  137. 'sign' => $params['uid'],
  138. 'end_time' => $params['end_time'],
  139. 'message' => sprintf('%s 用户账号即将过期', $params['username'])
  140. ),
  141. ),
  142. )
  143. ),
  144. 'work_message' => array(
  145. 'title' => '工单提醒',
  146. 'msg' => '站点有工单消息时,将会有消息提醒,建议打开',
  147. 'permission' => array('founder'),
  148. 'types' => array(
  149. MESSAGE_WORKORDER_TYPE => array(
  150. 'title' => '新工单',
  151. 'msg' => '站点有新工时,将会有消息提醒,建议打开',
  152. 'permission' => array('founder'),
  153. 'notice_data' => array(
  154. 'sign' => $params['uuid'],
  155. 'create_time' => $params['updated_at'],
  156. 'message' => $params['note']
  157. ),
  158. ),
  159. )
  160. ),
  161. 'register_message' => array(
  162. 'title' => '注册提醒',
  163. 'msg' => '用户注册后,将会有消息提醒,建议打开',
  164. 'permission' => array('founder'),
  165. 'types' => array(
  166. MESSAGE_REGISTER_TYPE => array(
  167. 'title' => '新用户注册',
  168. 'msg' => '新用户注册后,将会有消息提醒,建议打开',
  169. 'permission' => array('founder'),
  170. 'notice_data' => array(
  171. 'sign' => $params['uid'],
  172. 'status' => $params['status'],
  173. 'message' => sprintf('%s-%s %s注册成功--%s', $params['username'], $params['type_name'], date("Y-m-d H:i:s"), $params['source'])
  174. ),
  175. ),
  176. ),
  177. ),
  178. 'upgrade_message' => array(
  179. 'title' => '升级提醒',
  180. 'msg' => '账号内应用有升级时,将通知账号主管理员,建议打开',
  181. 'permission' => array('founder'),
  182. 'types' => array(
  183. MESSAGE_WXAPP_MODULE_UPGRADE => array(
  184. 'title' => '小程序应用升级',
  185. 'msg' => '小程序的应用有升级时,将通知账号主管理员,建议打开',
  186. 'permission' => array('founder'),
  187. 'notice_data' => array(
  188. 'sign' => $params['uniacid'],
  189. 'message' => sprintf('%s小程序中的%s应用有更新', $params['account_name'], $params['module_name'])
  190. ),
  191. )
  192. )
  193. ),
  194. );
  195. if (empty($type)) {
  196. return $data;
  197. }
  198. foreach ($data as $item) {
  199. foreach ($item['types'] as $key => $row) {
  200. $types[$key] = $row;
  201. }
  202. }
  203. if (!is_numeric($type) || !in_array($type, array_keys($types))) {
  204. return error(1, '消息类型有误');
  205. }
  206. $users_table = table('users');
  207. $founder_notice_setting = $users_table->getNoticeSettingByUid($_W['config']['setting']['founder']);
  208. if (!empty($founder_notice_setting[$type]) && $founder_notice_setting[$type] == MESSAGE_DISABLE) {
  209. return error(2, '创始人未开启提醒');
  210. }
  211. if (!user_is_founder($uid, true)) {
  212. $user_notice_setting = $users_table->getNoticeSettingByUid($uid);
  213. if (!empty($user_notice_setting[$type]) && $user_notice_setting[$type] == MESSAGE_DISABLE) {
  214. return error(3, '用户未开启提醒');
  215. }
  216. }
  217. $notice_data = $types[$type]['notice_data'];
  218. $notice_data['uid'] = $uid;
  219. $notice_data['type'] = $type;
  220. $notice_data['url'] = '';
  221. return $notice_data;
  222. }
  223. /**
  224. * 消息提醒记录
  225. * @param array $message_notice_log
  226. * @return bool
  227. */
  228. function message_notice_record($uid, $type, $params) {
  229. $notice_info = message_setting($uid, $type, $params);
  230. if (is_error($notice_info)) {
  231. return $notice_info;
  232. }
  233. $message_validate_exists = message_validate_exists($notice_info);
  234. if (!empty($message_validate_exists)) {
  235. return true;
  236. }
  237. $notice_info['create_time'] = empty($notice_info['create_time']) ? TIMESTAMP : $notice_info['create_time'];
  238. $notice_info['is_read'] = empty($notice_info['is_read']) ? MESSAGE_NOREAD : $notice_info['is_read'];
  239. if (in_array($notice_info['type'], array(MESSAGE_ORDER_TYPE, MESSAGE_WORKORDER_TYPE, MESSAGE_REGISTER_TYPE))) {
  240. message_notice_record_cloud($notice_info);
  241. }
  242. table('core_message_notice_log')->fill($notice_info)->save();
  243. message_send_wechat_notice($notice_info);
  244. return true;
  245. }
  246. function message_send_wechat_notice($notice_info) {
  247. global $_W;
  248. $setting = setting_load('message_wechat_notice_setting');
  249. $setting = $setting['message_wechat_notice_setting'];
  250. if (empty($setting['uniacid'])) {
  251. return error(-1, '未设置公众号');
  252. }
  253. $uniaccount = table('account')->getUniAccountByUniacid($setting['uniacid']);
  254. if (empty($uniaccount)) {
  255. return error(-1, '帐号不存在或是已经被删除');
  256. }
  257. $account_api = WeAccount::create($uniaccount);
  258. if (is_error($account_api)) {
  259. return $account_api;
  260. }
  261. $type_template = array(
  262. MESSAGE_ORDER_TYPE => 'order',
  263. MESSAGE_ORDER_PAY_TYPE => 'order_pay',
  264. MESSAGE_ACCOUNT_EXPIRE_TYPE => 'expire',
  265. MESSAGE_WECHAT_EXPIRE_TYPE => 'expire',
  266. MESSAGE_WEBAPP_EXPIRE_TYPE => 'expire',
  267. MESSAGE_USER_EXPIRE_TYPE => 'expire',
  268. MESSAGE_WORKORDER_TYPE => 'work_order',
  269. MESSAGE_REGISTER_TYPE => 'register',
  270. MESSAGE_WXAPP_MODULE_UPGRADE => '',
  271. MESSAGE_SYSTEM_UPGRADE => '',
  272. MESSAGE_OFFICIAL_DYNAMICS => '',
  273. );
  274. if (empty($setting['template'][$type_template[$notice_info['type']]])) {
  275. return error(-1, '未设置模板ID');
  276. }
  277. if ($type_template[$notice_info['type']] == 'expire' && user_is_founder($notice_info['uid'], true)) {
  278. return error(-1, '主管理员不发送过期消息');
  279. }
  280. if ($notice_info['type'] == MESSAGE_REGISTER_TYPE) {
  281. $notice_info['uid'] = $_W['config']['setting']['founder'];
  282. }
  283. $users_bind = table('users_bind')->getByTypeAndUid(USER_REGISTER_TYPE_OPEN_WECHAT, $notice_info['uid']);
  284. if (empty($users_bind['bind_sign'])) {
  285. return error(-1, '用户未绑定微信');
  286. }
  287. $mc_mapping_fans_table = table('mc_mapping_fans');
  288. $mc_mapping_fans_table->searchWithUniacid($setting['uniacid']);
  289. $mc_mapping_fans_table->searchWithUnionid($users_bind['bind_sign']);
  290. $fans = $mc_mapping_fans_table->get();
  291. if (empty($fans['openid'])) {
  292. return error(-1, '用户未关注公众号');
  293. }
  294. $msg_data = array();
  295. switch ($notice_info['type']) {
  296. case MESSAGE_ORDER_TYPE:
  297. $order = pdo_get('site_store_order', array('id' => $notice_info['sign']));
  298. $msg_data = array(
  299. 'first' => array('value' => '您好,您的商城有新的订单!'),
  300. 'keyword1' => array('value' => $order['orderid']),
  301. 'keyword2' => array('value' => date('Y年m月d日 H:i')),
  302. 'remark' => array('value' => $notice_info['message']),
  303. );
  304. break;
  305. case MESSAGE_ORDER_PAY_TYPE:
  306. $order = pdo_get('site_store_order', array('id' => $notice_info['sign']));
  307. $msg_data = array(
  308. 'first' => array('value' => '您好,您已经成功付款!'),
  309. 'keyword1' => array('value' => '商城购买商品'),
  310. 'keyword2' => array('value' => $order['amount']),
  311. 'keyword3' => array('value' => date('Y年m月d日 H:i')),
  312. 'remark' => array('value' => '感谢您的使用!'),
  313. );
  314. break;
  315. case MESSAGE_ACCOUNT_EXPIRE_TYPE:
  316. $time = empty($notice_info['end_time']) ? TIMESTAMP : $notice_info['end_time'];
  317. $msg_data = array(
  318. 'first' => array('value' => '您好,您有过期的账号!'),
  319. 'keyword1' => array('value' => $notice_info['message']),
  320. 'keyword2' => array('value' => '公众号'),
  321. 'keyword3' => array('value' => date('Y年m月d日 H:i', $time)),
  322. 'remark' => array('value' => '感谢您的使用!'),
  323. );
  324. break;
  325. case MESSAGE_WECHAT_EXPIRE_TYPE:
  326. $time = empty($notice_info['end_time']) ? TIMESTAMP : $notice_info['end_time'];
  327. $msg_data = array(
  328. 'first' => array('value' => '您好,您有过期的账号!'),
  329. 'keyword1' => array('value' => $notice_info['message']),
  330. 'keyword2' => array('value' => '小程序'),
  331. 'keyword3' => array('value' => date('Y年m月d日 H:i', $time)),
  332. 'remark' => array('value' => '感谢您的使用!'),
  333. );
  334. break;
  335. case MESSAGE_WEBAPP_EXPIRE_TYPE:
  336. $time = empty($notice_info['end_time']) ? TIMESTAMP : $notice_info['end_time'];
  337. $msg_data = array(
  338. 'first' => array('value' => '您好,您有过期的账号!'),
  339. 'keyword1' => array('value' => $notice_info['message']),
  340. 'keyword2' => array('value' => 'PC'),
  341. 'keyword3' => array('value' => date('Y年m月d日 H:i', $time)),
  342. 'remark' => array('value' => '感谢您的使用!'),
  343. );
  344. break;
  345. case MESSAGE_USER_EXPIRE_TYPE:
  346. $msg_data = array(
  347. 'first' => array('value' => '您好,您的账号即将过期!'),
  348. 'keyword1' => array('value' => $_W['user']['username']),
  349. 'keyword2' => array('value' => '用户账号'),
  350. 'keyword3' => array('value' => date('Y年m月d日 H:i', $_W['user']['endtime'])),
  351. 'remark' => array('value' => '感谢您的使用!'),
  352. );
  353. break;
  354. case MESSAGE_WORKORDER_TYPE:
  355. $time = empty($notice_info['create_time']) ? TIMESTAMP : $notice_info['create_time'];
  356. $msg_data = array(
  357. 'first' => array('value' => '您好,您有新的工单提交!!'),
  358. 'keyword1' => array('value' => $notice_info['sign']),
  359. 'keyword2' => array('value' => $notice_info['message']),
  360. 'keyword3' => array('value' => date('Y年m月d日 H:i', $time)),
  361. 'remark' => array('value' => '感谢您的使用!'),
  362. );
  363. break;
  364. case MESSAGE_REGISTER_TYPE:
  365. $source = substr($notice_info['message'], stripos($notice_info['message'], '--')+2);
  366. $source_array = array('mobile' => '手动注册', 'system' => '手动注册', 'qq' => 'QQ 注册', 'wechat' => '微信注册', 'admin' => '管理员添加');
  367. $user = pdo_get('users', array('uid' => $notice_info['sign']));
  368. $msg_data = array(
  369. 'first' => array('value' => '您好,有新用户在站点注册!'),
  370. 'keyword1' => array('value' => $user['username']),
  371. 'keyword2' => array('value' => date('Y年m月d日 H:i')),
  372. 'keyword3' => array('value' => $source_array[$source]),
  373. 'remark' => array('value' => '感谢您的使用!'),
  374. );
  375. break;
  376. case MESSAGE_WXAPP_MODULE_UPGRADE:
  377. case MESSAGE_SYSTEM_UPGRADE:
  378. case MESSAGE_OFFICIAL_DYNAMICS:
  379. break;
  380. }
  381. return $account_api->sendTplNotice($fans['openid'], $setting['template'][$type_template[$notice_info['type']]], $msg_data);
  382. }
  383. /**
  384. * 检测消息记录是否已经插入数据库
  385. */
  386. function message_validate_exists($message) {
  387. $message_exists = table('core_message_notice_log')->messageExists($message);
  388. if (!empty($message_exists)) {
  389. return true;
  390. }
  391. return false;
  392. }
  393. /**
  394. * frame 栏目小红点消息提醒获取
  395. * @return array
  396. */
  397. function message_event_notice_list() {
  398. load()->model('user');
  399. global $_W;
  400. $message_table = table('core_message_notice_log');
  401. $message_table->searchWithIsRead(MESSAGE_NOREAD);
  402. if (user_is_founder($_W['uid'], true)) {
  403. $message_table->searchWithOutType(MESSAGE_USER_EXPIRE_TYPE);
  404. } else {
  405. $message_table->searchWithUid($_W['uid']);
  406. $message_table->searchWithType(array(
  407. MESSAGE_ACCOUNT_EXPIRE_TYPE,
  408. MESSAGE_WECHAT_EXPIRE_TYPE,
  409. MESSAGE_WEBAPP_EXPIRE_TYPE,
  410. MESSAGE_USER_EXPIRE_TYPE,
  411. MESSAGE_WXAPP_MODULE_UPGRADE,
  412. MESSAGE_SYSTEM_UPGRADE,
  413. MESSAGE_OFFICIAL_DYNAMICS
  414. ));
  415. }
  416. $message_table->searchWithPage(1, 10);
  417. $lists = $message_table->orderby('id', 'DESC')->getall();
  418. $total = $message_table->getLastQueryTotal();
  419. $lists = message_list_detail($lists);
  420. return array(
  421. 'lists' => $lists,
  422. 'total' => $total
  423. );
  424. }
  425. /**
  426. * 公众号过期记录
  427. * @return bool
  428. */
  429. function message_account_expire() {
  430. global $_W;
  431. load()->model('account');
  432. if (!pdo_tableexists('message_notice_log')) {
  433. return true;
  434. }
  435. $account_table = table('account');
  436. $expire_account_list = $account_table->searchAccountList();
  437. if (empty($expire_account_list)) {
  438. return true;
  439. }
  440. foreach ($expire_account_list as $account) {
  441. $account_detail = uni_fetch($account['uniacid']);
  442. if (empty($account_detail->owner['uid'])) {
  443. continue;
  444. }
  445. if ($account_detail['endtime'] > 0 && $account_detail['endtime'] < TIMESTAMP) {
  446. switch ($account_detail['type']) {
  447. case ACCOUNT_TYPE_APP_NORMAL:
  448. $type = MESSAGE_WECHAT_EXPIRE_TYPE;
  449. break;
  450. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  451. $type = MESSAGE_WEBAPP_EXPIRE_TYPE;
  452. break;
  453. default:
  454. $type = MESSAGE_ACCOUNT_EXPIRE_TYPE;
  455. break;
  456. }
  457. $params = array(
  458. 'uniacid' => $account_detail['uniacid'],
  459. 'end_time' => $account_detail['endtime'],
  460. 'account_name' => $account_detail['name'],
  461. 'type_name' => $account_detail->typeName,
  462. );
  463. $result = message_notice_record($account_detail->owner['uid'], $type, $params);
  464. if (is_error($result) && $result['errno'] == 3) {
  465. message_notice_record($_W['config']['setting']['founder'], $type, $params);
  466. }
  467. }
  468. }
  469. return true;
  470. }
  471. /**
  472. * 工单消息记录
  473. */
  474. function message_notice_worker() {
  475. global $_W;
  476. load()->func('communication');
  477. load()->classs('cloudapi');
  478. $api = new CloudApi();
  479. $table = table('core_message_notice_log');
  480. $time = 0;
  481. $table->searchWithType(MESSAGE_WORKORDER_TYPE);
  482. $message_record = $table->orderby('id', 'DESC')->get();
  483. if (!empty($message_record)) {
  484. $time = $message_record['create_time'];
  485. }
  486. if (!empty($time) && TIMESTAMP - $time < 60 * 60 * 6) {
  487. return true;
  488. }
  489. $api_url = $api->get('system', 'workorder', array('do' => 'notload', 'time' => $time), 'json', false);
  490. if (is_error($api_url)) {
  491. return true;
  492. }
  493. $request_url = $api_url['data']['url'];
  494. $response = ihttp_get($request_url);
  495. $uid = $_W['config']['setting']['founder'];
  496. if ($response['code'] == 200) {
  497. $content = $response['content'];
  498. $worker_notice_lists = json_decode($content, JSON_OBJECT_AS_ARRAY);
  499. if (!empty($worker_notice_lists)) {
  500. foreach ($worker_notice_lists as $list) {
  501. message_notice_record($uid, MESSAGE_WORKORDER_TYPE, array(
  502. 'uuid' => $list['uuid'],
  503. 'note' => $list['note'],
  504. 'updated_at' => $list['updated_at'],
  505. ));
  506. }
  507. }
  508. }
  509. return true;
  510. }
  511. /**
  512. * 用户到期短信提醒
  513. * @return bool
  514. */
  515. function message_sms_expire_notice() {
  516. load()->model('cloud');
  517. load()->model('setting');
  518. $setting_user_expire = setting_load('user_expire');
  519. if (empty($setting_user_expire['user_expire']['status'])) {
  520. return true;
  521. }
  522. $setting_sms_sign = setting_load('site_sms_sign');
  523. $custom_sign = !empty($setting_sms_sign['site_sms_sign']['user_expire']) ? $setting_sms_sign['site_sms_sign']['user_expire'] : '';
  524. $day = !empty($setting_user_expire['user_expire']['day']) ? $setting_user_expire['user_expire']['day'] : 1;
  525. $user_table = table('users');
  526. $user_table->searchWithMobile();
  527. $user_table->searchWithEndtime($day);
  528. $user_table->searchWithSendStatus();
  529. $user_table->searchWithViceFounder();
  530. $users_expire = $user_table->getUsersList();
  531. if (empty($users_expire)) {
  532. return true;
  533. }
  534. foreach ($users_expire as $v) {
  535. if (empty($v['puid'])) {
  536. continue;
  537. }
  538. if (!empty($v['mobile']) && preg_match(REGULAR_MOBILE, $v['mobile'])) {
  539. $result = cloud_sms_send($v['mobile'], '800015', array('username' => $v['username']), $custom_sign, true);
  540. if (is_error($result)) {
  541. $content = "您的用户名{$v['username']}即将过期。";
  542. $data = array('mobile' => $v['mobile'], 'content' => $content, 'result' => $result['errno'] . $result['message'], 'createtime' => TIMESTAMP);
  543. table('core_sendsms_log')->fill($data)->save();
  544. } else {
  545. table('users_profile')->fill('send_expire_status', 1)->whereUid($v['uid'])->save();
  546. }
  547. }
  548. }
  549. return true;
  550. }
  551. /**
  552. * 用户到期消息提醒
  553. * @return bool
  554. */
  555. function message_user_expire_notice() {
  556. global $_W;
  557. if (!empty($_W['user']['endtime']) && $_W['user']['endtime'] < strtotime('+7 days')) {
  558. $params = array(
  559. 'uid' => $_W['user']['uid'],
  560. 'username' => $_W['user']['username'],
  561. 'end_time' => $_W['user']['endtime'],
  562. );
  563. $result = message_notice_record($_W['uid'], MESSAGE_USER_EXPIRE_TYPE, $params);
  564. if (is_error($result) && $result['errno'] == 3) {
  565. message_notice_record($_W['config']['setting']['founder'], MESSAGE_USER_EXPIRE_TYPE, $params);
  566. }
  567. }
  568. return true;
  569. }
  570. /**
  571. * 把消息推送到云服务
  572. * @param $message
  573. * @return array|mixed|string
  574. */
  575. function message_notice_record_cloud($message) {
  576. load()->classs('cloudapi');
  577. $api = new CloudApi();
  578. $result = $api->post('system', 'notify', array('json' => $message), 'html', false);
  579. return $result;
  580. }
  581. /**
  582. * 小程序拥有的应用有升级时,消息通知主管理员
  583. * @return bool
  584. */
  585. function message_wxapp_modules_version_upgrade() {
  586. global $_W;
  587. load()->model('miniapp');
  588. load()->model('account');
  589. $wxapp_table = table('account');
  590. $wxapp_table->searchWithType(array(ACCOUNT_TYPE_APP_NORMAL));
  591. $uniacid_list = $wxapp_table->searchAccountList();
  592. if (empty($uniacid_list)) {
  593. return true;
  594. }
  595. $wxapp_list = table('account_wxapp')->wxappInfo(array_keys($uniacid_list));
  596. $wxapp_modules = table('modules')->getSupportWxappList();
  597. foreach ($uniacid_list as $uniacid_info) {
  598. $account_owner = account_owner($uniacid_info['uniacid']);
  599. if (empty($account_owner) || $account_owner['uid'] != $_W['uid']) {
  600. continue;
  601. }
  602. $uniacid_modules = miniapp_version_all($uniacid_info['uniacid']);
  603. if (empty($uniacid_modules[0]['modules'])) {
  604. continue;
  605. }
  606. foreach ($uniacid_modules[0]['modules'] as $module) {
  607. if ($module['version'] < $wxapp_modules[$module['mid']]['version']) {
  608. message_notice_record($_W['uid'], MESSAGE_WXAPP_MODULE_UPGRADE, array(
  609. 'uniacid' => $uniacid_info['uniacid'],
  610. 'account_name' => $wxapp_list[$uniacid_info['uniacid']]['name'],
  611. 'module_name' => $module['title'],
  612. ));
  613. }
  614. }
  615. }
  616. return true;
  617. }
  618. /**
  619. * 列表详情
  620. * @param $lists
  621. * @return mixed
  622. */
  623. function message_list_detail($lists) {
  624. if (empty($lists)) {
  625. return $lists;
  626. }
  627. foreach ($lists as &$message) {
  628. $message['create_time'] = date('Y-m-d H:i:s', $message['create_time']);
  629. if (in_array($message['type'], array(MESSAGE_ORDER_TYPE, MESSAGE_ORDER_WISH_TYPE, MESSAGE_ORDER_PAY_TYPE))) {
  630. $message['url'] = url('site/entry/orders', array('m' => 'store', 'direct'=>1, 'message_id' => $message['id']));
  631. }
  632. if ($message['type'] == MESSAGE_ACCOUNT_EXPIRE_TYPE) {
  633. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_OFFCIAL_NORMAL, 'message_id' => $message['id']));
  634. }
  635. if ($message['type'] == MESSAGE_WECHAT_EXPIRE_TYPE) {
  636. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_APP_NORMAL, 'message_id' => $message['id']));
  637. }
  638. if ($message['type'] == MESSAGE_WEBAPP_EXPIRE_TYPE) {
  639. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_WEBAPP_NORMAL, 'message_id' => $message['id']));
  640. }
  641. if ($message['type'] == MESSAGE_REGISTER_TYPE) {
  642. if ($message['status'] == USER_STATUS_CHECK) {
  643. $message['url'] = url('user/display', array('type' => 'check', 'message_id' => $message['id']));
  644. }
  645. if ($message['status'] == USER_STATUS_NORMAL) {
  646. $message['url'] = url('user/display', array('message_id' => $message['id']));
  647. }
  648. $source_array = array('mobile' => '手动注册', 'system' => '手动注册', 'qq' => 'QQ 注册', 'wechat' => '微信注册', 'admin' => '管理员添加');
  649. $msg = explode('--', $message['message']);
  650. if (count($msg) > 1 && !empty($source_array[$msg[1]])) {
  651. $message['message'] = $msg[0];
  652. $message['source'] = $source_array[$msg[1]];
  653. }
  654. }
  655. if ($message['type'] == MESSAGE_USER_EXPIRE_TYPE) {
  656. $message['url'] = url('user/profile', array('message_id' => $message['id']));
  657. }
  658. if ($message['type'] == MESSAGE_WXAPP_MODULE_UPGRADE) {
  659. $message['url'] = url('message/notice', array('message_id' => $message['id']));
  660. }
  661. if ($message['type'] == MESSAGE_WORKORDER_TYPE) {
  662. $message['url'] = url('system/workorder/display', array('uuid' => $message['sign'], 'message_id' => $message['id']));
  663. }
  664. }
  665. return $lists;
  666. }
  667. function message_store_notice() {
  668. $cachekey = cache_system_key('cloud_ad_store_notice');
  669. $cache = cache_load($cachekey);
  670. if (!empty($cache['expire']) && $cache['expire'] > TIMESTAMP) {
  671. return true;
  672. }
  673. load()->model('cloud');
  674. $data = cloud_get_store_notice();
  675. if (is_error($data)) {
  676. return $data;
  677. }
  678. $insert_data = array();
  679. $signs = array();
  680. $create_time = array();
  681. foreach ($data['version'] as $item) {
  682. $signs[] = $item['itemid'];
  683. $create_time[] = $item['datetime'];
  684. $insert_data[] = array(
  685. 'sign' => $item['itemid'],
  686. 'message' => $item['title'],
  687. 'url' => $item['url'],
  688. 'create_time' => $item['datetime'],
  689. 'type' => MESSAGE_SYSTEM_UPGRADE,
  690. 'is_read' => MESSAGE_NOREAD,
  691. );
  692. }
  693. foreach ($data['info'] as $item) {
  694. $signs[] = $item['itemid'];
  695. $create_time[] = $item['datetime'];
  696. $insert_data[] = array(
  697. 'sign' => $item['itemid'],
  698. 'message' => $item['title'],
  699. 'url' => $item['url'],
  700. 'create_time' => $item['datetime'],
  701. 'type' => MESSAGE_OFFICIAL_DYNAMICS,
  702. 'is_read' => MESSAGE_NOREAD,
  703. );
  704. }
  705. if (!empty($signs)) {
  706. array_multisort($create_time, SORT_ASC, SORT_NUMERIC, $insert_data);
  707. $signs = pdo_getall('message_notice_log', array('sign' => $signs), array('sign'), 'sign');
  708. $signs = array_keys($signs);
  709. foreach ($insert_data as $item) {
  710. if (!in_array($item['sign'], $signs)) {
  711. pdo_insert('message_notice_log', $item);
  712. }
  713. }
  714. }
  715. cache_write($cachekey, array('expire' => TIMESTAMP + 3600));
  716. return true;
  717. }