BaseForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\ali;
  8. use app\models\AggregateQrcode;
  9. use app\models\Option;
  10. use AopClient;
  11. use app\models\StoreAliMini;
  12. use app\utils\CurlHelper;
  13. use yii\base\Model;
  14. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  15. class BaseForm extends Model
  16. {
  17. //支付宝model
  18. public $store_mini;
  19. //小程序ID
  20. public $mini_id;
  21. //商城ID
  22. public $store_id;
  23. //基础配置
  24. public $base_info;
  25. //获取模板信息
  26. public $template_info;
  27. //请求信息
  28. public $param_info;
  29. //小程序版本model
  30. public $mini_version;
  31. //二维码ID
  32. public $qrcode_id;
  33. //二维码Model
  34. public $qrcode;
  35. //设置规则
  36. public function rules()
  37. {
  38. return [
  39. [['mini_id', 'store_id', 'qrcode_id'], 'integer'],
  40. [['store_mini', 'mini_info', 'param_info'], 'array']
  41. ];
  42. }
  43. //构造函数
  44. public function __construct($config = [])
  45. {
  46. parent::__construct($config);
  47. //获取三方平台信息
  48. //三方平台Appid
  49. $this->base_info['appid'] = Option::get("alipay_appid", 0, 'saas')['value'];
  50. //获取当前时间戳
  51. $this->base_info['timestamp'] = date("Y-m-d H:i:s");
  52. //设置加解密方式
  53. $this->base_info['sign_type'] = "RSA2";
  54. //获取私钥
  55. $this->base_info['private_key'] = Option::get("alipay_app_private_key", 0, 'saas')['value'];
  56. //获取公钥
  57. $this->base_info['public_key'] = Option::get("alipay_public_key", 0, 'saas')['value'];
  58. //获取ase_key,加签方式
  59. $this->base_info['alipay_aes_key'] = Option::get("alipay_aes_key", 0, 'saas')['value'];
  60. //获取模板aes加签方式
  61. $this->template_info['ali_template_aes'] = Option::get("ali_template_aes", 0, 'saas')['value'];
  62. //获取模板appid
  63. $this->template_info['ali_template_appid'] = Option::get("ali_template_appid", 0, 'saas')['value'];
  64. //获取模板auth_token
  65. $this->template_info['ali_template_token'] = Option::get("ali_template_token", 0, 'saas')['value'];
  66. }
  67. public function miniCommon($request, $biz_content = null, $is_hidden_token = 0, $token = '')
  68. {
  69. try {
  70. //模板信息
  71. $template_info = $this->template_info;
  72. //请求的基础信息
  73. $base_info = $this->base_info;
  74. //模板token
  75. $auth_token = $template_info['ali_template_token'];
  76. //小程序ID
  77. $mini_id = $this->mini_id;
  78. if (!empty($mini_id)) {
  79. $store_mini = StoreAliMini::findOne($mini_id);
  80. //如果小程序ID存在,则将token从模板转成商家小程序的token
  81. if (!empty($store_mini['auth_token'])) {
  82. $auth_token = $store_mini['auth_token'];
  83. }
  84. }
  85. //设置基础请求数据
  86. $aop = new AopClient();
  87. //设置appid
  88. $aop->appId = $base_info['appid'];
  89. //设置私钥
  90. $aop->rsaPrivateKey = $base_info['private_key'];
  91. //设置公钥
  92. $aop->alipayrsaPublicKey = $base_info['public_key'];
  93. //设置ase_key,加签方式
  94. $aop->encryptKey = $base_info['alipay_aes_key'];
  95. //设置加解密方式
  96. $aop->signType = $base_info['sign_type'];
  97. //处理没有biz_content情况
  98. if ($biz_content) {
  99. $request->setBizContent($biz_content);
  100. }
  101. //处理不需要token情况
  102. if ($is_hidden_token === 0) {
  103. //开始请求
  104. $result = $aop->execute($request, null, ($token ?: $auth_token));
  105. } else {
  106. //开始请求
  107. $result = $aop->execute($request);
  108. }
  109. //请求后的数据处理
  110. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  111. return $result->$responseNode;
  112. } catch (\Exception $e) {
  113. return [
  114. 'sub_msg' => $e->getMessage()
  115. ];
  116. }
  117. }
  118. //处理分类子父级数据
  119. public function getdata($data, $id = "0")
  120. {
  121. foreach ($data as $k => $v) {
  122. if ($v['parent_category_id'] == $id) {
  123. $v['children'] = $this->getdata($data, $v['category_id']);
  124. $arr[] = $v;
  125. }
  126. }
  127. return $arr;
  128. }
  129. //获取网络图片到临时目录
  130. public function saveTempImage($url)
  131. {
  132. if (strpos($url, 'http') === false) {
  133. $url = 'http:' . trim($url);
  134. }
  135. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  136. mkdir(\Yii::$app->runtimePath . '/image');
  137. }
  138. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  139. CurlHelper::download($url, $save_path);
  140. return $save_path;
  141. }
  142. public function saveTempVideo($url)
  143. {
  144. if (strpos($url, 'http') === false) {
  145. $url = 'http:' . trim($url);
  146. }
  147. if (!is_dir(\Yii::$app->runtimePath . '/video')) {
  148. mkdir(\Yii::$app->runtimePath . '/video');
  149. }
  150. $save_path = \Yii::$app->runtimePath . '/video/' . md5($url) . '.mp4';
  151. CurlHelper::download($url, $save_path);
  152. return $save_path;
  153. }
  154. /**
  155. * 图片转base64
  156. */
  157. public function image2Base64($file)
  158. {
  159. if ($fp = fopen($file, "rb", 0)) {
  160. $gambar = fread($fp, filesize($file));
  161. fclose($fp);
  162. return chunk_split(base64_encode($gambar));
  163. } else {
  164. return false;
  165. }
  166. }
  167. }