<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * Trait类 微信公众号
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Factories\Wechat
 * @package   App\Factories\Wechat
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年5月6日10:44:56
 * @copyright 2019-2020 Richer (http://www.umu888.com)
 * @license   http://www.xxxxxx.com License
 * @link      http://www.xxxxxx.com
 */
namespace App\Factories\Wechat;

use EasyWeChat\Factory;
use Illuminate\Support\Facades\Storage;
use Yansongda\LaravelPay\Facades\Pay;
use Yansongda\Pay\Log;

/**
 * Trait Mini
 *
 * @category  App\Factories\Wechat
 * @package   App\Factories\Wechat
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年5月6日10:44:56
 * @copyright 2019-2020 Richer (http://www.umu888.com)
 * @license   http://www.xxxxxx.com License
 * @link      http://www.xxxxxx.com
 */
trait Mini
{
    /**
     * @param null $config
     * @return $this
     */
    public function setMiniConfigure($config = null)
    {
        if ($config) {
            $this->mini = Factory::miniProgram($config);
        }

        return $this;
    }

    /**
     * 通过code获取用户session信息
     *
     * @param $code
     * @return mixed
     */
    public function loginMini($code)
    {
        return $this->mini->auth->session($code);
    }


    /**
     * 比如获取电话等功能,信息是加密的,需要解密。
     *
     * @param $session
     * @param $iv
     * @param $encryptedData
     * @return mixed
     */
    public function getEncryptedData($session, $iv, $encryptedData)
    {
        try {
            return $decryptedData = $this->mini->encryptor->decryptData($session, $iv, $encryptedData);
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            Log::info('错误信息:' . $this->errMsg);
            return false;

        }
    }

    /**
     * 账户转账
     */
    public function transfer($partner_trade_no, $openid, $amount = 0, $desc = '帐户提现')
    {
        $order = [
            'partner_trade_no' => $partner_trade_no,              //商户订单号
            'openid'    => $openid,                        //收款人的openid
            'check_name' => 'NO_CHECK',            //NO_CHECK:不校验真实姓名\FORCE_CHECK:强校验真实姓名
            // 're_user_name'=>'张三',              //check_name为 FORCE_CHECK 校验实名的时候必须提交
            'amount' => $amount,                       //企业付款金额,单位为分
            'desc' => $desc,                  //付款说明
            'type' => 'miniapp', // 如果您需要通过 APP/小程序 的商户账号appid进行转账,请传入参数:['type' => 'app']/['type' => 'miniapp']
            'spbill_create_ip' => request()->ip(), // 如果您在队列中使用,请自行传参 spbill_create_ip。
        ];

        $result = Pay::wechat()->transfer($order);
    }

    /**
     * 生成小程序码,并且携带用参数
     *
     * @param int $user_id 参数值
     * @param string $page 页面路径
     * @return string
     */
    public function generateMiniProgramCode($user_id, $page = 'pages/index/index')
    {
        // 定义参数
        $scene = 'inviter_id='.$user_id;
        $result = $this->mini->app_code->getUnlimit($scene, [
            'page' => $page,
        ]);

        $filename = $user_id . '.png'; // 文件名
        Storage::disk('mini_program_code')->put($filename, $result); // 存储到本地

        return  $url = Storage::disk('mini_program_code')->url($filename); // 获取 URL;
//        return response($result, 200, [
//            'Content-Type' => 'image/png',
//        ]);
    }

}