人人商城

message.mod.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. function message_notice_read($id) {
  8. $id = intval($id);
  9. if (empty($id)) {
  10. return true;
  11. }
  12. pdo_update('message_notice_log', array('is_read' => MESSAGE_READ), array('id' => $id));
  13. return true;
  14. }
  15. function message_notice_all_read($type = '') {
  16. global $_W;
  17. $message_table = table('message');
  18. if (!empty($type)) {
  19. $message_table->whereType($type);
  20. }
  21. if (user_is_founder($_W['uid']) && !user_is_vice_founder($_W['uid'])) {
  22. $message_table->fillIsRead(MESSAGE_READ)->whereIsRead(MESSAGE_NOREAD)->save();
  23. return true;
  24. }
  25. $message_table->fillIsRead(MESSAGE_READ)->whereIsRead(MESSAGE_NOREAD)->whereUid($_W['uid'])->save();
  26. return true;
  27. }
  28. function message_notice_record($content, $uid, $sign, $type, $extend_message = array()) {
  29. $message['message'] = $content;
  30. $message['uid'] = $uid;
  31. $message['sign'] = $sign;
  32. $message['type'] = $type;
  33. $message_notice_log = array_merge($message, $extend_message);
  34. $message_exists = message_validate_exists($message_notice_log);
  35. if (!empty($message_exists)) {
  36. return true;
  37. }
  38. if (empty($message_notice_log['create_time'])) {
  39. $message_notice_log['create_time'] = TIMESTAMP;
  40. }
  41. if (empty($message_notice_log['is_read'])) {
  42. $message_notice_log['is_read'] = MESSAGE_NOREAD;
  43. }
  44. $push_cloud_message_type = array(MESSAGE_ORDER_TYPE, MESSAGE_WORKORDER_TYPE, MESSAGE_REGISTER_TYPE);
  45. if (in_array($type, $push_cloud_message_type)) {
  46. message_notice_record_cloud($message_notice_log);
  47. }
  48. pdo_insert('message_notice_log', $message_notice_log);
  49. return true;
  50. }
  51. function message_validate_exists($message) {
  52. $message_exists = pdo_get('message_notice_log', $message);
  53. if (!empty($message_exists)) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. function message_event_notice_list() {
  59. load()->model('user');
  60. global $_W;
  61. $message_table = table('message');
  62. $message_table->searchWithIsRead(MESSAGE_NOREAD);
  63. $message_table->searchWithType(array(MESSAGE_SYSTEM_UPGRADE, MESSAGE_OFFICIAL_DYNAMICS));
  64. $type = '';
  65. if (user_is_vice_founder() || !user_is_founder($_W['uid'])) {
  66. $type = array(MESSAGE_ACCOUNT_EXPIRE_TYPE, MESSAGE_WECHAT_EXPIRE_TYPE, MESSAGE_USER_EXPIRE_TYPE, MESSAGE_WEBAPP_EXPIRE_TYPE, MESSAGE_WXAPP_MODULE_UPGRADE);
  67. $message_table->searchWithType($type);
  68. }
  69. $message_table->searchWithPage(1, 10);
  70. $lists = $message_table->messageList();
  71. $message_table->searchWithIsRead(MESSAGE_NOREAD);
  72. if (user_is_vice_founder() || !user_is_founder($_W['uid'])) {
  73. $message_table->searchWithType($type);
  74. }
  75. $total = $message_table->messageNoReadCount();
  76. $lists = message_list_detail($lists);
  77. return array(
  78. 'lists' => $lists,
  79. 'total' => $total
  80. );
  81. }
  82. function message_account_expire() {
  83. load()->model('account');
  84. if (!pdo_tableexists('message_notice_log')) {
  85. return true;
  86. }
  87. $account_table = table('account');
  88. $expire_account_list = $account_table->searchAccountList();
  89. if (empty($expire_account_list)) {
  90. return true;
  91. }
  92. foreach ($expire_account_list as $account) {
  93. $account_detail = uni_fetch($account['uniacid']);
  94. if (empty($account_detail['uid'])) {
  95. continue;
  96. }
  97. if ($account_detail['endtime'] > 0 && $account_detail['endtime'] < TIMESTAMP) {
  98. switch ($account_detail['type']) {
  99. case ACCOUNT_TYPE_APP_NORMAL:
  100. $type = MESSAGE_WECHAT_EXPIRE_TYPE;
  101. $account_name = '-小程序过期';
  102. break;
  103. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  104. $type = MESSAGE_WEBAPP_EXPIRE_TYPE;
  105. $account_name = '-pc过期';
  106. break;
  107. default:
  108. $type = MESSAGE_ACCOUNT_EXPIRE_TYPE;
  109. $account_name = '-公众号过期';
  110. break;
  111. }
  112. $message = array(
  113. 'end_time' => $account_detail['endtime']
  114. );
  115. message_notice_record($account_detail['name'] . $account_name, $account_detail['uid'], $account['uniacid'], $type, $message);
  116. }
  117. }
  118. return true;
  119. }
  120. function message_notice_worker() {
  121. global $_W;
  122. load()->func('communication');
  123. load()->classs('cloudapi');
  124. $api = new CloudApi();
  125. $table = table('message');
  126. $time = 0;
  127. $table->searchWithType(MESSAGE_WORKORDER_TYPE);
  128. $message_record = $table->messageRecord();
  129. if (!empty($message_record)) {
  130. $time = $message_record['create_time'];
  131. }
  132. if (!empty($time) && TIMESTAMP - $time < 60 * 60 * 6) {
  133. return true;
  134. }
  135. $api_url = $api->get('system', 'workorder', array('do' => 'notload', 'time' => $time), 'json', false);
  136. if (is_error($api_url)) {
  137. return true;
  138. }
  139. $request_url = $api_url['data']['url'];
  140. $response = ihttp_get($request_url);
  141. $uid = $_W['config']['setting']['founder'];
  142. if ($response['code'] == 200) {
  143. $content = $response['content'];
  144. $worker_notice_lists = json_decode($content, JSON_OBJECT_AS_ARRAY);
  145. if (!empty($worker_notice_lists)) {
  146. foreach ($worker_notice_lists as $list) {
  147. message_notice_record($list['note'], $uid, $list['uuid'], MESSAGE_WORKORDER_TYPE, array('create_time' => strtotime($list['updated_at'])));
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. function message_sms_expire_notice() {
  154. load()->model('cloud');
  155. load()->model('setting');
  156. $setting_user_expire = setting_load('user_expire');
  157. if (empty($setting_user_expire['user_expire']['status'])) {
  158. return true;
  159. }
  160. $setting_sms_sign = setting_load('site_sms_sign');
  161. $custom_sign = !empty($setting_sms_sign['site_sms_sign']['user_expire']) ? $setting_sms_sign['site_sms_sign']['user_expire'] : '';
  162. $day = !empty($setting_user_expire['user_expire']['day']) ? $setting_user_expire['user_expire']['day'] : 1;
  163. $user_table = table('users');
  164. $user_table->searchWithMobile();
  165. $user_table->searchWithEndtime($day);
  166. $user_table->searchWithSendStatus();
  167. $users_expire = $user_table->searchUsersList();
  168. if (empty($users_expire)) {
  169. return true;
  170. }
  171. foreach ($users_expire as $v) {
  172. if (empty($v['puid'])) {
  173. continue;
  174. }
  175. if (!empty($v['mobile']) && preg_match(REGULAR_MOBILE, $v['mobile'])) {
  176. $result = cloud_sms_send($v['mobile'], '800015', array('username' => $v['username']), $custom_sign);
  177. if (is_error($result)) {
  178. $content = "您的用户名{$v['username']}即将过期。";
  179. pdo_insert('core_sendsms_log', array('mobile' => $v['mobile'], 'content' => $content, 'result' => $result['errno'] . $result['message'], 'createtime' => TIMESTAMP));
  180. } else {
  181. pdo_update('users_profile', array('send_expire_status' => 1), array('uid' => $v['uid']));
  182. }
  183. }
  184. }
  185. return true;
  186. }
  187. function message_user_expire_notice() {
  188. global $_W;
  189. if (!empty($_W['user']['endtime']) && $_W['user']['endtime'] < strtotime('+7 days')) {
  190. $content = $_W['user']['username'] . '即将过期';
  191. message_notice_record($content, $_W['uid'], $_W['uid'], MESSAGE_USER_EXPIRE_TYPE, array('end_time' => $_W['user']['endtime']));
  192. }
  193. return true;
  194. }
  195. function message_notice_record_cloud($message) {
  196. load()->classs('cloudapi');
  197. $api = new CloudApi();
  198. $result = $api->post('system', 'notify', array('json' => $message), 'html', false);
  199. return $result;
  200. }
  201. function message_wxapp_modules_version_upgrade() {
  202. global $_W;
  203. load()->model('miniapp');
  204. load()->model('account');
  205. $wxapp_table = table('account');
  206. $wxapp_table->searchWithType(array(ACCOUNT_TYPE_APP_NORMAL));
  207. $uniacid_list = $wxapp_table->searchAccountList();
  208. if (empty($uniacid_list)) {
  209. return true;
  210. }
  211. $wxapp_list = table('account_wxapp')->wxappInfo(array_keys($uniacid_list));
  212. $wxapp_modules = table('modules')->getSupportWxappList();
  213. foreach ($uniacid_list as $uniacid_info) {
  214. $account_owner = account_owner($uniacid_info['uniacid']);
  215. if (empty($account_owner) || $account_owner['uid'] != $_W['uid']) {
  216. continue;
  217. }
  218. $uniacid_modules = miniapp_version_all($uniacid_info['uniacid']);
  219. if (empty($uniacid_modules[0]['modules'])) {
  220. continue;
  221. }
  222. foreach ($uniacid_modules[0]['modules'] as $module) {
  223. if ($module['version'] < $wxapp_modules[$module['mid']]['version']) {
  224. $content = $wxapp_list[$uniacid_info['uniacid']]['name'] . '-' . '小程序中的' . $module['title'] . '应用有更新';
  225. message_notice_record($content, $_W['uid'], $uniacid_info['uniacid'], MESSAGE_WXAPP_MODULE_UPGRADE);
  226. }
  227. }
  228. }
  229. return true;
  230. }
  231. function message_list_detail($lists) {
  232. if (empty($lists)) {
  233. return $lists;
  234. }
  235. foreach ($lists as &$message) {
  236. $message['create_time'] = date('Y-m-d H:i:s', $message['create_time']);
  237. if ($message['type'] == MESSAGE_ORDER_TYPE) {
  238. $message['url'] = url('site/entry/orders', array('m' => 'store', 'direct'=>1, 'message_id' => $message['id']));
  239. }
  240. if ($message['type'] == MESSAGE_ACCOUNT_EXPIRE_TYPE) {
  241. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_OFFCIAL_NORMAL, 'message_id' => $message['id']));
  242. }
  243. if ($message['type'] == MESSAGE_WECHAT_EXPIRE_TYPE) {
  244. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_APP_NORMAL, 'message_id' => $message['id']));
  245. }
  246. if ($message['type'] == MESSAGE_WEBAPP_EXPIRE_TYPE) {
  247. $message['url'] = url('account/manage', array('account_type' => ACCOUNT_TYPE_WEBAPP_NORMAL, 'message_id' => $message['id']));
  248. }
  249. if ($message['type'] == MESSAGE_REGISTER_TYPE) {
  250. if ($message['status'] == USER_STATUS_CHECK) {
  251. $message['url'] = url('user/display', array('type' => 'check', 'message_id' => $message['id']));
  252. }
  253. if ($message['status'] == USER_STATUS_NORMAL) {
  254. $message['url'] = url('user/display', array('message_id' => $message['id']));
  255. }
  256. $source_array = array('mobile' => '手动注册', 'system' => '手动注册', 'qq' => 'QQ 注册', 'wechat' => '微信注册', 'admin' => '管理员添加');
  257. $msg = explode('--', $message['message']);
  258. if (count($msg) > 1 && !empty($source_array[$msg[1]])) {
  259. $message['message'] = $msg[0];
  260. $message['source'] = $source_array[$msg[1]];
  261. }
  262. }
  263. if ($message['type'] == MESSAGE_USER_EXPIRE_TYPE) {
  264. $message['url'] = url('user/profile', array('message_id' => $message['id']));
  265. }
  266. if ($message['type'] == MESSAGE_WXAPP_MODULE_UPGRADE) {
  267. $message['url'] = url('message/notice', array('message_id' => $message['id']));
  268. }
  269. if ($message['type'] == MESSAGE_WORKORDER_TYPE) {
  270. $message['url'] = url('system/workorder/display', array('uuid' => $message['sign'], 'message_id' => $message['id']));
  271. }
  272. }
  273. return $lists;
  274. }
  275. function message_setting() {
  276. $setting = setting_load('message_notice_setting');
  277. if (!empty($setting['message_notice_setting'])) {
  278. return $setting['message_notice_setting'];
  279. }
  280. return array(
  281. 'order' => array(
  282. 'title' => '订单消息',
  283. 'msg' => '用户购买模块,服务等,提交订单或付款后,将会有消息提醒,建议打开',
  284. 'types' => array(
  285. MESSAGE_ORDER_TYPE => array(
  286. 'type' => MESSAGE_ORDER_TYPE,
  287. 'title' => '提交订单',
  288. 'msg' => '用户购买模块,服务等,提交订单后,将会有消息提醒,建议打开',
  289. ),
  290. MESSAGE_ORDER_PAY_TYPE => array(
  291. 'type' => MESSAGE_ORDER_PAY_TYPE,
  292. 'title' => '支付成功',
  293. 'msg' => '用户购买模块,服务等,付款后,将会有消息提醒,建议打开',
  294. ),
  295. ),
  296. ),
  297. 'expire' => array(
  298. 'title' => '到期消息',
  299. 'msg' => '用户公众号到,小程序到期,平台类型到期,将会有消息提醒,建议打开',
  300. 'types' => array(
  301. MESSAGE_ACCOUNT_EXPIRE_TYPE => array(
  302. 'type' => MESSAGE_ACCOUNT_EXPIRE_TYPE,
  303. 'title' => '公众号到期',
  304. 'msg' => '用户公众号到期后,将会有消息提醒,建议打开',
  305. ),
  306. MESSAGE_WECHAT_EXPIRE_TYPE => array(
  307. 'type' => MESSAGE_WECHAT_EXPIRE_TYPE,
  308. 'title' => '小程序到期',
  309. 'msg' => '用户小程序到期后,将会有消息提醒,建议打开',
  310. ),
  311. MESSAGE_WEBAPP_EXPIRE_TYPE => array(
  312. 'type' => MESSAGE_WEBAPP_EXPIRE_TYPE,
  313. 'title' => 'pc过期',
  314. 'msg' => '用户pc类型到期后,将会有消息提醒,建议打开',
  315. ),
  316. MESSAGE_USER_EXPIRE_TYPE => array(
  317. 'type' => MESSAGE_USER_EXPIRE_TYPE,
  318. 'title' => '用户账号到期',
  319. 'msg' => '用户账号到期后,将会有消息提醒,建议打开',
  320. ),
  321. ),
  322. ),
  323. 'work' => array(
  324. 'title' => '工单提醒',
  325. 'msg' => '站点有工单消息时,将会有消息提醒,建议打开',
  326. 'types' => array(
  327. MESSAGE_WORKORDER_TYPE => array(
  328. 'type' => MESSAGE_WORKORDER_TYPE,
  329. 'title' => '新工单',
  330. 'msg' => '站点有新工时,将会有消息提醒,建议打开',
  331. ),
  332. ),
  333. ),
  334. 'register' => array(
  335. 'title' => '注册提醒',
  336. 'msg' => '用户注册后,将会有消息提醒,建议打开',
  337. 'types' => array(
  338. MESSAGE_REGISTER_TYPE => array(
  339. 'type' => MESSAGE_REGISTER_TYPE,
  340. 'title' => '新用户注册',
  341. 'msg' => '新用户注册后,将会有消息提醒,建议打开',
  342. ),
  343. ),
  344. ),
  345. );
  346. }
  347. function message_store_notice() {
  348. load()->model('cloud');
  349. $data = cloud_get_store_notice();
  350. if (is_error($data)) {
  351. return $data;
  352. }
  353. $insert_data = array();
  354. $signs = array();
  355. $create_time = array();
  356. foreach ($data['version'] as $item) {
  357. $signs[] = $item['itemid'];
  358. $create_time[] = $item['datetime'];
  359. $insert_data[] = array(
  360. 'sign' => $item['itemid'],
  361. 'message' => $item['title'],
  362. 'url' => $item['url'],
  363. 'create_time' => $item['datetime'],
  364. 'type' => MESSAGE_SYSTEM_UPGRADE,
  365. 'is_read' => MESSAGE_NOREAD,
  366. );
  367. }
  368. foreach ($data['info'] as $item) {
  369. $signs[] = $item['itemid'];
  370. $create_time[] = $item['datetime'];
  371. $insert_data[] = array(
  372. 'sign' => $item['itemid'],
  373. 'message' => $item['title'],
  374. 'url' => $item['url'],
  375. 'create_time' => $item['datetime'],
  376. 'type' => MESSAGE_OFFICIAL_DYNAMICS,
  377. 'is_read' => MESSAGE_NOREAD,
  378. );
  379. }
  380. if (!empty($signs)) {
  381. array_multisort($create_time, SORT_ASC, SORT_NUMERIC, $insert_data);
  382. $signs = pdo_getall('message_notice_log', array('sign' => $signs), array('sign'), 'sign');
  383. $signs = array_keys($signs);
  384. foreach ($insert_data as $item) {
  385. if (!in_array($item['sign'], $signs)) {
  386. pdo_insert('message_notice_log', $item);
  387. }
  388. }
  389. }
  390. return true;
  391. }