Sellermsg.php 14 KB

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