Sellermsg.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\api\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 MobileSeller
  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. * @api {POST} api/Sellermsg/get_list 消息列表
  24. * @apiVersion 1.0.0
  25. * @apiGroup Sellermsg
  26. *
  27. * @apiHeader {String} X-DS-KEY 用户授权token
  28. *
  29. * @apiParam {Int} page 页码
  30. * @apiParam {Int} per_page 每页数量
  31. *
  32. * @apiSuccess {String} code 返回码,10000为成功
  33. * @apiSuccess {String} message 返回消息
  34. * @apiSuccess {Object} result 返回数据
  35. * @apiSuccess {Object[]} result.notice_list 消息列表 (返回字段参考storemsg表)
  36. * @apiSuccess {String} result.notice_list.storemt_name 商家消息模板名称
  37. * @apiSuccess {Int[]} result.notice_list.storemsg_readids 已读消息ID
  38. * @apiSuccess {Int} result.page_total 总页数
  39. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  40. */
  41. public function get_list()
  42. {
  43. $where = array();
  44. $where[] = array('store_id', '=', $this->store_info['store_id']);
  45. if (!$this->seller_info['is_admin']) {
  46. $where[] = array('storemt_code', 'in', $this->seller_group_info['smt_limits']);
  47. }
  48. $storemsg_model = model('storemsg');
  49. $msg_list = $storemsg_model->getStoremsgList($where, '*', 10);
  50. // 整理数据
  51. if (!empty($msg_list)) {
  52. $storemsgtpl_model = model('storemsgtpl');
  53. $storemsgtpl_array = array();
  54. foreach ($msg_list as $key => $val) {
  55. if (!isset($storemsgtpl_array[$val['storemt_code']])) {
  56. $storemsgtpl_info = $storemsgtpl_model->getStoremsgtplInfo(array('storemt_code' => $val['storemt_code']));
  57. $storemsgtpl_array[$val['storemt_code']] = $storemsgtpl_info['storemt_name'];
  58. }
  59. $msg_list[$key]['storemt_name'] = $storemsgtpl_array[$val['storemt_code']];
  60. $msg_list[$key]['storemsg_readids'] = explode(',', $val['storemsg_readids']);
  61. }
  62. }
  63. ds_json_encode(10000, '', array_merge(array('notice_list' => $msg_list), mobile_page($storemsg_model->page_info)));
  64. }
  65. /**
  66. * 消息详细
  67. */
  68. public function msg_info()
  69. {
  70. $storemsg_id = intval(input('param.storemsg_id'));
  71. if ($storemsg_id <= 0) {
  72. $this->error(lang('param_error'));
  73. }
  74. $storemsg_model = model('storemsg');
  75. $where = array();
  76. $where[] = array('storemsg_id', '=', $storemsg_id);
  77. if ($this->seller_group_info['smt_limits'] !== false) {
  78. $where[] = array('storemt_code', 'in', $this->seller_group_info['smt_limits']);
  79. }
  80. $msg_info = $storemsg_model->getStoremsgInfo($where);
  81. if (empty($msg_info)) {
  82. $this->error(lang('param_error'));
  83. }
  84. View::assign('msg_list', $msg_info);
  85. // 验证时候已读
  86. $storemsg_readids = explode(',', $msg_info['storemsg_readids']);
  87. if (!in_array($this->seller_info['seller_id'], $storemsg_readids)) {
  88. // 消息阅读表插入数据
  89. $condition = array();
  90. $condition[] = array('seller_id', '=', $this->seller_info['seller_id']);
  91. $condition[] = array('storemsg_id', '=', $storemsg_id);
  92. model('storemsgread')->addStoremsgread($condition);
  93. $update = array();
  94. $storemsg_readids[] = $this->seller_info['seller_id'];
  95. $update['storemsg_readids'] = implode(',', $storemsg_readids) . ',';
  96. $storemsg_model->editStoremsg(array('storemsg_id' => $storemsg_id), $update);
  97. }
  98. return View::fetch($this->template_dir . 'msg_info');
  99. }
  100. /**
  101. * AJAX标记为已读
  102. */
  103. public function mark_as_read()
  104. {
  105. $smids = input('param.smids');
  106. if (!preg_match('/^[\d,]+$/i', $smids)) {
  107. ds_json_encode(10001, lang('param_error'));
  108. }
  109. $smids = explode(',', $smids);
  110. $storemsgread_model = model('storemsgread');
  111. $storemsg_model = model('storemsg');
  112. foreach ($smids as $val) {
  113. $condition = array();
  114. $condition[] = array('seller_id', '=', $this->seller_info['seller_id']);
  115. $condition[] = array('storemsg_id', '=', $val);
  116. $read_info = $storemsgread_model->getStoremsgreadInfo($condition);
  117. if (empty($read_info)) {
  118. // 消息阅读表插入数据
  119. $storemsgread_model->addStoremsgread($condition);
  120. // 更新店铺消息表
  121. $storemsg_info = $storemsg_model->getStoremsgInfo(array('storemsg_id' => $val));
  122. $storemsg_readids = explode(',', $storemsg_info['storemsg_readids']);
  123. $storemsg_readids[] = $this->seller_info['seller_id'];
  124. $storemsg_readids = array_unique($storemsg_readids);
  125. $update = array();
  126. $update['storemsg_readids'] = implode(',', $storemsg_readids) . ',';
  127. $storemsg_model->editStoremsg(array('storemsg_id' => $val), $update);
  128. }
  129. }
  130. ds_json_encode(10000, lang('ds_common_op_succ'));
  131. }
  132. /**
  133. * AJAX删除消息
  134. */
  135. public function del_msg()
  136. {
  137. // 验证参数
  138. $smids = input('param.smids');
  139. if (!preg_match('/^[\d,]+$/i', $smids)) {
  140. ds_json_encode(10001, lang('param_error'));
  141. }
  142. $smid_array = explode(',', $smids);
  143. // 验证是否为管理员
  144. if (!$this->checkIsAdmin()) {
  145. ds_json_encode(10001, lang('param_error'));
  146. }
  147. $where = array();
  148. $where[] = array('store_id', '=', $this->store_info['store_id']);
  149. $where[] = array('storemsg_id', 'in', $smid_array);
  150. // 删除消息记录
  151. model('storemsg')->delStoremsg($where);
  152. // 删除阅读记录
  153. $where = array();
  154. $where[] = array('storemsg_id', 'in', $smid_array);
  155. model('storemsgread')->delStoremsgread($where);
  156. ds_json_encode(10000, lang('ds_common_op_succ'));
  157. }
  158. /**
  159. * 消息接收设置
  160. */
  161. public function msg_setting()
  162. {
  163. // 验证是否为管理员
  164. if (!$this->checkIsAdmin()) {
  165. $this->error(lang('param_error'));
  166. }
  167. // 店铺消息模板列表
  168. $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');
  169. // 店铺接收设置
  170. $setting_list = model('storemsgsetting')->getStoremsgsettingList(array('store_id' => $this->store_info['store_id']), '*', 'storemt_code');
  171. if (!empty($smt_list)) {
  172. foreach ($smt_list as $key => $val) {
  173. // 站内信消息模板是否开启
  174. if ($val['storemt_message_switch']) {
  175. // 是否强制接收,强制接收必须开启
  176. $smt_list[$key]['storems_message_switch'] = $val['storemt_message_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_message_switch']);
  177. // 已开启接收模板
  178. if ($smt_list[$key]['storems_message_switch']) {
  179. $smt_list[$key]['is_opened'][] = lang('business_alert');
  180. }
  181. }
  182. // 短消息模板是否开启
  183. if ($val['storemt_short_switch']) {
  184. // 是否强制接收,强制接收必须开启
  185. $smt_list[$key]['storems_short_switch'] = $val['smt_short_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_short_switch']);
  186. // 已开启接收模板
  187. if ($smt_list[$key]['storems_short_switch']) {
  188. $smt_list[$key]['is_opened'][] = lang('sms_alerts');
  189. }
  190. }
  191. // 邮件模板是否开启
  192. if ($val['storemt_mail_switch']) {
  193. // 是否强制接收,强制接收必须开启
  194. $smt_list[$key]['storems_mail_switch'] = $val['storemt_mail_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_mail_switch']);
  195. // 已开启接收模板
  196. if ($smt_list[$key]['storems_mail_switch']) {
  197. $smt_list[$key]['is_opened'][] = lang('email_alerts');
  198. }
  199. }
  200. if (is_array($smt_list[$key]['is_opened'])) {
  201. $smt_list[$key]['is_opened'] = implode('&nbsp;|&nbsp;&nbsp;', $smt_list[$key]['is_opened']);
  202. }
  203. }
  204. }
  205. View::assign('smt_list', $smt_list);
  206. $this->setSellerCurMenu('Sellermsg');
  207. $this->setSellerCurItem('msg_setting');
  208. return View::fetch($this->template_dir . 'msg_setting');
  209. }
  210. /**
  211. * 编辑店铺消息接收设置
  212. */
  213. public function edit_msg_setting()
  214. {
  215. // 验证是否为管理员
  216. if (!$this->checkIsAdmin()) {
  217. $this->error(lang('param_error'));
  218. }
  219. $code = trim(input('param.code'));
  220. if ($code == '') {
  221. return false;
  222. }
  223. // 店铺消息模板
  224. $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');
  225. if (empty($smt_info)) {
  226. return false;
  227. }
  228. // 店铺消息接收设置
  229. $setting_info = model('storemsgsetting')->getStoremsgsettingInfo(array(
  230. 'storemt_code' => $code,
  231. 'store_id' => $this->store_info['store_id']
  232. ));
  233. View::assign('smt_info', $smt_info);
  234. View::assign('smsetting_info', $setting_info);
  235. return View::fetch($this->template_dir . 'setting_edit');
  236. }
  237. /**
  238. * 保存店铺接收设置
  239. */
  240. public function save_msg_setting()
  241. {
  242. // 验证是否为管理员
  243. if (!$this->checkIsAdmin()) {
  244. ds_json_encode(10001, lang('param_error'));
  245. }
  246. $code = trim(input('post.code'));
  247. if ($code == '') {
  248. ds_json_encode(10001, lang('param_error'));
  249. }
  250. $data = [
  251. 'storems_short_number' => input('post.storems_short_number'),
  252. 'storems_mail_number' => input('post.storems_mail_number'),
  253. ];
  254. $sellermsg_validate = ds_validate('sellermsg');
  255. if (!$sellermsg_validate->scene('save_msg_setting')->check($data)) {
  256. ds_json_encode(10001, $sellermsg_validate->getError());
  257. }
  258. $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');
  259. // 保存
  260. $data = array();
  261. $data['storemt_code'] = $smt_info['storemt_code'];
  262. $data['store_id'] = $this->store_info['store_id'];
  263. // 验证站内信是否开启
  264. if ($smt_info['storemt_message_switch']) {
  265. $data['storems_message_switch'] = $smt_info['storemt_message_forced'] ? 1 : intval(input('post.message_forced'));
  266. } else {
  267. $data['storems_message_switch'] = 0;
  268. }
  269. // 验证短消息是否开启
  270. if ($smt_info['storemt_short_switch']) {
  271. $data['storems_short_switch'] = $smt_info['smt_short_forced'] ? 1 : intval(input('post.short_forced'));
  272. } else {
  273. $data['storems_short_switch'] = 0;
  274. }
  275. $data['storems_short_number'] = input('post.storems_short_number', '');
  276. // 验证邮件是否开启
  277. if ($smt_info['storemt_mail_switch']) {
  278. $data['storems_mail_switch'] = $smt_info['storemt_mail_forced'] ? 1 : intval(input('post.mail_forced'));
  279. } else {
  280. $data['storems_mail_switch'] = 0;
  281. }
  282. $data['storems_mail_number'] = input('post.storems_mail_number', '');
  283. $conditiion = array();
  284. $conditiion['storemt_code'] = $smt_info['storemt_code'];
  285. $storemsgsetting_info = model('storemsgsetting')->getStoremsgsettingInfo($conditiion);
  286. // 插入数据
  287. if (empty($storemsgsetting_info)) {
  288. $result = model('storemsgsetting')->addStoremsgsetting($data);
  289. } else {
  290. $result = model('storemsgsetting')->editStoremsgsetting($data, $conditiion);
  291. }
  292. if ($result) {
  293. ds_json_encode(10000, lang('ds_common_op_succ'));
  294. } else {
  295. ds_json_encode(10001, lang('ds_common_op_fail'));
  296. }
  297. }
  298. private function checkIsAdmin()
  299. {
  300. return $this->seller_info['is_admin'] ? true : false;
  301. }
  302. }