SystemMessage.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use app\admin\model\system\SystemMessage as MessageModel;
  15. use app\admin\model\wechat\WechatTemplate as WechatTemplateModel;
  16. use service\JsonService;
  17. use service\WechatTemplateService;
  18. use service\SystemConfigService;
  19. use think\Request;
  20. use think\Url;
  21. /**
  22. * 消息通知 控制器
  23. * Class SystemMessage
  24. * @package app\admin\controller\setting
  25. */
  26. class SystemMessage extends AuthController
  27. {
  28. /**
  29. * 消息通知展示
  30. * @return
  31. * */
  32. public function index()
  33. {
  34. $data = [];
  35. if (SystemConfigService::get('wechat_appid') && SystemConfigService::get('wechat_appsecret')) {
  36. try {
  37. $data = WechatTemplateService::getIndustry();
  38. $data = count($data) > 0 ? $data->toArray() : [];
  39. } catch (\Exception $e) {
  40. }
  41. }
  42. $this->assign('data', $data);
  43. return $this->fetch();
  44. }
  45. /**
  46. * 同步微信模版消息
  47. */
  48. public function synchronousWechatTemplate()
  49. {
  50. if (!SystemConfigService::get('wechat_appid') || !SystemConfigService::get('wechat_appsecret')) return JsonService::fail('请先配置公众号');
  51. try {
  52. $data = WechatTemplateService::getIndustry();
  53. $data = count($data) > 0 ? $data->toArray() : [];
  54. } catch (\Exception $e) {
  55. return JsonService::fail($e->getMessage());
  56. }
  57. if ($data['primary_industry']['first_class'] == 'IT科技' && $data['primary_industry']['second_class'] == '互联网|电子商务' &&
  58. $data['secondary_industry']['first_class'] == 'IT科技' && $data['secondary_industry']['second_class'] == 'IT软件与服务') {
  59. $this->set_template();
  60. } else {
  61. try {
  62. WechatTemplateService::setIndustry(1, 2);
  63. } catch (\Exception $e) {
  64. return JsonService::fail('同步失败,请检查公众号配置');
  65. }
  66. $this->set_template();
  67. }
  68. return JsonService::successful('同步完成');
  69. }
  70. /**模版消息同步
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. public function set_template()
  76. {
  77. $list = WechatTemplateModel::where('status', 1)->select();
  78. foreach ($list as $key => $value) {
  79. try {
  80. $res = WechatTemplateService::addTemplate($value['tempkey']);
  81. } catch (\Exception $e) {
  82. continue;
  83. }
  84. if ($res['errcode'] == 0 && $res['errmsg'] == 'ok' && $res['template_id']) {
  85. $data['tempid'] = $res['template_id'];
  86. WechatTemplateModel::edit($data, $value['tempkey'], 'tempkey');
  87. }
  88. }
  89. return JsonService::successful('模版消息同步完成');
  90. }
  91. /**
  92. * 消息通知获取
  93. * @return
  94. * */
  95. public function system_message_list()
  96. {
  97. $where = parent::getMore([
  98. ['page', 1],
  99. ['limit', 20],
  100. ['title', '']
  101. ]);
  102. return JsonService::successlayui(MessageModel::systemMessageList($where));
  103. }
  104. /**添加/编辑
  105. * @return mixed|void
  106. * @throws \think\exception\DbException
  107. */
  108. public function create()
  109. {
  110. return $this->fetch();
  111. }
  112. /**
  113. * 添加和修改讲师
  114. * @param int $id 修改
  115. * @return JsonService
  116. * */
  117. public function save_message()
  118. {
  119. $data = parent::postMore([
  120. ['name', ''],
  121. ['template_const', ''],
  122. ['tempkey', ''],
  123. ['temp_id', ''],
  124. ['sms_content', ''],
  125. ['is_wechat', 1],
  126. ['is_sms', 1],
  127. ]);
  128. if (!$data['name']) return JsonService::fail('请输入消息名称');
  129. if (!$data['template_const']) return JsonService::fail('请输入模版常数');
  130. if (!$data['tempkey']) return JsonService::fail('请编辑模板编号');
  131. $data['add_time'] = time();
  132. if (!MessageModel::be(['name' => $data['name']])) {
  133. $res = MessageModel::set($data);
  134. } else {
  135. return JsonService::fail('消息已存在');
  136. }
  137. if ($res)
  138. return JsonService::successful('添加成功');
  139. else
  140. return JsonService::fail('添加失败');
  141. }
  142. /**
  143. * 编辑模板消息
  144. * @param $id
  145. * @return mixed|\think\response\Json|void
  146. */
  147. public function edit($tempkey)
  148. {
  149. if (!$tempkey) return $this->failed('数据不存在');
  150. $template = WechatTemplateModel::where('tempkey', $tempkey)->find();
  151. $status = MessageModel::where('tempkey', $tempkey)->value('is_wechat');
  152. $f = array();
  153. $f[] = Form::input('tempkey', '模板编号', $template->getData('tempkey'))->disabled(1);
  154. $f[] = Form::input('name', '模板名', $template->getData('name'))->disabled(1);
  155. $f[] = Form::input('content', '内容', $template->getData('content'))->type('textarea')->disabled(1);
  156. $f[] = Form::input('tempid', '模板ID', $template->getData('tempid'));
  157. $f[] = Form::radio('status', '状态', $status)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  158. $form = Form::make_post_form('编辑模板消息', $f, Url::build('update', compact('tempkey')), 2);
  159. $this->assign(compact('form'));
  160. return $this->fetch('public/form-builder');
  161. }
  162. /**微信模版保存
  163. * @param Request $request
  164. * @param $tempkey
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public function update(Request $request, $tempkey)
  170. {
  171. $data = parent::postMore([
  172. 'tempid',
  173. ['status', 0]
  174. ], $request);
  175. if ($data['tempid'] == '') return JsonService::fail('请输入模板ID');
  176. if (!$tempkey) return JsonService::fail('数据不存在');
  177. $template = WechatTemplateModel::where('tempkey', $tempkey)->find();
  178. if (!$template) return JsonService::fail('数据不存在!');
  179. WechatTemplateModel::edit($data, $tempkey, 'tempkey');
  180. MessageModel::where('tempkey', $tempkey)->update(['is_wechat' => $data['status']]);
  181. return JsonService::successful('修改成功!');
  182. }
  183. /**
  184. * 编辑短信模板消息
  185. * @param $id
  186. * @return mixed|\think\response\Json|void
  187. */
  188. public function sms($id)
  189. {
  190. if (!$id) return $this->failed('数据不存在');
  191. $template = MessageModel::where('id', $id)->find();
  192. $f = array();
  193. $f[] = Form::input('temp_id', '模板编号', $template->getData('temp_id'))->disabled(1);
  194. $f[] = Form::input('name', '模板名', $template->getData('name'))->disabled(1);
  195. $f[] = Form::input('sms_content', '内容', $template->getData('sms_content'))->type('textarea')->disabled(1);
  196. $f[] = Form::radio('is_sms', '状态', $template->getData('is_sms'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  197. $form = Form::make_post_form('编辑短信模板消息', $f, Url::build('sms_update', compact('id')), 2);
  198. $this->assign(compact('form'));
  199. return $this->fetch('public/form-builder');
  200. }
  201. /**短信状态保存
  202. * @param Request $request
  203. * @param $id
  204. */
  205. public function sms_update(Request $request, $id)
  206. {
  207. $data = parent::postMore([
  208. ['is_sms', 0]
  209. ], $request);
  210. if (!$id) return JsonService::fail('数据不存在');
  211. MessageModel::edit($data, $id);
  212. return JsonService::successful('修改成功!');
  213. }
  214. /**
  215. * 快速编辑
  216. * @param string $field 字段名
  217. * @param int $id 修改的主键
  218. * @param string value 修改后的值
  219. * @return JsonService
  220. */
  221. public function set_value($field = '', $id = '', $value = '')
  222. {
  223. ($field == '' || $id == '' || $value == '') && JsonService::fail('缺少参数');
  224. $message = MessageModel::where('id', $id)->find();
  225. if ($field == 'is_wechat' && $value == 1 && $message['tempkey'] == '') {
  226. return JsonService::fail('微信模板编号不能为空');
  227. }
  228. if ($field == 'is_sms' && $value == 1 && $message['temp_id'] == '') {
  229. return JsonService::fail('短信模板ID不能为空');
  230. }
  231. $res = MessageModel::where('id', $id)->update([$field => $value]);
  232. if ($res)
  233. return JsonService::successful('保存成功');
  234. else
  235. return JsonService::fail('保存失败');
  236. }
  237. /**
  238. * 删除讲师
  239. * @param int $id 修改的主键
  240. * @return json
  241. * */
  242. public function delete($id = 0)
  243. {
  244. if (!$id) return JsonService::fail('缺少参数');
  245. if (MessageModel::del($id))
  246. return JsonService::successful('删除成功');
  247. else
  248. return JsonService::fail(MessageModel::getErrorInfo('删除失败'));
  249. }
  250. }