刘忠健 лет назад: 2
Родитель
Сommit
2b41a227fa

+ 1 - 1
api/.example.env

@@ -1 +1 @@
-APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = time_manager
USERNAME = root
PASSWORD = root
HOSTPORT = 3306
CHARSET = utf8mb4
DEBUG = true

[LANG]
default_lang = zh-cn
+APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 192.168.0.80
DATABASE = yc_api
USERNAME = yfb
PASSWORD = yfb123#
HOSTPORT = 3306
CHARSET = utf8mb4
DEBUG = true

[LANG]
default_lang = zh-cn

+ 1 - 0
api/README.md

@@ -2,6 +2,7 @@
 提供各类服务,如生成二维码,获取微信 TOKEN等
 ### 安装依赖
 
+删除tracer调试
 ```
 composer install --no-dev
 ```

+ 22 - 0
api/app/api/controller/Hprose.php

@@ -0,0 +1,22 @@
+<?php
+
+
+namespace app\api\controller;
+
+
+use app\api\controller\hprose\MyExample;
+use app\api\controller\hprose\MyExample2;
+use app\common\hprose\Server;
+
+class Hprose extends Base
+{
+
+    public function server(){
+        $server=new Server();
+
+        $server->addInstanceMethods(new MyExample2());
+        $server->addInstanceMethods(new MyExample());
+        return $server->start();
+    }
+
+}

+ 20 - 0
api/app/api/controller/HproseClient.php

@@ -0,0 +1,20 @@
+<?php
+
+
+namespace app\api\controller;
+
+
+use Hprose\Http\Client;
+
+class HproseClient
+{
+    public function test1(){
+        $url="http://local74.lzj/yc_api/api/public/index.php/api/hprose/server";
+        $client = Client::create($url, false);
+        $result = $client->hello('张三');
+        var_dump($result);
+        $result = $client->hello2('李四');
+        print_r($result);
+    }
+
+}

+ 95 - 0
api/app/api/controller/WechatMpAuth.php

@@ -0,0 +1,95 @@
+<?php
+
+
+namespace app\api\controller;
+
+
+use app\common\model\WechatConfig;
+use app\common\util\wechat\WechatMpSDK;
+use EasyWeChat\Factory;
+use think\facade\View;
+
+class WechatMpAuth extends Base
+{
+
+    /**
+     * 测试例子
+     * http://local74.lzj/yc_api/api/public/index.php/api/wechat_mp_auth/test1
+     */
+    public function test1()
+    {
+        $callback = (string)url("WechatMpAuth/test2", ["testParam" => "hello"], '', true);
+        $toUrl = (string)url("WechatMpAuth/authorize", ['remark' => '备注1', 'wechatcode' => 'test1', 'callback' => urlencode2($callback)], '', true);
+        var_dump($toUrl);
+        header("Location:$toUrl");
+        //http://local74.lzj/yc_api/api/public/index.php/api/WechatMpAuth/authorize?remark=test1&wechatcode=test1&callback=http%25253A%25252F%25252Flocal74.lzj%25252Fyc_api%25252Fapi%25252Fpublic%25252Findex.php%25252Fapi%25252FWechatMpAuth%25252Ftest2%25253FtestParam%25253Dhello
+    }
+
+    public function test2()
+    {
+        echo "this is callback\n<br/>";
+        $param = request()->param();
+        $userInfo = json_decode(base64_decode(input("userinfo")), true);
+        print_r($param);
+        print_r($userInfo);
+
+    }
+
+    public function authorize()
+    {
+        $param = request()->param();
+        $rules = [
+            "callback|回调地址" => "require",
+            "wechatcode|微信代码" => "require",
+        ];
+        $this->autoValid($rules, $param);
+        $callback = trim(urldecode2(input("callback")));//回调地址
+        $scope = input("scope", "snsapi_base");
+        $remark = trim(urldecode2(input("remark", "")));//input("remark", "");//备注
+        $method = input("method", "post");//回调方式
+        $wechatcode = input("wechatcode");
+        $wechatConfig = WechatConfig::getEasyWechatConfig($wechatcode);
+        $app = Factory::officialAccount($wechatConfig);
+        $param = [
+            "callback" => urlencode2($callback),
+            "remark" => urlencode2($remark),
+            "method" => $method,
+            "wechatcode" => $wechatcode,
+        ];
+        $callback2 = (string)url('WechatMpAuth/callback', $param, "", true);
+        $response = $app->oauth->scopes([$scope])->redirect($callback2);
+        header("Location:$response");
+        //print_r($response);
+//        $response->send();
+    }
+
+    //http://local74.lzj/yc_api/api/public/index.php/api/WechatMpAuth/callback?callback=http%25253A%25252F%25252Flocal74.lzj%25252Fyc_api%25252Fapi%25252Fpublic%25252Findex.php%25252Fapi%25252FWechatMpAuth%25252Ftest2%25253FtestParam%25253Dhello&remark=test1&method=get&wechatcode=test1&code=011yLJ0w3kHLUY2cNF0w3w46qC2yLJ0m&state=6047de6f5bb28ccf632790e56bf614ac
+    public function callback()
+    {
+        $param = request()->param();
+        $callback = trim(urldecode2(input("callback")));
+        $method = input("method", "post");
+        $remark = input("remark", "");
+        View::assign("method", $method);
+        View::assign("callback", $callback);
+        View::assign("remark", $remark);
+        $wechatcode = input("wechatcode");
+        View::assign("wechatcode", $wechatcode);
+        $wechatConfig = WechatConfig::getEasyWechatConfig($wechatcode);
+        $app = Factory::officialAccount($wechatConfig);
+        $oauth = $app->oauth;
+        $code = input("code");
+        $user = $oauth->userFromCode($code);
+        $userArray = [
+            'openid' => $user->getId(),
+            'nickname' => $user->getNickname(),
+            'name' => $user->getNickname(),
+            'avatar' => $user->getAvatar(),
+            'raw' => $user->getRaw(),
+        ];
+        $userJson = json_encode($userArray, JSON_UNESCAPED_UNICODE);
+        $userinfo = base64_encode($userJson);
+        View::assign("userinfo", $userinfo);
+        return View::fetch();
+    }
+}

+ 13 - 0
api/app/api/controller/hprose/MyExample.php

@@ -0,0 +1,13 @@
+<?php
+
+
+namespace app\api\controller\hprose;
+
+
+class MyExample
+{
+    public function hello($name=""){
+        return "hello ".$name;
+    }
+
+}

+ 15 - 0
api/app/api/controller/hprose/MyExample2.php

@@ -0,0 +1,15 @@
+<?php
+
+
+namespace app\api\controller\hprose;
+
+
+class MyExample2
+{
+    public function hello($name=""){
+        return "hello1 ".$name;
+    }
+    public function hello2($name=""){
+        return "hello2 ".$name;
+    }
+}

+ 11 - 0
api/app/common.php

@@ -1,6 +1,17 @@
 <?php
 // 应用公共文件
 
+if (!function_exists('urlencode2')) {
+    /**
+     * 2次URL解码
+     * @param $str
+     * @return string
+     */
+    function urlencode2($str)
+    {
+        return urlencode(urlencode($str));
+    }
+}
 
 if (!function_exists('urldecode2')) {
     /**

+ 13 - 0
api/app/common/hprose/Server.php

@@ -0,0 +1,13 @@
+<?php
+
+
+namespace app\common\hprose;
+
+
+class Server extends Service
+{
+    public function start()
+    {
+        return $this->handle(request(), response());
+    }
+}

+ 43 - 0
api/app/common/hprose/Service.php

@@ -0,0 +1,43 @@
+<?php
+
+
+namespace app\common\hprose;
+
+
+class Service extends \Hprose\Http\Service
+{
+    public function header($name, $value, $context)
+    {
+        $context->response->header([$name => $value]);
+    }
+
+    public function getAttribute($name, $context)
+    {
+        return $context->request->header($name);
+    }
+
+    public function hasAttribute($name, $context)
+    {
+        return $context->request->header($name);
+    }
+
+    protected function readRequest($context)
+    {
+        return $context->request->getContent();
+    }
+
+    public function writeResponse($data, $context)
+    {
+        return $context->response->data($data);
+    }
+
+    public function isGet($context)
+    {
+        return $context->request->isGet();
+    }
+
+    public function isPost($context)
+    {
+        return $context->request->isPost();
+    }
+}

+ 32 - 0
api/app/common/model/WechatConfig.php

@@ -0,0 +1,32 @@
+<?php
+
+
+namespace app\common\model;
+
+
+class WechatConfig extends Base
+{
+    /**
+     * 获取微信号配置
+     * @param $key
+     * @return array|false
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getEasyWechatConfig($key)
+    {
+        $where = [];
+        $where[] = ["id|app_id|app_code", "=", $key];
+        $wechatConfig = self::where($where)->find();
+        if (!$wechatConfig) {
+            return false;
+        }
+        $config = [
+            'app_id' => $wechatConfig->app_id,
+            'secret' => $wechatConfig->secret,
+        ];
+        return $config;
+    }
+
+}

+ 49 - 0
api/app/common/util/wechat/MiniProgramSDK.php

@@ -0,0 +1,49 @@
+<?php
+
+
+namespace app\common\util\wechat;
+
+use app\common\util\SingleObjectClass;
+use EasyWeChat\Factory;
+use think\Log;
+
+
+/**
+ * 微信小程序实例化对象
+ * Class MiniProgramSDK
+ * @package app\common\util
+ */
+class MiniProgramSDK extends SingleObjectClass
+{
+    protected $app;
+
+    /**
+     * 构造函数
+     * MiniProgramSDK constructor.
+     */
+    public function __construct()
+    {
+        $miniProgramConfig = WxAccountConfig::getMiniProgramConfig();//获取小程序配置
+        $this->app = Factory::miniProgram($miniProgramConfig);
+        $cache = new WechatCache();
+        $this->app->rebind('cache', $cache);
+        Log::record($cache);
+
+    }
+
+    /**
+     * 获取小程序接口实例
+     * @return \EasyWeChat\MiniProgram\Application
+     */
+    public function getApp()
+    {
+        return $this->app;
+    }
+
+    public function getPhoneNumber($code){
+        $params = [
+            'code' => $code
+        ];
+        return $this->app->live->httpPostJson('wxa/business/getuserphonenumber', $params);
+    }
+}

+ 61 - 0
api/app/common/util/wechat/WechatCache.php

@@ -0,0 +1,61 @@
+<?php
+
+
+namespace app\common\util\wechat;
+
+
+use Psr\SimpleCache\CacheInterface;
+use think\Cache;
+
+
+class WechatCache implements CacheInterface
+{
+    public function get($key, $default = null)
+    {
+
+        // your code
+        return Cache::get($key,$default);
+    }
+
+    public function set($key, $value, $ttl = null)
+    {
+        // your code
+        return Cache::set($key,$value,$ttl);
+    }
+
+    public function delete($key)
+    {
+        // your code
+        return Cache::delete($key);
+    }
+
+    public function clear()
+    {
+        // your code
+        return Cache::clear();
+    }
+
+    public function getMultiple($keys, $default = null)
+    {
+        // your code
+        return Cache::getMultiple($keys,$default);
+    }
+
+    public function setMultiple($values, $ttl = null)
+    {
+        // your code
+        return Cache::setMultiple($values,$ttl);
+    }
+
+    public function deleteMultiple($keys)
+    {
+        // your code
+        return Cache::deleteMultiple($keys);
+    }
+
+    public function has($key)
+    {
+        // your code
+        return Cache::has($key);
+    }
+}

+ 33 - 0
api/app/common/util/wechat/WechatMp.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace app\common\util\wechat;
+
+use app\common\model\MsgTemplateLog;
+use app\common\model\Order;
+use app\common\model\Spot;
+use think\Model;
+
+/**
+ * 公众号帮助类
+ * Class WechatMp
+ * @package app\common\util\wechat
+ */
+class WechatMp
+{
+
+
+    /**
+     * 标准化回复
+     * @param $code
+     * @param $msg
+     */
+    private function standardReturn($code, $msg = "", $data = "")
+    {
+        $res = [
+            'code' => $code,
+            'msg' => $msg,
+            'data' => $data
+        ];
+        return $res;
+    }
+}

+ 45 - 0
api/app/common/util/wechat/WechatMpSDK.php

@@ -0,0 +1,45 @@
+<?php
+
+
+namespace app\common\util\wechat;
+
+use app\common\util\SingleObjectClass;
+
+use EasyWeChat\Factory;
+use think\Log;
+
+
+/**
+ * 微信小程序实例化对象
+ * Class MiniProgramSDK
+ * @package app\common\util
+ */
+class WechatMpSDK
+{
+    protected $app;
+
+    /**
+     * 构造函数
+     * MiniProgramSDK constructor.
+     */
+    public function __construct($config = [])
+    {
+        //WxAccountConfig::getMpConfig();//获取小程序配置
+        $this->app = Factory::officialAccount($config); //  Factory::officialAccount($config);
+        $cache = new WechatCache();
+        $this->app->rebind('cache', $cache);
+        Log::record($cache);
+        //return $this->app;
+
+    }
+
+    /**
+     * 获取小程序接口实例
+     * @return \EasyWeChat\MiniProgram\Application
+     */
+    public function getApp()
+    {
+        return $this->app;
+    }
+
+}

+ 45 - 0
api/app/common/util/wechat/WechatPaySDK.php

@@ -0,0 +1,45 @@
+<?php
+
+
+namespace app\common\util\wechat;
+
+use app\common\util\SingleObjectClass;
+use EasyWeChat\Factory;
+use think\Log;
+
+
+/**
+ * 微信小程序实例化对象
+ * Class MiniProgramSDK
+ * @package app\common\util
+ */
+class WechatPaySDK extends SingleObjectClass
+{
+    protected $app;
+
+    /**
+     * 构造函数
+     * MiniProgramSDK constructor.
+     */
+    public function __construct()
+    {
+        $miniProgramConfig = WxAccountConfig::getWechatPayConfig();//获取小程序配置
+//        print_r($miniProgramConfig);
+        $this->app = Factory::payment($miniProgramConfig);
+        $cache = new WechatCache();
+        $this->app->rebind('cache', $cache);
+        Log::record($cache);
+
+    }
+
+    /**
+     * 获取小程序接口实例
+     * @return \EasyWeChat\MiniProgram\Application
+     */
+    public function getApp()
+    {
+        return $this->app;
+    }
+
+
+}

+ 186 - 0
api/app/common/util/wechat/WxAccountConfig.php

@@ -0,0 +1,186 @@
+<?php
+
+namespace app\common\util\wechat;
+
+use app\common\model\Config;
+
+class WxAccountConfig
+{
+    /**
+     * 获取小程序接口配置参数
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getMiniProgramConfig()
+    {
+        $wxconfig = Config::getSettingModel("onlineRetailers");
+        $config = [
+            'app_id' => $wxconfig["appId"],
+            'secret' => $wxconfig["appSecret"],
+            'response_type' => "array",
+            'log' => [
+                'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
+                'channels' => [
+                    // 测试环境
+                    'dev' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mini_program.log',
+                        'level' => 'debug',
+                    ],
+                    // 生产环境
+                    'prod' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mini_program.log',
+                        'level' => 'info',
+                    ],
+                ],
+            ],
+        ];
+        return $config;
+    }
+
+    /**
+     * 获取公众号口配置参数
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getMpConfig()
+    {
+        $wxconfig = Config::getSettingModel("destination");
+        $config = [
+            'app_id' => $wxconfig["appId"],
+            'secret' => $wxconfig["appSecret"],
+            'response_type' => "array",
+            'log' => [
+                'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
+                'channels' => [
+                    // 测试环境
+                    'dev' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mp.log',
+                        'level' => 'debug',
+                    ],
+                    // 生产环境
+                    'prod' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mp.log',
+                        'level' => 'info',
+                    ],
+                ],
+            ],
+        ];
+        return $config;
+    }
+
+    /**
+     * 获取公众号口配置参数
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getWechatPayConfig()
+    {
+        $wxconfig = Config::getSettingModel("wechatPayment");
+        $config = [
+            'app_id' => $wxconfig["appId"],
+            'mch_id' => $wxconfig["merchantNumber"],
+            'key' => $wxconfig["merchantKey"],
+            'cert_path' => $wxconfig['certificate'] . "apiclient_cert.pem", // XXX: 绝对路径!!!!
+            'key_path' => $wxconfig['certificate'] . "apiclient_key.pem",      // XXX: 绝对路径!!!!
+            'response_type' => "array",
+            'log' => [
+                'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
+                'channels' => [
+                    // 测试环境
+                    'dev' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mp.log',
+                        'level' => 'debug',
+                    ],
+                    // 生产环境
+                    'prod' => [
+                        'driver' => 'daily',
+                        'path' => RUNTIME_PATH . '/easywechat_log/mp.log',
+                        'level' => 'info',
+                    ],
+                ],
+            ],
+        ];
+        return $config;
+    }
+
+    public function getFullConfigExample()
+    {
+        return [
+            /**
+             * 账号基本信息,请从微信公众平台/开放平台获取
+             */
+            'app_id' => 'your-app-id',         // AppID
+            'secret' => 'your-app-secret',     // AppSecret
+            'token' => 'your-token',          // Token
+            'aes_key' => '',                    // EncodingAESKey,兼容与安全模式下请一定要填写!!!
+
+            /**
+             * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
+             * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例
+             */
+            'response_type' => 'array',
+
+            /**
+             * 日志配置
+             *
+             * level: 日志级别, 可选为:
+             *         debug/info/notice/warning/error/critical/alert/emergency
+             * path:日志文件位置(绝对路径!!!),要求可写权限
+             */
+            'log' => [
+                'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
+                'channels' => [
+                    // 测试环境
+                    'dev' => [
+                        'driver' => 'single',
+                        'path' => '/tmp/easywechat.log',
+                        'level' => 'debug',
+                    ],
+                    // 生产环境
+                    'prod' => [
+                        'driver' => 'daily',
+                        'path' => '/tmp/easywechat.log',
+                        'level' => 'info',
+                    ],
+                ],
+            ],
+
+            /**
+             * 接口请求相关配置,超时时间等,具体可用参数请参考:
+             * http://docs.guzzlephp.org/en/stable/request-config.html
+             *
+             * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。
+             * - retry_delay: 重试延迟间隔(单位:ms),默认 500
+             * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
+             */
+            'http' => [
+                'max_retries' => 1,
+                'retry_delay' => 500,
+                'timeout' => 5.0,
+                // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
+            ],
+
+            /**
+             * OAuth 配置
+             *
+             * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
+             * callback:OAuth授权完成后的回调页地址
+             */
+            'oauth' => [
+                'scopes' => ['snsapi_userinfo'],
+                'callback' => '/examples/oauth_callback.php',
+            ],
+        ];
+    }
+}

+ 8 - 2
api/composer.json

@@ -24,7 +24,10 @@
         "topthink/framework": "^6.0.0",
         "topthink/think-orm": "^2.0",
         "topthink/think-multi-app": "^1.0",
-        "endroid/qr-code": "^4.4"
+        "endroid/qr-code": "^4.4",
+        "hprose/hprose": "^2.0",
+        "overtrue/wechat": "~5.0",
+        "topthink/think-view": "^1.0"
     },
     "require-dev": {
         "symfony/var-dumper": "^4.2",
@@ -39,7 +42,10 @@
         }
     },
     "config": {
-        "preferred-install": "dist"
+        "preferred-install": "dist",
+        "allow-plugins": {
+            "easywechat-composer/easywechat-composer": true
+        }
     },
     "scripts": {
         "post-autoload-dump": [

+ 19 - 0
api/view/api/wechat_mp_auth/callback.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>微信授权</title>
+</head>
+<body>
+<form method="{$method}" action="{$callback}" id="mainform">
+    <input type="hidden" name="remark" value="{$remark}">
+    <input type="hidden" name="userinfo" value="{$userinfo}">
+    <input type="hidden" name="wechatcode" value="{$wechatcode}">
+</form>
+<script>
+    window.onload=function(){
+        document.getElementById("mainform").submit();
+    }
+</script>
+</body>
+</html>

+ 6 - 0
api/说明.md

@@ -4,3 +4,9 @@
 ### app\common\util\SingleObjectClass 
 单例模式基类
 
+### 命令
+
+删除tracer调试
+```
+composer install --no-dev
+```