AlipayThirdForm.php 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentConfirmRequest;
  9. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentCreateRequest;
  10. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentFacetofaceSignRequest;
  11. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentOrderQueryRequest;
  12. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldApplyRequest;
  13. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiFieldQueryRequest;
  14. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiQueryRequest;
  15. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppApiSceneQueryRequest;
  16. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppMembersCreateRequest;
  17. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppMembersQueryRequest;
  18. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppQrcodeCreateRequest;
  19. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesGetRequest;
  20. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthTokenAppQueryRequest;
  21. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthTokenAppRequest;
  22. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniBaseinfoModifyRequest;
  23. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniBaseinfoQueryRequest;
  24. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniCategoryQueryRequest;
  25. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceCancelRequest;
  26. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceCreateRequest;
  27. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniExperienceQueryRequest;
  28. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIndividualBusinessCertifyRequest;
  29. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvCreateRequest;
  30. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
  31. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditApplyRequest;
  32. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditCancelRequest;
  33. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionAuditedCancelRequest;
  34. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionBuildQueryRequest;
  35. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionDeleteRequest;
  36. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionDetailQueryRequest;
  37. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionListQueryRequest;
  38. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionOnlineRequest;
  39. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniVersionUploadRequest;
  40. use app\utils\Alipay\alipaySdk\aop\request\AlipayTradeCreateRequest;
  41. use AopClient;
  42. use app\models\AggregateQrcode;
  43. use app\models\Option;
  44. use app\models\StoreAliMini;
  45. use app\models\StoreAliMiniVersion;
  46. use app\utils\CurlHelper;
  47. use yii\base\Model;
  48. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesSetRequest;
  49. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvQueryRequest;
  50. use yii\helpers\Json;
  51. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  52. class AlipayThirdForm extends Model
  53. {
  54. /**
  55. * @var mixed
  56. */
  57. public $alipay_aes_key;
  58. public $appid;
  59. public $method;
  60. public $sign_type;
  61. public $sign;
  62. public $version = "1.0";
  63. public $charset;
  64. public $biz_content;
  65. public $timestamp;
  66. public $auth_code;
  67. public $baseUrl = "https://openapi.alipay.com/gateway.do";
  68. public $private_key;
  69. public $public_key;
  70. public $license_no; //营业执照编号
  71. public $license_pic; //营业执照照片
  72. public $template_version; //模版版本号
  73. public $ext; //ext
  74. public $template_id; //模版ID
  75. public $app_version; //版本号
  76. public $bundle_id; //小程序投放的端参数
  77. public $AppName;
  78. public $AppEnglishName;
  79. public $AppSlogan;
  80. public $AppLogo;
  81. public $AppDesc;
  82. public $ServicePhone;
  83. public $ServiceEmail;
  84. public $MiniCategoryIds;
  85. public $page_num;
  86. public $page_size;
  87. public $auditData; //审核资料
  88. public $is_filter;
  89. public $gray_strategy;
  90. public $route_url;
  91. public $mode;
  92. public $page_redirection;
  93. public $route_pattern;
  94. public $mini_id;
  95. public $version_id;
  96. public $role;
  97. public $logon_id;
  98. public $license_name;
  99. public $license_date;
  100. public $is_long_effective;
  101. public $alipay_account;
  102. public $legal_personal_name;
  103. public $contact_name;
  104. public $merchant_id;
  105. public $bind_store_id;
  106. public $apply_msg;
  107. public $fun = 'setPicture';
  108. /*支付回调url*/
  109. private static $notify_url = 'alipay/notify';
  110. public function rules()
  111. {
  112. return [
  113. [["page_num", "page_size", 'is_filter', 'gray_strategy', 'version_id', 'role', 'is_long_effective', 'merchant_id', "bind_store_id"], 'integer'],
  114. [
  115. [
  116. "appid",
  117. "method",
  118. "sign_type",
  119. "sign",
  120. "version",
  121. "charset",
  122. "biz_content",
  123. "timestamp",
  124. "auth_code",
  125. "baseUrl",
  126. "license_no",
  127. "license_pic",
  128. "template_version",
  129. "ext",
  130. "template_id",
  131. "app_version",
  132. "bundle_id",
  133. "AppName",
  134. "AppEnglishName",
  135. "AppSlogan",
  136. "AppLogo",
  137. "AppDesc",
  138. "ServicePhone",
  139. "ServiceEmail",
  140. "MiniCategoryIds",
  141. "auditData",
  142. "route_pattern",
  143. "page_redirection",
  144. "mode",
  145. "route_url",
  146. "page_num",
  147. "page_size",
  148. "gray_strategy",
  149. "url_param",
  150. "query_param",
  151. "describe",
  152. "color",
  153. "size",
  154. "mini_id",
  155. "logon_id",
  156. "license_name",
  157. "license_date",
  158. "apply_msg"
  159. ], 'string'
  160. ],
  161. ];
  162. }
  163. public function __construct($config = [])
  164. {
  165. parent::__construct($config);
  166. $this->appid = Option::get("alipay_appid", 0, 'saas')['value'];
  167. $this->timestamp = date("Y-m-d H:i:s");
  168. $this->sign_type = "RSA2";
  169. $this->charset = "utf-8";
  170. $this->private_key = Option::get("alipay_app_private_key", 0, 'saas')['value'];
  171. $this->public_key = Option::get("alipay_public_key", 0, 'saas')['value'];
  172. $this->alipay_aes_key = Option::get("alipay_aes_key", 0, 'saas')['value'];
  173. }
  174. /**
  175. * 公共参数请求
  176. * @return \$1|false|mixed|\SimpleXMLElement
  177. * @throws \Exception
  178. *
  179. *
  180. */
  181. public function miniCommon($request, $is_hidden_biz_content = 0, $id = 0, $is_hidden_token = 0, $is_new = 0)
  182. {
  183. try {
  184. $auth_token = "";
  185. $mini_id = $this->mini_id;
  186. if (!empty($this->bind_store_id)) {
  187. $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  188. $mini_id = $StoreAliMini->id;
  189. }
  190. if (!empty($id != false ? $id : $mini_id)) {
  191. $mini = StoreAliMini::findOne($id != false ? $id : $mini_id);
  192. $auth_token = $mini->auth_token;
  193. }
  194. if (empty($auth_token) && $is_new == 1) {
  195. $auth_token = Option::get("ali_template_token", 0, 'saas')['value'];
  196. }
  197. //换取token
  198. $aop = new AopClient();
  199. $aop->gatewayUrl = $this->baseUrl;
  200. $aop->appId = $this->appid;
  201. $aop->rsaPrivateKey = $this->private_key;
  202. $aop->alipayrsaPublicKey = $this->public_key;
  203. $aop->encryptKey = $this->alipay_aes_key;
  204. $aop->encryptType = "AES";
  205. $aop->apiVersion = '1.0';
  206. $aop->signType = $this->sign_type;
  207. $aop->postCharset = $this->charset;
  208. if ($is_hidden_biz_content == 0) {
  209. $request->setBizContent($this->biz_content);
  210. }
  211. if (!empty($auth_token) && empty($is_hidden_token)) {
  212. $result = $aop->execute($request, null, $auth_token);
  213. } else {
  214. $result = $aop->execute($request);
  215. }
  216. \Yii::error($result);
  217. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  218. return $result->$responseNode;
  219. } catch (\Exception $e) {
  220. return (object)[
  221. 'sub_msg' => $e->getMessage()
  222. ];
  223. }
  224. }
  225. /**
  226. * 传递授权回调参数
  227. * @return array
  228. */
  229. public function getParams()
  230. {
  231. $data = [
  232. 'appid' => $this->appid,
  233. 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/admin/#/setting/alipayApplet')
  234. ];
  235. return [
  236. 'code' => 0,
  237. 'url' => "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=TINYAPP&redirect_uri=" . $data['redirect_uri'],
  238. 'msg' => "获取成功"
  239. ];
  240. }
  241. /**
  242. * 保存授权码
  243. * 获取保存AuthToken以及小程序信息
  244. * 同时获取aes密钥
  245. * 通过授权码AuthCode换取AuthToken
  246. *
  247. */
  248. public function setAuthCode()
  249. {
  250. try {
  251. $param = [
  252. "grant_type" => "authorization_code",
  253. "code" => $this->auth_code
  254. ];
  255. $this->biz_content = json_encode($param);
  256. $this->method = "alipay.open.auth.token.app";
  257. $request = new AlipayOpenAuthTokenAppRequest();
  258. $result = $this->miniCommon($request, 0, 0, 0, 0);
  259. \Yii::error($result);
  260. $token = $result->tokens[0];
  261. if (!empty($result->code) && $result->code == 10000) {
  262. $mini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'auth_app_id' => $token->auth_app_id])->one();
  263. if (empty($mini)) {
  264. $mini = new StoreAliMini();
  265. $mini->store_id = get_store_id();
  266. $mini->auth_app_id = $token->auth_app_id;
  267. }
  268. $mini->auth_code = $this->auth_code;
  269. $mini->userid = $token->user_id;
  270. $mini->auth_token = $token->app_auth_token;
  271. $mini->refresh_token = json_encode([
  272. 'app_refresh_token' => $token->app_refresh_token,
  273. 'end_time' => time() * 1 + $token->re_expires_in
  274. ]);
  275. $res = $mini->save();
  276. if ($res) {
  277. //获取应用aes密钥
  278. $res = $this->aesAuth(0, $mini->id);
  279. if ($res['code'] === 0) {
  280. $res = $this->getAppTokenInfo($mini->id);
  281. if ($res['code'] === 0) {
  282. $res = $this->updateMiniVersion($mini->id);
  283. if ($res['code'] === 0) {
  284. $res = $this->getMiniBaseInfo($mini->id);
  285. if ($res['code'] !== 0) {
  286. throw new \Exception($res['msg']);
  287. }
  288. } else {
  289. throw new \Exception($res['msg']);
  290. }
  291. } else {
  292. throw new \Exception($res['msg']);
  293. }
  294. } else {
  295. throw new \Exception($res['msg']);
  296. }
  297. } else {
  298. throw new \Exception($mini->errors);
  299. }
  300. } else {
  301. throw new \Exception($result->sub_msg);
  302. }
  303. return [
  304. 'code' => 0,
  305. 'msg' => "success"
  306. ];
  307. } catch (\Exception $e) {
  308. return [
  309. 'code' => 1,
  310. 'msg' => $e->getMessage() . $e->getLine()
  311. ];
  312. }
  313. }
  314. /**
  315. * 授权应用aes密钥设置/查询
  316. */
  317. public function aesAuth($is_get = 0, $id = 0)
  318. {
  319. $t = \Yii::$app->db->beginTransaction();
  320. try {
  321. $mini = StoreAliMini::findOne($id);
  322. $auth_app_id = Option::get("ali_template_appid", 0, 'saas')['value'];;
  323. if (!empty($mini)) {
  324. $auth_app_id = $mini->auth_app_id;
  325. }
  326. if (empty($auth_app_id)) {
  327. throw new \Exception("请先配置小程序信息");
  328. }
  329. \Yii::error($auth_app_id);
  330. $param = [
  331. "merchant_app_id" => $auth_app_id
  332. ];
  333. \Yii::error($param);
  334. $this->biz_content = json_encode($param);
  335. $this->method = "alipay.open.auth.app.aes.set";
  336. $request = new AlipayOpenAuthAppAesSetRequest();
  337. if ($is_get == 1) {
  338. $this->method = "alipay.open.auth.app.aes.get";
  339. $request = new AlipayOpenAuthAppAesGetRequest();
  340. }
  341. if (empty($id)) {
  342. $is_new = 1;
  343. } else {
  344. $is_new = 0;
  345. }
  346. $result = $this->miniCommon($request, 0, $id, 1, $is_new);
  347. \Yii::error($result);
  348. if (!empty($result->code) && $result->code == 10000) {
  349. if (!empty($id) && !empty($mini)) {
  350. $mini->aes_key = $result->aes_key;
  351. } else {
  352. Option::set('ali_template_aes', $result->aes_key, 0, 'saas');
  353. return [
  354. 'code' => 0,
  355. 'msg' => "获取成功",
  356. 'data' => $result->aes_key
  357. ];
  358. }
  359. $res = $mini->save();
  360. if ($res) {
  361. $t->commit();
  362. $data['aes_key'] = $mini->aes_key;
  363. $data['app_id'] = $mini->auth_app_id;
  364. $data['alipay_public_key'] = "";
  365. $data['app_public_key'] = "";
  366. $data['app_private_key'] = "";
  367. $data['user_id'] = $mini->userid;
  368. $data['name'] = $mini->license_name;
  369. $bind_store_id = $mini->store_id;
  370. $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $bind_store_id, 'alipay');
  371. if ($res) {
  372. \Yii::$app->cache->delete('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id());
  373. \Yii::$app->cache->set('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id(), Json::encode($data));
  374. }
  375. return [
  376. 'code' => 0,
  377. 'msg' => "操作成功"
  378. ];
  379. } else {
  380. throw new \Exception($mini->errors);
  381. }
  382. } else {
  383. throw new \Exception($result->sub_msg . $result->code);
  384. }
  385. } catch (\Exception $e) {
  386. $t->rollBack();
  387. return [
  388. 'code' => 1,
  389. 'msg' => $e->getMessage()
  390. ];
  391. }
  392. }
  393. /**
  394. * 个人账户升级为个体工商户
  395. */
  396. public function updateBussiness($id = 0)
  397. {
  398. $t = \Yii::$app->db->beginTransaction();
  399. try {
  400. $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id);
  401. if (empty($mini)) {
  402. throw new \Exception("授权失败");
  403. }
  404. $mini->license_no = $this->license_no;
  405. $mini->license_pic = $this->license_pic;
  406. $index = strpos($this->license_pic, \Yii::$app->request->hostName);
  407. $len = $index + strlen(\Yii::$app->request->hostName);
  408. $license_pic = substr($this->license_pic, $len);
  409. $license_pic = $this->image2Base64(\Yii::$app->basePath . $license_pic);
  410. if ($license_pic == false) {
  411. throw new \Exception("图片转Base64失败");
  412. }
  413. $param = [
  414. "license_no" => $this->license_no,
  415. "license_pic" => $license_pic
  416. ];
  417. $this->biz_content = json_encode($param);
  418. $this->method = "alipay.open.mini.individual.business.certify";
  419. $request = new AlipayOpenMiniIndividualBusinessCertifyRequest();
  420. $request->setLicenseNo($this->license_no);
  421. $request->setLicensePic($license_pic);
  422. $result = $this->miniCommon($request, 1);
  423. if (!empty($result->code) && $result->code == 10000) {
  424. $mini->is_merchant = $result->certify_result ? 1 : 0;
  425. $res = $mini->save();
  426. if ($res) {
  427. $t->commit();
  428. return [
  429. 'code' => 0,
  430. 'msg' => "操作成功"
  431. ];
  432. } else {
  433. throw new \Exception($mini->errors);
  434. }
  435. } else {
  436. throw new \Exception($result->sub_msg);
  437. }
  438. } catch (\Exception $e) {
  439. $t->rollBack();
  440. return [
  441. 'code' => 1,
  442. 'msg' => $e->getMessage()
  443. ];
  444. }
  445. }
  446. /**
  447. * 查询某个应用授权AppAuthToken的授权信息
  448. */
  449. public function getAppTokenInfo($id)
  450. {
  451. $t = \Yii::$app->db->beginTransaction();
  452. try {
  453. $mini = StoreAliMini::findOne($id);
  454. if (empty($mini)) {
  455. throw new \Exception("没有进行授权");
  456. }
  457. $param = [
  458. "app_auth_token" => $mini->auth_token
  459. ];
  460. $this->biz_content = json_encode($param);
  461. $this->method = "alipay.open.auth.token.app.query";
  462. $request = new AlipayOpenAuthTokenAppQueryRequest();
  463. $result = $this->miniCommon($request);
  464. if (!empty($result->code) && $result->code == 10000) {
  465. $t->commit();
  466. return [
  467. 'code' => 0,
  468. 'msg' => "获取成功",
  469. 'data' => $result
  470. ];
  471. } else {
  472. throw new \Exception($result->sub_msg);
  473. }
  474. } catch (\Exception $e) {
  475. $t->rollBack();
  476. return [
  477. 'code' => 1,
  478. 'msg' => $e->getMessage()
  479. ];
  480. }
  481. }
  482. /**
  483. * 小程序基于模板上传版本
  484. */
  485. public function updateMiniVersion($id = 0)
  486. {
  487. $t = \Yii::$app->db->beginTransaction();
  488. try {
  489. $id = !empty($id) ? $id : $this->mini_id;
  490. if (!empty($this->bind_store_id)) {
  491. $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  492. $id = $StoreAliMini->id;
  493. }
  494. $mini = StoreAliMini::findOne($id);
  495. $version = StoreAliMiniVersion::find()->where(['mini_id' => $id])
  496. ->orderBy("create_time desc")
  497. ->asArray()
  498. ->one();
  499. if ($version['is_submit'] == 1) {
  500. $res = $this->miniAuditCancel();
  501. if ($res['code'] !== 0) {
  502. throw new \Exception("有版本代码正在审核");
  503. }
  504. }
  505. if (empty($version)) {
  506. $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", 1);
  507. } else {
  508. \Yii::error("版本号:" . $version['template_version']);
  509. $app_version = substr($version['template_version'], 7);
  510. \Yii::error("版本号:" . $app_version);
  511. $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", $app_version + 1);
  512. }
  513. \Yii::error("版本号:" . $app_version);
  514. if (empty($mini)) {
  515. throw new \Exception("没有进行授权");
  516. }
  517. $ext = json_encode([
  518. 'extEnable' => true,
  519. 'ext' => [
  520. 'shopId' => $this->appid,
  521. 'mini_id' => $id,
  522. 'store_id' => get_store_id(),
  523. 'apiurl' => "https://" . \Yii::$app->request->hostName
  524. ]
  525. ]);
  526. $param = [
  527. 'ext' => $ext,
  528. 'template_id' => Option::get("ali_template_appid", 0, 'saas')['value'],
  529. 'app_version' => $app_version,
  530. ];
  531. $this->biz_content = json_encode($param);
  532. $this->method = "alipay.open.mini.version.upload";
  533. $request = new AlipayOpenMiniVersionUploadRequest();
  534. $result = $this->miniCommon($request, 0, $id);
  535. if (!empty($result->code) && $result->code == 10000) {
  536. $MiniVersion = new StoreAliMiniVersion();
  537. $MiniVersion->mini_id = $id;
  538. $MiniVersion->template_version = $app_version;
  539. $MiniVersion->create_time = time();
  540. $MiniVersion->is_up = 1;
  541. $res = $MiniVersion->save();
  542. if ($res) {
  543. $t->commit();
  544. return [
  545. 'code' => 0,
  546. 'msg' => "构建成功",
  547. 'data' => $result,
  548. "res" => $MiniVersion->errors
  549. ];
  550. } else {
  551. throw new \Exception(json_encode($MiniVersion->errors));
  552. }
  553. } else {
  554. throw new \Exception($result->sub_msg);
  555. }
  556. } catch (\Exception $e) {
  557. $t->rollBack();
  558. return [
  559. 'code' => 1,
  560. 'msg' => $e->getMessage()
  561. ];
  562. }
  563. }
  564. /**
  565. * 小程序查询版本构建状态
  566. * alipay.open.mini.version.build.query
  567. */
  568. public function getMiniStatus()
  569. {
  570. try {
  571. if (!empty($this->bind_store_id)) {
  572. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  573. } else {
  574. $mini_id = $this->mini_id;
  575. $mini = StoreAliMini::findOne($mini_id);
  576. }
  577. if (empty($mini)) {
  578. throw new \Exception("没有进行授权");
  579. }
  580. $version = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  581. $param = [
  582. 'app_version' => $version->template_version
  583. ];
  584. $this->biz_content = json_encode($param);
  585. $this->method = "alipay.open.mini.version.build.query";
  586. $request = new AlipayOpenMiniVersionBuildQueryRequest();
  587. $result = $this->miniCommon($request);
  588. if (!empty($result->code) && $result->code == 10000) {
  589. return [
  590. 'code' => 0,
  591. 'msg' => "获取成功",
  592. 'data' => $result
  593. ];
  594. } else {
  595. throw new \Exception($result->sub_msg);
  596. }
  597. } catch (\Exception $e) {
  598. return [
  599. 'code' => 1,
  600. 'msg' => $e->getMessage()
  601. ];
  602. }
  603. }
  604. /**
  605. * 小程序查询版本列表
  606. * alipay.open.mini.version.build.query
  607. */
  608. public function getMiniVersionList()
  609. {
  610. try {
  611. $mini = StoreAliMini::findOne($this->mini_id);
  612. if (empty($mini)) {
  613. throw new \Exception("没有进行授权");
  614. }
  615. $this->method = "alipay.open.mini.version.list.query";
  616. $request = new AlipayOpenMiniVersionListQueryRequest();
  617. $result = $this->miniCommon($request);
  618. if (!empty($result->code) && $result->code == 10000) {
  619. return [
  620. 'code' => 0,
  621. 'msg' => "获取成功",
  622. 'data' => $result
  623. ];
  624. } else {
  625. throw new \Exception($result->sub_msg);
  626. }
  627. } catch (\Exception $e) {
  628. return [
  629. 'code' => 1,
  630. 'msg' => $e->getMessage()
  631. ];
  632. }
  633. }
  634. /**
  635. * 小程序删除版本
  636. * alipay.open.mini.version.delete
  637. */
  638. public function delMiniVersion()
  639. {
  640. $t = \Yii::$app->db->beginTransaction();
  641. try {
  642. $mini = StoreAliMini::findOne($this->mini_id);
  643. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  644. if (empty($mini)) {
  645. throw new \Exception("没有进行授权");
  646. }
  647. $param = [
  648. 'app_version' => $MiniVersion->template_version,
  649. ];
  650. $this->biz_content = json_encode($param);
  651. $this->method = "alipay.open.mini.version.delete";
  652. $request = new AlipayOpenMiniVersionDeleteRequest();
  653. $result = $this->miniCommon($request);
  654. if (!empty($result->code) && $result->code == 10000) {
  655. $MiniVersion->delete();
  656. $t->commit();
  657. return [
  658. 'code' => 0,
  659. 'msg' => "删除成功",
  660. 'data' => $result
  661. ];
  662. } else {
  663. throw new \Exception($result->sub_msg);
  664. }
  665. } catch (\Exception $e) {
  666. $t->rollBack();
  667. return [
  668. 'code' => 1,
  669. 'msg' => $e->getMessage()
  670. ];
  671. }
  672. }
  673. /**
  674. * 小程序生成体验版
  675. * alipay.open.mini.experience.create
  676. */
  677. public function createMiniExperience()
  678. {
  679. $t = \Yii::$app->db->beginTransaction();
  680. try {
  681. $mini = StoreAliMini::findOne($this->mini_id);
  682. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  683. if (empty($mini)) {
  684. throw new \Exception("没有进行授权");
  685. }
  686. $param = [
  687. 'app_version' => $MiniVersion->template_version,
  688. ];
  689. $this->biz_content = json_encode($param);
  690. $this->method = "alipay.open.mini.experience.create";
  691. $request = new AlipayOpenMiniExperienceCreateRequest();
  692. $result = $this->miniCommon($request);
  693. if (!empty($result->code) && $result->code == 10000) {
  694. StoreAliMiniVersion::updateAll(['is_experience' => 0, 'experience_url' => ''], ['and', ['<>', 'id', $MiniVersion->id], ['mini_id' => $this->mini_id]]);
  695. $res = $this->createMiniExperienceQrcode();
  696. if ($res['code'] === 0) {
  697. $MiniVersion->is_experience = 1;
  698. $MiniVersion->experience_url = $res['url'];
  699. $MiniVersion->save();
  700. $t->commit();
  701. return [
  702. 'code' => 0,
  703. 'msg' => "成功",
  704. 'data' => $res['url']
  705. ];
  706. } else {
  707. throw new \Exception("获取体验码失败");
  708. }
  709. } else {
  710. throw new \Exception($result->sub_msg);
  711. }
  712. } catch (\Exception $e) {
  713. $t->rollBack();
  714. return [
  715. 'code' => 1,
  716. 'msg' => $e->getMessage()
  717. ];
  718. }
  719. }
  720. /**
  721. * 获取体验码
  722. * alipay.open.mini.experience.query
  723. */
  724. public function createMiniExperienceQrcode()
  725. {
  726. try {
  727. $mini = StoreAliMini::findOne($this->mini_id);
  728. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  729. if (empty($mini)) {
  730. throw new \Exception("没有进行授权");
  731. }
  732. $param = [
  733. 'app_version' => $MiniVersion->template_version,
  734. 'bundle_id' => $this->bundle_id ?? "com.alipay.alipaywallet",
  735. ];
  736. $this->biz_content = json_encode($param);
  737. $this->method = "alipay.open.mini.experience.query";
  738. $request = new AlipayOpenMiniExperienceQueryRequest();
  739. $result = $this->miniCommon($request);
  740. if (!empty($result->code) && $result->code == 10000) {
  741. $msg = "获取成功";
  742. if ($result->status == "expVersionPackaging") {
  743. $msg = "体验版打包中";
  744. } elseif ($result->status == "notExpVersion") {
  745. $msg = "非体验版";
  746. }
  747. return [
  748. 'code' => 0,
  749. 'msg' => $msg,
  750. 'url' => $result->exp_qr_code_url
  751. ];
  752. } else {
  753. throw new \Exception($result->sub_msg);
  754. }
  755. } catch (\Exception $e) {
  756. return [
  757. 'code' => 1,
  758. 'msg' => $e->getMessage()
  759. ];
  760. }
  761. }
  762. /**
  763. * 小程序取消体验版
  764. * alipay.open.mini.experience.create()
  765. */
  766. public function createMiniExperienceCancel()
  767. {
  768. $t = \Yii::$app->db->beginTransaction();
  769. try {
  770. $mini = StoreAliMini::findOne($this->mini_id);
  771. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  772. if (empty($mini)) {
  773. throw new \Exception("没有进行授权");
  774. }
  775. $param = [
  776. 'app_version' => $MiniVersion->template_version
  777. ];
  778. $this->biz_content = json_encode($param);
  779. $this->method = "alipay.open.mini.experience.cancel";
  780. $request = new AlipayOpenMiniExperienceCancelRequest();
  781. $result = $this->miniCommon($request);
  782. if (!empty($result->code) && $result->code == 10000) {
  783. $MiniVersion->is_experience = 0;
  784. $MiniVersion->experience_url = "";
  785. $MiniVersion->save();
  786. $t->commit();
  787. return [
  788. 'code' => 0,
  789. 'msg' => "成功",
  790. 'data' => $result
  791. ];
  792. } else {
  793. throw new \Exception($result->sub_msg);
  794. }
  795. } catch (\Exception $e) {
  796. $t->rollBack();
  797. return [
  798. 'code' => 1,
  799. 'msg' => $e->getMessage()
  800. ];
  801. }
  802. }
  803. /**
  804. * 查询小程序基础信息
  805. * alipay.open.mini.baseinfo.query()
  806. */
  807. public function getMiniBaseInfo($id = 0, $type = 0)
  808. {
  809. $t = \Yii::$app->db->beginTransaction();
  810. try {
  811. if (!empty($this->bind_store_id)) {
  812. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  813. $id = $mini->id;
  814. } else {
  815. $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id);
  816. }
  817. if (empty($mini)) {
  818. throw new \Exception("没有进行授权");
  819. }
  820. $this->method = "alipay.open.mini.baseinfo.query";
  821. $request = new AlipayOpenMiniBaseinfoQueryRequest();
  822. $result = $this->miniCommon($request, 1, $id != 0 ? $id : $this->mini_id);
  823. if (!empty($result->code) && $result->code == 10000) {
  824. if ($type === 0) {
  825. $mini->app_name = $result->app_name;
  826. $mini->app_desc = $result->app_desc;
  827. $mini->app_slogan = $result->app_slogan;
  828. $mini->app_english_name = $result->app_english_name;
  829. $mini->service_email = $result->service_email;
  830. $mini->service_phone = $result->service_phone;
  831. $mini->app_logo = $result->app_logo;
  832. }
  833. $mini->category_name = $result->category_names;
  834. $res = $mini->save();
  835. if ($res) {
  836. $t->commit();
  837. return [
  838. 'code' => 0,
  839. 'msg' => "成功",
  840. 'data' => $result,
  841. 'category' => $result->category_names
  842. ];
  843. } else {
  844. throw new \Exception("插入数据错误");
  845. }
  846. } else {
  847. throw new \Exception($result->sub_msg);
  848. }
  849. } catch (\Exception $e) {
  850. $t->rollBack();
  851. return [
  852. 'code' => 1,
  853. 'msg' => $e->getMessage()
  854. ];
  855. }
  856. }
  857. /**
  858. * 小程序修改基础信息
  859. * alipay.open.mini.baseinfo.modify(小程序修改基础信息)
  860. */
  861. public function setMiniBaseInfo()
  862. {
  863. $t = \Yii::$app->db->beginTransaction();
  864. try {
  865. $mini_id = $this->mini_id;
  866. if (!empty($this->bind_store_id)) {
  867. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  868. $mini_id = $mini->id;
  869. } else {
  870. $mini = StoreAliMini::findOne($mini_id);
  871. }
  872. if (empty($mini)) {
  873. throw new \Exception("没有进行授权");
  874. }
  875. $mini_category_ids = '';
  876. if (!empty($this->bind_store_id) && !empty($this->MiniCategoryIds)) {
  877. foreach ($this->MiniCategoryIds as $item) {
  878. $arr = [];
  879. foreach ($item as $child) {
  880. array_push($arr, $child['value']);
  881. }
  882. $str = implode('_', $arr);
  883. $mini_category_ids .= $str . ';';
  884. }
  885. } else {
  886. $mini_category_ids = implode(';', $this->MiniCategoryIds);
  887. }
  888. $this->method = "alipay.open.mini.baseinfo.modify";
  889. $request = new AlipayOpenMiniBaseinfoModifyRequest();
  890. $mini->app_name = $this->AppName;
  891. $mini->app_english_name = $this->AppEnglishName;
  892. $mini->app_slogan = $this->AppSlogan;
  893. if (empty($this->bind_store_id)) {
  894. !empty($this->license_no) && $mini->license_no = $this->license_no;
  895. !empty($this->license_pic) && $mini->license_pic = $this->license_pic;
  896. !empty($this->license_name) && $mini->license_name = $this->license_name;
  897. !empty($this->is_long_effective) && $mini->is_long_effective = $this->is_long_effective;
  898. !empty($this->license_date) && $mini->license_date = $this->license_date;
  899. $mini->is_long_effective = !empty($mini->is_long_effective) ? 1 : 0;
  900. }
  901. $mini->app_logo = $this->AppLogo;
  902. $mini->app_desc = $this->AppDesc;
  903. $mini->service_phone = $this->ServicePhone;
  904. $mini->service_email = $this->ServiceEmail;
  905. $mini->mini_categoryIds = $mini_category_ids;
  906. $mini->category_data = json_encode($this->MiniCategoryIds);
  907. $res = $mini->save();
  908. if ($res) {
  909. if (empty($this->bind_store_id)) {
  910. $res = $this->updateBussiness($mini_id);
  911. if ($res['code'] !== 0) {
  912. throw new \Exception($res['msg']);
  913. }
  914. }
  915. \Yii::error($this->AppName);
  916. \Yii::error($this->AppEnglishName);
  917. \Yii::error($this->AppSlogan);
  918. \Yii::error($this->AppLogo);
  919. \Yii::error($this->AppDesc);
  920. \Yii::error($this->ServicePhone);
  921. \Yii::error($this->ServiceEmail);
  922. \Yii::error($this->MiniCategoryIds);
  923. $request->setAppName($this->AppName); //小程序应用名称。
  924. $request->setAppEnglishName($this->AppEnglishName); //小程序应用英文名称。
  925. $request->setAppSlogan($this->AppSlogan); //小程序应用简介
  926. $request->setAppLogo("@" . $this->saveTempImage($this->AppLogo)); //小程序应用logo图标,图片格式必须为:png、jpeg、jpg,建议上传像素为180*180。
  927. $request->setAppDesc($this->AppDesc); //小程序应用描述,20-200个字
  928. $request->setServicePhone($this->ServicePhone); //小程序客服电话。
  929. $request->setServiceEmail($this->ServiceEmail); //小程序客服邮箱。
  930. $request->setMiniCategoryIds($mini_category_ids); //新小程序前台类目。格式为 第一个一级类目_第一个二级类目;第二个一级类目_第二个二级类目_第二个三级类目。详细类目可以通过
  931. $result = $this->miniCommon($request, 1);
  932. \Yii::error($result);
  933. if (!empty($result->code) && $result->code == 10000) {
  934. $t->commit();
  935. return [
  936. 'code' => 0,
  937. 'msg' => "成功",
  938. 'data' => $result
  939. ];
  940. } else {
  941. throw new \Exception($result->sub_msg);
  942. }
  943. }
  944. throw new \Exception("信息保存失败");
  945. } catch (\Exception $e) {
  946. $t->rollBack();
  947. return [
  948. 'code' => 1,
  949. 'msg' => $e->getMessage()
  950. ];
  951. }
  952. }
  953. /**
  954. * 查询使用模板的小程序列表
  955. * alipay.open.mini.template.usage.query(查询使用模板的小程序列表)
  956. */
  957. // public function getTemplateUsage(){
  958. // try {
  959. // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
  960. // if(empty($mini)){
  961. // throw new \Exception("没有进行授权");
  962. // }
  963. // $param = [
  964. // 'template_id'=>$this->template_id,
  965. // 'page_num'=>$this->page_num,
  966. // 'page_size'=>$this->page_size,
  967. // 'template_version'=>$this->template_version,
  968. // 'bundle_id'=>$this->bundle_id,
  969. // ];
  970. // $this->biz_content = json_encode($param);
  971. // $this->method = "alipay.open.mini.template.usage.query";
  972. // $request = new AlipayOpenMiniTemplateUsageQueryRequest();
  973. // $result = $this->miniCommon($request);
  974. // if(!empty($result->code)&&$result->code == 10000){
  975. // return [
  976. // 'code'=>0,
  977. // 'msg'=>"成功",
  978. // 'data'=>$result
  979. // ];
  980. // }else{
  981. // throw new \Exception($result->sub_msg);
  982. // }
  983. // }catch (\Exception $e){
  984. // return [
  985. // 'code'=>1,
  986. // 'msg'=>$e->getMessage()
  987. // ];
  988. // }
  989. // }
  990. /**
  991. * 添加成员
  992. * alipay.open.app.members.create(添加成员)
  993. */
  994. public function miniAddMember()
  995. {
  996. $t = \Yii::$app->db->beginTransaction();
  997. try {
  998. $mini = StoreAliMini::findOne($this->mini_id);
  999. if (empty($mini)) {
  1000. throw new \Exception("没有进行授权");
  1001. }
  1002. $role = $this->role;
  1003. switch ($role) {
  1004. case 0:
  1005. throw new \Exception("请选择身份");
  1006. break;
  1007. case 1:
  1008. $role = "DEVELOPER";
  1009. break;
  1010. case 2:
  1011. $role = "EXPERIENCER";
  1012. break;
  1013. }
  1014. $param = [
  1015. 'logon_id' => $this->logon_id,
  1016. 'role' => $role
  1017. ];
  1018. $this->biz_content = json_encode($param);
  1019. $this->method = "alipay.open.app.members.create";
  1020. $request = new AlipayOpenAppMembersCreateRequest();
  1021. $result = $this->miniCommon($request);
  1022. if (!empty($result->code) && $result->code == 10000) {
  1023. $t->commit();
  1024. return [
  1025. 'code' => 0,
  1026. 'msg' => "成功",
  1027. 'data' => $result
  1028. ];
  1029. } else {
  1030. throw new \Exception($result->sub_msg);
  1031. }
  1032. } catch (\Exception $e) {
  1033. $t->rollBack();
  1034. return [
  1035. 'code' => 1,
  1036. 'msg' => $e->getMessage()
  1037. ];
  1038. }
  1039. }
  1040. /**
  1041. * 获取成员列表
  1042. */
  1043. public function getRoleList()
  1044. {
  1045. try {
  1046. $mini = StoreAliMini::findOne($this->mini_id);
  1047. if (empty($mini)) {
  1048. throw new \Exception("没有进行授权");
  1049. }
  1050. $role = $this->role;
  1051. switch ($role) {
  1052. case 0:
  1053. throw new \Exception("请选择身份");
  1054. break;
  1055. case 1:
  1056. $role = "DEVELOPER";
  1057. break;
  1058. case 2:
  1059. $role = "EXPERIENCER";
  1060. break;
  1061. }
  1062. $param = [
  1063. 'role' => $role
  1064. ];
  1065. $this->biz_content = json_encode($param);
  1066. $this->method = "alipay.open.app.members.query";
  1067. $request = new AlipayOpenAppMembersQueryRequest();
  1068. $result = $this->miniCommon($request);
  1069. if (!empty($result->code) && $result->code == 10000) {
  1070. return [
  1071. 'code' => 0,
  1072. 'msg' => "成功",
  1073. 'data' => $result
  1074. ];
  1075. } else {
  1076. throw new \Exception($result->sub_msg);
  1077. }
  1078. } catch (\Exception $e) {
  1079. return [
  1080. 'code' => 1,
  1081. 'msg' => $e->getMessage()
  1082. ];
  1083. }
  1084. }
  1085. /**
  1086. * 获取审核资料
  1087. *
  1088. */
  1089. public function getMiniApplyInfo()
  1090. {
  1091. $mini = StoreAliMini::find()->alias('sam')
  1092. ->where(['sam.id' => $this->mini_id, 'samv.id' => $this->version_id])
  1093. ->select("
  1094. sam.id,
  1095. sam.license_name,
  1096. sam.license_date,
  1097. sam.is_long_effective,
  1098. sam.license_no,
  1099. sam.license_pic,
  1100. sam.mini_categoryIds,
  1101. sam.app_name,
  1102. sam.app_english_name,
  1103. sam.app_slogan,
  1104. sam.app_logo,
  1105. sam.app_desc,
  1106. sam.service_phone,
  1107. sam.service_email,
  1108. samv.mini_id,
  1109. samv.id as version_id,
  1110. samv.desc,
  1111. samv.template_version as version
  1112. ")
  1113. ->leftJoin(['samv' => StoreAliMiniVersion::tableName()], 'sam.id = samv.mini_id')
  1114. ->asArray()
  1115. ->one();
  1116. return [
  1117. 'code' => 0,
  1118. 'msg' => "获取成功",
  1119. 'data' => $mini
  1120. ];
  1121. }
  1122. /**
  1123. * 小程序提交审核
  1124. * alipay.open.mini.version.audit.apply(小程序提交审核)
  1125. */
  1126. public function miniAuditApply()
  1127. {
  1128. $t = \Yii::$app->db->beginTransaction();
  1129. try {
  1130. if (!empty($this->bind_store_id)) {
  1131. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  1132. } else {
  1133. $mini = StoreAliMini::findOne($this->auditData['id']);
  1134. }
  1135. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini->id])->orderBy("create_time desc")->one();
  1136. if (empty($mini)) {
  1137. throw new \Exception("没有进行授权");
  1138. }
  1139. $this->method = "alipay.open.mini.version.audit.apply";
  1140. $request = new AlipayOpenMiniVersionAuditApplyRequest();
  1141. $auditData = $this->auditData;
  1142. if (!empty($this->bind_store_id)) {
  1143. $mini_category_ids = $auditData['mini_category_ids'];
  1144. } else {
  1145. $mini_category_ids = implode(';', $auditData['mini_category_ids']);
  1146. }
  1147. $auditData['version_desc'] = '商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线';
  1148. !empty($auditData['service_phone']) && $request->setServicePhone($auditData['service_phone']); //小程序手机号
  1149. !empty($auditData['service_email']) && $request->setServiceEmail($auditData['service_email']); //小程序邮箱地址
  1150. $request->setVersionDesc($auditData['version_desc']); //小程序版本描述,30-500个字符。
  1151. $request->setRegionType("CHINA"); //小程序服务区
  1152. !empty($auditData['license_no']) && $request->setLicenseNo($auditData['license_no']); //营业执照号
  1153. !empty($auditData['mini_category_ids']) && $request->setMiniCategoryIds($mini_category_ids);
  1154. !empty($auditData['license_name']) && $request->setLicenseName($auditData['license_name']); //营业执照名称
  1155. !empty($auditData['license_pic']) && $request->setFirstLicensePic("@" . $this->saveTempImage($auditData['license_pic'])); //营业执照
  1156. !empty($auditData['license_date']) && $request->setLicenseValidDate(!empty($auditData['is_long_effective']) ? '9999-12-31' : $auditData['license_date']); //营业执照有效期
  1157. $request->setAppVersion($MiniVersion->template_version); //小程序版本号
  1158. !empty($auditData['app_name']) && $request->setAppName($auditData['app_name']); //小程序名称
  1159. !empty($auditData['app_english_name']) && $request->setAppEnglishName($auditData['app_english_name']); //英文名称
  1160. !empty($auditData['app_slogan']) && $request->setAppSlogan($auditData['app_slogan']); //小程序应用简介,一句话描述小程序功能
  1161. !empty($auditData['app_logo']) && $request->setAppLogo("@" . $this->saveTempImage($auditData['app_logo'])); //小程序logo图标
  1162. !empty($auditData['app_desc']) && $request->setAppDesc($auditData['app_desc']); //小程序应用描述
  1163. $mini->license_name = $auditData['license_name'];
  1164. $mini->license_date = $auditData['license_date'];
  1165. $mini->is_long_effective = $auditData['is_long_effective'];
  1166. $mini->license_no = $auditData['license_no'];
  1167. $mini->license_pic = $auditData['license_pic'];
  1168. $mini->mini_categoryIds = $mini_category_ids;
  1169. $mini->app_name = $auditData['app_name'];
  1170. $mini->app_english_name = $auditData['app_english_name'];
  1171. $mini->app_slogan = $auditData['app_slogan'];
  1172. $mini->app_logo = $auditData['app_logo'];
  1173. $mini->app_desc = $auditData['app_desc'];
  1174. $mini->service_phone = $auditData['service_phone'];
  1175. $mini->service_email = $auditData['service_email'];
  1176. $mini->category_name = json_encode($auditData['mini_category_ids']);
  1177. if (empty($this->bind_store_id)) {
  1178. $res = $mini->save();
  1179. } else {
  1180. $res = true;
  1181. }
  1182. if ($res) {
  1183. $result = $this->miniCommon($request, 1, $mini->id);
  1184. if (!empty($result->code) && $result->code != 10000) {
  1185. throw new \Exception($result->sub_msg);
  1186. }
  1187. $MiniVersion->is_submit = 1;
  1188. $res = $MiniVersion->save();
  1189. if ($res) {
  1190. $t->commit();
  1191. return [
  1192. 'code' => 0,
  1193. 'msg' => "开始审核",
  1194. 'data' => $result,
  1195. ];
  1196. } else {
  1197. return $MiniVersion->errors;
  1198. }
  1199. } else {
  1200. return $mini->errors;
  1201. }
  1202. } catch (\Exception $e) {
  1203. $t->rollBack();
  1204. return [
  1205. 'code' => 1,
  1206. 'msg' => $e->getMessage() . $e->getLine() . $e->getFile()
  1207. ];
  1208. }
  1209. }
  1210. /**
  1211. * 小程序撤销审核
  1212. * alipay.open.mini.version.audit.cancel(小程序撤销审核)
  1213. */
  1214. public function miniAuditCancel()
  1215. {
  1216. $t = \Yii::$app->db->beginTransaction();
  1217. try {
  1218. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  1219. $mini = StoreAliMini::findOne($this->mini_id);
  1220. if (empty($mini)) {
  1221. throw new \Exception("没有进行授权");
  1222. }
  1223. $param = [
  1224. 'app_version' => $MiniVersion->template_version
  1225. ];
  1226. $this->biz_content = json_encode($param);
  1227. $this->method = "alipay.open.mini.version.audit.cancel";
  1228. $request = new AlipayOpenMiniVersionAuditCancelRequest();
  1229. $result = $this->miniCommon($request);
  1230. if (!empty($result->code) && $result->code == 10000) {
  1231. $MiniVersion->is_submit = 0;
  1232. $MiniVersion->save();
  1233. $t->commit();
  1234. return [
  1235. 'code' => 0,
  1236. 'msg' => "成功",
  1237. 'data' => $result
  1238. ];
  1239. } else {
  1240. throw new \Exception($result->sub_msg);
  1241. }
  1242. } catch (\Exception $e) {
  1243. $t->rollBack();
  1244. return [
  1245. 'code' => 1,
  1246. 'msg' => $e->getMessage()
  1247. ];
  1248. }
  1249. }
  1250. /**
  1251. * 小程序类目树查询
  1252. * alipay.open.mini.category.query(小程序类目树查询)
  1253. */
  1254. public function getCategoryList()
  1255. {
  1256. try {
  1257. if (!empty($this->bind_store_id)) {
  1258. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one();
  1259. } else {
  1260. $mini = StoreAliMini::findOne($this->mini_id);
  1261. }
  1262. if (empty($mini)) {
  1263. throw new \Exception("没有进行授权");
  1264. }
  1265. $is_filter = $this->is_filter == 1;
  1266. $param = [
  1267. "is_filter" => $is_filter
  1268. ];
  1269. $this->biz_content = json_encode($param);
  1270. $this->method = "alipay.open.mini.category.query";
  1271. $request = new AlipayOpenMiniCategoryQueryRequest();
  1272. $result = $this->miniCommon($request);
  1273. if (!empty($result->code) && $result->code == 10000) {
  1274. $arr = json_decode(json_encode($result->mini_category_list), true);
  1275. $data = $this->getdata($arr);
  1276. return [
  1277. 'code' => 0,
  1278. 'msg' => "成功",
  1279. 'data' => $data
  1280. ];
  1281. } else {
  1282. throw new \Exception($result->sub_msg);
  1283. }
  1284. } catch (\Exception $e) {
  1285. return [
  1286. 'code' => 1,
  1287. 'msg' => $e->getMessage()
  1288. ];
  1289. }
  1290. }
  1291. //处理子父级数据
  1292. public function getdata($data, $id = "0")
  1293. {
  1294. foreach ($data as $k => $v) {
  1295. if ($v['parent_category_id'] == $id) {
  1296. $v['children'] = $this->getdata($data, $v['category_id']);
  1297. $arr[] = $v;
  1298. }
  1299. }
  1300. return $arr;
  1301. }
  1302. /**
  1303. * 小程序退回开发
  1304. * alipay.open.mini.version.audited.cancel(小程序退回开发)
  1305. */
  1306. public function miniVersionCancel()
  1307. {
  1308. $t = \Yii::$app->db->beginTransaction();
  1309. try {
  1310. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  1311. $mini = StoreAliMini::findOne($this->mini_id);
  1312. if (empty($mini)) {
  1313. throw new \Exception("没有进行授权");
  1314. }
  1315. $param = [
  1316. 'app_version' => $MiniVersion->template_version
  1317. ];
  1318. $this->biz_content = json_encode($param);
  1319. $this->method = "alipay.open.mini.version.audited.cancel";
  1320. $request = new AlipayOpenMiniVersionAuditedCancelRequest();
  1321. $result = $this->miniCommon($request);
  1322. if (!empty($result->code) && $result->code == 10000) {
  1323. $MiniVersion->is_submit = 0;
  1324. $MiniVersion->save();
  1325. $t->commit();
  1326. return [
  1327. 'code' => 0,
  1328. 'msg' => "成功",
  1329. 'data' => $result
  1330. ];
  1331. } else {
  1332. throw new \Exception($result->sub_msg);
  1333. }
  1334. } catch (\Exception $e) {
  1335. $t->rollBack();
  1336. return [
  1337. 'code' => 1,
  1338. 'msg' => $e->getMessage()
  1339. ];
  1340. }
  1341. }
  1342. /**
  1343. * 小程序版本详情查询
  1344. * alipay.open.mini.version.detail.query(小程序版本详情查询)
  1345. */
  1346. public function miniVersionDetail()
  1347. {
  1348. try {
  1349. $mini = StoreAliMini::findOne($this->mini_id);
  1350. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one();
  1351. if (empty($mini)) {
  1352. throw new \Exception("没有进行授权");
  1353. }
  1354. $param = [
  1355. 'app_version' => $MiniVersion->template_version
  1356. ];
  1357. $this->biz_content = json_encode($param);
  1358. $this->method = "alipay.open.mini.version.detail.query";
  1359. $request = new AlipayOpenMiniVersionDetailQueryRequest();
  1360. $result = $this->miniCommon($request);
  1361. if (!empty($result->code) && $result->code == 10000) {
  1362. return [
  1363. 'code' => 0,
  1364. 'msg' => "成功",
  1365. 'data' => $result
  1366. ];
  1367. } else {
  1368. throw new \Exception($result->sub_msg);
  1369. }
  1370. } catch (\Exception $e) {
  1371. return [
  1372. 'code' => 1,
  1373. 'msg' => $e->getMessage()
  1374. ];
  1375. }
  1376. }
  1377. /**
  1378. * 小程序灰度上架
  1379. * alipay.open.mini.version.gray.online(小程序灰度上架)
  1380. */
  1381. // public function miniGrayOnline(){
  1382. // try {
  1383. // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
  1384. // if(empty($mini)){
  1385. // throw new \Exception("没有进行授权");
  1386. // }
  1387. // $gray_strategy = 'p'.(int)abs($this->gray_strategy);//只支持10/30/50
  1388. // $param = [
  1389. // 'app_version'=>$this->app_version,
  1390. // 'gray_strategy'=>$gray_strategy,
  1391. // 'bundle_id'=>$this->bundle_id,
  1392. // ];
  1393. // $this->biz_content = json_encode($param);
  1394. // $this->method = "alipay.open.mini.version.gray.online";
  1395. // $request = new AlipayOpenMiniVersionGrayOnlineRequest();
  1396. // $result = $this->miniCommon($request);
  1397. // if(!empty($result->code)&&$result->code == 10000){
  1398. // return [
  1399. // 'code'=>0,
  1400. // 'msg'=>"成功",
  1401. // 'data'=>$result
  1402. // ];
  1403. // }else{
  1404. // throw new \Exception($result->sub_msg);
  1405. // }
  1406. // }catch (\Exception $e){
  1407. // return [
  1408. // 'code'=>1,
  1409. // 'msg'=>$e->getMessage()
  1410. // ];
  1411. // }
  1412. // }
  1413. /**
  1414. * 小程序结束灰度
  1415. * alipay.open.mini.version.gray.cancel(小程序结束灰度)
  1416. */
  1417. // public function miniGrayCancel(){
  1418. // try {
  1419. // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one();
  1420. // if(empty($mini)){
  1421. // throw new \Exception("没有进行授权");
  1422. // }
  1423. // $param = [
  1424. // 'app_version'=>$this->app_version,
  1425. // 'bundle_id'=>$this->bundle_id,
  1426. // ];
  1427. // $this->biz_content = json_encode($param);
  1428. // $this->method = "alipay.open.mini.version.gray.cancel";
  1429. // $request = new AlipayOpenMiniVersionGrayCancelRequest();
  1430. // $result = $this->miniCommon($request);
  1431. // if(!empty($result->code)&&$result->code == 10000){
  1432. // return [
  1433. // 'code'=>0,
  1434. // 'msg'=>"成功",
  1435. // 'data'=>$result
  1436. // ];
  1437. // }else{
  1438. // throw new \Exception($result->sub_msg);
  1439. // }
  1440. // }catch (\Exception $e){
  1441. // return [
  1442. // 'code'=>1,
  1443. // 'msg'=>$e->getMessage()
  1444. // ];
  1445. // }
  1446. // }
  1447. /**
  1448. * 小程序上架
  1449. * alipay.open.mini.version.online(小程序上架)
  1450. */
  1451. public function miniOnline()
  1452. {
  1453. $t = \Yii::$app->db->beginTransaction();
  1454. try {
  1455. if (!empty($this->bind_store_id)) {
  1456. $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id,aes_key,auth_app_id,userid,license_name')->orderBy('id desc')->one();
  1457. $mini_id = $mini->id;
  1458. } else {
  1459. $mini_id = $this->mini_id;
  1460. $mini = StoreAliMini::findOne($mini_id);
  1461. }
  1462. $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy("create_time desc")->one();
  1463. if (empty($mini)) {
  1464. throw new \Exception("没有进行授权");
  1465. }
  1466. $param = [
  1467. 'app_version' => $MiniVersion->template_version
  1468. ];
  1469. $this->biz_content = json_encode($param);
  1470. $this->method = "alipay.open.mini.version.online";
  1471. $request = new AlipayOpenMiniVersionOnlineRequest();
  1472. $result = $this->miniCommon($request);
  1473. if (!empty($result->code) && $result->code == 10000) {
  1474. $MiniVersion->is_submit = 4;
  1475. $res = $MiniVersion->save();
  1476. if ($res) {
  1477. $t->commit();
  1478. return [
  1479. 'code' => 0,
  1480. 'msg' => '成功',
  1481. 'data' => $result
  1482. ];
  1483. } else {
  1484. throw new \Exception("保存数据错误");
  1485. }
  1486. } else {
  1487. throw new \Exception($result->sub_msg);
  1488. }
  1489. } catch (\Exception $e) {
  1490. $t->rollBack();
  1491. return [
  1492. 'code' => 1,
  1493. 'msg' => $e->getMessage()
  1494. ];
  1495. }
  1496. }
  1497. /**
  1498. * 小程序生成推广二维码接口
  1499. * alipay.open.app.qrcode.create(小程序生成推广二维码接口)
  1500. */
  1501. public function miniQrcodeCreate()
  1502. {
  1503. $t = \Yii::$app->db->beginTransaction();
  1504. try {
  1505. $mini = StoreAliMini::findOne($this->mini_id);
  1506. if (empty($mini)) {
  1507. throw new \Exception("没有进行授权");
  1508. }
  1509. $param = [
  1510. 'url_param' => "pages/home/home",
  1511. 'query_param' => "mini_id=" . $this->mini_id,
  1512. 'describe' => "小程序开发上线使用"
  1513. ];
  1514. $this->biz_content = json_encode($param);
  1515. $this->method = "alipay.open.app.qrcode.create";
  1516. $request = new AlipayOpenAppQrcodeCreateRequest();
  1517. $result = $this->miniCommon($request);
  1518. if (!empty($result->code) && $result->code == 10000) {
  1519. $t->commit();
  1520. return [
  1521. 'code' => 0,
  1522. 'msg' => "成功",
  1523. 'data' => $result
  1524. ];
  1525. } else {
  1526. throw new \Exception($result->sub_msg);
  1527. }
  1528. } catch (\Exception $e) {
  1529. $t->rollBack();
  1530. return [
  1531. 'code' => 1,
  1532. 'msg' => $e->getMessage()
  1533. ];
  1534. }
  1535. }
  1536. /**
  1537. * 图片转base64
  1538. */
  1539. public function image2Base64($file)
  1540. {
  1541. if ($fp = fopen($file, "rb", 0)) {
  1542. $gambar = fread($fp, filesize($file));
  1543. fclose($fp);
  1544. $base64 = chunk_split(base64_encode($gambar));
  1545. return $base64;
  1546. } else {
  1547. return false;
  1548. }
  1549. }
  1550. /**
  1551. * 获取列表
  1552. */
  1553. public function getMiniList()
  1554. {
  1555. $query = StoreAliMini::find()->where(['store_id' => get_store_id()])
  1556. ->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");
  1557. if ($this->AppName) {
  1558. $query->andWhere(['LIKE', 'app_name', $this->AppName]);
  1559. }
  1560. $query->andWhere(['is_cancel' => 0]);
  1561. $pagination = pagination_make($query);
  1562. $admins = $pagination['list'];
  1563. foreach ($admins as &$admin) {
  1564. $version = StoreAliMiniVersion::find()->where(['mini_id' => $admin['id']])->orderBy('create_time desc')->asArray()->one();
  1565. \Yii::error($version);
  1566. $admin['is_submit'] = !empty($version['is_submit']) ? $version['is_submit'] * 1 : 0;
  1567. if (empty($version)) {
  1568. $admin['is_submit'] = -1;
  1569. if ($admin['order_cancel'] == 1) {
  1570. $admin['is_submit'] = -2;
  1571. }
  1572. }
  1573. $admin['apply_msg'] = [];
  1574. if (!empty($admin['apply_phone_msg'])) {
  1575. $admin['apply_msg'] = json_decode($admin['apply_phone_msg']);
  1576. }
  1577. $admin['is_get_phone'] = empty($this->getField($admin['id'])['data']) ? 2 : $admin['is_get_phone'];
  1578. $admin['is_experience'] = !empty($version['is_experience']) ? $version['is_experience'] : 0;
  1579. $admin['experience_url'] = !empty($version['experience_url']) ? $version['experience_url'] : '';
  1580. $admin['mini_categoryIds'] = !empty($admin['mini_categoryIds']) ? explode(';', $admin['mini_categoryIds']) : [];
  1581. }
  1582. return [
  1583. 'code' => 0,
  1584. 'msg' => "获取成功",
  1585. 'data' => $admins,
  1586. 'pageNo' => $pagination['pageNo'],
  1587. 'totalCount' => $pagination['totalCount'],
  1588. ];
  1589. }
  1590. /**
  1591. * 获取版本列表
  1592. */
  1593. // public function getMiniVersionList(){
  1594. // $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");
  1595. // $pagination = pagination_make($query);
  1596. // $admins = $pagination['list'];
  1597. // return [
  1598. // 'code'=>0,
  1599. // 'msg'=>"获取成功",
  1600. // 'data'=>$admins,
  1601. // 'pageNo' => $pagination['pageNo'],
  1602. // 'totalCount' => $pagination['totalCount'],
  1603. // ];
  1604. // }
  1605. /**
  1606. * 获取修改信息
  1607. */
  1608. public function getMiniInfo()
  1609. {
  1610. $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");
  1611. $mini_info = $query->asArray()->one();
  1612. $version_info = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy('create_time desc')->select('template_version,is_submit')->one();
  1613. $mini_info['version'] = $version_info->template_version;
  1614. $mini_info['is_submit'] = $version_info->is_submit;
  1615. $mini_info['mini_category_ids'] = !empty($mini_info['mini_categoryIds']) ? explode(';', $mini_info['mini_categoryIds']) : [];
  1616. return [
  1617. 'code' => 0,
  1618. 'msg' => "获取成功",
  1619. 'data' => $mini_info
  1620. ];
  1621. }
  1622. /**
  1623. * 单一版本信息
  1624. */
  1625. // public function getVersionInfo(){
  1626. // $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();
  1627. // $mini['create_time'] = date("Y-m-d H:i:s",$mini['create_time']);
  1628. // return [
  1629. // 'code'=>0,
  1630. // 'msg'=>"获取成功",
  1631. // 'data'=>$mini
  1632. // ];
  1633. // }
  1634. /**
  1635. * isv服务商代商户创建小程序
  1636. */
  1637. public function isvCreateMini()
  1638. {
  1639. $t = \Yii::$app->db->beginTransaction();
  1640. try {
  1641. if (!empty($this->bind_store_id)) {
  1642. $store_id = $this->bind_store_id;
  1643. $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id')->orderBy('id desc')->one();
  1644. $mini_id = $mini->id;
  1645. } else {
  1646. $mini_id = $this->mini_id;
  1647. $store_id = get_store_id();
  1648. }
  1649. if (!empty($mini_id)) {
  1650. $ali_mini = StoreAliMini::find()->where(['id' => $mini_id])->one();
  1651. } else {
  1652. //$ali_mini = StoreAliMini::find()->where(['license_no'=>$this->license_no])->one();
  1653. //if(!empty($ali_mini)){
  1654. //throw new \Exception("营业执照信息已经存在");
  1655. // }
  1656. $ali_mini = new StoreAliMini();
  1657. }
  1658. $new_mini = StoreAliMini::find()->where(['store_id' => $store_id])->orderBy('id desc')->one();
  1659. $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $new_mini->id + 1);
  1660. $param = [
  1661. 'create_mini_request' => [
  1662. 'out_order_no' => $out_order_no,
  1663. 'alipay_account' => $this->alipay_account,
  1664. 'legal_personal_name' => $this->legal_personal_name,
  1665. 'cert_name' => $this->license_name,
  1666. 'cert_no' => $this->license_no,
  1667. 'app_name' => $this->AppName,
  1668. 'contact_phone' => $this->ServicePhone,
  1669. 'contact_name' => $this->contact_name,
  1670. 'is_individual' => true
  1671. ]
  1672. ];
  1673. $ali_mini->store_id = $store_id;
  1674. $ali_mini->license_name = $this->license_name;
  1675. $ali_mini->order_no = $out_order_no;
  1676. $ali_mini->license_no = $this->license_no;
  1677. $ali_mini->app_name = $this->AppName;
  1678. $ali_mini->service_phone = $this->ServicePhone;
  1679. $ali_mini->license_pic = $this->license_pic;
  1680. $ali_mini->alipay_account = $this->alipay_account;
  1681. $ali_mini->legal_personal_name = $this->legal_personal_name;
  1682. if (!empty($this->merchant_id)) {
  1683. $ali_mini->merchant_id = $this->merchant_id;
  1684. }
  1685. $ali_mini->contact_name = $this->contact_name;
  1686. $res = $ali_mini->save();
  1687. if ($res) {
  1688. $this->biz_content = json_encode($param);
  1689. $this->method = "alipay.open.mini.isv.create";
  1690. $request = new AlipayOpenMiniIsvCreateRequest();
  1691. $result = $this->miniCommon($request);
  1692. if (!empty($result->code) && $result->code == 10000) {
  1693. $t->commit();
  1694. return [
  1695. 'code' => 0,
  1696. 'msg' => "成功",
  1697. 'data' => $result,
  1698. 'id' => $ali_mini->id
  1699. ];
  1700. }
  1701. throw new \Exception($result->sub_msg);
  1702. } else {
  1703. throw new \Exception("插入数据失败");
  1704. }
  1705. } catch (\Exception $e) {
  1706. $t->rollBack();
  1707. return [
  1708. 'code' => 1,
  1709. 'msg' => $e->getMessage(),
  1710. 'line' => $e->getLine(),
  1711. 'file' => $e->getFile()
  1712. ];
  1713. }
  1714. }
  1715. //创建事务
  1716. public function agentCreate($id = 0)
  1717. {
  1718. $t = \Yii::$app->db->beginTransaction();
  1719. try {
  1720. if (!empty($this->bind_store_id)) {
  1721. $store_id = $this->bind_store_id;
  1722. $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();
  1723. $id = $mini->id;
  1724. } else {
  1725. $id = !empty($id) ? $id : $this->mini_id;
  1726. $mini = StoreAliMini::findOne($id);
  1727. }
  1728. \Yii::error(json_encode($mini));
  1729. if (empty($mini)) {
  1730. throw new \Exception("查询不到任何信息");
  1731. }
  1732. $data = [
  1733. 'account' => $mini->alipay_account,
  1734. 'contact_info' => [
  1735. 'contact_name' => $mini->contact_name,
  1736. 'contact_mobile' => $mini->service_phone,
  1737. 'contact_email' => $mini->service_email
  1738. ]
  1739. ];
  1740. $this->method = "alipay.open.agent.create";
  1741. $this->biz_content = json_encode($data);
  1742. $request = new AlipayOpenAgentCreateRequest();
  1743. $batch_no = $mini->batch_no;
  1744. $res = [
  1745. 'code' => 0,
  1746. 'msg' => "执行成功"
  1747. ];
  1748. if ($mini->batch_status == 0 || $mini->batch_status == 1) {
  1749. $result = $this->miniCommon($request);
  1750. if (!empty($result->code) && $result->code == 10000) {
  1751. $mini->batch_no = $result->batch_no;
  1752. $mini->batch_status = 1;
  1753. $res = $mini->save();
  1754. if (!$res) {
  1755. throw new \Exception("保存信息失败");
  1756. }
  1757. $batch_no = $result->batch_no;
  1758. }
  1759. }
  1760. if ($mini->batch_status === 1 || $mini->batch_status === 4) {
  1761. $res = $this->faceToFace($id, $batch_no);
  1762. \Yii::error("当面付");
  1763. \Yii::error($res);
  1764. if ($res['code'] !== 0) {
  1765. throw new \Exception($res['msg']);
  1766. }
  1767. }
  1768. if ($mini->batch_status === 2) {
  1769. $res = $this->agentConfirm($id, $batch_no);
  1770. \Yii::error("事务提交");
  1771. \Yii::error($res);
  1772. if ($res['code'] !== 0) {
  1773. throw new \Exception($res['msg']);
  1774. }
  1775. }
  1776. if ($mini->batch_status === 3) {
  1777. $res = $this->getBatchStatus($id, $batch_no);
  1778. \Yii::error("查询状态");
  1779. \Yii::error($res);
  1780. if ($res['code'] !== 0) {
  1781. throw new \Exception($res['msg']);
  1782. }
  1783. }
  1784. $t->commit();
  1785. return $res;
  1786. } catch (\Exception $e) {
  1787. $t->rollBack();
  1788. return [
  1789. 'code' => 1,
  1790. 'msg' => $e->getMessage()
  1791. ];
  1792. }
  1793. }
  1794. //当面付签约
  1795. public function faceToFace($id = 0, $batch_no = '')
  1796. {
  1797. try {
  1798. if (!empty($this->bind_store_id)) {
  1799. $store_id = $this->bind_store_id;
  1800. $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,mini_categoryIds,app_logo')->orderBy('id desc')->one();
  1801. } else {
  1802. $mini_id = !empty($id) ? $id : $this->mini_id;
  1803. $mini = StoreAliMini::findOne($mini_id);
  1804. }
  1805. if (empty($mini)) {
  1806. throw new \Exception("查询不到任何信息");
  1807. }
  1808. $this->method = "alipay.open.agent.facetoface.sign";
  1809. $request = new AlipayOpenAgentFacetofaceSignRequest();
  1810. $request->setBatchNo($batch_no);
  1811. $request->setMccCode("A0002_B0201");
  1812. $request->setRate('0.38');
  1813. $request->setSignAndAuth(false);
  1814. $request->setShopSignBoardPic('@' . $this->saveTempImage($mini->app_logo));
  1815. \Yii::error($id);
  1816. $result = $this->miniCommon($request, 1, $mini_id, 1);
  1817. \Yii::error($result);
  1818. if ((!empty($result->code) && $result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") {
  1819. $mini->batch_status = 2;
  1820. if ($result->sub_code == "ANT_PRODUCT_CONFLICT") {
  1821. $mini->batch_status = 6;
  1822. }
  1823. $res = $mini->save();
  1824. if ($res) {
  1825. return [
  1826. 'code' => 0,
  1827. 'msg' => "成功",
  1828. 'data' => $result
  1829. ];
  1830. } else {
  1831. throw new \Exception("添加数据失败");
  1832. }
  1833. }
  1834. throw new \Exception($result->sub_msg);
  1835. } catch (\Exception $e) {
  1836. return [
  1837. 'code' => 1,
  1838. 'msg' => $e->getMessage() . $e->getLine()
  1839. ];
  1840. }
  1841. }
  1842. //提交事务
  1843. public function agentConfirm($id = 0, $batch_no = '')
  1844. {
  1845. try {
  1846. if (!empty($this->bind_store_id)) {
  1847. $store_id = $this->bind_store_id;
  1848. $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no')->orderBy('id desc')->one();
  1849. } else {
  1850. $mini_id = !empty($id) ? $id : $this->mini_id;
  1851. $mini = StoreAliMini::findOne($mini_id);
  1852. }
  1853. if (empty($mini)) {
  1854. throw new \Exception("查询不到任何信息");
  1855. }
  1856. $data = [
  1857. 'batch_no' => $batch_no
  1858. ];
  1859. $this->method = "alipay.open.agent.confirm";
  1860. $this->biz_content = json_encode($data);
  1861. $request = new AlipayOpenAgentConfirmRequest();
  1862. $result = $this->miniCommon($request, 0, $id, 1);
  1863. \Yii::error($result);
  1864. if (!empty($result->code) && $result->code == 10000) {
  1865. $mini->batch_status = 3;
  1866. $res = $mini->save();
  1867. if ($res) {
  1868. return [
  1869. 'code' => 0,
  1870. 'msg' => "成功",
  1871. 'data' => $result
  1872. ];
  1873. } else {
  1874. throw new \Exception("添加数据失败");
  1875. }
  1876. }
  1877. throw new \Exception($result->sub_msg);
  1878. } catch (\Exception $e) {
  1879. return [
  1880. 'code' => 1,
  1881. 'msg' => $e->getMessage()
  1882. ];
  1883. }
  1884. }
  1885. //查询状态
  1886. public function getBatchStatus($id = 0, $batch = '')
  1887. {
  1888. try {
  1889. \Yii::error("查询状态");
  1890. if (!empty($this->bind_store_id)) {
  1891. $store_id = $this->bind_store_id;
  1892. \Yii::error($store_id);
  1893. $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,alipay_account')->orderBy('id desc')->one();
  1894. } else {
  1895. $mini_id = !empty($id) ? $id : $this->mini_id;
  1896. \Yii::error($mini_id);
  1897. $mini = StoreAliMini::findOne($mini_id);
  1898. }
  1899. \Yii::error($mini);
  1900. if (empty($mini)) {
  1901. throw new \Exception("查询不到任何信息");
  1902. }
  1903. $data = [
  1904. 'batch_no' => $batch
  1905. ];
  1906. $this->method = "alipay.open.agent.order.query";
  1907. $this->biz_content = json_encode($data);
  1908. $request = new AlipayOpenAgentOrderQueryRequest();
  1909. $result = $this->miniCommon($request, 0, $id, 1);
  1910. \Yii::error("查询状态查询状态查询状态查询状态");
  1911. \Yii::error(json_encode($result));
  1912. if (!empty($result->code) && $result->code == 10000) {
  1913. $status = $result->order_status;
  1914. switch ($status) {
  1915. case "MERCHANT_INFO_HOLD": //异常单 4
  1916. $mini->batch_status = 4;
  1917. break;
  1918. case "MERCHANT_AUDITING": //审核中 3
  1919. $mini->batch_status = 3;
  1920. break;
  1921. case "MERCHANT_CONFIRM": //待确认 5
  1922. $mini->batch_status = 5;
  1923. break;
  1924. case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6
  1925. $mini->batch_status = 6;
  1926. break;
  1927. case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7
  1928. $mini->batch_status = 7;
  1929. break;
  1930. case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8
  1931. $mini->batch_status = 8;
  1932. break;
  1933. }
  1934. $res = $mini->save();
  1935. if ($res) {
  1936. return [
  1937. 'code' => 0,
  1938. 'msg' => "成功",
  1939. 'data' => $result
  1940. ];
  1941. } else {
  1942. throw new \Exception("添加数据失败");
  1943. }
  1944. }
  1945. throw new \Exception($result->sub_msg);
  1946. } catch (\Exception $e) {
  1947. return [
  1948. 'code' => 1,
  1949. 'msg' => $e->getMessage()
  1950. ];
  1951. }
  1952. }
  1953. //关联普通二维码
  1954. public function bindQrcode($id)
  1955. {
  1956. $t = \Yii::$app->db->beginTransaction();
  1957. try {
  1958. $qrcode = AggregateQrcode::find()->where(['ali_mini_id' => $id])->one();
  1959. if (empty($qrcode->param_url) || empty($qrcode->ali_url)) {
  1960. throw new \Exception("参数错误");
  1961. }
  1962. $data = [
  1963. "route_url" => $qrcode->param_url,
  1964. 'mode' => "EXACT",
  1965. "page_redirection" => $qrcode->ali_url
  1966. ];
  1967. \Yii::error($data);
  1968. $this->method = "alipay.open.mini.qrcode.bind";
  1969. $this->biz_content = json_encode($data);
  1970. $request = new AlipayOpenMiniQrcodeBindRequest();
  1971. $result = $this->miniCommon($request, 0, $id);
  1972. if (!empty($result->code) && $result->code == 10000) {
  1973. $qrcode->ali_status = 1;
  1974. $res = $qrcode->save();
  1975. if (!$res) {
  1976. throw new \Exception('保存数据失败,绑定二维码成功');
  1977. }
  1978. $t->commit();
  1979. return [
  1980. 'code' => 0,
  1981. 'msg' => "成功",
  1982. 'data' => $result
  1983. ];
  1984. } else {
  1985. throw new \Exception($result->sub_msg);
  1986. }
  1987. } catch (\Exception $e) {
  1988. $t->rollBack();
  1989. return [
  1990. 'code' => 1,
  1991. 'msg' => $e->getMessage()
  1992. ];
  1993. }
  1994. }
  1995. //支付
  1996. public function payconfig($id, $out_trade_no, $user_id, $pay_price)
  1997. {
  1998. $t = \Yii::$app->db->beginTransaction();
  1999. try {
  2000. $data = [
  2001. "out_trade_no" => $out_trade_no,
  2002. 'total_amount' => $pay_price,
  2003. "subject" => "当面付",
  2004. "buyer_id" => $user_id
  2005. ];
  2006. $this->method = "alipay.trade.create";
  2007. $this->biz_content = json_encode($data);
  2008. $request = new AlipayTradeCreateRequest();
  2009. $request->setNotifyUrl(pay_notify_url(self::$notify_url));
  2010. $result = $this->miniCommon($request, 0, $id, 0, 1);
  2011. if (!empty($result->code) && $result->code == 10000) {
  2012. $t->commit();
  2013. $data = [
  2014. 'trade_no' => $result->trade_no,
  2015. 'order_no' => $out_trade_no
  2016. ];
  2017. return [
  2018. 'code' => 0,
  2019. 'msg' => "成功",
  2020. 'data' => $data
  2021. ];
  2022. } else {
  2023. throw new \Exception($result->sub_msg);
  2024. }
  2025. } catch (\Exception $e) {
  2026. $t->rollBack();
  2027. return [
  2028. 'code' => 1,
  2029. 'msg' => $e->getMessage()
  2030. ];
  2031. }
  2032. }
  2033. //获取敏感字段
  2034. public function getField($id = 0)
  2035. {
  2036. try {
  2037. $mini_id = !empty($id) ? $id : $this->mini_id;
  2038. \Yii::error($mini_id);
  2039. $mini = StoreAliMini::findOne($mini_id);
  2040. $this->method = "alipay.open.app.api.query";
  2041. $request = new AlipayOpenAppApiQueryRequest();
  2042. $result = $this->miniCommon($request, 1, $mini->id);
  2043. \Yii::error("获取敏感字段");
  2044. \Yii::error($result);
  2045. if (!empty($result->code) && $result->code == 10000) {
  2046. if (!empty($result->apis)) {
  2047. $scene = $this->getScene($mini->id);
  2048. if ($scene['code'] === 0) {
  2049. return [
  2050. 'code' => 0,
  2051. 'msg' => "成功",
  2052. 'data' => $scene['data']
  2053. ];
  2054. }
  2055. throw new \Exception($scene['msg']);
  2056. }
  2057. return [
  2058. 'code' => 0,
  2059. 'msg' => "成功",
  2060. 'data' => []
  2061. ];
  2062. } else {
  2063. throw new \Exception($result->sub_msg);
  2064. }
  2065. } catch (\Exception $e) {
  2066. return [
  2067. 'code' => 1,
  2068. 'msg' => $e->getMessage()
  2069. ];
  2070. }
  2071. }
  2072. //获取敏感字段使用场景
  2073. public function getScene($id)
  2074. {
  2075. try {
  2076. if (empty($id)) {
  2077. throw new \Exception("未知的小程序信息");
  2078. }
  2079. $data = [
  2080. 'field_name' => 'mobile',
  2081. 'api_name' => 'getPhoneNumber'
  2082. ];
  2083. $this->method = "alipay.open.app.api.scene.query";
  2084. $this->biz_content = json_encode($data);
  2085. $request = new AlipayOpenAppApiSceneQueryRequest();
  2086. $result = $this->miniCommon($request, 0, $id);
  2087. \Yii::error("获取敏感字段场景值");
  2088. \Yii::error($result);
  2089. if (!empty($result->code) && $result->code == 10000) {
  2090. return [
  2091. 'code' => 0,
  2092. 'msg' => "成功",
  2093. 'data' => $result->auth_field_scene
  2094. ];
  2095. } else {
  2096. throw new \Exception($result->sub_msg);
  2097. }
  2098. } catch (\Exception $e) {
  2099. return [
  2100. 'code' => 1,
  2101. 'msg' => $e->getMessage()
  2102. ];
  2103. }
  2104. }
  2105. //申请接入敏感字段
  2106. public function applyPhone()
  2107. {
  2108. try {
  2109. $apply_msg = $this->apply_msg;
  2110. $mini_id = $apply_msg['id'];
  2111. if (empty($mini_id)) {
  2112. throw new \Exception("获取小程序信息失败");
  2113. }
  2114. \Yii::error($mini_id);
  2115. $mini = StoreAliMini::findOne($mini_id);
  2116. $this->method = "alipay.open.app.api.field.apply";
  2117. $request = new AlipayOpenAppApiFieldApplyRequest();
  2118. !empty($apply_msg['video']) && $request->setVideo('@' . $this->saveTempImage($apply_msg['video']));
  2119. foreach ($apply_msg['picture'] as $index => $item) {
  2120. \Yii::error($item);
  2121. \Yii::error($this->saveTempImage($item));
  2122. switch ($index) {
  2123. case 0:
  2124. $request->setPicture1('@' . $this->saveTempImage($item));
  2125. break;
  2126. case 1:
  2127. $request->setPicture2('@' . $this->saveTempImage($item));
  2128. break;
  2129. case 2:
  2130. $request->setPicture3('@' . $this->saveTempImage($item));
  2131. break;
  2132. case 3:
  2133. $request->setPicture4('@' . $this->saveTempImage($item));
  2134. break;
  2135. case 4:
  2136. $request->setPicture5('@' . $this->saveTempImage($item));
  2137. break;
  2138. }
  2139. }
  2140. $data = [
  2141. 'api_name' => "getPhoneNumber",
  2142. 'field_name' => "mobile",
  2143. "package_code" => "20180927110154092444",
  2144. 'scene_code' => $apply_msg['scene_code'],
  2145. 'qps_answer' => $apply_msg['qps_answer'],
  2146. 'customer_answer' => $apply_msg['customer_answer'],
  2147. 'memo' => $apply_msg['memo']
  2148. ];
  2149. $request->setAuthFieldApply(json_encode($data));
  2150. $result = $this->miniCommon($request, 1, $apply_msg['id']);
  2151. \Yii::error($result);
  2152. $mini->apply_phone_msg = json_encode($apply_msg);
  2153. $mini->save();
  2154. if (!empty($result->code) && $result->code == 10000) {
  2155. $mini->is_get_phone = 1;
  2156. $mini->save();
  2157. return [
  2158. 'code' => 0,
  2159. 'msg' => "开始审核",
  2160. 'data' => $result
  2161. ];
  2162. } else {
  2163. throw new \Exception($result->sub_msg);
  2164. }
  2165. } catch (\Exception $e) {
  2166. return [
  2167. 'code' => 1,
  2168. 'msg' => $e->getMessage()
  2169. ];
  2170. }
  2171. }
  2172. //手动查询手机号申请状态
  2173. public function getPhoneStatus($id)
  2174. {
  2175. try {
  2176. $mini_id = $this->mini_id;
  2177. $mini = StoreAliMini::find()->where(['id' => $mini_id])->select("id,is_get_phone,apply_phone_fail,auth_app_id")->one();
  2178. if (empty($mini)) {
  2179. throw new \Exception("获取小程序信息失败");
  2180. }
  2181. $this->method = "alipay.open.app.api.field.query";
  2182. $request = new AlipayOpenAppApiFieldQueryRequest();
  2183. $result = $this->miniCommon($request, 1, $mini_id);
  2184. \Yii::error("获取敏感字段申请状态");
  2185. \Yii::error($result);
  2186. if (!empty($result->code) && $result->code == 10000) {
  2187. $data = $result->auth_field_response->records;
  2188. $data_arr = json_decode(json_encode($data), true);
  2189. \Yii::error($data_arr);
  2190. $status = $data_arr[0]['status'];
  2191. if ($status === "AGREE") {
  2192. $mini->is_get_phone = 2;
  2193. } elseif ($status === "REJECT" || $status === "INVALID") {
  2194. $reason = !empty($data->reason) ? $data->reason : '';
  2195. $mini->is_get_phone = 3;
  2196. $mini->apply_phone_fail = $reason;
  2197. }
  2198. if ($mini->save()) {
  2199. return [
  2200. 'code' => 0,
  2201. 'msg' => "成功",
  2202. 'data' => $data,
  2203. 'status' => $status
  2204. ];
  2205. }
  2206. throw new \Exception("保存数据错误" . json_encode($mini->errors));
  2207. } else {
  2208. throw new \Exception($result->sub_msg);
  2209. }
  2210. } catch (\Exception $e) {
  2211. return [
  2212. 'code' => 1,
  2213. 'msg' => $e->getMessage()
  2214. ];
  2215. }
  2216. }
  2217. //获取网络图片到临时目录
  2218. public function saveTempImage($url)
  2219. {
  2220. if (strpos($url, 'http') === false) {
  2221. $url = 'http:' . trim($url);
  2222. }
  2223. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  2224. mkdir(\Yii::$app->runtimePath . '/image');
  2225. }
  2226. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  2227. CurlHelper::download($url, $save_path);
  2228. return $save_path;
  2229. }
  2230. public function getIsvCreateMini()
  2231. {
  2232. $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();
  2233. return [
  2234. 'code' => 0,
  2235. 'msg' => "获取成功",
  2236. 'data' => $mini
  2237. ];
  2238. }
  2239. // 获取小程序审核状态
  2240. public function getMiniShenheStatus($id){
  2241. try {
  2242. $store_ali_mini = StoreAliMini::findOne($id);
  2243. if (!$store_ali_mini || !$store_ali_mini->order_no) {
  2244. return false;
  2245. }
  2246. $data = [
  2247. "order_no" => $store_ali_mini->order_no,
  2248. ];
  2249. $this->method = "alipay.open.mini.isv.query";
  2250. $this->biz_content = json_encode($data);
  2251. $request = new AlipayOpenMiniIsvQueryRequest();
  2252. $result = $this->miniCommon($request,0,$id);
  2253. if(!empty($result->code)&&$result->code == 10000){
  2254. return $result;
  2255. }else{
  2256. throw new \Exception($result->sub_msg);
  2257. }
  2258. }catch (\Exception $e){
  2259. return [
  2260. 'code'=> 2,
  2261. 'msg'=>$e->getMessage()
  2262. ];
  2263. }
  2264. }
  2265. }