/hdcmsedu/app/site/controller/Setting.php

https://github.com/houdunwang/video · PHP · 275 lines · 152 code · 33 blank · 90 comment · 12 complexity · 1f721eb9623e4e3ee8bee5b72c133929 MD5 · raw file

  1. <?php namespace app\site\controller;
  2. use houdunwang\request\Request;
  3. use houdunwang\view\View;
  4. use system\model\Message;
  5. use system\model\SiteSetting;
  6. use system\model\Site;
  7. use system\model\SiteWeChat;
  8. use houdunwang\config\Config;
  9. use houdunwang\mail\Mail;
  10. /**
  11. * 网站配置管理
  12. * Class Setting
  13. *
  14. * @package app\site\controller
  15. */
  16. class Setting extends Admin
  17. {
  18. //模型
  19. protected $db;
  20. //主键
  21. protected $id;
  22. /**
  23. * Setting constructor.
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. auth();
  29. $this->id = SiteSetting::where('siteid', SITEID)->pluck('id');
  30. $this->db = $this->id ? SiteSetting::find($this->id) : new SiteSetting();
  31. }
  32. /**
  33. * 站点全局配置项
  34. *
  35. * @param \system\model\Site $SiteModel
  36. *
  37. * @return mixed|string
  38. * @throws \Exception
  39. */
  40. public function config(Site $SiteModel)
  41. {
  42. if (IS_POST) {
  43. $this->db['config'] = Request::post('config');
  44. $this->db->save();
  45. $SiteModel->updateCache();
  46. return message('站点设置更新成功');
  47. }
  48. $field = v('site.setting.config');
  49. return view('', compact('field'));
  50. }
  51. /**
  52. * 积分设置
  53. *
  54. * @param \system\model\Site $SiteModel
  55. *
  56. * @return mixed|string
  57. * @throws \Exception
  58. */
  59. public function credit(Site $SiteModel)
  60. {
  61. if (IS_POST) {
  62. //积分/余额必须开启
  63. $_POST['creditnames']['credit1']['status'] = 1;
  64. $_POST['creditnames']['credit2']['status'] = 1;
  65. foreach ($_POST['creditnames'] as $credit => $d) {
  66. $_POST['creditnames'][$credit]['status'] = isset($d['status']) ? intval($d['status']) : 0;
  67. }
  68. $this->db['creditnames'] = json_encode($_POST['creditnames'], JSON_UNESCAPED_UNICODE);
  69. $this->db->save();
  70. $SiteModel->updateCache();
  71. return message('积分设置成功');
  72. }
  73. View::with('creditnames', v('site.setting.creditnames'));
  74. return view();
  75. }
  76. /**
  77. * 积分策略
  78. *
  79. * @param \system\model\Site $siteModel
  80. *
  81. * @return mixed|string
  82. * @throws \Exception
  83. */
  84. public function tactics(Site $siteModel)
  85. {
  86. if (IS_POST) {
  87. $this->db['creditbehaviors'] = json_encode(Request::post('creditbehaviors'), JSON_UNESCAPED_UNICODE);
  88. $this->db->save();
  89. $siteModel->updateCache();
  90. return message('积分策略更新成功');
  91. }
  92. return view();
  93. }
  94. /**
  95. * 注册设置
  96. *
  97. * @param \system\model\Site $siteModel
  98. *
  99. * @return mixed|string
  100. * @throws \Exception
  101. */
  102. public function register(Site $siteModel)
  103. {
  104. if (IS_POST) {
  105. $this->db['register'] = Request::post('register');
  106. $this->db['login'] = Request::post('login');
  107. $this->db->save();
  108. $siteModel->updateCache();
  109. return message('修改会员注册设置成功');
  110. }
  111. $register = v('site.setting.register');
  112. $login = v('site.setting.login');
  113. return view('', compact('register', 'login'));
  114. }
  115. /**
  116. * 邮件通知设置
  117. * @param \system\model\Site $siteModel
  118. *
  119. * @return mixed|string
  120. * @throws \Exception
  121. */
  122. public function mail(Site $siteModel)
  123. {
  124. if (IS_POST) {
  125. $config = Request::post('smtp');
  126. $this->db['smtp'] = json_encode($config, JSON_UNESCAPED_UNICODE);
  127. $this->db->save();
  128. $siteModel->updateCache();
  129. Config::set('mail', $config);
  130. //发送测试邮件
  131. if ($config['testing']) {
  132. $d = Mail::send($config['testusername'], $config['testusername'], "邮箱配置测试成功", function () {
  133. return View::instance()->with(['title' => '测试邮件发送成功', 'content' => '恭喜!站点邮箱配置正确'])->fetch('resource/view/email');
  134. });
  135. if ($d) {
  136. return message("测试邮件发送成功", 'refresh', 'success', 3);
  137. } else {
  138. return message("测试邮件发送失败", 'refresh', 'error', 3);
  139. }
  140. }
  141. return message('邮箱配置保存成功', 'refresh');
  142. }
  143. return view();
  144. }
  145. /**
  146. * 短信通知设置
  147. *
  148. * @param \system\model\Site $siteModel
  149. *
  150. * @return mixed|string
  151. * @throws \Exception
  152. */
  153. public function mobile(Site $siteModel)
  154. {
  155. if (IS_POST) {
  156. $this->db['sms'] = Request::post('data');
  157. $this->db->save();
  158. $siteModel->updateCache();
  159. return message('配置保存成功', 'refresh');
  160. }
  161. $sms = json_encode(v('site.setting.sms'));
  162. return view()->with('sms', $sms);
  163. }
  164. /**
  165. * 发送阿里云测试验证码
  166. *
  167. * @return mixed
  168. */
  169. public function aliyunCodeTest()
  170. {
  171. $post = json_decode(Request::post('data'), true);
  172. $message = new Message();
  173. $config = [
  174. 'sign' => $post['aliyun']['code']['sign'],
  175. 'template' => $post['aliyun']['code']['template'],
  176. 'user' => $post['aliyun']['code']['test_mobile'],
  177. ];
  178. return $message->sendCode($config, 60);
  179. }
  180. /**
  181. * 微信支付
  182. *
  183. * @param \system\model\Site $SiteModel
  184. * @param \system\model\SiteWeChat $WeChatModel
  185. *
  186. * @return mixed|string
  187. * @throws \Exception
  188. */
  189. public function wepay(Site $SiteModel, SiteWeChat $WeChatModel)
  190. {
  191. if (IS_POST) {
  192. $this->db['id'] = $this->id;
  193. $this->db['pay'] = Request::post('data');
  194. $this->db->save();
  195. $SiteModel->updateCache();
  196. return message('修改会员支付参数成功', 'back');
  197. }
  198. $wechat = SiteWeChat::where('siteid', siteid())->first();
  199. $weChatLevel = $WeChatModel->chatNameBylevel($wechat['level']);
  200. $data = v('site.setting.pay');
  201. return view()->with(compact('wechat', 'weChatLevel', 'data'));
  202. }
  203. /**
  204. * 支付宝支付
  205. *
  206. * @param \system\model\Site $SiteModel
  207. *
  208. * @return mixed|string
  209. * @throws \Exception
  210. */
  211. public function alipay(Site $SiteModel)
  212. {
  213. if (IS_POST) {
  214. $this->db['id'] = $this->id;
  215. $this->db['pay'] = Request::post('data');
  216. $this->db->save();
  217. $SiteModel->updateCache();
  218. return message('修改支付宝参数成功', 'back');
  219. }
  220. $data = v('site.setting.pay');
  221. return view()->with(compact('data'));
  222. }
  223. /**
  224. * 阿里云配置
  225. *
  226. * @param \system\model\Site $SiteModel
  227. *
  228. * @return mixed|string
  229. * @throws \Exception
  230. */
  231. public function aliyun(Site $SiteModel)
  232. {
  233. if (IS_POST) {
  234. $this->db['id'] = $this->id;
  235. $this->db['aliyun'] = Request::post('config');
  236. $this->db->save();
  237. $SiteModel->updateCache();
  238. return message('修改会员支付参数成功', 'back');
  239. }
  240. return view();
  241. }
  242. }