basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";; class ShareQrcode { /** * 二维码类型 */ public $type = 0; public static function wxQrcodeByPlatform($page, $scene, $width = 600) { return self::wxQrcode($page, $scene, $width, true); } /** * 获取小程序码 * @param $page * @param $scene * @param int $width * @return array * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException */ public static function wxQrcode($page, $scene, $width = 600, $is_platform = false, $is_server = false, $store_id = 0) { $store_id = $store_id != 0 ? $store_id : get_store_id(); $file_name = md5($page.$scene.$store_id); // 保存小程序码到文件 $dir = \Yii::$app->runtimePath . '/image/wx_qrcode'; if (! is_dir($dir)) { mkdir($dir, 0777, true); } $url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/wx_qrcode/' . $file_name); // if (file_exists($dir. '/' .$file_name . '.jpg')) { // return [ // 'code' => 0, // 'root_path' => $dir . '/' . $file_name . '.jpg', // 'url_path' => $url . '.jpg', // ]; // } if($store_id == -1){ $storeInfo = []; }else{ $storeInfo = Store::findOne($store_id); } if (((is_h5()) && $store_id > -1) || is_alipay_platform() || !is_wechat_platform() || is_app_platform()) { // $page = \Yii::$app->request->hostInfo . '/h5/#/' . $page . '?scene=' . $scene; $page .= '?scene=' . \urlencode($scene); QrCode::image($page, $width, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $dir. '/' .$file_name . '.jpg'); return [ 'code' => 0, 'root_path' => $dir . '/' . $file_name . '.jpg', 'url_path' => $url . '.jpg', ]; } // $plugin = \Yii::$app->getAttr('plugin'); // $open = false; // if (in_array('duli_store', $plugin['union'])) { // $open = true; // } // $self_mini = Option::get('self_mini', $store_id,'store', 0)['value']; // if (\Yii::$app->prod_is_dandianpu() && (!$self_mini || !$open)) { // $app = self::setSingleStoreWechat($is_platform); // } else { // if((isset($storeInfo->business_model) && $storeInfo->business_model != 1) || !$storeInfo || $is_platform){ // $app = self::setBussinessWechat(); // }else{ // // if (is_alipay_platform()) { // // if (is_alipay_platform()) { // // $request = new AlipayOpenAppQrcodeCreateRequest(); // // $param = [ // // 'url_param' => 'user/clerk/clerk', // // 'query_param' => $scene, // // 'describe' => "小程序开发上线使用" // // ]; // // $aliConfigQr = self::aliConfigQr($request, $param); // // $url_path = ""; // // if ($aliConfigQr->code === "10000") { // // $url_path = $aliConfigQr->qr_code_url_circle_blue; // // } // // } // // } // if (is_wechat_platform()) { // $app = self::setWechat(); // } // } // } $app = \app\utils\Wechat\WechatMini::getWechatConfig($store_id, 0, 0, $is_platform, $is_server); if (!$app) { return [ 'code' => 1, 'msg' => '获取小程序信息失败', ]; } $app = $app->app_code; // 服务端小程序 (商家端) // if ($is_server) { // $app = self::serServerWechat(); // } if (is_wechat_platform()) { $response = $app->getUnlimit($scene, [ 'width' => $width, 'page' => $page ]); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->save($dir, $file_name); } else { return [ 'code' => 1, 'response' => $response, ]; } $file_path = $dir. '/' .$file_name . '.jpg'; $root_path = $url_path = ''; if(file_exists($file_path)){ $root_path = $file_path; $url_path = $url . '.jpg'; }else{ $root_path = $dir. '/' .$file_name ; $url_path = $url; } } return [ 'code' => 0, 'root_path' => $root_path, 'url_path' => $url_path ]; } //获取支付宝码 public static function getAlipayQrcode($url_param, $query_param) { try { $store_mini = StoreAliMini::findOne(['store_id' => get_store_id(), 'is_cancel' => 0, 'is_use' => 1]); if (\Yii::$app->isSaas() && $store_mini) { $appid = Option::get("alipay_appid", 0, 'saas')['value']; $private_key = Option::get("alipay_app_private_key", 0, 'saas')['value']; $public_key = Option::get("alipay_public_key", 0, 'saas')['value']; $alipay_aes_key = Option::get("alipay_aes_key", 0, 'saas')['value']; } elseif (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) { $alipay_config = Json::decode(Option::get('one_store_alipay_config', 0, 'saas', '')['value']); $appid = $alipay_config['app_id']; $private_key = $alipay_config['app_private_key']; $public_key = $alipay_config['alipay_public_key']; $alipay_aes_key = $alipay_config['aes_key']; } if (!\Yii::$app->isSaas()) { $alipay_config = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']; $alipay_config = json_decode($alipay_config, true); $alipay_aes_key = $alipay_config['aes_key']; $appid = $alipay_config['app_id']; $public_key = $alipay_config['alipay_public_key']; // $public_key = $alipay_config['app_public_key']; $private_key = $alipay_config['app_private_key']; } //换取token $aop = new AopClient(); $aop->appId = $appid; $aop->rsaPrivateKey = $private_key; $aop->alipayrsaPublicKey = $public_key; $aop->encryptKey = $alipay_aes_key; $aop->encryptType = "AES"; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $param = [ 'url_param' => $url_param, 'query_param' => 'scene=' . $query_param, 'describe' => "小程序开发上线使用" ]; $request = new AlipayOpenAppQrcodeCreateRequest(); $request->setBizContent(json_encode($param)); if ($store_mini) { $result = $aop->execute($request, null, $store_mini->auth_token); } else { $result = $aop->execute($request); } \Yii::error($result); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $result = $result->$responseNode; if (empty($result->code) || $result->code != 10000) { throw new \Exception($result->sub_msg); } $result->root_path = self::saveTempImage($result->qr_code_url_circle_blue); return json_decode(json_encode($result), true); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取网络图片到临时目录 public static 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 static function setSingleStoreWechat($is_platform = false) { if($is_platform){ // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem_dandianpu')) { mkdir(\Yii::$app->runtimePath . '/pem_dandianpu'); file_put_contents(\Yii::$app->runtimePath . '/pem_dandianpu/index.html', ''); } $config = [ 'app_id' => Option::get('platform_mch_appid', 0, 'saas', '')['value'], 'secret' => Option::get('platform_mch_key', 0, 'saas', '')['value'], 'response_type' => 'array' ]; $wechatMini = Factory::miniProgram($config); return $wechatMini->app_code; } // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $config = [ 'app_id' => Option::get('one_store_wechat_appid', 0, 'saas', '')['value'], 'secret' => Option::get('one_store_wechat_secret', 0, 'saas', '')['value'], 'response_type' => 'array' ]; $wechatMini = Factory::miniProgram($config); return $wechatMini->app_code; } public static function setWechat() { // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $store = Store::findOne(get_store_id()); if ((int)$store->business_model !== 1) { $config = [ 'app_id' => Option::get('platform_appid', 0, 'saas')['value'], 'secret' => Option::get('platform_key', 0, 'saas')['value'], 'response_type' => 'array' ]; } else { // 判断是否授权第三方 $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]); if (is_open_platform() && $wechat_config->app_id) { $platform_third_appid = Option::get('platform_third_appid', 0, 'saas')['value']; $platform_third_secret = Option::get('platform_third_secret', 0, 'saas')['value']; $platform_encodingAesKey = Option::get('platform_encodingAesKey', 0, 'saas')['value']; $platform_token = Option::get('platform_token', 0, 'saas')['value']; $config = [ 'app_id' => $platform_third_appid, 'secret' => $platform_third_secret, 'token' => $platform_token, 'aes_key' => $platform_encodingAesKey ]; $openPlatform = Factory::openPlatform($config); $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]); $bese_info = $openPlatform->getAuthorizer($wechat_config->app_id); $wechatMini = $openPlatform->miniProgram($wechat_config->app_id, $bese_info['authorization_info']['authorizer_refresh_token']); return $wechatMini->app_code; } else { $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]); if ((int)get_store_id() === -1) { $wechat_config = []; $wechat_config['app_id'] = Option::get('platform_appid', 0, 'saas')['value'];//platform_key platform_pay_key platform_mch_id platform_apiclient_cert platform_apiclient_key $wechat_config['app_secret'] = Option::get('platform_key', 0, 'saas')['value']; $wechat_config['pay_key'] = Option::get('platform_pay_key', 0, 'saas')['value']; $wechat_config['mch_id'] = Option::get('platform_mch_id', 0, 'saas')['value']; $wechat_config['cert_pem'] = Option::get('platform_apiclient_cert', 0, 'saas')['value']; $wechat_config['key_pem'] = Option::get('platform_apiclient_key', 0, 'saas')['value']; $wechat_config = (object)$wechat_config; } $cert_pem_file = null; if (isset($wechat_config->cert_pem)) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem); if (!file_exists($cert_pem_file)) { file_put_contents($cert_pem_file, $wechat_config->cert_pem); } } $key_pem_file = null; if (isset($wechat_config->key_pem)) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem); if (!file_exists($key_pem_file)) { file_put_contents($key_pem_file, $wechat_config->key_pem); } } $config = [ 'app_id' => $wechat_config->app_id, 'secret' => $wechat_config->app_secret, 'key' => $wechat_config->pay_key, 'mch_id' => $wechat_config->mch_id, 'cert_path' => $cert_pem_file, 'key_path' => $key_pem_file, 'response_type' => 'array' ]; } } $wechatMini = Factory::miniProgram($config); return $wechatMini->app_code; } public static function setBussinessWechat() { // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $config = [ 'app_id' => Option::get('platform_appid', 0, 'saas')['value'], 'secret' => Option::get('platform_key', 0, 'saas')['value'], 'response_type' => 'array' ]; if (\Yii::$app->prod_is_dandianpu()) { $config['app_id'] = Option::get('one_store_wechat_appid', 0, 'saas')['value']; $config['secret'] = Option::get('one_store_wechat_secret', 0, 'saas')['value']; } $wechatMini = Factory::miniProgram($config); return $wechatMini->app_code; } // 服务端小程序(商家端小程序) public static function serServerWechat() { $config = [ 'app_id' => Option::get('platform_mch_appid', 0, 'saas')['value'], 'secret' => Option::get('platform_mch_key', 0, 'saas')['value'], 'response_type' => 'array' ]; $wechatMini = Factory::miniProgram($config); return $wechatMini->app_code; } public static function aliConfigQr($request, $data = []) { try { $third_appid = Option::get("alipay_appid", 0, 'saas')['value']; $third_private_key = Option::get("alipay_app_private_key", 0, 'saas')['value']; $third_public_key = Option::get("alipay_public_key", 0, 'saas')['value']; $data = json_encode($data); $config_ali = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']; $config_ali = json_decode($config_ali, true); $aop = new AopClient(); //如果商城的支付宝小程序信息存在,则使用支付宝小程序的配置信息 if (!empty($config_ali['app_id']) && !empty($config_ali['app_private_key']) && !empty($config_ali['app_public_key'])) { $aop->appId = $config_ali['app_id']; $aop->rsaPrivateKey = $config_ali['app_private_key']; $aop->encryptKey = $config_ali['aes_key']; $aop->alipayrsaPublicKey = $config_ali['alipay_public_key']; } else { //使用三方的配置信息 $aop->appId = $third_appid; $aop->rsaPrivateKey = $third_private_key; $aop->alipayrsaPublicKey = $third_public_key; $ali_mini = StoreAliMini::find()->where(['auth_app_id' => $config_ali['app_id'], 'is_cancel' => 0, 'store_id' => get_store_id()])->asArray()->one(); if (empty($ali_mini)) { throw new \Exception("查找小程序信息失败"); } $token = $ali_mini['auth_token']; } $aop->encryptType = "AES"; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request->setBizContent($data); if (!empty($token)) { $result = $aop->execute ($request, null, $token); } else { $result = $aop->execute ($request); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return $result->$responseNode; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }