Sellermsg.php 14 KB

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