Procházet zdrojové kódy

工程师端微信小程序配置

whitefang před 1 rokem
rodič
revize
752326c0c8

+ 23 - 4
app/adminapi/controller/channel/MnpSettingsController.php

@@ -25,11 +25,10 @@ use app\adminapi\validate\channel\MnpSettingsValidate;
  */
 class MnpSettingsController extends BaseAdminController
 {
+    //客户端微信小程序
     /**
      * @notes 获取小程序配置
      * @return \think\response\Json
-     * @author ljj
-     * @date 2022/2/16 9:38 上午
      */
     public function getConfig()
     {
@@ -40,8 +39,6 @@ class MnpSettingsController extends BaseAdminController
     /**
      * @notes 设置小程序配置
      * @return \think\response\Json
-     * @author ljj
-     * @date 2022/2/16 9:51 上午
      */
     public function setConfig()
     {
@@ -49,4 +46,26 @@ class MnpSettingsController extends BaseAdminController
         (new MnpSettingsLogic())->setConfig($params);
         return $this->success('操作成功', [], 1, 1);
     }
+
+    //工程师端微信小程序
+    /**
+     * @notes 获取小程序配置
+     * @return \think\response\Json
+     */
+    public function getWorkerConfig()
+    {
+        $result = (new MnpSettingsLogic())->getWorkerConfig();
+        return $this->data($result);
+    }
+
+    /**
+     * @notes 设置小程序配置
+     * @return \think\response\Json
+     */
+    public function setWorkerConfig()
+    {
+        $params = (new MnpSettingsValidate())->post()->goCheck();
+        (new MnpSettingsLogic())->setWorkerConfig($params);
+        return $this->success('操作成功', [], 1, 1);
+    }
 }

+ 41 - 0
app/adminapi/logic/channel/MnpSettingsLogic.php

@@ -69,4 +69,45 @@ class MnpSettingsLogic extends BaseLogic
         ConfigService::set('mnp_setting','app_id',$params['app_id']);
         ConfigService::set('mnp_setting','app_secret',$params['app_secret']);
     }
+
+    /**
+     * @notes 获取师傅端小程序配置
+     * @return array
+     */
+    public function getWorkerConfig()
+    {
+        $domainName = $_SERVER['SERVER_NAME'];
+        $qrCode = ConfigService::get('mnp_worker_setting', 'qr_code', '');
+        $qrCode = empty($qrCode) ? $qrCode : FileService::getFileUrl($qrCode);
+        $config = [
+            'name'                  => ConfigService::get('mnp_worker_setting', 'name', ''),
+            'original_id'           => ConfigService::get('mnp_worker_setting', 'original_id', ''),
+            'qr_code'               => $qrCode,
+            'app_id'                => ConfigService::get('mnp_worker_setting', 'app_id', ''),
+            'app_secret'            => ConfigService::get('mnp_worker_setting', 'app_secret', ''),
+            'request_domain'        => 'https://'.$domainName,
+            'socket_domain'         => 'wss://'.$domainName,
+            'upload_file_domain'     => 'https://'.$domainName,
+            'download_file_domain'   => 'https://'.$domainName,
+            'udp_domain'            => 'udp://'.$domainName,
+            'business_domain'       => $domainName,
+        ];
+
+        return $config;
+    }
+
+    /**
+     * @notes 设置师傅端小程序配置
+     * @param $params
+     */
+    public function setWorkerConfig($params)
+    {
+        $qrCode = isset($params['qr_code']) ? FileService::setFileUrl($params['qr_code']) : '';
+
+        ConfigService::set('mnp_worker_setting','name', $params['name'] ?? '');
+        ConfigService::set('mnp_worker_setting','original_id',$params['original_id'] ?? '');
+        ConfigService::set('mnp_worker_setting','qr_code',$qrCode);
+        ConfigService::set('mnp_worker_setting','app_id',$params['app_id']);
+        ConfigService::set('mnp_worker_setting','app_secret',$params['app_secret']);
+    }
 }

+ 164 - 0
app/common/service/wechat/WorkerWeChatConfigService.php

@@ -0,0 +1,164 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+namespace app\common\service\wechat;
+
+use app\common\enum\PayEnum;
+use app\common\enum\user\UserTerminalEnum;
+use app\common\model\pay\PayConfig;
+use app\common\service\ConfigService;
+
+/**
+ * 微信配置类
+ * Class WeChatConfigService
+ * @package app\common\service
+ */
+class WorkerWeChatConfigService
+{
+
+    /**
+     * @notes 获取小程序配置
+     * @return array
+     * @author 段誉
+     * @date 2022/9/6 19:49
+     */
+    public static function getMnpConfig()
+    {
+        return [
+            'app_id' => ConfigService::get('mnp_worker_setting', 'app_id'),
+            'secret' => ConfigService::get('mnp_worker_setting', 'app_secret'),
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
+            ],
+        ];
+    }
+
+
+    /**
+     * @notes 获取微信公众号配置
+     * @return array
+     * @author 段誉
+     * @date 2022/9/6 19:49
+     */
+    public static function getOaConfig()
+    {
+        return [
+            'app_id' => ConfigService::get('oa_setting', 'app_id'),
+            'secret' => ConfigService::get('oa_setting', 'app_secret'),
+            'token' => ConfigService::get('oa_setting', 'token'),
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
+            ],
+        ];
+    }
+
+
+    /**
+     * @notes 获取微信开放平台配置
+     * @return array
+     * @author 段誉
+     * @date 2022/10/20 15:51
+     */
+    public static function getOpConfig()
+    {
+        return [
+            'app_id' => ConfigService::get('open_platform', 'app_id'),
+            'secret' => ConfigService::get('open_platform', 'app_secret'),
+            'response_type' => 'array',
+            'log' => [
+                'level' => 'debug',
+                'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
+            ],
+        ];
+    }
+
+
+    /**
+     * @notes 根据终端获取支付配置
+     * @param $terminal
+     * @return array
+     * @author 段誉
+     * @date 2023/2/27 15:45
+     */
+    public static function getPayConfigByTerminal($terminal)
+    {
+        switch ($terminal) {
+            case UserTerminalEnum::WECHAT_MMP:
+                $notifyUrl = (string)url('pay/notifyMnp', [], false, true);
+                break;
+            case UserTerminalEnum::WECHAT_OA:
+            case UserTerminalEnum::PC:
+            case UserTerminalEnum::H5:
+                $notifyUrl = (string)url('pay/notifyOa', [], false, true);
+                break;
+            case UserTerminalEnum::ANDROID:
+            case UserTerminalEnum::IOS:
+                $notifyUrl = (string)url('pay/notifyApp', [], false, true);
+                break;
+        }
+
+        $pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
+        //判断是否已经存在证书文件夹,不存在则新建
+        if (!file_exists(app()->getRootPath() . 'runtime/cert')) {
+            mkdir(app()->getRootPath() . 'runtime/cert', 0775, true);
+        }
+        //写入文件
+        $apiclientCert = $pay['config']['apiclient_cert'] ?? '';
+        $apiclientKey = $pay['config']['apiclient_key'] ?? '';
+
+        $certPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientCert) . '.pem';
+        $keyPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientKey) . '.pem';
+
+        if (!empty($apiclientCert) && !file_exists($certPath)) {
+            static::setCert($certPath, trim($apiclientCert));
+        }
+        if (!empty($apiclientKey) && !file_exists($keyPath)) {
+            static::setCert($keyPath, trim($apiclientKey));
+        }
+        return [
+            // 商户号
+            'mch_id' => $pay['config']['mch_id'] ?? '',
+            // 商户证书
+            'private_key' => $keyPath,
+            'certificate' => $certPath,
+            // v3 API 秘钥
+            'secret_key' => $pay['config']['pay_sign_key'] ?? '',
+            'notify_url' => $notifyUrl,
+            'http' => [
+                'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
+                'timeout' => 5.0,
+            ]
+        ];
+    }
+
+
+    /**
+     * @notes 临时写入证书
+     * @param $path
+     * @param $cert
+     * @author 段誉
+     * @date 2023/2/27 15:48
+     */
+    public static function setCert($path, $cert)
+    {
+        $fopenPath = fopen($path, 'w');
+        fwrite($fopenPath, $cert);
+        fclose($fopenPath);
+    }
+
+
+}

+ 98 - 0
app/common/service/wechat/WorkerWeChatMnpService.php

@@ -0,0 +1,98 @@
+<?php
+namespace app\common\service\wechat;
+
+use EasyWeChat\Kernel\Exceptions\Exception;
+use EasyWeChat\MiniApp\Application;
+
+
+/**
+ * 微信功能类
+ * Class WeChatMnpService
+ * @package app\common\service
+ */
+class WorkerWeChatMnpService
+{
+
+    protected $app;
+
+    protected $config;
+
+    public function __construct()
+    {
+        $this->config = $this->getConfig();
+        $this->app = new Application($this->config);
+    }
+
+
+    /**
+     * @notes 配置
+     * @return array
+     * @throws \Exception
+     * @author 段誉
+     * @date 2023/2/27 12:03
+     */
+    protected function getConfig()
+    {
+        $config = WorkerWeChatConfigService::getMnpConfig();
+        if (empty($config['app_id']) || empty($config['secret'])) {
+            throw new \Exception('请先设置小程序配置');
+        }
+        return $config;
+    }
+
+
+    /**
+     * @notes 小程序-根据code获取微信信息
+     * @param string $code
+     * @return array
+     * @throws Exception
+     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
+     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
+     * @author 段誉
+     * @date 2023/2/27 11:03
+     */
+    public function getMnpResByCode(string $code)
+    {
+        $utils = $this->app->getUtils();
+        $response = $utils->codeToSession($code);
+
+        if (!isset($response['openid']) || empty($response['openid'])) {
+            throw new Exception('获取openID失败');
+        }
+
+        return $response;
+    }
+
+
+    /**
+     * @notes 获取手机号
+     * @param string $code
+     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
+     * @author 段誉
+     * @date 2023/2/27 11:46
+     */
+    public function getUserPhoneNumber(string $code)
+    {
+        return $this->app->getClient()->postJson('wxa/business/getuserphonenumber', [
+            'code' => $code,
+        ]);
+    }
+
+    public function getUnlimitedQRCode($scene,$page,$env_version='release',$check_path=true)
+    {
+        return $this->app->getClient()->postJson('wxa/getwxacodeunlimit', [
+            'scene' => $scene,
+            'page'=>$page,
+            'env_version'=>$env_version,//trial小程序版本
+            'check_path'=>$check_path
+        ]);
+    }
+
+
+}

+ 2 - 1
app/workerapi/logic/LoginLogic.php

@@ -10,6 +10,7 @@ use app\common\model\master_worker_register\MasterWorkerRegister;
 use app\common\service\FileService;
 use app\common\service\sms\SmsDriver;
 use app\common\service\wechat\WeChatMnpService;
+use app\common\service\wechat\WorkerWeChatMnpService;
 use think\facade\Config;
 use app\workerapi\service\MasterWokerTokenService;
 
@@ -117,7 +118,7 @@ class LoginLogic extends BaseLogic
     {
         try {
             //通过code获取微信openid
-            $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
+            $response = (new WorkerWeChatMnpService())->getMnpResByCode($params['code']);
             $response['user_id'] = $params['user_id'];
             $response['terminal'] = UserTerminalEnum::WECHAT_MMP;