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