| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentConfirmRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentFacetofaceSignRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentOrderQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldApplyRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiSceneQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppMembersCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppMembersQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppQrcodeCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesGetRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthTokenAppQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthTokenAppRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniBaseinfoModifyRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniBaseinfoQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniCategoryQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceCancelRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIndividualBusinessCertifyRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditApplyRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditCancelRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditedCancelRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionBuildQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionDeleteRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionDetailQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionListQueryRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionOnlineRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionUploadRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayTradeCreateRequest;
- use AopClient;
- use app\models\AggregateQrcode;
- use app\models\Option;
- use app\models\StoreAliMini;
- use app\models\StoreAliMiniVersion;
- use app\utils\CurlHelper;
- use yii\base\Model;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesSetRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvQueryRequest;
- use yii\helpers\Json;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class AlipayThirdForm extends Model
- {
- /**
- * @var mixed
- */
- public $alipay_aes_key;
- public $appid;
- public $method;
- public $sign_type;
- public $sign;
- public $version = "1.0";
- public $charset;
- public $biz_content;
- public $timestamp;
- public $auth_code;
- public $baseUrl = "https://openapi.alipay.com/gateway.do";
- public $private_key;
- public $public_key;
- public $license_no; //营业执照编号
- public $license_pic; //营业执照照片
- public $template_version; //模版版本号
- public $ext; //ext
- public $template_id; //模版ID
- public $app_version; //版本号
- public $bundle_id; //小程序投放的端参数
- public $AppName;
- public $AppEnglishName;
- public $AppSlogan;
- public $AppLogo;
- public $AppDesc;
- public $ServicePhone;
- public $ServiceEmail;
- public $MiniCategoryIds;
- public $page_num;
- public $page_size;
- public $auditData; //审核资料
- public $is_filter;
- public $gray_strategy;
- public $route_url;
- public $mode;
- public $page_redirection;
- public $route_pattern;
- public $mini_id;
- public $version_id;
- public $role;
- public $logon_id;
- public $license_name;
- public $license_date;
- public $is_long_effective;
- public $alipay_account;
- public $legal_personal_name;
- public $contact_name;
- public $merchant_id;
- public $bind_store_id;
- public $apply_msg;
- public $fun = 'setPicture';
- /*支付回调url*/
- private static $notify_url = 'alipay/notify';
- public function rules()
- {
- return [
- [["page_num", "page_size", 'is_filter', 'gray_strategy', 'version_id', 'role', 'is_long_effective', 'merchant_id', "bind_store_id"], 'integer'],
- [
- [
- "appid",
- "method",
- "sign_type",
- "sign",
- "version",
- "charset",
- "biz_content",
- "timestamp",
- "auth_code",
- "baseUrl",
- "license_no",
- "license_pic",
- "template_version",
- "ext",
- "template_id",
- "app_version",
- "bundle_id",
- "AppName",
- "AppEnglishName",
- "AppSlogan",
- "AppLogo",
- "AppDesc",
- "ServicePhone",
- "ServiceEmail",
- "MiniCategoryIds",
- "auditData",
- "route_pattern",
- "page_redirection",
- "mode",
- "route_url",
- "page_num",
- "page_size",
- "gray_strategy",
- "url_param",
- "query_param",
- "describe",
- "color",
- "size",
- "mini_id",
- "logon_id",
- "license_name",
- "license_date",
- "apply_msg"
- ], 'string'
- ],
- ];
- }
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->appid = Option::get("alipay_appid", 0, 'saas')['value'];
- $this->timestamp = date("Y-m-d H:i:s");
- $this->sign_type = "RSA2";
- $this->charset = "utf-8";
- $this->private_key = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- $this->public_key = Option::get("alipay_public_key", 0, 'saas')['value'];
- $this->alipay_aes_key = Option::get("alipay_aes_key", 0, 'saas')['value'];
- }
- /**
- * 公共参数请求
- * @return \$1|false|mixed|\SimpleXMLElement
- * @throws \Exception
- *
- *
- */
- public function miniCommon($request, $is_hidden_biz_content = 0, $id = 0, $is_hidden_token = 0, $is_new = 0)
- {
- try {
- $auth_token = "";
- $mini_id = $this->mini_id;
-
- if (!empty($this->bind_store_id)) {
- $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- $mini_id = $StoreAliMini->id;
- }
- if (!empty($id != false ? $id : $mini_id)) {
- $mini = StoreAliMini::findOne($id != false ? $id : $mini_id);
- $auth_token = $mini->auth_token;
- }
- if (empty($auth_token) && $is_new == 1) {
- $auth_token = Option::get("ali_template_token", 0, 'saas')['value'];
- }
- //换取token
- $aop = new AopClient();
- $aop->gatewayUrl = $this->baseUrl;
- $aop->appId = $this->appid;
- $aop->rsaPrivateKey = $this->private_key;
- $aop->alipayrsaPublicKey = $this->public_key;
- $aop->encryptKey = $this->alipay_aes_key;
- $aop->encryptType = "AES";
- $aop->apiVersion = '1.0';
- $aop->signType = $this->sign_type;
- $aop->postCharset = $this->charset;
- if ($is_hidden_biz_content == 0) {
- $request->setBizContent($this->biz_content);
- }
- if (!empty($auth_token) && empty($is_hidden_token)) {
- $result = $aop->execute($request, null, $auth_token);
- } else {
- $result = $aop->execute($request);
- }
- \Yii::error($result);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return $result->$responseNode;
- } catch (\Exception $e) {
- return (object)[
- 'sub_msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 传递授权回调参数
- * @return array
- */
- public function getParams()
- {
- $data = [
- 'appid' => $this->appid,
- 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/admin/#/setting/alipayApplet')
- ];
- return [
- 'code' => 0,
- 'url' => "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=TINYAPP&redirect_uri=" . $data['redirect_uri'],
- 'msg' => "获取成功"
- ];
- }
- /**
- * 保存授权码
- * 获取保存AuthToken以及小程序信息
- * 同时获取aes密钥
- * 通过授权码AuthCode换取AuthToken
- *
- */
- public function setAuthCode()
- {
- try {
- $param = [
- "grant_type" => "authorization_code",
- "code" => $this->auth_code
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.auth.token.app";
- $request = new AlipayOpenAuthTokenAppRequest();
- $result = $this->miniCommon($request, 0, 0, 0, 0);
- \Yii::error($result);
- $token = $result->tokens[0];
- if (!empty($result->code) && $result->code == 10000) {
- $mini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'auth_app_id' => $token->auth_app_id])->one();
- if (empty($mini)) {
- $mini = new StoreAliMini();
- $mini->store_id = get_store_id();
- $mini->auth_app_id = $token->auth_app_id;
- }
- $mini->auth_code = $this->auth_code;
- $mini->userid = $token->user_id;
- $mini->auth_token = $token->app_auth_token;
- $mini->refresh_token = json_encode([
- 'app_refresh_token' => $token->app_refresh_token,
- 'end_time' => time() * 1 + $token->re_expires_in
- ]);
- $res = $mini->save();
- if ($res) {
- //获取应用aes密钥
- $res = $this->aesAuth(0, $mini->id);
- if ($res['code'] === 0) {
- $res = $this->getAppTokenInfo($mini->id);
- if ($res['code'] === 0) {
- $res = $this->updateMiniVersion($mini->id);
- if ($res['code'] === 0) {
- $res = $this->getMiniBaseInfo($mini->id);
- if ($res['code'] !== 0) {
- throw new \Exception($res['msg']);
- }
- } else {
- throw new \Exception($res['msg']);
- }
- } else {
- throw new \Exception($res['msg']);
- }
- } else {
- throw new \Exception($res['msg']);
- }
- } else {
- throw new \Exception($mini->errors);
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- return [
- 'code' => 0,
- 'msg' => "success"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- /**
- * 授权应用aes密钥设置/查询
- */
- public function aesAuth($is_get = 0, $id = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($id);
- $auth_app_id = Option::get("ali_template_appid", 0, 'saas')['value'];;
- if (!empty($mini)) {
- $auth_app_id = $mini->auth_app_id;
- }
- if (empty($auth_app_id)) {
- throw new \Exception("请先配置小程序信息");
- }
- \Yii::error($auth_app_id);
- $param = [
- "merchant_app_id" => $auth_app_id
- ];
- \Yii::error($param);
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.auth.app.aes.set";
- $request = new AlipayOpenAuthAppAesSetRequest();
- if ($is_get == 1) {
- $this->method = "alipay.open.auth.app.aes.get";
- $request = new AlipayOpenAuthAppAesGetRequest();
- }
- if (empty($id)) {
- $is_new = 1;
- } else {
- $is_new = 0;
- }
- $result = $this->miniCommon($request, 0, $id, 1, $is_new);
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- if (!empty($id) && !empty($mini)) {
- $mini->aes_key = $result->aes_key;
- } else {
- Option::set('ali_template_aes', $result->aes_key, 0, 'saas');
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result->aes_key
- ];
- }
- $res = $mini->save();
- if ($res) {
- $t->commit();
- $data['aes_key'] = $mini->aes_key;
- $data['app_id'] = $mini->auth_app_id;
- $data['alipay_public_key'] = "";
- $data['app_public_key'] = "";
- $data['app_private_key'] = "";
- $data['user_id'] = $mini->userid;
- $data['name'] = $mini->license_name;
- $bind_store_id = $mini->store_id;
- $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $bind_store_id, 'alipay');
- if ($res) {
- \Yii::$app->cache->delete('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id());
- \Yii::$app->cache->set('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id(), Json::encode($data));
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } else {
- throw new \Exception($mini->errors);
- }
- } else {
- throw new \Exception($result->sub_msg . $result->code);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 个人账户升级为个体工商户
- */
- public function updateBussiness($id = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id);
- if (empty($mini)) {
- throw new \Exception("授权失败");
- }
- $mini->license_no = $this->license_no;
- $mini->license_pic = $this->license_pic;
- $index = strpos($this->license_pic, \Yii::$app->request->hostName);
- $len = $index + strlen(\Yii::$app->request->hostName);
- $license_pic = substr($this->license_pic, $len);
- $license_pic = $this->image2Base64(\Yii::$app->basePath . $license_pic);
- if ($license_pic == false) {
- throw new \Exception("图片转Base64失败");
- }
- $param = [
- "license_no" => $this->license_no,
- "license_pic" => $license_pic
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.individual.business.certify";
- $request = new AlipayOpenMiniIndividualBusinessCertifyRequest();
- $request->setLicenseNo($this->license_no);
- $request->setLicensePic($license_pic);
- $result = $this->miniCommon($request, 1);
- if (!empty($result->code) && $result->code == 10000) {
- $mini->is_merchant = $result->certify_result ? 1 : 0;
- $res = $mini->save();
- if ($res) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } else {
- throw new \Exception($mini->errors);
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询某个应用授权AppAuthToken的授权信息
- */
- public function getAppTokenInfo($id)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- "app_auth_token" => $mini->auth_token
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.auth.token.app.query";
- $request = new AlipayOpenAuthTokenAppQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序基于模板上传版本
- */
- public function updateMiniVersion($id = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = !empty($id) ? $id : $this->mini_id;
- if (!empty($this->bind_store_id)) {
- $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- $id = $StoreAliMini->id;
- }
- $mini = StoreAliMini::findOne($id);
- $version = StoreAliMiniVersion::find()->where(['mini_id' => $id])
- ->orderBy("create_time desc")
- ->asArray()
- ->one();
- if ($version['is_submit'] == 1) {
- $res = $this->miniAuditCancel();
- if ($res['code'] !== 0) {
- throw new \Exception("有版本代码正在审核");
- }
- }
- if (empty($version)) {
- $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", 1);
- } else {
- \Yii::error("版本号:" . $version['template_version']);
- $app_version = substr($version['template_version'], 7);
- \Yii::error("版本号:" . $app_version);
- $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", $app_version + 1);
- }
- \Yii::error("版本号:" . $app_version);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $ext = json_encode([
- 'extEnable' => true,
- 'ext' => [
- 'shopId' => $this->appid,
- 'mini_id' => $id,
- 'store_id' => get_store_id(),
- 'apiurl' => "https://" . \Yii::$app->request->hostName
- ]
- ]);
- $param = [
- 'ext' => $ext,
- 'template_id' => Option::get("ali_template_appid", 0, 'saas')['value'],
- 'app_version' => $app_version,
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.upload";
- $request = new AlipayOpenMiniVersionUploadRequest();
- $result = $this->miniCommon($request, 0, $id);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion = new StoreAliMiniVersion();
- $MiniVersion->mini_id = $id;
- $MiniVersion->template_version = $app_version;
- $MiniVersion->create_time = time();
- $MiniVersion->is_up = 1;
- $res = $MiniVersion->save();
- if ($res) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "构建成功",
- 'data' => $result,
- "res" => $MiniVersion->errors
- ];
- } else {
- throw new \Exception(json_encode($MiniVersion->errors));
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序查询版本构建状态
- * alipay.open.mini.version.build.query
- */
- public function getMiniStatus()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- } else {
- $mini_id = $this->mini_id;
- $mini = StoreAliMini::findOne($mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $version = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- $param = [
- 'app_version' => $version->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.build.query";
- $request = new AlipayOpenMiniVersionBuildQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序查询版本列表
- * alipay.open.mini.version.build.query
- */
- public function getMiniVersionList()
- {
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $this->method = "alipay.open.mini.version.list.query";
- $request = new AlipayOpenMiniVersionListQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序删除版本
- * alipay.open.mini.version.delete
- */
- public function delMiniVersion()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version,
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.delete";
- $request = new AlipayOpenMiniVersionDeleteRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion->delete();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "删除成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序生成体验版
- * alipay.open.mini.experience.create
- */
- public function createMiniExperience()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version,
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.experience.create";
- $request = new AlipayOpenMiniExperienceCreateRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- StoreAliMiniVersion::updateAll(['is_experience' => 0, 'experience_url' => ''], ['and', ['<>', 'id', $MiniVersion->id], ['mini_id' => $this->mini_id]]);
- $res = $this->createMiniExperienceQrcode();
- if ($res['code'] === 0) {
- $MiniVersion->is_experience = 1;
- $MiniVersion->experience_url = $res['url'];
- $MiniVersion->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res['url']
- ];
- } else {
- throw new \Exception("获取体验码失败");
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取体验码
- * alipay.open.mini.experience.query
- */
- public function createMiniExperienceQrcode()
- {
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version,
- 'bundle_id' => $this->bundle_id ?? "com.alipay.alipaywallet",
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.experience.query";
- $request = new AlipayOpenMiniExperienceQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $msg = "获取成功";
- if ($result->status == "expVersionPackaging") {
- $msg = "体验版打包中";
- } elseif ($result->status == "notExpVersion") {
- $msg = "非体验版";
- }
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'url' => $result->exp_qr_code_url
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序取消体验版
- * alipay.open.mini.experience.create()
- */
- public function createMiniExperienceCancel()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.experience.cancel";
- $request = new AlipayOpenMiniExperienceCancelRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion->is_experience = 0;
- $MiniVersion->experience_url = "";
- $MiniVersion->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询小程序基础信息
- * alipay.open.mini.baseinfo.query()
- */
- public function getMiniBaseInfo($id = 0, $type = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- $id = $mini->id;
- } else {
- $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $this->method = "alipay.open.mini.baseinfo.query";
- $request = new AlipayOpenMiniBaseinfoQueryRequest();
- $result = $this->miniCommon($request, 1, $id != 0 ? $id : $this->mini_id);
- if (!empty($result->code) && $result->code == 10000) {
- if ($type === 0) {
- $mini->app_name = $result->app_name;
- $mini->app_desc = $result->app_desc;
- $mini->app_slogan = $result->app_slogan;
- $mini->app_english_name = $result->app_english_name;
- $mini->service_email = $result->service_email;
- $mini->service_phone = $result->service_phone;
- $mini->app_logo = $result->app_logo;
- }
- $mini->category_name = $result->category_names;
- $res = $mini->save();
- if ($res) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result,
- 'category' => $result->category_names
- ];
- } else {
- throw new \Exception("插入数据错误");
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序修改基础信息
- * alipay.open.mini.baseinfo.modify(小程序修改基础信息)
- */
- public function setMiniBaseInfo()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini_id = $this->mini_id;
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- $mini_id = $mini->id;
- } else {
- $mini = StoreAliMini::findOne($mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $mini_category_ids = '';
- if (!empty($this->bind_store_id) && !empty($this->MiniCategoryIds)) {
- foreach ($this->MiniCategoryIds as $item) {
- $arr = [];
- foreach ($item as $child) {
- array_push($arr, $child['value']);
- }
- $str = implode('_', $arr);
- $mini_category_ids .= $str . ';';
- }
- } else {
- $mini_category_ids = implode(';', $this->MiniCategoryIds);
- }
- $this->method = "alipay.open.mini.baseinfo.modify";
- $request = new AlipayOpenMiniBaseinfoModifyRequest();
- $mini->app_name = $this->AppName;
- $mini->app_english_name = $this->AppEnglishName;
- $mini->app_slogan = $this->AppSlogan;
- if (empty($this->bind_store_id)) {
- !empty($this->license_no) && $mini->license_no = $this->license_no;
- !empty($this->license_pic) && $mini->license_pic = $this->license_pic;
- !empty($this->license_name) && $mini->license_name = $this->license_name;
- !empty($this->is_long_effective) && $mini->is_long_effective = $this->is_long_effective;
- !empty($this->license_date) && $mini->license_date = $this->license_date;
- $mini->is_long_effective = !empty($mini->is_long_effective) ? 1 : 0;
- }
- $mini->app_logo = $this->AppLogo;
- $mini->app_desc = $this->AppDesc;
- $mini->service_phone = $this->ServicePhone;
- $mini->service_email = $this->ServiceEmail;
- $mini->mini_categoryIds = $mini_category_ids;
- $mini->category_data = json_encode($this->MiniCategoryIds);
- $res = $mini->save();
- if ($res) {
- if (empty($this->bind_store_id)) {
- $res = $this->updateBussiness($mini_id);
- if ($res['code'] !== 0) {
- throw new \Exception($res['msg']);
- }
- }
- \Yii::error($this->AppName);
- \Yii::error($this->AppEnglishName);
- \Yii::error($this->AppSlogan);
- \Yii::error($this->AppLogo);
- \Yii::error($this->AppDesc);
- \Yii::error($this->ServicePhone);
- \Yii::error($this->ServiceEmail);
- \Yii::error($this->MiniCategoryIds);
- $request->setAppName($this->AppName); //小程序应用名称。
- $request->setAppEnglishName($this->AppEnglishName); //小程序应用英文名称。
- $request->setAppSlogan($this->AppSlogan); //小程序应用简介
- $request->setAppLogo("@" . $this->saveTempImage($this->AppLogo)); //小程序应用logo图标,图片格式必须为:png、jpeg、jpg,建议上传像素为180*180。
- $request->setAppDesc($this->AppDesc); //小程序应用描述,20-200个字
- $request->setServicePhone($this->ServicePhone); //小程序客服电话。
- $request->setServiceEmail($this->ServiceEmail); //小程序客服邮箱。
- $request->setMiniCategoryIds($mini_category_ids); //新小程序前台类目。格式为 第一个一级类目_第一个二级类目;第二个一级类目_第二个二级类目_第二个三级类目。详细类目可以通过
- $result = $this->miniCommon($request, 1);
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- }
- throw new \Exception("信息保存失败");
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询使用模板的小程序列表
- * alipay.open.mini.template.usage.query(查询使用模板的小程序列表)
- */
- // public function getTemplateUsage(){
- // try {
- // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
- // if(empty($mini)){
- // throw new \Exception("没有进行授权");
- // }
- // $param = [
- // 'template_id'=>$this->template_id,
- // 'page_num'=>$this->page_num,
- // 'page_size'=>$this->page_size,
- // 'template_version'=>$this->template_version,
- // 'bundle_id'=>$this->bundle_id,
- // ];
- // $this->biz_content = json_encode($param);
- // $this->method = "alipay.open.mini.template.usage.query";
- // $request = new AlipayOpenMiniTemplateUsageQueryRequest();
- // $result = $this->miniCommon($request);
- // if(!empty($result->code)&&$result->code == 10000){
- // return [
- // 'code'=>0,
- // 'msg'=>"成功",
- // 'data'=>$result
- // ];
- // }else{
- // throw new \Exception($result->sub_msg);
- // }
- // }catch (\Exception $e){
- // return [
- // 'code'=>1,
- // 'msg'=>$e->getMessage()
- // ];
- // }
- // }
- /**
- * 添加成员
- * alipay.open.app.members.create(添加成员)
- */
- public function miniAddMember()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $role = $this->role;
- switch ($role) {
- case 0:
- throw new \Exception("请选择身份");
- break;
- case 1:
- $role = "DEVELOPER";
- break;
- case 2:
- $role = "EXPERIENCER";
- break;
- }
- $param = [
- 'logon_id' => $this->logon_id,
- 'role' => $role
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.app.members.create";
- $request = new AlipayOpenAppMembersCreateRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取成员列表
- */
- public function getRoleList()
- {
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $role = $this->role;
- switch ($role) {
- case 0:
- throw new \Exception("请选择身份");
- break;
- case 1:
- $role = "DEVELOPER";
- break;
- case 2:
- $role = "EXPERIENCER";
- break;
- }
- $param = [
- 'role' => $role
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.app.members.query";
- $request = new AlipayOpenAppMembersQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取审核资料
- *
- */
- public function getMiniApplyInfo()
- {
- $mini = StoreAliMini::find()->alias('sam')
- ->where(['sam.id' => $this->mini_id, 'samv.id' => $this->version_id])
- ->select("
- sam.id,
- sam.license_name,
- sam.license_date,
- sam.is_long_effective,
- sam.license_no,
- sam.license_pic,
- sam.mini_categoryIds,
- sam.app_name,
- sam.app_english_name,
- sam.app_slogan,
- sam.app_logo,
- sam.app_desc,
- sam.service_phone,
- sam.service_email,
- samv.mini_id,
- samv.id as version_id,
- samv.desc,
- samv.template_version as version
- ")
- ->leftJoin(['samv' => StoreAliMiniVersion::tableName()], 'sam.id = samv.mini_id')
- ->asArray()
- ->one();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $mini
- ];
- }
- /**
- * 小程序提交审核
- * alipay.open.mini.version.audit.apply(小程序提交审核)
- */
- public function miniAuditApply()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- } else {
- $mini = StoreAliMini::findOne($this->auditData['id']);
- }
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini->id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $this->method = "alipay.open.mini.version.audit.apply";
- $request = new AlipayOpenMiniVersionAuditApplyRequest();
- $auditData = $this->auditData;
- if (!empty($this->bind_store_id)) {
- $mini_category_ids = $auditData['mini_category_ids'];
- } else {
- $mini_category_ids = implode(';', $auditData['mini_category_ids']);
- }
- $auditData['version_desc'] = '商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线';
- !empty($auditData['service_phone']) && $request->setServicePhone($auditData['service_phone']); //小程序手机号
- !empty($auditData['service_email']) && $request->setServiceEmail($auditData['service_email']); //小程序邮箱地址
- $request->setVersionDesc($auditData['version_desc']); //小程序版本描述,30-500个字符。
- $request->setRegionType("CHINA"); //小程序服务区
- !empty($auditData['license_no']) && $request->setLicenseNo($auditData['license_no']); //营业执照号
- !empty($auditData['mini_category_ids']) && $request->setMiniCategoryIds($mini_category_ids);
- !empty($auditData['license_name']) && $request->setLicenseName($auditData['license_name']); //营业执照名称
- !empty($auditData['license_pic']) && $request->setFirstLicensePic("@" . $this->saveTempImage($auditData['license_pic'])); //营业执照
- !empty($auditData['license_date']) && $request->setLicenseValidDate(!empty($auditData['is_long_effective']) ? '9999-12-31' : $auditData['license_date']); //营业执照有效期
- $request->setAppVersion($MiniVersion->template_version); //小程序版本号
- !empty($auditData['app_name']) && $request->setAppName($auditData['app_name']); //小程序名称
- !empty($auditData['app_english_name']) && $request->setAppEnglishName($auditData['app_english_name']); //英文名称
- !empty($auditData['app_slogan']) && $request->setAppSlogan($auditData['app_slogan']); //小程序应用简介,一句话描述小程序功能
- !empty($auditData['app_logo']) && $request->setAppLogo("@" . $this->saveTempImage($auditData['app_logo'])); //小程序logo图标
- !empty($auditData['app_desc']) && $request->setAppDesc($auditData['app_desc']); //小程序应用描述
- $mini->license_name = $auditData['license_name'];
- $mini->license_date = $auditData['license_date'];
- $mini->is_long_effective = $auditData['is_long_effective'];
- $mini->license_no = $auditData['license_no'];
- $mini->license_pic = $auditData['license_pic'];
- $mini->mini_categoryIds = $mini_category_ids;
- $mini->app_name = $auditData['app_name'];
- $mini->app_english_name = $auditData['app_english_name'];
- $mini->app_slogan = $auditData['app_slogan'];
- $mini->app_logo = $auditData['app_logo'];
- $mini->app_desc = $auditData['app_desc'];
- $mini->service_phone = $auditData['service_phone'];
- $mini->service_email = $auditData['service_email'];
- $mini->category_name = json_encode($auditData['mini_category_ids']);
- if (empty($this->bind_store_id)) {
- $res = $mini->save();
- } else {
- $res = true;
- }
- if ($res) {
- $result = $this->miniCommon($request, 1, $mini->id);
- if (!empty($result->code) && $result->code != 10000) {
- throw new \Exception($result->sub_msg);
- }
- $MiniVersion->is_submit = 1;
- $res = $MiniVersion->save();
- if ($res) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "开始审核",
- 'data' => $result,
- ];
- } else {
- return $MiniVersion->errors;
- }
- } else {
- return $mini->errors;
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine() . $e->getFile()
- ];
- }
- }
- /**
- * 小程序撤销审核
- * alipay.open.mini.version.audit.cancel(小程序撤销审核)
- */
- public function miniAuditCancel()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.audit.cancel";
- $request = new AlipayOpenMiniVersionAuditCancelRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion->is_submit = 0;
- $MiniVersion->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序类目树查询
- * alipay.open.mini.category.query(小程序类目树查询)
- */
- public function getCategoryList()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
- } else {
- $mini = StoreAliMini::findOne($this->mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $is_filter = $this->is_filter == 1;
- $param = [
- "is_filter" => $is_filter
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.category.query";
- $request = new AlipayOpenMiniCategoryQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $arr = json_decode(json_encode($result->mini_category_list), true);
- $data = $this->getdata($arr);
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $data
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- '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;
- }
- /**
- * 小程序退回开发
- * alipay.open.mini.version.audited.cancel(小程序退回开发)
- */
- public function miniVersionCancel()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.audited.cancel";
- $request = new AlipayOpenMiniVersionAuditedCancelRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion->is_submit = 0;
- $MiniVersion->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序版本详情查询
- * alipay.open.mini.version.detail.query(小程序版本详情查询)
- */
- public function miniVersionDetail()
- {
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.detail.query";
- $request = new AlipayOpenMiniVersionDetailQueryRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序灰度上架
- * alipay.open.mini.version.gray.online(小程序灰度上架)
- */
- // public function miniGrayOnline(){
- // try {
- // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
- // if(empty($mini)){
- // throw new \Exception("没有进行授权");
- // }
- // $gray_strategy = 'p'.(int)abs($this->gray_strategy);//只支持10/30/50
- // $param = [
- // 'app_version'=>$this->app_version,
- // 'gray_strategy'=>$gray_strategy,
- // 'bundle_id'=>$this->bundle_id,
- // ];
- // $this->biz_content = json_encode($param);
- // $this->method = "alipay.open.mini.version.gray.online";
- // $request = new AlipayOpenMiniVersionGrayOnlineRequest();
- // $result = $this->miniCommon($request);
- // if(!empty($result->code)&&$result->code == 10000){
- // return [
- // 'code'=>0,
- // 'msg'=>"成功",
- // 'data'=>$result
- // ];
- // }else{
- // throw new \Exception($result->sub_msg);
- // }
- // }catch (\Exception $e){
- // return [
- // 'code'=>1,
- // 'msg'=>$e->getMessage()
- // ];
- // }
- // }
- /**
- * 小程序结束灰度
- * alipay.open.mini.version.gray.cancel(小程序结束灰度)
- */
- // public function miniGrayCancel(){
- // try {
- // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
- // if(empty($mini)){
- // throw new \Exception("没有进行授权");
- // }
- // $param = [
- // 'app_version'=>$this->app_version,
- // 'bundle_id'=>$this->bundle_id,
- // ];
- // $this->biz_content = json_encode($param);
- // $this->method = "alipay.open.mini.version.gray.cancel";
- // $request = new AlipayOpenMiniVersionGrayCancelRequest();
- // $result = $this->miniCommon($request);
- // if(!empty($result->code)&&$result->code == 10000){
- // return [
- // 'code'=>0,
- // 'msg'=>"成功",
- // 'data'=>$result
- // ];
- // }else{
- // throw new \Exception($result->sub_msg);
- // }
- // }catch (\Exception $e){
- // return [
- // 'code'=>1,
- // 'msg'=>$e->getMessage()
- // ];
- // }
- // }
- /**
- * 小程序上架
- * alipay.open.mini.version.online(小程序上架)
- */
- public function miniOnline()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id,aes_key,auth_app_id,userid,license_name')->orderBy('id desc')->one();
- $mini_id = $mini->id;
- } else {
- $mini_id = $this->mini_id;
- $mini = StoreAliMini::findOne($mini_id);
- }
- $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy("create_time desc")->one();
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'app_version' => $MiniVersion->template_version
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.version.online";
- $request = new AlipayOpenMiniVersionOnlineRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $MiniVersion->is_submit = 4;
- $res = $MiniVersion->save();
- if ($res) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'data' => $result
- ];
- } else {
- throw new \Exception("保存数据错误");
- }
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序生成推广二维码接口
- * alipay.open.app.qrcode.create(小程序生成推广二维码接口)
- */
- public function miniQrcodeCreate()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mini = StoreAliMini::findOne($this->mini_id);
- if (empty($mini)) {
- throw new \Exception("没有进行授权");
- }
- $param = [
- 'url_param' => "pages/home/home",
- 'query_param' => "mini_id=" . $this->mini_id,
- 'describe' => "小程序开发上线使用"
- ];
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.app.qrcode.create";
- $request = new AlipayOpenAppQrcodeCreateRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 图片转base64
- */
- public function image2Base64($file)
- {
- if ($fp = fopen($file, "rb", 0)) {
- $gambar = fread($fp, filesize($file));
- fclose($fp);
- $base64 = chunk_split(base64_encode($gambar));
- return $base64;
- } else {
- return false;
- }
- }
- /**
- * 获取列表
- */
- public function getMiniList()
- {
- $query = StoreAliMini::find()->where(['store_id' => get_store_id()])
- ->select("id,userid,auth_app_id,license_name,license_no,app_logo,app_name,category_name,mini_categoryIds,apply_phone_msg,is_get_phone,apply_phone_fail");
- if ($this->AppName) {
- $query->andWhere(['LIKE', 'app_name', $this->AppName]);
- }
- $query->andWhere(['is_cancel' => 0]);
- $pagination = pagination_make($query);
- $admins = $pagination['list'];
- foreach ($admins as &$admin) {
- $version = StoreAliMiniVersion::find()->where(['mini_id' => $admin['id']])->orderBy('create_time desc')->asArray()->one();
- \Yii::error($version);
- $admin['is_submit'] = !empty($version['is_submit']) ? $version['is_submit'] * 1 : 0;
- if (empty($version)) {
- $admin['is_submit'] = -1;
- if ($admin['order_cancel'] == 1) {
- $admin['is_submit'] = -2;
- }
- }
- $admin['apply_msg'] = [];
- if (!empty($admin['apply_phone_msg'])) {
- $admin['apply_msg'] = json_decode($admin['apply_phone_msg']);
- }
- $admin['is_get_phone'] = empty($this->getField($admin['id'])['data']) ? 2 : $admin['is_get_phone'];
- $admin['is_experience'] = !empty($version['is_experience']) ? $version['is_experience'] : 0;
- $admin['experience_url'] = !empty($version['experience_url']) ? $version['experience_url'] : '';
- $admin['mini_categoryIds'] = !empty($admin['mini_categoryIds']) ? explode(';', $admin['mini_categoryIds']) : [];
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $admins,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ];
- }
- /**
- * 获取版本列表
- */
- // public function getMiniVersionList(){
- // $query = StoreAliMiniVersion::find()->where(['mini_id'=>$this->mini_id])->select("id,template_version,create_time,is_up,is_experience,experience_url,is_submit,is_put");
- // $pagination = pagination_make($query);
- // $admins = $pagination['list'];
- // return [
- // 'code'=>0,
- // 'msg'=>"获取成功",
- // 'data'=>$admins,
- // 'pageNo' => $pagination['pageNo'],
- // 'totalCount' => $pagination['totalCount'],
- // ];
- // }
- /**
- * 获取修改信息
- */
- public function getMiniInfo()
- {
- $query = StoreAliMini::find()->where(['id' => $this->mini_id])->select("id,app_name,app_english_name,app_slogan,app_logo,app_desc,service_phone,service_email,license_no,license_pic,license_name,license_date,is_long_effective,category_name,mini_categoryIds");
- $mini_info = $query->asArray()->one();
- $version_info = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy('create_time desc')->select('template_version,is_submit')->one();
- $mini_info['version'] = $version_info->template_version;
- $mini_info['is_submit'] = $version_info->is_submit;
- $mini_info['mini_category_ids'] = !empty($mini_info['mini_categoryIds']) ? explode(';', $mini_info['mini_categoryIds']) : [];
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $mini_info
- ];
- }
- /**
- * 单一版本信息
- */
- // public function getVersionInfo(){
- // $mini = StoreAliMiniVersion::find()->where(['id'=>$this->version_id])->select('id,create_time,template_version as version,is_up,is_experience,experience_url,is_submit,is_put')->asArray()->one();
- // $mini['create_time'] = date("Y-m-d H:i:s",$mini['create_time']);
- // return [
- // 'code'=>0,
- // 'msg'=>"获取成功",
- // 'data'=>$mini
- // ];
- // }
- /**
- * isv服务商代商户创建小程序
- */
- public function isvCreateMini()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id')->orderBy('id desc')->one();
- $mini_id = $mini->id;
- } else {
- $mini_id = $this->mini_id;
- $store_id = get_store_id();
- }
- if (!empty($mini_id)) {
- $ali_mini = StoreAliMini::find()->where(['id' => $mini_id])->one();
- } else {
- //$ali_mini = StoreAliMini::find()->where(['license_no'=>$this->license_no])->one();
- //if(!empty($ali_mini)){
- //throw new \Exception("营业执照信息已经存在");
- // }
- $ali_mini = new StoreAliMini();
- }
- $new_mini = StoreAliMini::find()->where(['store_id' => $store_id])->orderBy('id desc')->one();
- $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $new_mini->id + 1);
- $param = [
- 'create_mini_request' => [
- 'out_order_no' => $out_order_no,
- 'alipay_account' => $this->alipay_account,
- 'legal_personal_name' => $this->legal_personal_name,
- 'cert_name' => $this->license_name,
- 'cert_no' => $this->license_no,
- 'app_name' => $this->AppName,
- 'contact_phone' => $this->ServicePhone,
- 'contact_name' => $this->contact_name,
- 'is_individual' => true
- ]
- ];
- $ali_mini->store_id = $store_id;
- $ali_mini->license_name = $this->license_name;
- $ali_mini->order_no = $out_order_no;
- $ali_mini->license_no = $this->license_no;
- $ali_mini->app_name = $this->AppName;
- $ali_mini->service_phone = $this->ServicePhone;
- $ali_mini->license_pic = $this->license_pic;
- $ali_mini->alipay_account = $this->alipay_account;
- $ali_mini->legal_personal_name = $this->legal_personal_name;
- if (!empty($this->merchant_id)) {
- $ali_mini->merchant_id = $this->merchant_id;
- }
- $ali_mini->contact_name = $this->contact_name;
- $res = $ali_mini->save();
- if ($res) {
- $this->biz_content = json_encode($param);
- $this->method = "alipay.open.mini.isv.create";
- $request = new AlipayOpenMiniIsvCreateRequest();
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result,
- 'id' => $ali_mini->id
- ];
- }
- throw new \Exception($result->sub_msg);
- } else {
- throw new \Exception("插入数据失败");
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile()
- ];
- }
- }
- //创建事务
- public function agentCreate($id = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_status,batch_no,alipay_account,contact_name,service_phone,service_email,')->orderBy('id desc')->one();
- $id = $mini->id;
- } else {
- $id = !empty($id) ? $id : $this->mini_id;
- $mini = StoreAliMini::findOne($id);
- }
- \Yii::error(json_encode($mini));
- if (empty($mini)) {
- throw new \Exception("查询不到任何信息");
- }
- $data = [
- 'account' => $mini->alipay_account,
- 'contact_info' => [
- 'contact_name' => $mini->contact_name,
- 'contact_mobile' => $mini->service_phone,
- 'contact_email' => $mini->service_email
- ]
- ];
- $this->method = "alipay.open.agent.create";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenAgentCreateRequest();
- $batch_no = $mini->batch_no;
- $res = [
- 'code' => 0,
- 'msg' => "执行成功"
- ];
- if ($mini->batch_status == 0 || $mini->batch_status == 1) {
- $result = $this->miniCommon($request);
- if (!empty($result->code) && $result->code == 10000) {
- $mini->batch_no = $result->batch_no;
- $mini->batch_status = 1;
- $res = $mini->save();
- if (!$res) {
- throw new \Exception("保存信息失败");
- }
- $batch_no = $result->batch_no;
- }
- }
- if ($mini->batch_status === 1 || $mini->batch_status === 4) {
- $res = $this->faceToFace($id, $batch_no);
- \Yii::error("当面付");
- \Yii::error($res);
- if ($res['code'] !== 0) {
- throw new \Exception($res['msg']);
- }
- }
- if ($mini->batch_status === 2) {
- $res = $this->agentConfirm($id, $batch_no);
- \Yii::error("事务提交");
- \Yii::error($res);
- if ($res['code'] !== 0) {
- throw new \Exception($res['msg']);
- }
- }
- if ($mini->batch_status === 3) {
- $res = $this->getBatchStatus($id, $batch_no);
- \Yii::error("查询状态");
- \Yii::error($res);
- if ($res['code'] !== 0) {
- throw new \Exception($res['msg']);
- }
- }
- $t->commit();
- return $res;
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //当面付签约
- public function faceToFace($id = 0, $batch_no = '')
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,mini_categoryIds,app_logo')->orderBy('id desc')->one();
- } else {
- $mini_id = !empty($id) ? $id : $this->mini_id;
- $mini = StoreAliMini::findOne($mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("查询不到任何信息");
- }
- $this->method = "alipay.open.agent.facetoface.sign";
- $request = new AlipayOpenAgentFacetofaceSignRequest();
- $request->setBatchNo($batch_no);
- $request->setMccCode("A0002_B0201");
- $request->setRate('0.38');
- $request->setSignAndAuth(false);
- $request->setShopSignBoardPic('@' . $this->saveTempImage($mini->app_logo));
- \Yii::error($id);
- $result = $this->miniCommon($request, 1, $mini_id, 1);
- \Yii::error($result);
- if ((!empty($result->code) && $result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") {
- $mini->batch_status = 2;
- if ($result->sub_code == "ANT_PRODUCT_CONFLICT") {
- $mini->batch_status = 6;
- }
- $res = $mini->save();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception("添加数据失败");
- }
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- //提交事务
- public function agentConfirm($id = 0, $batch_no = '')
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no')->orderBy('id desc')->one();
- } else {
- $mini_id = !empty($id) ? $id : $this->mini_id;
- $mini = StoreAliMini::findOne($mini_id);
- }
- if (empty($mini)) {
- throw new \Exception("查询不到任何信息");
- }
- $data = [
- 'batch_no' => $batch_no
- ];
- $this->method = "alipay.open.agent.confirm";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenAgentConfirmRequest();
- $result = $this->miniCommon($request, 0, $id, 1);
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- $mini->batch_status = 3;
- $res = $mini->save();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception("添加数据失败");
- }
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //查询状态
- public function getBatchStatus($id = 0, $batch = '')
- {
- try {
- \Yii::error("查询状态");
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- \Yii::error($store_id);
- $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,alipay_account')->orderBy('id desc')->one();
- } else {
- $mini_id = !empty($id) ? $id : $this->mini_id;
- \Yii::error($mini_id);
- $mini = StoreAliMini::findOne($mini_id);
- }
- \Yii::error($mini);
- if (empty($mini)) {
- throw new \Exception("查询不到任何信息");
- }
- $data = [
- 'batch_no' => $batch
- ];
- $this->method = "alipay.open.agent.order.query";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenAgentOrderQueryRequest();
- $result = $this->miniCommon($request, 0, $id, 1);
- \Yii::error("查询状态查询状态查询状态查询状态");
- \Yii::error(json_encode($result));
- if (!empty($result->code) && $result->code == 10000) {
- $status = $result->order_status;
- switch ($status) {
- case "MERCHANT_INFO_HOLD": //异常单 4
- $mini->batch_status = 4;
- break;
- case "MERCHANT_AUDITING": //审核中 3
- $mini->batch_status = 3;
- break;
- case "MERCHANT_CONFIRM": //待确认 5
- $mini->batch_status = 5;
- break;
- case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6
- $mini->batch_status = 6;
- break;
- case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7
- $mini->batch_status = 7;
- break;
- case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8
- $mini->batch_status = 8;
- break;
- }
- $res = $mini->save();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception("添加数据失败");
- }
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //关联普通二维码
- public function bindQrcode($id)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $qrcode = AggregateQrcode::find()->where(['ali_mini_id' => $id])->one();
- if (empty($qrcode->param_url) || empty($qrcode->ali_url)) {
- throw new \Exception("参数错误");
- }
- $data = [
- "route_url" => $qrcode->param_url,
- 'mode' => "EXACT",
- "page_redirection" => $qrcode->ali_url
- ];
- \Yii::error($data);
- $this->method = "alipay.open.mini.qrcode.bind";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenMiniQrcodeBindRequest();
- $result = $this->miniCommon($request, 0, $id);
- if (!empty($result->code) && $result->code == 10000) {
- $qrcode->ali_status = 1;
- $res = $qrcode->save();
- if (!$res) {
- throw new \Exception('保存数据失败,绑定二维码成功');
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //支付
- public function payconfig($id, $out_trade_no, $user_id, $pay_price)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = [
- "out_trade_no" => $out_trade_no,
- 'total_amount' => $pay_price,
- "subject" => "当面付",
- "buyer_id" => $user_id
- ];
- $this->method = "alipay.trade.create";
- $this->biz_content = json_encode($data);
- $request = new AlipayTradeCreateRequest();
- $request->setNotifyUrl(pay_notify_url(self::$notify_url));
- $result = $this->miniCommon($request, 0, $id, 0, 1);
- if (!empty($result->code) && $result->code == 10000) {
- $t->commit();
- $data = [
- 'trade_no' => $result->trade_no,
- 'order_no' => $out_trade_no
- ];
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $data
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取敏感字段
- public function getField($id = 0)
- {
- try {
- $mini_id = !empty($id) ? $id : $this->mini_id;
- \Yii::error($mini_id);
- $mini = StoreAliMini::findOne($mini_id);
- $this->method = "alipay.open.app.api.query";
- $request = new AlipayOpenAppApiQueryRequest();
- $result = $this->miniCommon($request, 1, $mini->id);
- \Yii::error("获取敏感字段");
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- if (!empty($result->apis)) {
- $scene = $this->getScene($mini->id);
- if ($scene['code'] === 0) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $scene['data']
- ];
- }
- throw new \Exception($scene['msg']);
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => []
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取敏感字段使用场景
- public function getScene($id)
- {
- try {
- if (empty($id)) {
- throw new \Exception("未知的小程序信息");
- }
- $data = [
- 'field_name' => 'mobile',
- 'api_name' => 'getPhoneNumber'
- ];
- $this->method = "alipay.open.app.api.scene.query";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenAppApiSceneQueryRequest();
- $result = $this->miniCommon($request, 0, $id);
- \Yii::error("获取敏感字段场景值");
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result->auth_field_scene
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //申请接入敏感字段
- public function applyPhone()
- {
- try {
- $apply_msg = $this->apply_msg;
- $mini_id = $apply_msg['id'];
- if (empty($mini_id)) {
- throw new \Exception("获取小程序信息失败");
- }
- \Yii::error($mini_id);
- $mini = StoreAliMini::findOne($mini_id);
- $this->method = "alipay.open.app.api.field.apply";
- $request = new AlipayOpenAppApiFieldApplyRequest();
- !empty($apply_msg['video']) && $request->setVideo('@' . $this->saveTempImage($apply_msg['video']));
- foreach ($apply_msg['picture'] as $index => $item) {
- \Yii::error($item);
- \Yii::error($this->saveTempImage($item));
- switch ($index) {
- case 0:
- $request->setPicture1('@' . $this->saveTempImage($item));
- break;
- case 1:
- $request->setPicture2('@' . $this->saveTempImage($item));
- break;
- case 2:
- $request->setPicture3('@' . $this->saveTempImage($item));
- break;
- case 3:
- $request->setPicture4('@' . $this->saveTempImage($item));
- break;
- case 4:
- $request->setPicture5('@' . $this->saveTempImage($item));
- break;
- }
- }
- $data = [
- 'api_name' => "getPhoneNumber",
- 'field_name' => "mobile",
- "package_code" => "20180927110154092444",
- 'scene_code' => $apply_msg['scene_code'],
- 'qps_answer' => $apply_msg['qps_answer'],
- 'customer_answer' => $apply_msg['customer_answer'],
- 'memo' => $apply_msg['memo']
- ];
- $request->setAuthFieldApply(json_encode($data));
- $result = $this->miniCommon($request, 1, $apply_msg['id']);
- \Yii::error($result);
- $mini->apply_phone_msg = json_encode($apply_msg);
- $mini->save();
- if (!empty($result->code) && $result->code == 10000) {
- $mini->is_get_phone = 1;
- $mini->save();
- return [
- 'code' => 0,
- 'msg' => "开始审核",
- 'data' => $result
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //手动查询手机号申请状态
- public function getPhoneStatus($id)
- {
- try {
- $mini_id = $this->mini_id;
- $mini = StoreAliMini::find()->where(['id' => $mini_id])->select("id,is_get_phone,apply_phone_fail,auth_app_id")->one();
- if (empty($mini)) {
- throw new \Exception("获取小程序信息失败");
- }
- $this->method = "alipay.open.app.api.field.query";
- $request = new AlipayOpenAppApiFieldQueryRequest();
- $result = $this->miniCommon($request, 1, $mini_id);
- \Yii::error("获取敏感字段申请状态");
- \Yii::error($result);
- if (!empty($result->code) && $result->code == 10000) {
- $data = $result->auth_field_response->records;
- $data_arr = json_decode(json_encode($data), true);
- \Yii::error($data_arr);
- $status = $data_arr[0]['status'];
- if ($status === "AGREE") {
- $mini->is_get_phone = 2;
- } elseif ($status === "REJECT" || $status === "INVALID") {
- $reason = !empty($data->reason) ? $data->reason : '';
- $mini->is_get_phone = 3;
- $mini->apply_phone_fail = $reason;
- }
- if ($mini->save()) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $data,
- 'status' => $status
- ];
- }
- throw new \Exception("保存数据错误" . json_encode($mini->errors));
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取网络图片到临时目录
- 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 getIsvCreateMini()
- {
- $mini = StoreAliMini::find()->where(['id' => $this->mini_id])->select("id,license_name,,license_no,app_name,license_pic,service_phone,alipay_account,legal_personal_name,contact_name")->asArray()->one();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $mini
- ];
- }
- // 获取小程序审核状态
- public function getMiniShenheStatus($id){
-
- try {
- $store_ali_mini = StoreAliMini::findOne($id);
- if (!$store_ali_mini || !$store_ali_mini->order_no) {
- return false;
- }
- $data = [
- "order_no" => $store_ali_mini->order_no,
- ];
- $this->method = "alipay.open.mini.isv.query";
- $this->biz_content = json_encode($data);
- $request = new AlipayOpenMiniIsvQueryRequest();
- $result = $this->miniCommon($request,0,$id);
- if(!empty($result->code)&&$result->code == 10000){
- return $result;
- }else{
- throw new \Exception($result->sub_msg);
- }
- }catch (\Exception $e){
- return [
- 'code'=> 2,
- 'msg'=>$e->getMessage()
- ];
- }
- }
- }
|