DataService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\data\service;
  3. use think\admin\Exception;
  4. use think\admin\Service;
  5. use think\admin\storage\LocalStorage;
  6. /**
  7. * 基础数据服务
  8. * Class DataService
  9. * @package app\agent\service
  10. */
  11. class DataService extends Service
  12. {
  13. /**
  14. * 获取支付配置
  15. * @return array|void
  16. * @throws Exception
  17. */
  18. public function payment(): array
  19. {
  20. try {
  21. $map = ['type' => PaymentService::PAYMENT_WECHAT_GZH, 'status' => 1, 'deleted' => 0];
  22. $payment = $this->app->db->name('ShopPayment')->where($map)->order('sort desc,id desc')->find();
  23. if (empty($payment)) throw new Exception('读取有效的支付参数失败');
  24. // 解析服务号支付参数
  25. [, , $params] = PaymentService::config('', $payment);
  26. if (empty($params)) throw new Exception('读取有效的支付参数失败');
  27. if (empty($params['wechat_mch_key_text']) || empty($params['wechat_mch_cert_text'])) {
  28. throw new Exception('微信商户支付证书内容不能为空');
  29. }
  30. $k1 = LocalStorage::instance()->set("{$params['code']}_key.pem", $params['wechat_mch_key_text'], true);
  31. $k2 = LocalStorage::instance()->set("{$params['code']}_cert.pem", $params['wechat_mch_cert_text'], true);
  32. return [
  33. 'appid' => $params['wechat_appid'],
  34. 'mch_id' => $params['wechat_mch_id'],
  35. 'mch_key' => $params['wechat_mch_key'],
  36. 'ssl_key' => $k1['file'],
  37. 'ssl_cer' => $k2['file'],
  38. 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
  39. ];
  40. } catch (\Exception $exception) {
  41. throw new Exception($exception->getMessage());
  42. }
  43. }
  44. }