| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\third\ali;
- use app\models\AggregateQrcode;
- use app\models\Option;
- use AopClient;
- use app\models\StoreAliMini;
- use app\utils\CurlHelper;
- use yii\base\Model;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class BaseForm extends Model
- {
- //支付宝model
- public $store_mini;
- //小程序ID
- public $mini_id;
- //商城ID
- public $store_id;
- //基础配置
- public $base_info;
- //获取模板信息
- public $template_info;
- //请求信息
- public $param_info;
- //小程序版本model
- public $mini_version;
- //二维码ID
- public $qrcode_id;
- //二维码Model
- public $qrcode;
- //设置规则
- public function rules()
- {
- return [
- [['mini_id', 'store_id', 'qrcode_id'], 'integer'],
- [['store_mini', 'mini_info', 'param_info'], 'array']
- ];
- }
- //构造函数
- public function __construct($config = [])
- {
- parent::__construct($config);
- //获取三方平台信息
- //三方平台Appid
- $this->base_info['appid'] = Option::get("alipay_appid", 0, 'saas')['value'];
- //获取当前时间戳
- $this->base_info['timestamp'] = date("Y-m-d H:i:s");
- //设置加解密方式
- $this->base_info['sign_type'] = "RSA2";
- //获取私钥
- $this->base_info['private_key'] = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- //获取公钥
- $this->base_info['public_key'] = Option::get("alipay_public_key", 0, 'saas')['value'];
- //获取ase_key,加签方式
- $this->base_info['alipay_aes_key'] = Option::get("alipay_aes_key", 0, 'saas')['value'];
- //获取模板aes加签方式
- $this->template_info['ali_template_aes'] = Option::get("ali_template_aes", 0, 'saas')['value'];
- //获取模板appid
- $this->template_info['ali_template_appid'] = Option::get("ali_template_appid", 0, 'saas')['value'];
- //获取模板auth_token
- $this->template_info['ali_template_token'] = Option::get("ali_template_token", 0, 'saas')['value'];
- }
- public function miniCommon($request, $biz_content = null, $is_hidden_token = 0, $token = '')
- {
- try {
- //模板信息
- $template_info = $this->template_info;
- //请求的基础信息
- $base_info = $this->base_info;
- //模板token
- $auth_token = $template_info['ali_template_token'];
- //小程序ID
- $mini_id = $this->mini_id;
- if (!empty($mini_id)) {
- $store_mini = StoreAliMini::findOne($mini_id);
- //如果小程序ID存在,则将token从模板转成商家小程序的token
- if (!empty($store_mini['auth_token'])) {
- $auth_token = $store_mini['auth_token'];
- }
- }
- //设置基础请求数据
- $aop = new AopClient();
- //设置appid
- $aop->appId = $base_info['appid'];
- //设置私钥
- $aop->rsaPrivateKey = $base_info['private_key'];
- //设置公钥
- $aop->alipayrsaPublicKey = $base_info['public_key'];
- //设置ase_key,加签方式
- $aop->encryptKey = $base_info['alipay_aes_key'];
- //设置加解密方式
- $aop->signType = $base_info['sign_type'];
- //处理没有biz_content情况
- if ($biz_content) {
- $request->setBizContent($biz_content);
- }
- //处理不需要token情况
- if ($is_hidden_token === 0) {
- //开始请求
- $result = $aop->execute($request, null, ($token ?: $auth_token));
- } else {
- //开始请求
- $result = $aop->execute($request);
- }
- //请求后的数据处理
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return $result->$responseNode;
- } catch (\Exception $e) {
- return [
- 'sub_msg' => $e->getMessage()
- ];
- }
- }
- //处理分类子父级数据
- public function getdata($data, $id = "0")
- {
- foreach ($data as $k => $v) {
- if ($v['parent_category_id'] == $id) {
- $v['children'] = $this->getdata($data, $v['category_id']);
- $arr[] = $v;
- }
- }
- return $arr;
- }
- //获取网络图片到临时目录
- public function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- public function saveTempVideo($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/video')) {
- mkdir(\Yii::$app->runtimePath . '/video');
- }
- $save_path = \Yii::$app->runtimePath . '/video/' . md5($url) . '.mp4';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- /**
- * 图片转base64
- */
- public function image2Base64($file)
- {
- if ($fp = fopen($file, "rb", 0)) {
- $gambar = fread($fp, filesize($file));
- fclose($fp);
- return chunk_split(base64_encode($gambar));
- } else {
- return false;
- }
- }
- }
|