Sellermsg.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Sellermsg extends BaseSeller
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellermsg.lang.php');
  18. }
  19. /**
  20. * 消息列表
  21. */
  22. public function index()
  23. {
  24. $where = array();
  25. $where[] = array('store_id', '=', session('store_id'));
  26. if (!session('seller_is_admin')) {
  27. $where[] = array('storemt_code', 'in', session('seller_smt_limits'));
  28. }
  29. $storemsg_model = model('storemsg');
  30. $msg_list = $storemsg_model->getStoremsgList($where, '*', 10);
  31. // 整理数据
  32. if (!empty($msg_list)) {
  33. foreach ($msg_list as $key => $val) {
  34. $msg_list[$key]['storemsg_readids'] = explode(',', $val['storemsg_readids']);
  35. }
  36. }
  37. View::assign('msg_list', $msg_list);
  38. View::assign('show_page', $storemsg_model->page_info->render());
  39. $this->setSellerCurMenu('Sellermsg');
  40. $this->setSellerCurItem('msg_list');
  41. return View::fetch($this->template_dir . 'index');
  42. }
  43. /**
  44. * 消息详细
  45. */
  46. public function msg_info()
  47. {
  48. $storemsg_id = intval(input('param.storemsg_id'));
  49. if ($storemsg_id <= 0) {
  50. $this->error(lang('param_error'));
  51. }
  52. $storemsg_model = model('storemsg');
  53. $where = array();
  54. $where[] = array('storemsg_id', '=', $storemsg_id);
  55. if (session('seller_smt_limits') !== false) {
  56. $where[] = array('storemt_code', 'in', session('seller_smt_limits'));
  57. }
  58. $msg_info = $storemsg_model->getStoremsgInfo($where);
  59. if (empty($msg_info)) {
  60. $this->error(lang('param_error'));
  61. }
  62. View::assign('msg_list', $msg_info);
  63. // 验证时候已读
  64. $storemsg_readids = explode(',', $msg_info['storemsg_readids']);
  65. if (!in_array(session('seller_id'), $storemsg_readids)) {
  66. // 消息阅读表插入数据
  67. $data = array();
  68. $data['seller_id'] = session('seller_id');
  69. $data['storemsg_id'] = $storemsg_id;
  70. model('storemsgread')->addStoremsgread($data);
  71. $update = array();
  72. $storemsg_readids[] = session('seller_id');
  73. $update['storemsg_readids'] = implode(',', $storemsg_readids) . ',';
  74. $storemsg_model->editStoremsg(array('storemsg_id' => $storemsg_id), $update);
  75. }
  76. return View::fetch($this->template_dir . 'msg_info');
  77. }
  78. /**
  79. * AJAX标记为已读
  80. */
  81. public function mark_as_read()
  82. {
  83. $smids = input('param.smids');
  84. if (!preg_match('/^[\d,]+$/i', $smids)) {
  85. ds_json_encode(10001, lang('param_error'));
  86. }
  87. $smids = explode(',', $smids);
  88. $storemsgread_model = model('storemsgread');
  89. $storemsg_model = model('storemsg');
  90. foreach ($smids as $val) {
  91. $condition = array();
  92. $condition[] = array('seller_id', '=', session('seller_id'));
  93. $condition[] = array('storemsg_id', '=', $val);
  94. $read_info = $storemsgread_model->getStoremsgreadInfo($condition);
  95. if (empty($read_info)) {
  96. $data = array();
  97. $data['seller_id'] = session('seller_id');
  98. $data['storemsg_id'] = $val;
  99. // 消息阅读表插入数据
  100. $storemsgread_model->addStoremsgread($data);
  101. // 更新店铺消息表
  102. $storemsg_info = $storemsg_model->getStoremsgInfo(array('storemsg_id' => $val));
  103. $storemsg_readids = explode(',', $storemsg_info['storemsg_readids']);
  104. $storemsg_readids[] = session('seller_id');
  105. $storemsg_readids = array_unique($storemsg_readids);
  106. $update = array();
  107. $update['storemsg_readids'] = implode(',', $storemsg_readids) . ',';
  108. $storemsg_model->editStoremsg(array('storemsg_id' => $val), $update);
  109. }
  110. }
  111. ds_json_encode(10000, lang('ds_common_op_succ'));
  112. }
  113. /**
  114. * AJAX删除消息
  115. */
  116. public function del_msg()
  117. {
  118. // 验证参数
  119. $smids = input('param.smids');
  120. if (!preg_match('/^[\d,]+$/i', $smids)) {
  121. ds_json_encode(10001, lang('param_error'));
  122. }
  123. $smid_array = explode(',', $smids);
  124. // 验证是否为管理员
  125. if (!$this->checkIsAdmin()) {
  126. ds_json_encode(10001, lang('param_error'));
  127. }
  128. $condition = array();
  129. $condition[] = array('store_id', '=', session('store_id'));
  130. $condition[] = array('storemsg_id', 'in', $smid_array);
  131. // 删除消息记录
  132. model('storemsg')->delStoremsg($condition);
  133. // 删除阅读记录
  134. $condition = array();
  135. $condition[] = array('storemsg_id', 'in', $smid_array);
  136. model('storemsgread')->delStoremsgread($condition);
  137. ds_json_encode(10000, lang('ds_common_op_succ'));
  138. }
  139. /**
  140. * 消息接收设置
  141. */
  142. public function msg_setting()
  143. {
  144. // 验证是否为管理员
  145. if (!$this->checkIsAdmin()) {
  146. $this->error(lang('param_error'));
  147. }
  148. // 店铺消息模板列表
  149. $smt_list = model('storemsgtpl')->getStoremsgtplList(array(), 'storemt_code,storemt_name,storemt_message_switch,storemt_message_forced,storemt_short_switch,smt_short_forced,storemt_mail_switch,storemt_mail_forced,storemt_weixin_switch,storemt_weixin_forced');
  150. // 店铺接收设置
  151. $setting_list = model('storemsgsetting')->getStoremsgsettingList(array('store_id' => session('store_id')), '*', 'storemt_code');
  152. if (!empty($smt_list)) {
  153. foreach ($smt_list as $key => $val) {
  154. // 站内信消息模板是否开启
  155. if ($val['storemt_message_switch']) {
  156. // 是否强制接收,强制接收必须开启
  157. $smt_list[$key]['storems_message_switch'] = $val['storemt_message_forced'] ? 1 : (isset($setting_list[$val['storemt_code']]) ? intval($setting_list[$val['storemt_code']]['storems_message_switch']) : 0);
  158. // 已开启接收模板
  159. if ($smt_list[$key]['storems_message_switch']) {
  160. $smt_list[$key]['is_opened'][] = lang('business_alert');
  161. }
  162. }
  163. // 短消息模板是否开启
  164. if ($val['storemt_short_switch']) {
  165. // 是否强制接收,强制接收必须开启
  166. $smt_list[$key]['storems_short_switch'] = $val['smt_short_forced'] ? 1 : (isset($setting_list[$val['storemt_code']]) ? intval($setting_list[$val['storemt_code']]['storems_short_switch']) : 0);
  167. // 已开启接收模板
  168. if ($smt_list[$key]['storems_short_switch']) {
  169. $smt_list[$key]['is_opened'][] = lang('sms_alerts');
  170. }
  171. }
  172. // 邮件模板是否开启
  173. if ($val['storemt_mail_switch']) {
  174. // 是否强制接收,强制接收必须开启
  175. $smt_list[$key]['storems_mail_switch'] = $val['storemt_mail_forced'] ? 1 : (isset($setting_list[$val['storemt_code']]) ? intval($setting_list[$val['storemt_code']]['storems_mail_switch']) : 0);
  176. // 已开启接收模板
  177. if ($smt_list[$key]['storems_mail_switch']) {
  178. $smt_list[$key]['is_opened'][] = lang('email_alerts');
  179. }
  180. }
  181. // 微信模板是否开启
  182. if ($val['storemt_weixin_switch']) {
  183. // 是否强制接收,强制接收必须开启
  184. $smt_list[$key]['storems_weixin_switch'] = $val['storemt_weixin_forced'] ? 1 : (isset($setting_list[$val['storemt_code']]) ? intval($setting_list[$val['storemt_code']]['storems_weixin_switch']) : 0);
  185. // 已开启接收模板
  186. if ($smt_list[$key]['storems_weixin_switch']) {
  187. $smt_list[$key]['is_opened'][] = lang('weixin_alerts');
  188. }
  189. }
  190. if (!empty($smt_list[$key]['is_opened']) && is_array($smt_list[$key]['is_opened'])) {
  191. $smt_list[$key]['is_opened'] = implode('&nbsp;|&nbsp;&nbsp;', $smt_list[$key]['is_opened']);
  192. } else {
  193. $smt_list[$key]['is_opened'] = '';
  194. }
  195. }
  196. }
  197. View::assign('smt_list', $smt_list);
  198. $this->setSellerCurMenu('Sellermsg');
  199. $this->setSellerCurItem('msg_setting');
  200. return View::fetch($this->template_dir . 'msg_setting');
  201. }
  202. /**
  203. * 编辑店铺消息接收设置
  204. */
  205. public function edit_msg_setting()
  206. {
  207. // 验证是否为管理员
  208. if (!$this->checkIsAdmin()) {
  209. $this->error(lang('param_error'));
  210. }
  211. $code = trim(input('param.code'));
  212. if ($code == '') {
  213. return false;
  214. }
  215. // 店铺消息模板
  216. $smt_info = model('storemsgtpl')->getStoremsgtplInfo(array('storemt_code' => $code), 'storemt_code,storemt_name,storemt_message_switch,storemt_message_forced,storemt_short_switch,smt_short_forced,storemt_mail_switch,storemt_mail_forced,storemt_weixin_switch,storemt_weixin_forced');
  217. if (empty($smt_info)) {
  218. return false;
  219. }
  220. // 店铺消息接收设置
  221. $setting_info = model('storemsgsetting')->getStoremsgsettingInfo(array(
  222. 'storemt_code' => $code,
  223. 'store_id' => session('store_id')
  224. ));
  225. View::assign('smt_info', $smt_info);
  226. View::assign('smsetting_info', $setting_info);
  227. return View::fetch($this->template_dir . 'setting_edit');
  228. }
  229. /**
  230. * 保存店铺接收设置
  231. */
  232. public function save_msg_setting()
  233. {
  234. // 验证是否为管理员
  235. if (!$this->checkIsAdmin()) {
  236. ds_json_encode(10001, lang('param_error'));
  237. }
  238. $code = trim(input('post.code'));
  239. if ($code == '') {
  240. ds_json_encode(10001, lang('param_error'));
  241. }
  242. $data = [
  243. 'storems_short_number' => input('post.storems_short_number'),
  244. 'storems_mail_number' => input('post.storems_mail_number'),
  245. ];
  246. $sellermsg_validate = ds_validate('sellermsg');
  247. if (!$sellermsg_validate->scene('save_msg_setting')->check($data)) {
  248. ds_json_encode(10001, $sellermsg_validate->getError());
  249. }
  250. $smt_info = model('storemsgtpl')->getStoremsgtplInfo(array('storemt_code' => $code), 'storemt_code,storemt_name,storemt_message_switch,storemt_message_forced,storemt_short_switch,smt_short_forced,storemt_mail_switch,storemt_mail_forced,storemt_weixin_switch,storemt_weixin_forced');
  251. // 保存
  252. $data = array();
  253. $data['storemt_code'] = $smt_info['storemt_code'];
  254. $data['store_id'] = session('store_id');
  255. // 验证站内信是否开启
  256. if ($smt_info['storemt_message_switch']) {
  257. $data['storems_message_switch'] = $smt_info['storemt_message_forced'] ? 1 : intval(input('post.message_forced'));
  258. } else {
  259. $data['storems_message_switch'] = 0;
  260. }
  261. // 验证短消息是否开启
  262. if ($smt_info['storemt_short_switch']) {
  263. $data['storems_short_switch'] = $smt_info['smt_short_forced'] ? 1 : intval(input('post.short_forced'));
  264. } else {
  265. $data['storems_short_switch'] = 0;
  266. }
  267. $data['storems_short_number'] = input('post.storems_short_number', '');
  268. // 验证邮件是否开启
  269. if ($smt_info['storemt_mail_switch']) {
  270. $data['storems_mail_switch'] = $smt_info['storemt_mail_forced'] ? 1 : intval(input('post.mail_forced'));
  271. } else {
  272. $data['storems_mail_switch'] = 0;
  273. }
  274. // 验证微信是否开启
  275. if ($smt_info['storemt_weixin_switch']) {
  276. $data['storems_weixin_switch'] = $smt_info['storemt_weixin_forced'] ? 1 : intval(input('post.weixin_forced'));
  277. } else {
  278. $data['storems_weixin_switch'] = 0;
  279. }
  280. $data['storems_mail_number'] = input('post.storems_mail_number', '');
  281. $conditiion = array();
  282. $conditiion['storemt_code'] = $smt_info['storemt_code'];
  283. $conditiion['store_id'] = session('store_id');
  284. $storemsgsetting_info = model('storemsgsetting')->getStoremsgsettingInfo($conditiion);
  285. // 插入数据
  286. if (empty($storemsgsetting_info)) {
  287. $result = model('storemsgsetting')->addStoremsgsetting($data);
  288. } else {
  289. $result = model('storemsgsetting')->editStoremsgsetting($data, $conditiion);
  290. }
  291. if ($result) {
  292. ds_json_encode(10000, lang('ds_common_op_succ'));
  293. } else {
  294. ds_json_encode(10001, lang('ds_common_op_fail'));
  295. }
  296. }
  297. private function checkIsAdmin()
  298. {
  299. return session('seller_is_admin') ? true : false;
  300. }
  301. /**
  302. * 用户中心右边,小导航
  303. *
  304. * @param string $name 当前导航的name
  305. * @param array $array 附加菜单
  306. * @return
  307. */
  308. protected function getSellerItemList()
  309. {
  310. $menu_array = array(
  311. 1 => array('name' => 'msg_list', 'text' => lang('system_message'), 'url' => (string) url('Sellermsg/index')), 2 => array(
  312. 'name' => 'msg_setting', 'text' => lang('receiving_setting'), 'url' => (string) url('Sellermsg/msg_setting')
  313. ),
  314. );
  315. if (!$this->checkIsAdmin()) {
  316. unset($menu_array[2]);
  317. }
  318. return $menu_array;
  319. }
  320. }