quanwei
2025-12-04 12913c1069347ea4b1f6ab87f480da0f8d8c646a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
 
namespace app\shop\model\app;
 
use app\common\model\app\AppWx as AppWxModel;
use think\facade\Cache;
use app\common\library\fbpay\FbPay;
 
/**
 * 微信小程序模型
 */
class AppWx extends AppWxModel
{
    /**
     * 更新小程序设置
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            // 删除app缓存
            self::deleteCache();
            if (!empty($data['cert_pem']) && !empty($data['key_pem'])) {
                // 写入微信支付证书文件
                $this->writeCertPemFiles($data['cert_pem'], $data['key_pem']);
            }
            $where['app_id'] = self::$app_id;
 
            $count = $this->count($where);
            // 更新小程序设置
            if ($count > 0) {
                self::update($data, $where);
            }
            if ($count == 0) {
                $data['app_id'] = self::$app_id;
                self::create($data);
            }
 
            //如果是付呗接口,配置参数 by lyzflash
            /*if($data['apitype']) {
                $app = FbPay::getFbPayApp(self::$app_id, 'wx');
                $FbPay = new FbPay($app);
                $res = $FbPay->wxconfig();
                if($res != true) {
                    $this->error = $res;
                    $this->rollback();
                    return false;
                }
            }*/
 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 总后台获取付呗支付配置
     */
    public function syncGet($data)
    {
       $setting = AppWxModel::getAppWxCache($data["app_id"]);
       return $setting;
    }
 
    /**
     * 总后台同步设置付呗支付方法
     */
    public function syncEdit($data)
    {
        $this->startTrans();
        try {
            // 删除app缓存
            self::deleteCache();
            $app_id = $data["app_id"];
            $where['app_id'] = $app_id;
 
            $count = $this->count($where);
            // 更新小程序设置
            if ($count > 0) {
                self::update($data, $where);
            }
            if ($count == 0) {
                $data['app_id'] = $app_id;
                self::create($data);
            }
 
            //如果是付呗接口,配置参数 by yj
            $app = FbPay::getFbPayApp($app_id, 'wx');
            $FbPay = new FbPay($app);
            $res = $FbPay->wxconfig();
            if($res != true) {
                $this->error = $res;
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function count($where)
    {
        return $this->where($where)->count();
    }
 
    /**
     * 写入cert证书文件
     */
    private function writeCertPemFiles($cert_pem = '', $key_pem = '')
    {
        if (empty($cert_pem) || empty($key_pem)) {
            return false;
        }
        // 证书目录
        $filePath = root_path() . 'runtime/cert/appwx/' . self::$app_id . '/';
        // 目录不存在则自动创建
        if (!is_dir($filePath)) {
            mkdir($filePath, 0755, true);
        }
        // 写入cert.pem文件
        if (!empty($cert_pem)) {
            file_put_contents($filePath . 'cert.pem', $cert_pem);
        }
        // 写入key.pem文件
        if (!empty($key_pem)) {
            file_put_contents($filePath . 'key.pem', $key_pem);
        }
        return true;
    }
    /**
     * 删除app缓存
     */
    public static function deleteCache()
    {
        return Cache::delete('app_wx_' . self::$app_id);
    }
 
}