Sellermsg.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 卖家消息控制器
  16. */
  17. class Sellermsg extends MobileSeller {
  18. public function initialize() {
  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. $where = array();
  43. $where[] = array('store_id', '=', $this->store_info['store_id']);
  44. if (!$this->seller_info['is_admin']) {
  45. $where[] = array('storemt_code', 'in', $this->seller_group_info['smt_limits']);
  46. }
  47. $storemsg_model = model('storemsg');
  48. $msg_list = $storemsg_model->getStoremsgList($where, '*', 10);
  49. // 整理数据
  50. if (!empty($msg_list)) {
  51. $storemsgtpl_model = model('storemsgtpl');
  52. $storemsgtpl_array = array();
  53. foreach ($msg_list as $key => $val) {
  54. if (!isset($storemsgtpl_array[$val['storemt_code']])) {
  55. $storemsgtpl_info = $storemsgtpl_model->getStoremsgtplInfo(array('storemt_code' => $val['storemt_code']));
  56. $storemsgtpl_array[$val['storemt_code']] = $storemsgtpl_info['storemt_name'];
  57. }
  58. $msg_list[$key]['storemt_name'] = $storemsgtpl_array[$val['storemt_code']];
  59. $msg_list[$key]['storemsg_readids'] = explode(',', $val['storemsg_readids']);
  60. }
  61. }
  62. ds_json_encode(10000, '', array_merge(array('notice_list' => $msg_list), mobile_page($storemsg_model->page_info)));
  63. }
  64. /**
  65. * 消息详细
  66. */
  67. public function msg_info() {
  68. $storemsg_id = intval(input('param.storemsg_id'));
  69. if ($storemsg_id <= 0) {
  70. $this->error(lang('param_error'));
  71. }
  72. $storemsg_model = model('storemsg');
  73. $where = array();
  74. $where[] = array('storemsg_id', '=', $storemsg_id);
  75. if ($this->seller_group_info['smt_limits'] !== false) {
  76. $where[] = array('storemt_code', 'in', $this->seller_group_info['smt_limits']);
  77. }
  78. $msg_info = $storemsg_model->getStoremsgInfo($where);
  79. if (empty($msg_info)) {
  80. $this->error(lang('param_error'));
  81. }
  82. View::assign('msg_list', $msg_info);
  83. // 验证时候已读
  84. $storemsg_readids = explode(',', $msg_info['storemsg_readids']);
  85. if (!in_array($this->seller_info['seller_id'], $storemsg_readids)) {
  86. // 消息阅读表插入数据
  87. $condition = array();
  88. $condition[] = array('seller_id','=',$this->seller_info['seller_id']);
  89. $condition[] = array('storemsg_id','=',$storemsg_id);
  90. model('storemsgread')->addStoremsgread($condition);
  91. $update = array();
  92. $storemsg_readids[] = $this->seller_info['seller_id'];
  93. $update['storemsg_readids'] = implode(',', $storemsg_readids) . ',';
  94. $storemsg_model->editStoremsg(array('storemsg_id' => $storemsg_id), $update);
  95. }
  96. return View::fetch($this->template_dir . 'msg_info');
  97. }
  98. /**
  99. * AJAX标记为已读
  100. */
  101. public function mark_as_read() {
  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. $smids = input('param.smids');
  135. if (!preg_match('/^[\d,]+$/i', $smids)) {
  136. ds_json_encode(10001, lang('param_error'));
  137. }
  138. $smid_array = explode(',', $smids);
  139. // 验证是否为管理员
  140. if (!$this->checkIsAdmin()) {
  141. ds_json_encode(10001, lang('param_error'));
  142. }
  143. $where = array();
  144. $where[] = array('store_id', '=', $this->store_info['store_id']);
  145. $where[] = array('storemsg_id', 'in', $smid_array);
  146. // 删除消息记录
  147. model('storemsg')->delStoremsg($where);
  148. // 删除阅读记录
  149. $where = array();
  150. $where[] = array('storemsg_id', 'in', $smid_array);
  151. model('storemsgread')->delStoremsgread($where);
  152. ds_json_encode(10000, lang('ds_common_op_succ'));
  153. }
  154. /**
  155. * 消息接收设置
  156. */
  157. public function msg_setting() {
  158. // 验证是否为管理员
  159. if (!$this->checkIsAdmin()) {
  160. $this->error(lang('param_error'));
  161. }
  162. // 店铺消息模板列表
  163. $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');
  164. // 店铺接收设置
  165. $setting_list = model('storemsgsetting')->getStoremsgsettingList(array('store_id' => $this->store_info['store_id']), '*', 'storemt_code');
  166. if (!empty($smt_list)) {
  167. foreach ($smt_list as $key => $val) {
  168. // 站内信消息模板是否开启
  169. if ($val['storemt_message_switch']) {
  170. // 是否强制接收,强制接收必须开启
  171. $smt_list[$key]['storems_message_switch'] = $val['storemt_message_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_message_switch']);
  172. // 已开启接收模板
  173. if ($smt_list[$key]['storems_message_switch']) {
  174. $smt_list[$key]['is_opened'][] = lang('business_alert');
  175. }
  176. }
  177. // 短消息模板是否开启
  178. if ($val['storemt_short_switch']) {
  179. // 是否强制接收,强制接收必须开启
  180. $smt_list[$key]['storems_short_switch'] = $val['smt_short_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_short_switch']);
  181. // 已开启接收模板
  182. if ($smt_list[$key]['storems_short_switch']) {
  183. $smt_list[$key]['is_opened'][] = lang('sms_alerts');
  184. }
  185. }
  186. // 邮件模板是否开启
  187. if ($val['storemt_mail_switch']) {
  188. // 是否强制接收,强制接收必须开启
  189. $smt_list[$key]['storems_mail_switch'] = $val['storemt_mail_forced'] ? 1 : intval($setting_list[$val['storemt_code']]['storems_mail_switch']);
  190. // 已开启接收模板
  191. if ($smt_list[$key]['storems_mail_switch']) {
  192. $smt_list[$key]['is_opened'][] = lang('email_alerts');
  193. }
  194. }
  195. if (is_array($smt_list[$key]['is_opened'])) {
  196. $smt_list[$key]['is_opened'] = implode('&nbsp;|&nbsp;&nbsp;', $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. if (!$this->checkIsAdmin()) {
  211. $this->error(lang('param_error'));
  212. }
  213. $code = trim(input('param.code'));
  214. if ($code == '') {
  215. return false;
  216. }
  217. // 店铺消息模板
  218. $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');
  219. if (empty($smt_info)) {
  220. return false;
  221. }
  222. // 店铺消息接收设置
  223. $setting_info = model('storemsgsetting')->getStoremsgsettingInfo(array(
  224. 'storemt_code' => $code,
  225. 'store_id' => $this->store_info['store_id']
  226. ));
  227. View::assign('smt_info', $smt_info);
  228. View::assign('smsetting_info', $setting_info);
  229. return View::fetch($this->template_dir . 'setting_edit');
  230. }
  231. /**
  232. * 保存店铺接收设置
  233. */
  234. public function save_msg_setting() {
  235. // 验证是否为管理员
  236. if (!$this->checkIsAdmin()) {
  237. ds_json_encode(10001, lang('param_error'));
  238. }
  239. $code = trim(input('post.code'));
  240. if ($code == '') {
  241. ds_json_encode(10001, lang('param_error'));
  242. }
  243. $data = [
  244. 'storems_short_number' => input('post.storems_short_number'),
  245. 'storems_mail_number' => input('post.storems_mail_number'),
  246. ];
  247. $sellermsg_validate = ds_validate('sellermsg');
  248. if (!$sellermsg_validate->scene('save_msg_setting')->check($data)) {
  249. ds_json_encode(10001, $sellermsg_validate->getError());
  250. }
  251. $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');
  252. // 保存
  253. $data = array();
  254. $data['storemt_code'] = $smt_info['storemt_code'];
  255. $data['store_id'] = $this->store_info['store_id'];
  256. // 验证站内信是否开启
  257. if ($smt_info['storemt_message_switch']) {
  258. $data['storems_message_switch'] = $smt_info['storemt_message_forced'] ? 1 : intval(input('post.message_forced'));
  259. } else {
  260. $data['storems_message_switch'] = 0;
  261. }
  262. // 验证短消息是否开启
  263. if ($smt_info['storemt_short_switch']) {
  264. $data['storems_short_switch'] = $smt_info['smt_short_forced'] ? 1 : intval(input('post.short_forced'));
  265. } else {
  266. $data['storems_short_switch'] = 0;
  267. }
  268. $data['storems_short_number'] = input('post.storems_short_number', '');
  269. // 验证邮件是否开启
  270. if ($smt_info['storemt_mail_switch']) {
  271. $data['storems_mail_switch'] = $smt_info['storemt_mail_forced'] ? 1 : intval(input('post.mail_forced'));
  272. } else {
  273. $data['storems_mail_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. $storemsgsetting_info = model('storemsgsetting')->getStoremsgsettingInfo($conditiion);
  279. // 插入数据
  280. if (empty($storemsgsetting_info)) {
  281. $result = model('storemsgsetting')->addStoremsgsetting($data);
  282. } else {
  283. $result = model('storemsgsetting')->editStoremsgsetting($data, $conditiion);
  284. }
  285. if ($result) {
  286. ds_json_encode(10000, lang('ds_common_op_succ'));
  287. } else {
  288. ds_json_encode(10001, lang('ds_common_op_fail'));
  289. }
  290. }
  291. private function checkIsAdmin() {
  292. return $this->seller_info['is_admin'] ? true : false;
  293. }
  294. }