Setting.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\store\controller;
  3. use app\store\model\Printer as PrinterModel;
  4. use app\store\model\Setting as SettingModel;
  5. use app\common\library\sms\Driver as SmsDriver;
  6. /**
  7. * 系统设置
  8. * Class Setting
  9. * @package app\store\controller
  10. */
  11. class Setting extends Controller
  12. {
  13. /**
  14. * 商城设置
  15. * @return mixed
  16. * @throws \think\exception\DbException
  17. */
  18. public function store()
  19. {
  20. return $this->updateEvent('store');
  21. }
  22. /**
  23. * 交易设置
  24. * @return mixed
  25. * @throws \think\exception\DbException
  26. */
  27. public function trade()
  28. {
  29. return $this->updateEvent('trade');
  30. }
  31. /**
  32. * 短信通知
  33. * @return mixed
  34. * @throws \think\exception\DbException
  35. */
  36. public function sms()
  37. {
  38. return $this->updateEvent('sms');
  39. }
  40. /**
  41. * 发送订阅消息
  42. * @return mixed
  43. * @throws \think\exception\DbException
  44. */
  45. public function tplMsg()
  46. {
  47. return $this->updateEvent('tplMsg');
  48. }
  49. /**
  50. * 发送短信通知测试
  51. * @param $AccessKeyId
  52. * @param $AccessKeySecret
  53. * @param $sign
  54. * @param $msg_type
  55. * @param $template_code
  56. * @param $accept_phone
  57. * @return array
  58. * @throws \think\Exception
  59. */
  60. public function smsTest($AccessKeyId, $AccessKeySecret, $sign, $msg_type, $template_code, $accept_phone)
  61. {
  62. $SmsDriver = new SmsDriver([
  63. 'default' => 'aliyun',
  64. 'engine' => [
  65. 'aliyun' => [
  66. 'AccessKeyId' => $AccessKeyId,
  67. 'AccessKeySecret' => $AccessKeySecret,
  68. 'sign' => $sign,
  69. $msg_type => compact('template_code', 'accept_phone'),
  70. ],
  71. ],
  72. ]);
  73. $templateParams = [];
  74. if ($msg_type === 'order_pay') {
  75. $templateParams = ['order_no' => '2018071200000000'];
  76. }
  77. if ($SmsDriver->sendSms($msg_type, $templateParams, true)) {
  78. return $this->renderSuccess('发送成功');
  79. }
  80. return $this->renderError('发送失败 ' . $SmsDriver->getError());
  81. }
  82. /**
  83. * 上传设置
  84. * @return mixed
  85. * @throws \think\exception\DbException
  86. */
  87. public function storage()
  88. {
  89. return $this->updateEvent('storage');
  90. }
  91. /**
  92. * 小票打印设置
  93. * @return mixed
  94. * @throws \think\exception\DbException
  95. */
  96. public function printer()
  97. {
  98. // 获取打印机列表
  99. $printerList = PrinterModel::getAll();
  100. return $this->updateEvent('printer', compact('printerList'));
  101. }
  102. /**
  103. * 更新商城设置事件
  104. * @param $key
  105. * @param $vars
  106. * @return array|mixed
  107. * @throws \think\exception\DbException
  108. */
  109. private function updateEvent($key, $vars = [])
  110. {
  111. if (!$this->request->isAjax()) {
  112. $vars['values'] = SettingModel::getItem($key);
  113. return $this->fetch($key, $vars);
  114. }
  115. $model = new SettingModel;
  116. if ($model->edit($key, $this->postData($key))) {
  117. return $this->renderSuccess('操作成功');
  118. }
  119. return $this->renderError($model->getError() ?: '操作失败');
  120. }
  121. }