Просмотр исходного кода

getWxappCache 改为 getWxappConfig

Yahoo 4 лет назад
Родитель
Сommit
cf046b48f9

+ 10 - 10
app/api/service/Payment.php

@@ -8,17 +8,17 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\api\service;
 
-use app\api\model\Wxapp as WxappModel;
 use app\api\service\User as UserService;
+use app\api\model\wxapp\Setting as WxappSettingModel;
 use app\common\enum\OrderType as OrderTypeEnum;
 use app\common\enum\order\PayType as OrderPayTypeEnum;
+use app\common\service\BaseService;
 use app\common\library\wechat\WxPay;
 use app\common\exception\BaseException;
-use app\common\service\BaseService;
 
 /**
  * 订单支付服务类
@@ -37,7 +37,7 @@ class Payment extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function orderPayment($order, $payType)
+    public static function orderPayment($order, $payType): array
     {
         if ($payType == OrderPayTypeEnum::WECHAT) {
             return self::wechat(
@@ -55,7 +55,7 @@ class Payment extends BaseService
      * @param $orderId
      * @param $orderNo
      * @param $payPrice
-     * @param $orderType
+     * @param int $orderType
      * @return array
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
@@ -66,8 +66,8 @@ class Payment extends BaseService
         $orderId,
         $orderNo,
         $payPrice,
-        $orderType = OrderTypeEnum::ORDER
-    )
+        int $orderType = OrderTypeEnum::ORDER
+    ): array
     {
         // 获取当前用户信息
         $userInfo = UserService::getCurrentLoginUser(true);
@@ -87,9 +87,9 @@ class Payment extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private static function getWxConfig()
+    private static function getWxConfig(): array
     {
-        return WxappModel::getWxappCache(getStoreId());
+        $storeId = getStoreId();
+        return WxappSettingModel::getWxappConfig($storeId);
     }
-
 }

+ 17 - 5
app/api/service/user/Oauth.php

@@ -12,11 +12,11 @@ declare (strict_types=1);
 
 namespace app\api\service\user;
 
-use app\api\model\Wxapp as WxappModel;
-use app\common\exception\BaseException;
-use app\common\library\wechat\WxUser;
-use app\common\service\BaseService;
 use app\api\model\UserOauth as UserOauthModel;
+use app\api\model\wxapp\Setting as WxappSettingModel;
+use app\common\service\BaseService;
+use app\common\library\wechat\WxUser;
+use app\common\exception\BaseException;
 
 /**
  * 服务类: 第三方用户服务类
@@ -94,7 +94,7 @@ class Oauth extends BaseService
     public static function wxCode2Session(string $code)
     {
         // 获取当前小程序信息
-        $wxConfig = WxappModel::getWxappCache();
+        $wxConfig = static::getMpWxConfig();
         // 微信登录 (获取session_key)
         $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
         $result = $WxUser->jscode2session($code);
@@ -102,6 +102,18 @@ class Oauth extends BaseService
         return $result;
     }
 
+    /**
+     * 获取微信小程序配置项
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private static function getMpWxConfig(): array
+    {
+        return WxappSettingModel::getWxappConfig();
+    }
+
     /**
      * 根据openid获取用户ID
      * @param string $oauthId 第三方用户唯一标识 (openid)

+ 53 - 68
app/common/library/wechat/WxPay.php

@@ -10,7 +10,7 @@
 // +----------------------------------------------------------------------
 namespace app\common\library\wechat;
 
-use app\common\model\Wxapp as WxappModel;
+use app\common\model\wxapp\Setting as WxappSettingModel;
 use app\common\enum\OrderType as OrderTypeEnum;
 use app\common\enum\order\PayType as OrderPayTypeEnum;
 use app\common\library\helper;
@@ -26,6 +26,9 @@ class WxPay extends WxBase
     // 微信支付配置
     private $config;
 
+    // 当前商城ID
+    private $storeId;
+
     // 订单模型
     private $modelClass = [
         OrderTypeEnum::ORDER => \app\api\service\order\PaySuccess::class,
@@ -34,14 +37,17 @@ class WxPay extends WxBase
 
     /**
      * 构造函数
-     * WxPay constructor.
-     * @param $config
+     * @param array $config
+     * @param int|null $storeId
      */
-    public function __construct($config = false)
+    public function __construct(array $config = [], ?int $storeId = null)
     {
         parent::__construct();
         $this->config = $config;
-        $this->config !== false && $this->setConfig($this->config['app_id'], $this->config['app_secret']);
+        $this->storeId = $storeId;
+        if (!empty($this->config)) {
+            $this->setConfig($this->config['app_id'], $this->config['app_secret']);
+        }
     }
 
     /**
@@ -51,9 +57,9 @@ class WxPay extends WxBase
      * @param $totalFee
      * @param int $orderType 订单类型
      * @return array
-     * @throws BaseException
+     * @throws \cores\exception\BaseException
      */
-    public function unifiedorder($orderNo, $openid, $totalFee, $orderType = OrderTypeEnum::ORDER)
+    public function unifiedorder($orderNo, $openid, $totalFee, int $orderType = OrderTypeEnum::ORDER): array
     {
         // 当前时间
         $time = time();
@@ -82,11 +88,11 @@ class WxPay extends WxBase
         // 请求失败
         if ($prepay['return_code'] === 'FAIL') {
             $errMsg = "微信支付api:{$prepay['return_msg']}";
-            throwError($errMsg, null, ['error_code' => 'WECHAT_PAY', 'is_created' => true]);
+            throwError($errMsg, null, ['errorCode' => 'WECHAT_PAY', 'isCreated' => true]);
         }
         if ($prepay['result_code'] === 'FAIL') {
             $errMsg = "微信支付api:{$prepay['err_code_des']}";
-            throwError($errMsg, null, ['error_code' => 'WECHAT_PAY', 'is_created' => true]);
+            throwError($errMsg, null, ['errorCode' => 'WECHAT_PAY', 'isCreated' => true]);
         }
         // 生成 nonce_str 供前端使用
         $paySign = $this->makePaySign($params['nonce_str'], $prepay['prepay_id'], $time);
@@ -100,31 +106,12 @@ class WxPay extends WxBase
 
     /**
      * 支付成功异步通知
-     * @throws BaseException
-     * @throws \Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
     public function notify()
     {
-//        $xml = <<<EOF
-//<xml><appid><![CDATA[wx8908532a27c5dd4f]]></appid>
-//<attach><![CDATA[{"order_type":10}]]></attach>
-//<bank_type><![CDATA[CFT]]></bank_type>
-//<cash_fee><![CDATA[1]]></cash_fee>
-//<fee_type><![CDATA[CNY]]></fee_type>
-//<is_subscribe><![CDATA[N]]></is_subscribe>
-//<mch_id><![CDATA[1509822581]]></mch_id>
-//<nonce_str><![CDATA[ca1fe6d2b4f667cf249bd1d7176c6178]]></nonce_str>
-//<openid><![CDATA[oZDDE5JLnVyc6qe6nbNWdbFHtY5I]]></openid>
-//<out_trade_no><![CDATA[2019040155491005]]></out_trade_no>
-//<result_code><![CDATA[SUCCESS]]></result_code>
-//<return_code><![CDATA[SUCCESS]]></return_code>
-//<sign><![CDATA[3880232710B7328822D079DC405FB09D]]></sign>
-//<time_end><![CDATA[20190401104804]]></time_end>
-//<total_fee>1</total_fee>
-//<trade_type><![CDATA[JSAPI]]></trade_type>
-//<transaction_id><![CDATA[4200000265201904014227830207]]></transaction_id>
-//</xml>
-//EOF;
         if (!$xml = file_get_contents('php://input')) {
             $this->returnCode(false, 'Not found DATA');
         }
@@ -139,7 +126,7 @@ class WxPay extends WxBase
         $order = $model->getOrderInfo();
         empty($order) && $this->returnCode(false, '订单不存在');
         // 小程序配置信息
-        $wxConfig = WxappModel::getWxappCache($order['store_id']);
+        $wxConfig = WxappSettingModel::getWxappConfig($order['store_id']);
         // 设置支付秘钥
         $this->config['apikey'] = $wxConfig['apikey'];
         // 保存微信服务器返回的签名sign
@@ -167,27 +154,28 @@ class WxPay extends WxBase
 
     /**
      * 申请退款API
-     * @param $transaction_id
-     * @param double $total_fee 订单总金额
-     * @param double $refund_fee 退款金额
+     * @param string $transactionId 微信支付交易流水号
+     * @param string $totalFee 订单总金额
+     * @param string $refundFee 退款金额
      * @return bool
      * @throws BaseException
+     * @throws \cores\exception\BaseException
      */
-    public function refund($transaction_id, $total_fee, $refund_fee)
+    public function refund(string $transactionId, string $totalFee, string $refundFee): bool
     {
         // 当前时间
         $time = time();
         // 生成随机字符串
-        $nonceStr = md5($time . $transaction_id . $total_fee . $refund_fee);
+        $nonceStr = md5($time . $transactionId . $totalFee . $refundFee);
         // API参数
         $params = [
             'appid' => $this->appId,
             'mch_id' => $this->config['mchid'],
             'nonce_str' => $nonceStr,
-            'transaction_id' => $transaction_id,
+            'transaction_id' => $transactionId,
             'out_refund_no' => $time,
-            'total_fee' => $total_fee * 100,
-            'refund_fee' => $refund_fee * 100,
+            'total_fee' => $totalFee * 100,
+            'refund_fee' => $refundFee * 100,
         ];
         // 生成签名
         $params['sign'] = $this->makeSign($params);
@@ -218,14 +206,15 @@ class WxPay extends WxBase
 
     /**
      * 企业付款到零钱API
-     * @param $orderNo
-     * @param $openid
-     * @param $amount
-     * @param $desc
+     * @param string $orderNo 订单号
+     * @param string $openid 微信用户openid
+     * @param string $amount 支付金额
+     * @param string $desc 描述
      * @return bool
      * @throws BaseException
+     * @throws \cores\exception\BaseException
      */
-    public function transfers($orderNo, $openid, $amount, $desc)
+    public function transfers(string $orderNo, string $openid, string $amount, string $desc): bool
     {
         // API参数
         $params = [
@@ -262,16 +251,16 @@ class WxPay extends WxBase
 
     /**
      * 获取cert证书文件
-     * @return array
-     * @throws BaseException
+     * @return string[]
+     * @throws \cores\exception\BaseException
      */
-    private function getCertPem()
+    private function getCertPem(): array
     {
         if (empty($this->config['cert_pem']) || empty($this->config['key_pem'])) {
             throwError('请先到后台小程序设置填写微信支付证书文件');
         }
         // cert目录
-        $filePath = __DIR__ . '/cert/' . $this->config['store_id'] . '/';
+        $filePath = __DIR__ . "/cert/{$this->storeId}/";
         return [
             'certPem' => $filePath . 'cert.pem',
             'keyPem' => $filePath . 'key.pem'
@@ -314,17 +303,17 @@ class WxPay extends WxBase
 
     /**
      * 生成paySign
-     * @param $nonceStr
-     * @param $prepay_id
-     * @param $timeStamp
+     * @param string $nonceStr
+     * @param string $prepayId
+     * @param int $timeStamp
      * @return string
      */
-    private function makePaySign($nonceStr, $prepay_id, $timeStamp)
+    private function makePaySign(string $nonceStr, string $prepayId, int $timeStamp): string
     {
         $data = [
             'appId' => $this->appId,
             'nonceStr' => $nonceStr,
-            'package' => 'prepay_id=' . $prepay_id,
+            'package' => 'prepay_id=' . $prepayId,
             'signType' => 'MD5',
             'timeStamp' => $timeStamp,
         ];
@@ -336,16 +325,15 @@ class WxPay extends WxBase
         // 签名步骤三:MD5加密
         $string = md5($string);
         // 签名步骤四:所有字符转为大写
-        $result = strtoupper($string);
-        return $result;
+        return strtoupper($string);
     }
 
     /**
      * 将xml转为array
-     * @param $xml
+     * @param string $xml
      * @return mixed
      */
-    private function fromXml($xml)
+    private function fromXml(string $xml)
     {
         // 禁止引用外部xml实体
         libxml_disable_entity_loader(true);
@@ -354,10 +342,10 @@ class WxPay extends WxBase
 
     /**
      * 生成签名
-     * @param $values
+     * @param array $values
      * @return string 本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
      */
-    private function makeSign($values)
+    private function makeSign(array $values): string
     {
         //签名步骤一:按字典序排序参数
         ksort($values);
@@ -367,16 +355,15 @@ class WxPay extends WxBase
         //签名步骤三:MD5加密
         $string = md5($string);
         //签名步骤四:所有字符转为大写
-        $result = strtoupper($string);
-        return $result;
+        return strtoupper($string);
     }
 
     /**
      * 格式化参数格式化成url参数
-     * @param $values
+     * @param array $values
      * @return string
      */
-    private function toUrlParams($values)
+    private function toUrlParams(array $values): string
     {
         $buff = '';
         foreach ($values as $k => $v) {
@@ -389,17 +376,16 @@ class WxPay extends WxBase
 
     /**
      * 输出xml字符
-     * @param $values
+     * @param array $values
      * @return bool|string
      */
-    private function toXml($values)
+    private function toXml(array $values)
     {
         if (!is_array($values)
             || count($values) <= 0
         ) {
             return false;
         }
-
         $xml = "<xml>";
         foreach ($values as $key => $val) {
             if (is_numeric($val)) {
@@ -411,5 +397,4 @@ class WxPay extends WxBase
         $xml .= "</xml>";
         return $xml;
     }
-
 }

+ 8 - 30
app/common/model/Wxapp.php

@@ -12,6 +12,7 @@ declare (strict_types=1);
 
 namespace app\common\model;
 
+use app\common\library\helper;
 use cores\BaseModel;
 use think\facade\Cache;
 use app\common\exception\BaseException;
@@ -30,40 +31,17 @@ class Wxapp extends BaseModel
     protected $pk = 'id';
 
     /**
-     * 获取小程序信息
-     * @param int|null $storeId
-     * @return array|static|null
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\DbException
-     * @throws \think\db\exception\ModelNotFoundException
-     */
-    public static function detail(?int $storeId = null)
-    {
-        $storeId = $storeId ?: static::$storeId;
-        return (new static)->where(['store_id' => $storeId])->find();
-    }
-
-    /**
-     * 从缓存中获取小程序信息
+     * 获取微信小程序配置 (即将废弃, 用于兼容v2.0.4之前)
      * @param int|null $storeId
      * @return array
-     * @throws BaseException
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\DbException
-     * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function getWxappCache(?int $storeId = null)
+    public static function getOldData(?int $storeId = null): array
     {
-        // 小程序id
-        is_null($storeId) && $storeId = static::$storeId;
-        if (!$data = Cache::get("wxapp_{$storeId}")) {
-            // 获取小程序详情, 解除hidden属性
-            $detail = self::detail($storeId);
-            empty($detail) && throwError('未找到当前小程序信息');
-            // 写入缓存
-            $data = $detail->hidden([])->toArray();
-            Cache::tag('cache')->set("wxapp_{$storeId}", $data);
+        empty($storeId) && $storeId = static::$storeId;
+        $detail = static::get(['store_id' => $storeId]);
+        if (empty($detail)) {
+            return [];
         }
-        return $data;
+        return helper::pick($detail->toArray(), ['app_id', 'app_secret', 'mchid', 'apikey', 'cert_pem', 'key_pem']);
     }
 }

+ 3 - 3
app/common/model/h5/Setting.php

@@ -73,8 +73,8 @@ class Setting extends BaseModel
      */
     public static function getAll(int $storeId = null): array
     {
+        is_null($storeId) && $storeId = static::$storeId;
         $model = new static;
-        is_null($storeId) && $storeId = $model::$storeId;
         if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
             // 获取全部设置
             $setting = $model->getList($storeId);
@@ -82,8 +82,8 @@ class Setting extends BaseModel
             // 写入缓存中
             Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
         }
-        // 重组setting缓存数据 (多维)
-        return static::reorganize($model->defaultData(), $data, $type = 'cache');
+        // 合并默认设置
+        return array_merge_multiple($model->defaultData(), $data);
     }
 
     /**

+ 3 - 3
app/common/model/store/Setting.php

@@ -90,10 +90,10 @@ class Setting extends BaseModel
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function getAll(int $storeId = null)
+    public static function getAll(int $storeId = null): array
     {
+        is_null($storeId) && $storeId = static::$storeId;
         $model = new static;
-        is_null($storeId) && $storeId = $model::$storeId;
         if (!$data = Cache::get("setting_{$storeId}")) {
             // 获取商城设置列表
             $setting = $model->getList($storeId);
@@ -122,7 +122,7 @@ class Setting extends BaseModel
      * @param array $actualData
      * @return array
      */
-    private function getMergeData(array $actualData)
+    private function getMergeData(array $actualData): array
     {
         return array_merge_multiple($this->defaultData(), $actualData);
     }

+ 48 - 9
app/common/model/wxapp/Setting.php

@@ -14,6 +14,7 @@ namespace app\common\model\wxapp;
 
 use cores\BaseModel;
 use think\facade\Cache;
+use app\common\model\Wxapp as WxappModel;
 use app\common\library\helper;
 
 /**
@@ -63,6 +64,19 @@ class Setting extends BaseModel
         return isset($data[$key]) ? $data[$key]['values'] : [];
     }
 
+    /**
+     * 获取微信小程序基础配置
+     * @param int|null $storeId
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getWxappConfig(?int $storeId = null): array
+    {
+        return static::getItem('basic', $storeId);
+    }
+
     /**
      * 获取全部设置
      * @param int|null $storeId
@@ -71,32 +85,57 @@ class Setting extends BaseModel
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public static function getAll(int $storeId = null): array
+    public static function getAll(?int $storeId = null): array
     {
         $model = new static;
-        is_null($storeId) && $storeId = $model::$storeId;
+        is_null($storeId) && $storeId = static::$storeId;
         if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
             // 获取全部设置
-            $setting = $model->getList($storeId);
-            $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key');
+            $data = $model->getList($storeId);
             // 写入缓存中
             Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
         }
-        // 重组setting缓存数据 (多维)
-        return static::reorganize($model->defaultData(), $data, $type = 'cache');
+        // 合并默认设置
+        return array_merge_multiple($model->defaultData(), $data);
     }
 
     /**
      * 获取商城设置列表
      * @param int $storeId
-     * @return \think\Collection
+     * @return array
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function getList(int $storeId): \think\Collection
+    private function getList(int $storeId): array
     {
-        return $this->where('store_id', '=', $storeId)->select();
+        // 获取所有设置项
+        $data = $this->where('store_id', '=', $storeId)->select();
+        $setting = $data->isEmpty() ? [] : helper::arrayColumn2Key($data->toArray(), 'key');
+        // 兼容旧数据
+        return static::compatibleOld($setting, $storeId);
+    }
+
+    /**
+     * 兼容老版本数据 (v2.0.4之前)
+     * @param array $newSetting
+     * @param int|null $storeId
+     * @return array
+     */
+    private static function compatibleOld(array $newSetting, ?int $storeId = null): array
+    {
+        if (empty($newSetting) || !isset($newSetting['basic'])) {
+            $basic = WxappModel::getOldData($storeId);
+            $newSetting['basic']['values'] = $basic;
+            (new static)->save([
+                'key' => 'basic',
+                'describe' => '基础设置',
+                'values' => $basic,
+                'store_id' => $storeId,
+                'update_time' => time(),
+            ]);
+        }
+        return $newSetting;
     }
 
     /**

+ 16 - 10
app/common/service/order/Refund.php

@@ -8,12 +8,13 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\service\order;
 
+use app\common\exception\BaseException;
 use app\common\model\User as UserModel;
-use app\common\model\Wxapp as WxappModel;
+use app\common\model\wxapp\Setting as WxappSettingModel;
 use app\common\model\user\BalanceLog as BalanceLogModel;
 use app\common\enum\order\PayType as OrderPayTypeEnum;
 use app\common\enum\user\balanceLog\Scene as SceneEnum;
@@ -29,12 +30,15 @@ class Refund extends BaseService
 {
     /**
      * 执行订单退款
-     * @param object $order 订单信息
+     * @param mixed $order 订单信息
      * @param null $money 指定退款金额
      * @return bool
-     * @throws \app\common\exception\BaseException
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
-    public function execute($order, $money = null)
+    public function execute($order, $money = null): bool
     {
         // 退款金额,如不指定则默认为订单实付款金额
         is_null($money) && $money = $order['pay_price'];
@@ -55,7 +59,7 @@ class Refund extends BaseService
      * @param $money
      * @return bool
      */
-    private function balance($order, $money)
+    private function balance($order, $money): bool
     {
         // 回退用户余额
         UserModel::setIncBalance((int)$order['user_id'], (float)$money);
@@ -72,13 +76,15 @@ class Refund extends BaseService
      * @param $order
      * @param $money
      * @return bool
-     * @throws \app\common\exception\BaseException
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
-    private function wxpay($order, $money)
+    private function wxpay($order, $money): bool
     {
-        $wxConfig = WxappModel::getWxappCache($order['store_id']);
+        $wxConfig = WxappSettingModel::getWxappConfig($order['store_id']);
         $WxPay = new WxPay($wxConfig);
         return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
     }
-
 }