Wxapp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model;
  10. use app\common\model\Wxapp as WxappModel;
  11. use think\Cache;
  12. /**
  13. * 微信小程序模型
  14. * Class Wxapp
  15. * @package app\store\model
  16. */
  17. class Wxapp extends WxappModel
  18. {
  19. /**
  20. * 更新小程序设置
  21. * @param $data
  22. * @return bool
  23. * @throws \think\exception\PDOException
  24. */
  25. public function edit($data)
  26. {
  27. $this->startTrans();
  28. try {
  29. // 删除wxapp缓存
  30. self::deleteCache();
  31. // 写入微信支付证书文件
  32. $this->writeCertPemFiles($data['cert_pem'], $data['key_pem']);
  33. // 更新小程序设置
  34. $this->allowField(true)->save($data);
  35. $this->commit();
  36. return true;
  37. } catch (\Exception $e) {
  38. $this->error = $e->getMessage();
  39. $this->rollback();
  40. return false;
  41. }
  42. }
  43. /**
  44. * 写入cert证书文件
  45. * @param string $cert_pem
  46. * @param string $key_pem
  47. * @return bool
  48. */
  49. private function writeCertPemFiles($cert_pem = '', $key_pem = '')
  50. {
  51. if (empty($cert_pem) || empty($key_pem)) {
  52. return false;
  53. }
  54. // 证书目录
  55. $filePath = APP_PATH . 'common/library/wechat/cert/' . self::$wxapp_id . '/';
  56. // 目录不存在则自动创建
  57. if (!is_dir($filePath)) {
  58. mkdir($filePath, 0755, true);
  59. }
  60. // 写入cert.pem文件
  61. if (!empty($cert_pem)) {
  62. file_put_contents($filePath . 'cert.pem', $cert_pem);
  63. }
  64. // 写入key.pem文件
  65. if (!empty($key_pem)) {
  66. file_put_contents($filePath . 'key.pem', $key_pem);
  67. }
  68. return true;
  69. }
  70. /**
  71. * 记录图片信息
  72. * @param $wxapp_id
  73. * @param $oldFileId
  74. * @param $newFileName
  75. * @param $fromType
  76. * @return int|mixed
  77. */
  78. private function uploadImage($wxapp_id, $oldFileId, $newFileName, $fromType)
  79. {
  80. // $UploadFile = new UploadFile;
  81. $UploadFileUsed = new UploadFileUsed;
  82. if ($oldFileId > 0) {
  83. // 获取原图片path
  84. $oldFileName = UploadFile::getFileName($oldFileId);
  85. // 新文件与原来路径一致, 代表用户未修改, 不做更新
  86. if ($newFileName === $oldFileName)
  87. return $oldFileId;
  88. // 删除原文件使用记录
  89. $UploadFileUsed->remove('service', $oldFileId);
  90. }
  91. // 删除图片
  92. if (empty($newFileName)) return 0;
  93. // 查询新文件file_id
  94. $fileId = UploadFile::getFildIdByName($newFileName);
  95. // 添加文件使用记录
  96. $UploadFileUsed->add([
  97. 'file_id' => $fileId,
  98. 'wxapp_id' => $wxapp_id,
  99. 'from_type' => $fromType
  100. ]);
  101. return $fileId;
  102. }
  103. /**
  104. * 删除wxapp缓存
  105. * @return bool
  106. */
  107. public static function deleteCache()
  108. {
  109. return Cache::rm('wxapp_' . self::$wxapp_id);
  110. }
  111. }