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