WechatThirdForm.php 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\models\Admin;
  10. use app\models\AggregateQrcode;
  11. use app\models\MerchantInfo;
  12. use app\models\Option;
  13. use app\models\StoreMedia;
  14. use app\models\StoreMini;
  15. use app\models\StoreMiniAuth;
  16. use app\models\StoreMiniAuthIcp;
  17. use app\models\StoreMiniIcp;
  18. use app\models\StoreSchedule;
  19. use app\models\WechatConfig;
  20. use app\modules\admin\models\file\FileForm;
  21. use app\utils\CurlHelper;
  22. use app\utils\Wechat\WechatMini;
  23. use app\utils\WechatMerchant\Merchant;
  24. use EasyWeChat\Factory;
  25. use EasyWeChat\Kernel\BaseClient;
  26. use EasyWeChat\BasicService\Media\Client;
  27. use yii\base\Model;
  28. use yii\log\Logger;
  29. use app\modules\admin\models\WechatThirdErrorMsgForm as ErrorMsg;
  30. use Exception;
  31. use app\models\OrderUnion;
  32. use app\models\Store;
  33. use app\models\Order;
  34. use app\utils\Wechat\Wechat;
  35. use app\utils\Wechat\WechatProfit;
  36. use app\models\ReOrder;
  37. class WechatThirdForm extends Model
  38. {
  39. /**
  40. * @var mixed
  41. */
  42. public $fuwu_type;
  43. public $openPlatform;
  44. public $config;
  45. public $appid;
  46. public $data = [];
  47. public $base_url = "https://api.weixin.qq.com/cgi-bin/";
  48. public $name; // 企业名
  49. public $code; // 企业代码
  50. public $code_type; // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
  51. public $legal_persona_wechat; // 法人微信
  52. public $legal_persona_name; // 法人姓名
  53. public $component_phone; //第三方联系电话
  54. public $authorization_code;
  55. public $action; // 0获取 1添加 2删除 3覆盖
  56. public $requestdomain;
  57. public $wsrequestdomain;
  58. public $uploaddomain;
  59. public $downloaddomain;
  60. public $udpdomain;
  61. public $tcpdomai;
  62. public $webviewdomain;
  63. public $media_image;
  64. public $mini_nickname;
  65. public $id_card;
  66. public $license;
  67. public $naming_other_stuff_1;
  68. public $naming_other_stuff_2;
  69. public $naming_other_stuff_3;
  70. public $naming_other_stuff_4;
  71. public $naming_other_stuff_5;
  72. public $head_img_media_id; //上传头像 头像素材 media_id
  73. public $x1 = 0; //上传头像 裁剪框左上角 x 坐标(取值范围:[0, 1])
  74. public $y1 = 0; //上传头像 裁剪框左上角 y 坐标(取值范围:[0, 1])
  75. public $x2 = 1; //上传头像 裁剪框右下角 x 坐标(取值范围:[0, 1])
  76. public $y2 = 1; //上传头像 裁剪框右下角 y 坐标(取值范围:[0, 1])
  77. public $signature; //简介
  78. public $categories;
  79. public $cate_first;
  80. public $cate_second;
  81. public $user_version; //代码版本号
  82. public $user_desc = "小程序新版本上传"; //代码描述
  83. public $is_fast_audit = 0;
  84. public $privacy_ver = 2; //用户隐私保护指引的版本,1表示现网版本;2表示开发版。默认是2开发版。
  85. public $owner_setting;
  86. public $setting_list;
  87. public $file_url;
  88. public $file;
  89. public $mini_id;
  90. public $mini_path;
  91. public $item_list;
  92. public $feedback_info;
  93. public $feedback_stuff;
  94. public $is_use;
  95. public $license_pic;
  96. public $wechatId;
  97. public $status;
  98. public $merchant_id = 0;
  99. public $store_id;
  100. public $bind_store_id;
  101. public $start_time;
  102. public $end_time;
  103. public function rules()
  104. {
  105. return [
  106. [[
  107. 'name', 'code', 'legal_persona_wechat', 'legal_persona_name', 'component_phone', 'authorization_code', "action", "requestdomain", "wsrequestdomain", "uploaddomain", "downloaddomain", "udpdomain", "tcpdomai", "webviewdomai", "head_img_media_id", "x1", "y1", "x2", "y2", 'signature', 'certicates', 'media_image', 'mini_path', "feedback_info", 'is_use', 'wechatId', 'userstr', 'status', 'license_pic'
  108. , 'start_time', 'end_time'], 'string'],
  109. [['code_type', 'mini_id', 'merchant_id', 'store_id', 'bind_store_id', 'fuwu_type'], 'integer'],
  110. [['file'], 'file'],
  111. [['item_list', 'feedback_stuff'], 'array']
  112. ];
  113. }
  114. public function __construct($config = [])
  115. {
  116. parent::__construct($config);
  117. $this->store_id = $this->store_id ?: get_store_id();
  118. $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
  119. if (empty($verify_ticket)) {
  120. $this->openPlatform = null;
  121. }else {
  122. $this->config = [
  123. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  124. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  125. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  126. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  127. ];
  128. $this->config['http'] = [
  129. 'max_retries' => 0,
  130. 'retry_delay' => 500,
  131. 'timeout' => 50.0,
  132. 'read_timeout' => 50,
  133. // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  134. ];
  135. $this->openPlatform = Factory::openPlatform($this->config);
  136. }
  137. }
  138. public function is_trade_managed($app) {
  139. if (!$app) {
  140. return;
  141. }
  142. $appid = $app->getConfig()['app_id'];
  143. $key = 'is_trade_managed' . (string)$appid;
  144. $cache = cache()->get($key);
  145. if($cache > 0){
  146. return true;
  147. }
  148. if($cache < 0){
  149. return false;
  150. }
  151. $postData = [
  152. 'appid' => (string)$appid,
  153. ];
  154. $client = new BaseClient($app);
  155. $result = $client->httpPostJson('wxa/sec/order/is_trade_managed', $postData);
  156. if (!empty($result)) {
  157. if ($result['errcode'] == '0') {
  158. if($result['is_trade_managed']){
  159. cache()->set($key, 1, 86400);
  160. return true;
  161. }else{
  162. cache()->set($key, -1, 86400);
  163. }
  164. }else{
  165. cache()->set($key, -1, 86400);
  166. }
  167. \Yii::error(['--------is_trade_managed-error---- ', $result]);
  168. }
  169. return false;
  170. }
  171. public function storeWxOrderShipping($store_id) {
  172. try {
  173. Wechat::init($store_id);
  174. //$app = Wechat::$wechat_mini;
  175. $app = WechatMini::getWechatConfig($store_id);
  176. if (!$app) {
  177. return;
  178. }
  179. $is_trade_managed = $this->is_trade_managed($app);
  180. if(!$is_trade_managed){
  181. // \Yii::error(['--------$is_trade_managed=false---- ', $store_id]);
  182. return;
  183. }
  184. //收货
  185. $query = Order::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'pay_type' => Order::PAY_TYPE_WECHAT, 'is_pay' => 1]);
  186. $query->andWhere(['trade_status' => Order::ORDER_FLOW_SEND]);
  187. $orders = $query->limit(50)->all();
  188. // debug_log([$store_id, count($orders)], __CLASS__);
  189. foreach ($orders as $order) {
  190. if($this->wxOrderStateIsConfirm($app, $order)){
  191. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  192. $order->confirm_time = time();
  193. $order->save();
  194. }
  195. }
  196. //发货
  197. $wxOrderGetNoShippingList = $this->wxOrderGetNoShippingList($app);
  198. // debug_log([$store_id, $wxOrderGetNoShippingList], __CLASS__);
  199. if($wxOrderGetNoShippingList['code'] != 0){
  200. return;
  201. }
  202. if(empty($wxOrderGetNoShippingList['data']['order_list'])){
  203. return;
  204. }
  205. $max = 300;
  206. foreach($wxOrderGetNoShippingList['data']['order_list'] as $item){
  207. $order_no = (string)$item['merchant_trade_no'];
  208. $order = Order::findOne(['order_no' => $order_no]);
  209. //商城订单需要看发货状态,其他订单直接发货
  210. if($order && !$order['send_time']){
  211. continue;
  212. }
  213. //有些订单会延时写order表,所以支付后3分钟再处理发货逻辑
  214. if(!$order && ($item['pay_time'] > (time() - 60 * 3))){
  215. continue;
  216. }
  217. //拼团订单,没写order表时,跳出
  218. if((!$order) && \app\utils\OrderNo::checkOrderNo($order_no, \app\utils\OrderNo::ORDER_PT)){
  219. continue;
  220. }
  221. $client = new BaseClient($app);
  222. $postData = [
  223. 'order_key' => [
  224. 'order_number_type' => 2,
  225. 'transaction_id' => (string)$item['transaction_id'],
  226. ],
  227. 'logistics_type' => Order::getWxOrderLocalType($store_id, (string)$item['merchant_trade_no'], $order),
  228. 'delivery_mode' => 1,
  229. 'shipping_list' => [
  230. [
  231. 'item_desc' => '微信支付',
  232. ],
  233. ],
  234. 'upload_time' => date(DATE_RFC3339),
  235. 'payer' => [
  236. 'openid' => (string)$item['openid'],
  237. ],
  238. ];
  239. //快递发货
  240. if($postData['logistics_type'] == 1){
  241. $tracking_no = $order['express_no'] ?: $order['order_no'];
  242. $express_company = \app\models\Express::getExpressCode($order['express']);
  243. $receiver_contact = substr_replace($order['mobile'], '****', 3, 4);
  244. $postData['shipping_list'] = [
  245. [
  246. 'item_desc' => '微信支付',
  247. 'tracking_no' => $tracking_no,
  248. 'express_company' => $express_company,
  249. 'contact' => [
  250. 'receiver_contact' => $receiver_contact,
  251. ],
  252. ]
  253. ];
  254. }
  255. $result = $client->httpPostJson('wxa/sec/order/upload_shipping_info', $postData);
  256. \Yii::error([$result, json_encode($postData)]);
  257. if($max-- <= 0){
  258. break;
  259. }
  260. }
  261. } catch (\Exception $e) {
  262. }
  263. }
  264. //订单是否已收货状态
  265. public function wxOrderStateIsConfirm($app, $order) {
  266. if(!$order){
  267. return false;
  268. }
  269. if ($order['pay_time'] > 0 && $order['pay_time'] < strtotime('-30 DAY')) {
  270. return true;
  271. }
  272. $orderRes = $this->wxOrderGet($app, $order['transaction_id']);
  273. if(!empty($orderRes['data']) && !empty($orderRes['data']['order'])){
  274. return in_array($orderRes['data']['order']['order_state'], [3, 4]);
  275. }
  276. return false;
  277. }
  278. //查询订单
  279. public function wxOrderGet($app, $transaction_id) {
  280. try {
  281. $postData = [
  282. 'transaction_id' => (string)$transaction_id,
  283. ];
  284. $client = new BaseClient($app);
  285. $result = $client->httpPostJson('wxa/sec/order/get_order', $postData);
  286. if (!empty($result)) {
  287. if ($result['errcode'] == '0') {
  288. return [
  289. 'code' => 0,
  290. 'msg' => "获取成功",
  291. 'data' => $result,
  292. ];
  293. }
  294. if ($result['errcode'] == "10060001") {//支付单不存在
  295. return [
  296. 'code' => 0,
  297. 'msg' => "获取成功",
  298. 'data' => [
  299. 'order' => [
  300. 'order_state' => 4
  301. ]
  302. ],
  303. ];
  304. }
  305. }
  306. \Yii::warning(['--------wxOrderGet-error---- ', $result]);
  307. return [
  308. 'code' => 1,
  309. 'msg' => "获取失败" . $result['errmsg'],
  310. 'data' => $result,
  311. ];
  312. } catch(\Exception $e) {
  313. \Yii::warning(['--------wxOrderGet-error---- ', $e]);
  314. }
  315. return [
  316. 'code' => 1,
  317. 'msg' => "获取失败",
  318. ];
  319. }
  320. //查询订单
  321. public function wxOrderGetNoShippingList($app, $last_index = null) {
  322. try {
  323. $postData = [
  324. 'order_state' => 1,
  325. ];
  326. $last_index && $postData['last_index'] = $last_index;
  327. $client = new BaseClient($app);
  328. $result = $client->httpPostJson('wxa/sec/order/get_order_list', $postData);
  329. if (!empty($result)) {
  330. if ($result['errcode'] == '0') {
  331. if($result['has_more']){
  332. $more = $this->wxOrderGetNoShippingList($app, $result['last_index']);
  333. if(!$more['code'] && $more['data']['order_list']){
  334. $result['order_list'] = array_merge($result['order_list'], $more['data']['order_list']);
  335. }
  336. }
  337. return [
  338. 'code' => 0,
  339. 'msg' => "获取成功",
  340. 'data' => $result,
  341. ];
  342. }
  343. }
  344. \Yii::warning(['--------wxOrderGet-error---- ', $result]);
  345. return [
  346. 'code' => 1,
  347. 'msg' => "获取失败" . $result['errmsg'],
  348. 'data' => $result,
  349. ];
  350. } catch(\Exception $e) {
  351. \Yii::warning(['--------wxOrderGet-error---- ', $e]);
  352. }
  353. return [
  354. 'code' => 1,
  355. 'msg' => "获取失败",
  356. ];
  357. }
  358. /**
  359. * mini业务
  360. */
  361. public function miniProgram($appid = "")
  362. {
  363. $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : $this->store_id;
  364. $store = StoreMini::find()->where(['appid' => $appid, 'store_id' => $store_id ?: get_store_id()])->one();
  365. if (empty($store)) {
  366. $store = StoreMini::find()->where(['or', ['id' => $this->mini_id], ['store_id' => $store_id ?: get_store_id()]])->orderBy("id desc")->one();
  367. }
  368. return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
  369. }
  370. /**
  371. * 获取授权方的帐号基本信息
  372. */
  373. public function api_get_authorizer_info()
  374. {
  375. $t = \Yii::$app->db->beginTransaction();
  376. try {
  377. $store = StoreMini::findOne($this->mini_id);
  378. if (empty($store->appid)) {
  379. throw new \Exception("当前没有绑定小程序信息");
  380. }
  381. if (!$this->openPlatform) {
  382. throw new \Exception("参数配置错误");
  383. }
  384. $res = $this->openPlatform->getAuthorizer($store->appid);
  385. if (empty($res->errcode) && !empty($res)) {
  386. $store = StoreMini::find()->where(['store_id' => $this->store_id])->one();
  387. if (!empty($res->authorizer_info->qrcode_url)) {
  388. $store->mini_url = $res->authorizer_info->qrcode_url;
  389. $store->save();
  390. }
  391. $t->commit();
  392. return [
  393. 'code' => 0,
  394. 'msg' => "获取成功",
  395. 'data' => $res
  396. ];
  397. } elseif (empty($res)) {
  398. throw new \Exception("数据错误");
  399. } else {
  400. throw new \Exception($res->errmsg);
  401. }
  402. } catch (\Exception $e) {
  403. $t->rollBack();
  404. return [
  405. 'code' => 1,
  406. 'msg' => $e->getMessage()
  407. ];
  408. }
  409. }
  410. /**
  411. * 快速注册小程序
  412. */
  413. public function fastRegisterMini($refresh = 0)
  414. {
  415. $t = \Yii::$app->db->beginTransaction();
  416. try {
  417. $data = [
  418. 'name' => $this->name,
  419. 'code' => $this->code,
  420. 'code_type' => $this->code_type,
  421. 'legal_persona_wechat' => $this->legal_persona_wechat,
  422. 'legal_persona_name' => $this->legal_persona_name,
  423. 'component_phone' => Option::get("platform_phone", 0, 'saas')['value']
  424. ];
  425. if (!empty($this->bind_store_id)) {
  426. $store_id = $this->bind_store_id;
  427. } else {
  428. $store_id = $this->store_id;
  429. }
  430. if(!$refresh){
  431. $store = StoreMini::find()->where(['AND',
  432. ['code' => $this->code, 'status' => -1, 'store_id' => $store_id],
  433. ['IS', 'appid', NULL]
  434. ])->one();
  435. }
  436. if ($refresh || empty($store)) {
  437. $store = new StoreMini();
  438. $store->store_id = $store_id;
  439. }
  440. $store->name = $this->name;
  441. $store->code = $this->code;
  442. $store->code_type = $this->code_type * 1;
  443. $store->legal_persona_wechat = $this->legal_persona_wechat;
  444. $store->legal_persona_name = $this->legal_persona_name;
  445. $store->license_pic = $this->license_pic;
  446. $store->merchant_id = $this->merchant_id;
  447. if (!$this->openPlatform) {
  448. throw new \Exception("参数配置错误");
  449. }
  450. $res = $this->openPlatform->component->registerMiniProgram($data);
  451. // if ($res['errcode'] == '89249') {
  452. // debug_log([__METHOD__, $res], __CLASS__ . '.log');
  453. // $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name);
  454. // if ($res['errcode'] == 89251) {
  455. // debug_log([__METHOD__, $res], __CLASS__ . '.log');
  456. // $store->status = -1;
  457. // $store->save();
  458. // }
  459. // return [
  460. // 'code' => $res['errcode'],
  461. // 'msg' => $this->getZnMsg($res),
  462. // ];
  463. // }
  464. if (empty($res['errcode']) && !empty($res)) {
  465. $store->status = -1;
  466. $store->save();
  467. $t->commit();
  468. return [
  469. 'code' => 0,
  470. 'msg' => "申请成功,请等待审核",
  471. 'data' => $store->id
  472. ];
  473. } elseif (empty($res)) {
  474. $store->save();
  475. return [
  476. 'code' => 1,
  477. 'msg' => "数据错误"
  478. ];
  479. } else {
  480. $store->save();
  481. throw new \Exception($this->getZnMsg($res));
  482. }
  483. } catch (\Exception $e) {
  484. $t->rollBack();
  485. return [
  486. 'code' => 1,
  487. 'msg' => $e->getMessage(),
  488. ];
  489. }
  490. // $weChatUrl = $this->base_url."component/fastregisterweapp?action=create&component_access_token=".$this->data['component_access_token'];
  491. }
  492. /**
  493. * 手动查询快速创建任务状态
  494. */
  495. public function getRegisterStatus($id = 0)
  496. {
  497. try {
  498. if (!empty($this->bind_store_id)) {
  499. $store_id = $this->bind_store_id;
  500. } else {
  501. $store_id = $this->store_id;
  502. }
  503. if($id){
  504. $store = StoreMini::findOne($id);
  505. }else{
  506. $store = StoreMini::findOne(['store_id' => $store_id]);
  507. }
  508. if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) {
  509. if (!$this->openPlatform) {
  510. throw new \Exception("参数配置错误");
  511. }
  512. $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name);
  513. \Yii::error($res);
  514. return $res;
  515. }
  516. } catch (\Exception $e) {
  517. return [
  518. 'code' => 1,
  519. 'msg' => $e->getMessage(),
  520. ];
  521. }
  522. }
  523. /**
  524. * 返回授权需要的参数
  525. */
  526. public function backAuthInfo()
  527. {
  528. try {
  529. if (!$this->openPlatform) {
  530. throw new \Exception("参数配置错误");
  531. }
  532. $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]);
  533. return [
  534. 'code' => 0,
  535. 'msg' => "请访问链接依次来授权",
  536. 'url' => $res
  537. ];
  538. } catch (\Exception $e) {
  539. return [
  540. 'code' => 1,
  541. 'msg' => $e->getMessage()
  542. ];
  543. }
  544. }
  545. /**
  546. * 存储授权码
  547. */
  548. // public function setAuthorization_code(){
  549. // try {
  550. // $store = new StoreMini();
  551. // $store->store_id = get_store_id();
  552. // $store->auth_code = json_encode([
  553. // 'authorization_code'=>$this->authorization_code,
  554. // 'end_time'=>time()*1 + 3600*1
  555. // ]);
  556. // $store->save();
  557. // return $this->getAuthorization_info($this->authorization_code);
  558. // }catch (\Exception $e){
  559. // return [
  560. // 'code'=>1,
  561. // 'msg'=>$e->getMessage()
  562. // ];
  563. // }
  564. //
  565. //
  566. // }
  567. public function getAuthorizers()
  568. {
  569. $res = $this->openPlatform->getAuthorizers();
  570. return [
  571. 'code' => 0,
  572. 'data' => $res,
  573. ];
  574. }
  575. /**
  576. * 使用授权码获取授权信息
  577. */
  578. public function getAuthorization_info($authorization_code)
  579. {
  580. try {
  581. if (!$this->openPlatform) {
  582. throw new \Exception("参数配置错误");
  583. }
  584. $res = $this->openPlatform->handleAuthorize($authorization_code);
  585. \Yii::error($res);
  586. if (empty($res['errcode']) && !empty($res)) {
  587. $store_id = $this->store_id;
  588. if ($store_id * 1 > 0 || !empty($this->mini_id)) {
  589. $store = StoreMini::find()->where(['appid' => $res['authorization_info']['authorizer_appid'], 'store_id' => $store_id])->one();
  590. if (empty($store)) {
  591. $store = new StoreMini();
  592. $store->store_id = $store_id;
  593. }
  594. $store->appid = $res['authorization_info']['authorizer_appid'];
  595. $store->is_cancle = 0;
  596. $store->status = 1;
  597. $store->auth_code = json_encode([
  598. 'authorization_code' => $authorization_code,
  599. 'end_time' => time() * 1 + 3600 * 1
  600. ]);
  601. $store->authorizer_access_token = json_encode([
  602. 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
  603. 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
  604. ]);
  605. $store->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
  606. $result = $store->save();
  607. \Yii::error($result);
  608. if ($result) {
  609. return $this->getMiniInfo($res['authorization_info']['authorizer_access_token'], $res['authorization_info']['authorizer_appid']);
  610. }
  611. }
  612. } else {
  613. throw new \Exception($this->getZnMsg($res));
  614. }
  615. } catch (\Exception $e) {
  616. return [
  617. 'code' => 1,
  618. 'msg' => $e->getMessage(),
  619. ];
  620. }
  621. }
  622. /**
  623. * 获取小程序基础信息
  624. */
  625. public function getMiniInfo($authorizer_access_token = "", $appid = "")
  626. {
  627. $t = \Yii::$app->db->beginTransaction();
  628. try {
  629. $store_id = $this->store_id;
  630. $store = StoreMini::findOne(['appid' => $appid, 'store_id' => $store_id]);
  631. if (empty($authorizer_access_token) && empty($store)) {
  632. throw new \Exception("暂未授权");
  633. }
  634. if (!$this->openPlatform) {
  635. throw new \Exception("参数配置错误");
  636. }
  637. $miniProgram = $this->miniProgram($appid);
  638. $res = $miniProgram->account->getBasicInfo();
  639. if (empty($res['errcode']) && !empty($res)) {
  640. $store->mini_info = json_encode($res, JSON_UNESCAPED_UNICODE);
  641. if ($res['nickname']) {
  642. $store->mini_nickname = $res['nickname'];
  643. }
  644. if ($res['head_image_info']['head_image_url']) {
  645. $store->mini_url = $res['head_image_info']['head_image_url'];
  646. }
  647. $store->signature = $res['signature_info']['signature'];
  648. //$store->mini_url = $res['head_image_info']['head_image_url'];
  649. $saveRes = $store->save();
  650. if ($saveRes) {
  651. switch ($res['account_type']) {
  652. case 1:
  653. $res['account_type'] = "订阅号";
  654. break;
  655. case 2:
  656. $res['account_type'] = "服务号";
  657. break;
  658. case 3:
  659. $res['account_type'] = "小程序";
  660. break;
  661. }
  662. $t->commit();
  663. //将Appid填充系统设置
  664. if ((int)$store->is_use === 1) {
  665. $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
  666. if (empty($WechatConfig)) {
  667. $WechatConfig = new WechatConfig();
  668. $WechatConfig->store_id = $store_id;
  669. $WechatConfig->type = 1;
  670. }
  671. $WechatConfig->app_id = $store->appid;
  672. $WechatConfig->app_secret = "0";
  673. // $WechatConfig->name = ($res['principal_name'] ?: $store->name) ?: '';
  674. $WechatConfig->updated_at = time();
  675. if (!$WechatConfig->save()) {
  676. throw new \Exception(json_encode($WechatConfig->errors));
  677. }
  678. }
  679. return [
  680. 'code' => 0,
  681. 'msg' => "获取成功",
  682. 'data' => $res,
  683. 'appid' => $appid,
  684. 'store_mini' => $store,
  685. ];
  686. }
  687. throw new \Exception(json_encode($store->errors));
  688. } else {
  689. $t->rollBack();
  690. return [
  691. 'code' => $res['errcode'],
  692. 'msg' => $this->getZnMsg($res),
  693. ];
  694. }
  695. } catch (\Exception $e) {
  696. return [
  697. 'code' => 1,
  698. 'msg' => $e->getMessage(),
  699. ];
  700. }
  701. }
  702. /**
  703. * 设置获取修改服务器域名
  704. */
  705. public function setDomainName()
  706. {
  707. $t = \Yii::$app->db->beginTransaction();
  708. try {
  709. if (!empty($this->bind_store_id)) {
  710. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  711. } else {
  712. $store = StoreMini::findOne($this->mini_id);
  713. }
  714. if (empty($store)) {
  715. throw new \Exception("数据错误,获取小程序信息失败");
  716. }
  717. $res = $this->setWebviewDomainName();
  718. if ($res['code'] == 1) {
  719. throw new \Exception($res['msg']);
  720. }
  721. $DomainData = $res;
  722. if (!$this->openPlatform) {
  723. throw new \Exception("参数配置错误");
  724. }
  725. $webview_domain_name = $store->webview_domain_name;
  726. $requestdomain = [\Yii::$app->request->hostName];
  727. if (!empty($webview_domain_name)) {
  728. $webview_domain_name = json_decode($webview_domain_name, true);
  729. if (!empty($webview_domain_name)) {
  730. foreach ($webview_domain_name as $url_item) {
  731. if (!empty($url_item['url'])) {
  732. $index = strstr($url_item['url'], 'https');
  733. if ($index) {
  734. $url_item['url'] = \str_replace('https://', '', $url_item['url']);
  735. } else {
  736. $index = strstr($url_item['url'], 'http');
  737. if ($index) {
  738. $url_item['url'] = \str_replace('http://', '', $url_item['url']);
  739. }
  740. }
  741. array_push($requestdomain, $url_item['url']);
  742. }
  743. }
  744. }
  745. }
  746. $uploaddomain = $downloaddomain = $udpdomain = $tcpdomain = $wsrequestdomain = $requestdomain;
  747. array_walk($requestdomain, function(&$item) {
  748. $item = "https://" . $item;
  749. });
  750. array_walk($wsrequestdomain, function(&$item) {
  751. $item = "wss://" . $item;
  752. });
  753. array_walk($uploaddomain, function(&$item) {
  754. $item = "https://" . $item;
  755. });
  756. array_walk($downloaddomain, function(&$item) {
  757. $item = "https://" . $item;
  758. });
  759. array_walk($udpdomain, function(&$item) {
  760. $item = "udp://" . $item;
  761. });
  762. array_walk($tcpdomain, function(&$item) {
  763. $item = "tcp://" . $item;
  764. });
  765. $miniProgram = $this->miniProgram($store->appid);
  766. $data = [
  767. 'action' => 'set',
  768. 'requestdomain' => $requestdomain,
  769. "wsrequestdomain" => $wsrequestdomain,
  770. "uploaddomain" => $uploaddomain,
  771. "downloaddomain" => $downloaddomain,
  772. "udpdomain" => $udpdomain,
  773. "tcpdomain" => $tcpdomain
  774. ];
  775. $res = $miniProgram->domain->modify($data);
  776. if (empty($res['errcode']) && !empty($res)) {
  777. $t->commit();
  778. return [
  779. 'code' => 0,
  780. 'msg' => "设置成功",
  781. 'data' => $res,
  782. 'DomainData' => $DomainData
  783. ];
  784. } elseif (empty($res)) {
  785. throw new \Exception("数据错误");
  786. } else {
  787. throw new \Exception($this->getZnMsg($res));
  788. }
  789. } catch (\Exception $e) {
  790. $t->rollBack();
  791. return [
  792. 'code' => 1,
  793. 'msg' => '服务器域名错误' . $e->getMessage(),
  794. ];
  795. }
  796. }
  797. /**
  798. * 下载校验文件并提交至客服系统
  799. * @param mixed $app
  800. * @return array
  801. * @throws \Exception
  802. * @date 2023-02-20
  803. */
  804. public function addConfirmFile($app)
  805. {
  806. try {
  807. $access_token = $app->access_token->getToken()['authorizer_access_token'];
  808. $res = http_post('https://api.weixin.qq.com/wxa/get_webviewdomain_confirmfile?access_token=' . $access_token, [
  809. 'body' => '{}'
  810. ]);
  811. if ($res->getStatusCode() != 200) {
  812. throw new \Exception('下载校验文件出错');
  813. }
  814. $result = \json_decode((string)$res->getBody(), true);
  815. if ($result['errcode'] != 0) {
  816. throw new \Exception($result['errmsg']);
  817. }
  818. return [
  819. 'code' => 0,
  820. 'msg' => '',
  821. 'data' => $result
  822. ];
  823. } catch (\Exception $e) {
  824. return [
  825. 'code' => 1,
  826. 'msg' => $e->getMessage()
  827. ];
  828. }
  829. }
  830. public function addFile2Ws($result) {
  831. try {
  832. $res = http_post('https://ws2.cyyvip.com/web/index.php?r=common/default/add-confirm-file', [
  833. 'form_params' => [
  834. 'token' => 'tianxin100',
  835. 'file_name' => $result['file_name'],
  836. 'file_content' => $result['file_content'],
  837. ]
  838. ]);
  839. if ($res->getStatusCode() != 200) {
  840. throw new \Exception('添加校验文件出错');
  841. }
  842. return [
  843. 'code' => 0,
  844. 'msg' => '完成'
  845. ];
  846. } catch (\Exception $e) {
  847. return [
  848. 'code' => 1,
  849. 'msg' => $e->getMessage()
  850. ];
  851. }
  852. }
  853. public function addFile2Other($result) {
  854. try {
  855. if (!is_dir(\Yii::$app->runtimePath . '/store/mini/')) {
  856. mkdir(\Yii::$app->runtimePath . '/store/mini/', 0777, true);
  857. }
  858. $file_name = \Yii::$app->runtimePath . '/store/mini/' . $result['file_name'];
  859. file_put_contents($file_name, $result['file_content']);
  860. return [
  861. 'code' => 0,
  862. 'msg' => '完成',
  863. 'data' => [
  864. 'file' => 'https://' . \Yii::$app->request->hostName . '/runtime/store/mini/' . $result['file_name']
  865. ]
  866. ];
  867. } catch (\Exception $e) {
  868. return [
  869. 'code' => 1,
  870. 'msg' => $e->getMessage()
  871. ];
  872. }
  873. }
  874. /**
  875. * @return array
  876. * 设置业务域名
  877. * @throws \GuzzleHttp\Exception\GuzzleException
  878. */
  879. public function setWebviewDomainName()
  880. {
  881. $t = \Yii::$app->db->beginTransaction();
  882. try {
  883. if (!empty($this->bind_store_id)) {
  884. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  885. } else {
  886. $store = StoreMini::findOne($this->mini_id);
  887. }
  888. if (empty($store)) {
  889. throw new \Exception("数据错误,获取小程序信息失败");
  890. }
  891. if (!$this->openPlatform) {
  892. throw new \Exception("参数配置错误");
  893. }
  894. $miniProgram = $this->miniProgram($store->appid);
  895. $result = $this->addConfirmFile($miniProgram);
  896. if ($result['code'] === 1) {
  897. return $result;
  898. }
  899. $result_ws = $this->addFile2Ws($result['data']);
  900. if ($result_ws['code'] === 1) {
  901. return $result_ws;
  902. }
  903. $webview_domain_name = $store->webview_domain_name;
  904. $url_arr = [];
  905. if (!empty($webview_domain_name)) {
  906. $webview_domain_name = json_decode($webview_domain_name, true);
  907. if (!empty($webview_domain_name)) {
  908. foreach ($webview_domain_name as $url_item) {
  909. if (!empty($url_item['url'])) {
  910. array_push($url_arr, $url_item['url']);
  911. }
  912. }
  913. }
  914. }
  915. $res = $miniProgram->domain->setWebviewDomain($url_arr, 'set');
  916. if (empty($res['errcode']) && !empty($res)) {
  917. $store->is_set_web = 1;
  918. $store->save();
  919. $t->commit();
  920. return [
  921. 'code' => 0,
  922. 'msg' => "设置成功",
  923. 'data' => $res
  924. ];
  925. } elseif (empty($res)) {
  926. throw new \Exception("数据错误");
  927. } else {
  928. throw new \Exception($this->getZnMsg($res));
  929. }
  930. } catch (\Exception $e) {
  931. $t->rollBack();
  932. return [
  933. 'code' => 1,
  934. 'msg' => '业务域名错误' . $e->getMessage()
  935. ];
  936. }
  937. }
  938. /**
  939. * 暂停/开始使用
  940. */
  941. public function unbind()
  942. {
  943. try {
  944. if (!empty($this->bind_store_id)) {
  945. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  946. } else {
  947. $store = StoreMini::findOne($this->mini_id);
  948. }
  949. if (empty($store)) {
  950. throw new \Exception("数据错误,获取小程序信息失败");
  951. }
  952. if ($this->is_use == 1 || empty($this->is_use)) {
  953. StoreMini::updateAll(['is_use' => 2], ['store_id' => $this->store_id]);
  954. $store->is_use = 1;
  955. } elseif ($this->is_use == 2) {
  956. $store->is_use = 2;
  957. }
  958. $res = $store->save();
  959. if ($res) {
  960. //设置店铺的appid
  961. if($store->is_use == 1){
  962. $wechat = WechatConfig::find()->where(['store_id'=>$this->store_id,'type'=>1,'is_delete'=>0])->one();
  963. if (!$wechat) {
  964. $wechat = new WechatConfig();
  965. $wechat->store_id = $this->store_id;
  966. $wechat->type = 1;
  967. }
  968. $wechat->app_id = $store->appid;
  969. $wechat->save();
  970. }
  971. //$store->save();
  972. return [
  973. 'code' => 0,
  974. 'msg' => "设置成功",
  975. 'data' => $res
  976. ];
  977. } else {
  978. throw new \Exception("数据库添加失败");
  979. }
  980. } catch (\Exception $e) {
  981. return [
  982. 'code' => 1,
  983. 'msg' => $e->getMessage()
  984. ];
  985. }
  986. }
  987. /**
  988. * 注:前端提交微信临时素材接口,获取素材ID
  989. * 设置小程序名称
  990. */
  991. public function setMiniNianname()
  992. {
  993. $t = \Yii::$app->db->beginTransaction();
  994. try {
  995. if (!empty($this->bind_store_id)) {
  996. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  997. } else {
  998. $store = StoreMini::findOne($this->mini_id);
  999. }
  1000. if (empty($store)) {
  1001. throw new \Exception("数据错误,获取小程序信息失败");
  1002. }
  1003. if (!$this->openPlatform) {
  1004. throw new \Exception("参数配置错误");
  1005. }
  1006. $miniProgram = $this->miniProgram($store->appid);
  1007. $media = new Client($miniProgram);
  1008. $data = [
  1009. 'id_card' => $this->id_card ? $this->id_card : '',
  1010. 'license' => $this->license ? $this->license : $store->license_pic,
  1011. 'naming_other_stuff_1' => $this->naming_other_stuff_1,
  1012. 'naming_other_stuff_2' => $this->naming_other_stuff_2,
  1013. 'naming_other_stuff_3' => $this->naming_other_stuff_3,
  1014. 'naming_other_stuff_4' => $this->naming_other_stuff_4,
  1015. 'naming_other_stuff_5' => $this->naming_other_stuff_5,
  1016. ];
  1017. $store->apply_mini_info = json_encode($data);
  1018. // $this->addMedia($data);
  1019. foreach ($data as $index => $item) {
  1020. if (!empty($item)) {
  1021. $head_img_media_id = $media->uploadImage($this->saveTempImage($item));
  1022. if (empty($head_img_media_id['media_id'])) {
  1023. throw new \Exception($this->getZnMsg($head_img_media_id));
  1024. }
  1025. $data[$index] = $head_img_media_id['media_id'];
  1026. }
  1027. }
  1028. $id_card = $data['id_card'] ?? "";
  1029. $license = $data['license'] ?? "";
  1030. $res = $miniProgram->setting->setNickname($this->mini_nickname, $id_card, $license, [
  1031. $data['naming_other_stuff_1'],
  1032. $data['naming_other_stuff_2'],
  1033. $data['naming_other_stuff_3'],
  1034. $data['naming_other_stuff_4'],
  1035. $data['naming_other_stuff_5']
  1036. ]);
  1037. //self::addCategory();
  1038. $store->mini_nickname = $this->mini_nickname;
  1039. if (empty($res['errcode']) && !empty($res)) {
  1040. $store->nickname_audit_id = $res['audit_id'];
  1041. $store->apply_name_status = 1;
  1042. if (empty($res['audit_id'])) {
  1043. $store->apply_name_status = 0;
  1044. }
  1045. $store->save();
  1046. $t->commit();
  1047. return [
  1048. 'code' => 0,
  1049. 'msg' => "开始审核",
  1050. 'data' => $res
  1051. ];
  1052. } elseif (empty($res)) {
  1053. $store->save();
  1054. throw new \Exception("数据错误");
  1055. } else {
  1056. if ($res['errcode'] == 91019) {
  1057. $store->apply_name_status = 1;
  1058. $store->save();
  1059. $t->commit();
  1060. return [
  1061. 'code' => 0,
  1062. 'msg' => "小程序审核中"
  1063. ];
  1064. }
  1065. throw new \Exception($this->getZnMsg($res).'|'.$res['errcode']);
  1066. }
  1067. } catch (\Exception $e) {
  1068. $t->rollBack();
  1069. return [
  1070. 'code' => 1,
  1071. 'msg' => $e->getMessage()
  1072. ];
  1073. }
  1074. }
  1075. /**
  1076. * 查询名称审核状态
  1077. */
  1078. public function getNicknameAuditStatus()
  1079. {
  1080. $t = \Yii::$app->db->beginTransaction();
  1081. try {
  1082. if (!empty($this->bind_store_id)) {
  1083. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1084. } else {
  1085. $store = StoreMini::findOne($this->mini_id);
  1086. }
  1087. if (empty($store) || empty($store->nickname_audit_id)) {
  1088. $t->commit();
  1089. return [
  1090. 'code' => 0,
  1091. 'msg' => '数据错误或审核已经通过',
  1092. 'status' => 3
  1093. ];
  1094. }
  1095. if (!$this->openPlatform) {
  1096. throw new \Exception("参数配置错误");
  1097. }
  1098. $miniProgram = $this->miniProgram($store->appid);
  1099. $result = $miniProgram->setting->getNicknameAuditStatus($store->nickname_audit_id);
  1100. if (empty($result['errcode']) && !empty($result)) {
  1101. $msg = "获取成功";
  1102. switch ($result['audit_stat']) {
  1103. case 0:
  1104. $msg = "名称审核中";
  1105. $store->apply_name_status = 1;
  1106. break;
  1107. case 1:
  1108. $msg = "名称审核完成";
  1109. $store->apply_name_status = 3;
  1110. break;
  1111. case 2:
  1112. $msg = "名称审核失败";
  1113. $store->apply_name_status = 2;
  1114. $store->apply_name_error = $result['fail_reason'];
  1115. break;
  1116. }
  1117. if (!$store->save()) {
  1118. throw new \Exception("保存失败");
  1119. }
  1120. $t->commit();
  1121. return [
  1122. 'code' => 0,
  1123. 'msg' => $msg,
  1124. 'status' => $store->apply_name_status
  1125. ];
  1126. }
  1127. throw new \Exception("查询失败");
  1128. } catch (\Exception $e) {
  1129. $t->rollBack();
  1130. return [
  1131. 'code' => 1,
  1132. 'msg' => $e->getMessage()
  1133. ];
  1134. }
  1135. }
  1136. /**
  1137. * 增加Media事件
  1138. */
  1139. public function addMedia($data)
  1140. {
  1141. foreach ($data as $index => $item) {
  1142. $mediaForm = StoreMedia::find()->where(['media_id' => $item])->one();
  1143. if (empty($mediaForm)) {
  1144. $mediaForm = new StoreMedia();
  1145. $mediaForm->store_id = $this->store_id;
  1146. $mediaForm->media_id = $item;
  1147. $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
  1148. }
  1149. $mediaForm->event = $index;
  1150. $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
  1151. switch ($index) {
  1152. case 'id_card':
  1153. $mediaForm->desc = "身份证照片";
  1154. break;
  1155. case 'license':
  1156. $mediaForm->desc = "组织机构代码证或营业执照";
  1157. break;
  1158. case 'naming_other_stuff_1':
  1159. $mediaForm->desc = "其他证明材料1";
  1160. break;
  1161. case 'naming_other_stuff_2':
  1162. $mediaForm->desc = "其他证明材料2";
  1163. break;
  1164. case 'naming_other_stuff_3':
  1165. $mediaForm->desc = "其他证明材料3";
  1166. break;
  1167. case 'naming_other_stuff_4':
  1168. $mediaForm->desc = "其他证明材料4";
  1169. break;
  1170. case 'naming_other_stuff_5':
  1171. $mediaForm->desc = "其他证明材料5";
  1172. break;
  1173. case 'head_img_media_id':
  1174. $mediaForm->desc = "头像素材";
  1175. break;
  1176. case "ext_file_media_id":
  1177. $mediaForm->desc = "用户隐私指引";
  1178. }
  1179. $result = $mediaForm->save();
  1180. }
  1181. }
  1182. /**
  1183. * 微信认证名称检测
  1184. */
  1185. public function checkWxVerifyNickname()
  1186. {
  1187. try {
  1188. if (!empty($this->bind_store_id)) {
  1189. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1190. } else {
  1191. $store = StoreMini::findOne($this->mini_id);
  1192. }
  1193. if (empty($store)) {
  1194. return [
  1195. 'code' => 1,
  1196. 'msg' => "数据错误"
  1197. ];
  1198. }
  1199. if (!$this->openPlatform) {
  1200. throw new \Exception("参数配置错误");
  1201. }
  1202. $miniProgram = $this->miniProgram($store->appid);
  1203. $res = $miniProgram->setting->isAvailableNickname($this->mini_nickname);
  1204. if (empty($res['errcode']) && !empty($res)) {
  1205. return [
  1206. 'code' => 0,
  1207. 'msg' => "成功",
  1208. 'data' => $res
  1209. ];
  1210. } elseif (empty($res)) {
  1211. return [
  1212. 'code' => 1,
  1213. 'msg' => "数据错误"
  1214. ];
  1215. } else {
  1216. return [
  1217. 'code' => $res['errcode'],
  1218. 'msg' => $this->getZnMsg($res),
  1219. 'data' => $res
  1220. ];
  1221. }
  1222. } catch (\Exception $e) {
  1223. return [
  1224. 'code' => 1,
  1225. 'msg' => $e->getMessage(),
  1226. ];
  1227. }
  1228. }
  1229. /**
  1230. * 修改头像
  1231. */
  1232. public function setHeadImage()
  1233. {
  1234. $t = \Yii::$app->db->beginTransaction();
  1235. try {
  1236. if (!empty($this->bind_store_id)) {
  1237. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1238. } else {
  1239. $store = StoreMini::findOne($this->mini_id);
  1240. }
  1241. if (empty($store)) {
  1242. throw new \Exception("数据错误,获取小程序信息失败");
  1243. }
  1244. if (!$this->openPlatform) {
  1245. throw new \Exception("参数配置错误");
  1246. }
  1247. $miniProgram = $this->miniProgram($store->appid);
  1248. $media = new Client($miniProgram);
  1249. $head_img_media_id = $media->uploadImage($this->saveTempImage($this->head_img_media_id));
  1250. if (empty($head_img_media_id['media_id'])) {
  1251. throw new \Exception($this->getZnMsg($head_img_media_id));
  1252. }
  1253. \Yii::error($head_img_media_id['thumb_media_id']);
  1254. $res = $miniProgram->account->updateAvatar($head_img_media_id['media_id'], $this->x1, $this->y1, $this->x2, $this->y2);
  1255. if ($res['errcode'] == '53202') {
  1256. $store->mini_url = $this->head_img_media_id;
  1257. $store->save();
  1258. $t->commit();
  1259. return [
  1260. 'code' => 0,
  1261. 'msg' => "设置成功",
  1262. ];
  1263. }
  1264. if (empty($res['errcode']) && !empty($res)) {
  1265. $store->mini_url = $this->head_img_media_id;
  1266. $store->save();
  1267. $t->commit();
  1268. return [
  1269. 'code' => 0,
  1270. 'msg' => "设置成功",
  1271. 'data' => $res
  1272. ];
  1273. } elseif (empty($res)) {
  1274. throw new \Exception("数据错误");
  1275. } else {
  1276. throw new \Exception($this->getZnMsg($res));
  1277. }
  1278. } catch (\Exception $e) {
  1279. $t->rollBack();
  1280. return [
  1281. 'code' => 1,
  1282. 'msg' => $e->getMessage()
  1283. ];
  1284. }
  1285. }
  1286. /**
  1287. * 修改简介
  1288. */
  1289. public function setSignature()
  1290. {
  1291. $t = \Yii::$app->db->beginTransaction();
  1292. try {
  1293. if (!empty($this->bind_store_id)) {
  1294. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1295. } else {
  1296. $store = StoreMini::findOne($this->mini_id);
  1297. }
  1298. if (empty($store)) {
  1299. throw new \Exception("数据错误,获取小程序信息错误");
  1300. }
  1301. if (!$this->openPlatform) {
  1302. throw new \Exception("参数配置错误");
  1303. }
  1304. $miniProgram = $this->miniProgram($store->appid);
  1305. $res = $miniProgram->account->updateSignature($this->signature);
  1306. if ($res['errcode'] == 53200) {
  1307. $store->signature = $this->signature;
  1308. $store->save();
  1309. $t->commit();
  1310. return [
  1311. 'code' => 0,
  1312. 'msg' => "设置成功",
  1313. 'data' => $res
  1314. ];
  1315. }
  1316. if (empty($res['errcode']) && !empty($res)) {
  1317. $store->signature = $this->signature;
  1318. $store->save();
  1319. $t->commit();
  1320. return [
  1321. 'code' => 0,
  1322. 'msg' => "设置成功",
  1323. 'data' => $res
  1324. ];
  1325. } elseif (empty($res)) {
  1326. throw new \Exception("数据错误");
  1327. } else {
  1328. throw new \Exception($this->getZnMsg($res));
  1329. }
  1330. } catch (\Exception $e) {
  1331. $t->rollBack();
  1332. return [
  1333. 'code' => 1,
  1334. 'msg' => $e->getMessage()
  1335. ];
  1336. }
  1337. }
  1338. public function getdata($data, $id = [0])
  1339. {
  1340. foreach ($data as $k => $v) {
  1341. if (in_array($v['id'], $id)) {
  1342. $v['children'] = $this->getdata($data, $v['children']);
  1343. $arr[] = $v;
  1344. }
  1345. }
  1346. return $arr;
  1347. }
  1348. /**
  1349. * @return array
  1350. * 获取全部可选分类
  1351. */
  1352. public function getAllCategories()
  1353. {
  1354. try {
  1355. if (!empty($this->bind_store_id)) {
  1356. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1357. } else {
  1358. $store = StoreMini::findOne($this->mini_id);
  1359. }
  1360. if (empty($store->authorizer_access_token)) {
  1361. return [
  1362. 'code' => 1,
  1363. 'msg' => "没有授权数据",
  1364. 'data' => []
  1365. ];
  1366. }
  1367. if (empty($store)) {
  1368. return [
  1369. 'code' => 1,
  1370. 'msg' => "数据错误"
  1371. ];
  1372. }
  1373. if (!$this->openPlatform) {
  1374. throw new \Exception("参数配置错误");
  1375. }
  1376. $miniProgram = $this->miniProgram($store->appid);
  1377. $res = $miniProgram->setting->getAllCategories();
  1378. if (empty($res['errcode']) && !empty($res)) {
  1379. $data = $res['categories_list']['categories'];
  1380. $data = $this->getdata($data);
  1381. return [
  1382. 'code' => 0,
  1383. 'msg' => "获取成功",
  1384. 'data' => $data,
  1385. 'old_data' => $res['categories_list']['categories']
  1386. ];
  1387. } elseif (empty($res)) {
  1388. return [
  1389. 'code' => 1,
  1390. 'msg' => "数据错误"
  1391. ];
  1392. } else {
  1393. return [
  1394. 'code' => $res['errcode'],
  1395. 'msg' => $this->getZnMsg($res),
  1396. 'data' => $res
  1397. ];
  1398. }
  1399. } catch (\Exception $e) {
  1400. return [
  1401. 'code' => 1,
  1402. 'msg' => $e->getMessage()
  1403. ];
  1404. }
  1405. }
  1406. /**
  1407. * @return array
  1408. * 获取已有分类
  1409. */
  1410. public function getCategories()
  1411. {
  1412. try {
  1413. if (!empty($this->bind_store_id)) {
  1414. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1415. } else {
  1416. $store = StoreMini::findOne($this->mini_id);
  1417. }
  1418. if (empty($store)) {
  1419. throw new \Exception("数据错误,查询小程序信息失败");
  1420. }
  1421. if (!$this->openPlatform) {
  1422. throw new \Exception("参数配置错误");
  1423. }
  1424. if (!$store->appid) {
  1425. throw new \Exception("参数配置错误");
  1426. }
  1427. $miniProgram = $this->miniProgram($store->appid);
  1428. $res = $miniProgram->setting->getCategories();
  1429. if (empty($res['errcode']) && !empty($res)) {
  1430. $store->categories = !empty($res['categories']) ? json_encode($res['categories']) : "";
  1431. $store->save();
  1432. return [
  1433. 'code' => 0,
  1434. 'msg' => "获取成功",
  1435. 'data' => $res['categories']
  1436. ];
  1437. } elseif (empty($res)) {
  1438. throw new \Exception("数据错误");
  1439. } else {
  1440. throw new \Exception($this->getZnMsg($res));
  1441. }
  1442. } catch (\Exception $e) {
  1443. return [
  1444. 'code' => 1,
  1445. 'msg' => $e->getMessage()
  1446. ];
  1447. }
  1448. }
  1449. /**
  1450. * 添加类目
  1451. */
  1452. public function addCategory()
  1453. {
  1454. try {
  1455. if (!empty($this->bind_store_id)) {
  1456. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1457. } else {
  1458. $store = StoreMini::findOne($this->mini_id);
  1459. }
  1460. if (empty($store)) {
  1461. throw new \Exception("数据错误,查找不到小程序信息");
  1462. }
  1463. if (!$this->openPlatform) {
  1464. throw new \Exception("参数配置错误");
  1465. }
  1466. $miniProgram = $this->miniProgram($store->appid);
  1467. $data = $this->categories;
  1468. $media = new Client($miniProgram);
  1469. \Yii::error($data);
  1470. foreach ($data as $index => &$item) {
  1471. if(!empty($item['certicates'])){
  1472. if(isset($item['certicates']['key'])){
  1473. $item['certicates'] = [$item['certicates']];
  1474. }
  1475. foreach ($item['certicates'] as $cateIndex => &$cate) {
  1476. $url = $this->saveTempImage($cate['value']);
  1477. $cate['value'] = $media->uploadImage($url)['media_id'];
  1478. }
  1479. }
  1480. }
  1481. \Yii::error($data);
  1482. // $data = [
  1483. // [
  1484. // "first" => 304,
  1485. // "second"=> 1309
  1486. // ]
  1487. // ];
  1488. $res = $miniProgram->setting->addCategories($data);
  1489. if (empty($res['errcode']) && !empty($res)) {
  1490. $cat_res = $this->getCategories();
  1491. return [
  1492. 'code' => 0,
  1493. 'msg' => "设置成功",
  1494. 'data' => $res,
  1495. 'cat_data' => $cat_res['data']
  1496. ];
  1497. } elseif (empty($res)) {
  1498. throw new \Exception("数据错误");
  1499. } else {
  1500. throw new \Exception($this->getZnMsg($res));
  1501. }
  1502. } catch (\Exception $e) {
  1503. return [
  1504. 'code' => 0,
  1505. 'msg' => $e->getMessage(),
  1506. 'e' => $e,
  1507. ];
  1508. }
  1509. }
  1510. /**
  1511. * 删除类目
  1512. */
  1513. public function delCategory()
  1514. {
  1515. $t = \Yii::$app->db->beginTransaction();
  1516. try {
  1517. if (!empty($this->bind_store_id)) {
  1518. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1519. } else {
  1520. $store = StoreMini::findOne($this->mini_id);
  1521. }
  1522. if (empty($store)) {
  1523. throw new \Exception("数据错误");
  1524. }
  1525. if (!$this->openPlatform) {
  1526. throw new \Exception("参数配置错误");
  1527. }
  1528. $miniProgram = $this->miniProgram($store->appid);
  1529. $res = $miniProgram->setting->deleteCategories($this->cate_first, $this->cate_second);
  1530. if (empty($res['errcode']) && !empty($res)) {
  1531. $t->commit();
  1532. return [
  1533. 'code' => 0,
  1534. 'msg' => "删除成功",
  1535. 'data' => $res
  1536. ];
  1537. } elseif (empty($res)) {
  1538. throw new \Exception("数据错误");
  1539. } else {
  1540. throw new \Exception($this->getZnMsg($res));
  1541. }
  1542. } catch (\Exception $e) {
  1543. $t->rollBack();
  1544. return [
  1545. 'code' => 1,
  1546. 'msg' => $e->getMessage()
  1547. ];
  1548. }
  1549. }
  1550. /**
  1551. * 获取代码模版列表
  1552. */
  1553. public function getTemplateList()
  1554. {
  1555. try {
  1556. if (!$this->openPlatform) {
  1557. throw new \Exception("参数配置错误");
  1558. }
  1559. $res = $this->openPlatform->code_template->list(0);
  1560. if (empty($res['errcode']) && !empty($res)) {
  1561. $templateList = $res['template_list'];
  1562. foreach ($templateList as &$item) {
  1563. $item['c_time'] = date('Y-m-d H:i:s', $item['create_time']);
  1564. $item['template_id'] = (string)$item['template_id'];
  1565. }
  1566. $last_names = array_column($templateList, 'create_time');
  1567. array_multisort($last_names, SORT_DESC, $templateList);
  1568. if (!empty($templateList) && count($templateList) > 15) {
  1569. $last_template = end($templateList);
  1570. if (isset($last_template['template_id'])) {
  1571. $result = $this->delLastTemplate($last_template['template_id']);
  1572. if ($result['code'] === 0) {
  1573. array_pop($templateList);
  1574. }
  1575. }
  1576. }
  1577. return [
  1578. 'code' => 0,
  1579. 'msg' => "获取成功",
  1580. 'data' => [
  1581. 'template_list' => $templateList
  1582. ]
  1583. ];
  1584. } elseif (empty($res)) {
  1585. return [
  1586. 'code' => 1,
  1587. 'msg' => "数据错误"
  1588. ];
  1589. } else {
  1590. return [
  1591. 'code' => $res['errcode'],
  1592. 'msg' => $this->getZnMsg($res),
  1593. 'data' => $res
  1594. ];
  1595. }
  1596. } catch (\Exception $e) {
  1597. return [
  1598. 'code' => 1,
  1599. 'msg' => $e->getMessage(),
  1600. ];
  1601. }
  1602. }
  1603. public function delLastTemplate($templateId = 0) {
  1604. try {
  1605. if (!$this->openPlatform) {
  1606. throw new \Exception("参数配置错误");
  1607. }
  1608. if (empty(trim($templateId))) {
  1609. throw new \Exception("缺失关键参数");
  1610. }
  1611. $res = $this->openPlatform->code_template->delete($templateId);
  1612. if (empty($res['errcode']) && !empty($res)) {
  1613. return [
  1614. 'code' => 0,
  1615. 'msg' => "操作成功",
  1616. ];
  1617. } elseif (empty($res)) {
  1618. return [
  1619. 'code' => 1,
  1620. 'msg' => "数据错误"
  1621. ];
  1622. } else {
  1623. return [
  1624. 'code' => $res['errcode'],
  1625. 'msg' => $this->getZnMsg($res),
  1626. 'data' => $res
  1627. ];
  1628. }
  1629. } catch (\Exception $e) {
  1630. return [
  1631. 'code' => 1,
  1632. 'msg' => $e->getMessage(),
  1633. ];
  1634. }
  1635. }
  1636. public function pluginList($miniProgram) {
  1637. return $miniProgram->plugin->list();
  1638. }
  1639. //具备某个插件权限
  1640. public function hasPlugin($miniProgram, $appid = 'wx2b03c6e691cd7370') {
  1641. $res = $this->pluginList($miniProgram);
  1642. if (empty($res['errcode']) && !empty($res)) {
  1643. if (!empty($res['plugin_list'])) {
  1644. foreach($res['plugin_list'] as $item){
  1645. //直播
  1646. if($item['appid'] == $appid && $item['status'] == 2){
  1647. return true;
  1648. }
  1649. }
  1650. } else {
  1651. throw new \Exception("权限数据失败");
  1652. }
  1653. } elseif (empty($res)) {
  1654. throw new \Exception("数据错误");
  1655. } else {
  1656. throw new \Exception($this->getZnMsg($res));
  1657. }
  1658. return false;
  1659. }
  1660. //具备某个三方授权权限
  1661. public function hasAuthorizationId($id = 52) {
  1662. $res = $this->api_get_authorizer_info();
  1663. if($res['code'] == 0){
  1664. $items = $res['data']['authorization_info']['func_info'];
  1665. foreach($items as $item){
  1666. if($item['funcscope_category']['id'] == $id){
  1667. return true;
  1668. }
  1669. }
  1670. }
  1671. return false;
  1672. }
  1673. public function canUsePlugin($appid = 'wx2b03c6e691cd7370', $aid = 52) {
  1674. if (!empty($this->bind_store_id)) {
  1675. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1676. } else {
  1677. $store = StoreMini::findOne($this->mini_id);
  1678. }
  1679. if (empty($store)) {
  1680. throw new \Exception("数据错误,获取不到小程序信息");
  1681. }
  1682. if (!$this->openPlatform) {
  1683. throw new \Exception("参数配置错误");
  1684. }
  1685. $miniProgram = $this->miniProgram($store->appid);
  1686. $hasPlugin = $this->hasPlugin($miniProgram, $appid);
  1687. $hasAuthorizationId = $this->hasAuthorizationId($aid);
  1688. $canUse = $hasPlugin && $hasAuthorizationId;
  1689. return [
  1690. 'code' => 0,
  1691. 'msg' => "成功",
  1692. 'canUse' => intval($canUse),
  1693. 'hasPlugin' => intval($hasPlugin),
  1694. 'hasAuthorizationId' => intval($hasAuthorizationId),
  1695. ];
  1696. }
  1697. /**
  1698. * 上传小程序
  1699. */
  1700. public function upMini($useLive = 0, $useStudent = 0, $useB2b = 0)
  1701. {
  1702. $t = \Yii::$app->db->beginTransaction();
  1703. try {
  1704. if (!empty($this->bind_store_id)) {
  1705. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1706. } else {
  1707. $store = StoreMini::findOne($this->mini_id);
  1708. }
  1709. if (empty($store)) {
  1710. throw new \Exception("数据错误,获取不到小程序信息");
  1711. }
  1712. if (!$this->openPlatform) {
  1713. throw new \Exception("参数配置错误");
  1714. }
  1715. $miniProgram = $this->miniProgram($store->appid);
  1716. $self_mini = Option::get('self_mini', $store->store_id, 'store', 0)['value'];
  1717. $ext = [
  1718. 'extEnable' => true,
  1719. 'extAppid' => $store->appid,
  1720. 'ext' => [
  1721. 'self_mini' => $self_mini ? 1 : 0,
  1722. "apiurl" => "https://" . \Yii::$app->request->hostName,
  1723. "store_id" => $store->store_id,
  1724. "mini_id" => $store->id
  1725. ],
  1726. "window" => [
  1727. "navigationBarTitleText" => "0000"
  1728. ]
  1729. ];
  1730. // 此方法是把插件放主包,暂时不用
  1731. // $plugins = [];
  1732. // if($useLive){
  1733. // $plugins['live-player-plugin'] = [
  1734. // 'version' => '1.3.2',
  1735. // 'provider' => 'wx2b03c6e691cd7370',
  1736. // ];
  1737. // }
  1738. // if($useB2b){
  1739. // $plugins['B2bPlugin'] = [
  1740. // 'version' => 'latest',
  1741. // 'provider' => 'wx69b7451feb427f0e',
  1742. // ];
  1743. // }
  1744. // if ($useLive || $useB2b) {
  1745. // $ext['plugins'] = $plugins;
  1746. // }
  1747. if($useLive){
  1748. $ext['plugins'] = [
  1749. 'live-player-plugin' => [
  1750. 'version' => '1.3.2',
  1751. 'provider' => 'wx2b03c6e691cd7370',
  1752. ],
  1753. ];
  1754. }
  1755. // 此方法是把插件放分包,每次更新需要更新config/ext.php
  1756. if ($useB2b) {
  1757. // 获取config/ext.php内容
  1758. $extSub = require \Yii::getAlias('@app/config/ext.php');
  1759. $extSub = \json_decode($extSub, true);
  1760. $ext['subPackages'] = $extSub;
  1761. }
  1762. if ($useStudent) {
  1763. $ext['usingComponents'] = [
  1764. 'student' => '/wxcomponents/student/index',
  1765. 'single-header' => '/components/diySystemPage/components/singleHeader/index',
  1766. 'foot-nav' => '/components/diyNew/footNav/index',
  1767. 'transition-page' => '/components/transitionPage/index',
  1768. 'get-success' => '/components/coupon/getSuccessPopup',
  1769. 'shenhe' => '/components/shenhe/shenhe',
  1770. ];
  1771. }
  1772. $template = Option::get("platform_template_id", 0, 'saas');//
  1773. $template_id = $template['value'];
  1774. if (empty($template) || $template_id === "-1") {
  1775. throw new \Exception("未选择模板");
  1776. }
  1777. $res = $this->setDomainName();
  1778. if ($res['code'] == 1) {
  1779. throw new \Exception($res['msg']);
  1780. }
  1781. $DomainData = $res;
  1782. $res = $miniProgram->code->commit($template_id, json_encode($ext), cyy_version(), $this->user_desc);
  1783. if (empty($res['errcode']) && !empty($res)) {
  1784. $store->mini_up = -1;
  1785. $store->template_id = $template_id;
  1786. $store->user_version = cyy_version();
  1787. $store->user_desc = $this->user_desc;
  1788. $store->ext = json_encode($ext);
  1789. $res = $store->save();
  1790. if ($res) {
  1791. $res = $this->getMiniQrcode($store->id);
  1792. if ($res['code'] === 0) {
  1793. $t->commit();
  1794. return [
  1795. 'code' => 0,
  1796. 'msg' => "成功",
  1797. 'data' => $res['data'],
  1798. 'datas' => $DomainData
  1799. ];
  1800. } else {
  1801. throw new \Exception("获取体验二维码失败");
  1802. }
  1803. } else {
  1804. throw new \Exception("存储数据失败");
  1805. }
  1806. } elseif (empty($res)) {
  1807. throw new \Exception("数据错误");
  1808. } else {
  1809. throw new \Exception($this->getZnMsg($res));
  1810. }
  1811. } catch (\Exception $e) {
  1812. $t->rollBack();
  1813. if (strpos($e->getMessage(), 'cURL error 28') !== false) {
  1814. return $this->upMini();
  1815. }
  1816. return [
  1817. 'code' => 1,
  1818. 'msg' => $e->getMessage(),
  1819. ];
  1820. }
  1821. }
  1822. /**
  1823. * 添加体验者
  1824. * @return array
  1825. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  1826. * @throws \GuzzleHttp\Exception\GuzzleException
  1827. */
  1828. public function addTester()
  1829. {
  1830. try {
  1831. $store = StoreMini::findOne($this->mini_id);
  1832. if (empty($store)) {
  1833. return [
  1834. 'code' => 1,
  1835. 'msg' => "数据错误"
  1836. ];
  1837. }
  1838. if (!$this->openPlatform) {
  1839. throw new \Exception("参数配置错误");
  1840. }
  1841. $miniProgram = $this->miniProgram($store->appid);
  1842. $res = $miniProgram->tester->bind($this->wechatId);
  1843. if (empty($res['errcode']) && !empty($res)) {
  1844. return [
  1845. 'code' => 0,
  1846. 'msg' => "成功",
  1847. 'data' => $res
  1848. ];
  1849. } elseif (empty($res)) {
  1850. return [
  1851. 'code' => 1,
  1852. 'msg' => "数据错误"
  1853. ];
  1854. } else {
  1855. return [
  1856. 'code' => $res['errcode'],
  1857. 'msg' => $this->getZnMsg($res),
  1858. 'data' => $res
  1859. ];
  1860. }
  1861. } catch (\Exception $e) {
  1862. return [
  1863. 'code' => 1,
  1864. 'msg' => $e->getMessage()
  1865. ];
  1866. }
  1867. }
  1868. /**
  1869. * 获取体验版二维码
  1870. */
  1871. public function getMiniQrcode($id = 0)
  1872. {
  1873. try {
  1874. if (!empty($this->bind_store_id)) {
  1875. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1876. } else {
  1877. $id = $id !== 0 ? $id : $this->mini_id;
  1878. $store = StoreMini::findOne($id);
  1879. }
  1880. if (empty($store)) {
  1881. return [
  1882. 'code' => 1,
  1883. 'msg' => "数据错误"
  1884. ];
  1885. }
  1886. if (!$this->openPlatform) {
  1887. throw new \Exception("参数配置错误");
  1888. }
  1889. $miniProgram = $this->miniProgram($store->appid);
  1890. $qrcode = \Yii::$app->basePath . "/web/temp/" . $this->store_id . "_" . date('YmdHis') . ".jpg";
  1891. $res = $miniProgram->code->getQrCode($this->mini_path);
  1892. if (empty(json_decode($res, true)['errcode'])) {
  1893. file_put_contents($qrcode, $res);
  1894. $url = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $qrcode);
  1895. return [
  1896. 'code' => 0,
  1897. 'msg' => "成功",
  1898. 'data' => $url
  1899. ];
  1900. } elseif (empty($res)) {
  1901. return [
  1902. 'code' => 1,
  1903. 'msg' => "数据错误"
  1904. ];
  1905. } else {
  1906. $res = json_decode($res, true);
  1907. return [
  1908. 'code' => $res->errcode,
  1909. 'msg' => $this->getZnMsg($res),
  1910. 'data' => $res
  1911. ];
  1912. }
  1913. } catch (\Exception $e) {
  1914. return [
  1915. 'code' => 1,
  1916. 'msg' => $e->getMessage()
  1917. ];
  1918. }
  1919. }
  1920. /**
  1921. * 提交审核
  1922. */
  1923. public function submitAudit()
  1924. {
  1925. $t = \Yii::$app->db->beginTransaction();
  1926. try {
  1927. $result = $this->setPrivacySetting();
  1928. if ($result['code'] === 0) {
  1929. if (!empty($this->bind_store_id)) {
  1930. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1931. } else {
  1932. $store = StoreMini::findOne($this->mini_id);
  1933. }
  1934. if (empty($store)) {
  1935. throw new \Exception("数据错误,未找到对应的小程序信息");
  1936. }
  1937. if (!$this->openPlatform) {
  1938. throw new \Exception("参数配置错误");
  1939. }
  1940. $miniProgram = $this->miniProgram($store->appid);
  1941. $data = [
  1942. 'item_list' => $this->item_list ?? [],
  1943. 'order_path' => 'order/order/order',
  1944. ];
  1945. $res = $miniProgram->code->submitAudit($data, '', '');
  1946. if (empty($res['errcode']) && !empty($res)) {
  1947. //是否加急审核
  1948. if ($this->is_fast_audit === 1) {
  1949. $res = $miniProgram->code->speedupAudit($res['auditid']);
  1950. }
  1951. $store->mini_up = 1;
  1952. $store->audit_id = (string)$res['auditid'];
  1953. $store->save();
  1954. $t->commit();
  1955. return [
  1956. 'code' => 0,
  1957. 'msg' => "成功",
  1958. 'data' => $res
  1959. ];
  1960. } elseif (empty($res)) {
  1961. throw new \Exception("数据错误");
  1962. } else {
  1963. throw new \Exception($this->getZnMsg($res));
  1964. }
  1965. } else {
  1966. throw new \Exception($result['msg']);
  1967. }
  1968. } catch (\Exception $e) {
  1969. $t->rollBack();
  1970. if (strpos($e->getMessage(), 'cURL error 28') !== false) {
  1971. return $this->submitAudit();
  1972. }
  1973. return [
  1974. 'code' => 1,
  1975. 'msg' => $e->getMessage()
  1976. ];
  1977. }
  1978. }
  1979. /**
  1980. * 查询最后一次提交审核的状态
  1981. */
  1982. public function lastAuditStatus()
  1983. {
  1984. $t = \Yii::$app->db->beginTransaction();
  1985. try {
  1986. if (!empty($this->bind_store_id)) {
  1987. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1988. } else {
  1989. $store = StoreMini::findOne($this->mini_id);
  1990. }
  1991. if (empty($store)) {
  1992. throw new \Exception("数据错误");
  1993. }
  1994. if (!$this->openPlatform) {
  1995. throw new \Exception("参数配置错误");
  1996. }
  1997. $miniProgram = $this->miniProgram($store->appid);
  1998. $result = $miniProgram->code->getLatestAuditStatus();
  1999. $msg = "返回成功";
  2000. if (empty($result['errcode']) && !empty($result)) {
  2001. switch ($result['status']) {
  2002. case 0:
  2003. $msg = "审核通过";
  2004. $store->mini_up = 2;
  2005. break;
  2006. case 1:
  2007. $msg = "审核已拒绝:" . $result['reason'];
  2008. $store->mini_up = 3;
  2009. $store->mini_up_error = $result['reason'];
  2010. break;
  2011. case 2:
  2012. $msg = "审核中";
  2013. $store->mini_up = 1;
  2014. break;
  2015. case 3:
  2016. $msg = "未提交审核或已撤回";
  2017. $store->mini_up = 0;
  2018. break;
  2019. }
  2020. if (!$store->save()) {
  2021. throw new \Exception("保存失败");
  2022. }
  2023. $t->commit();
  2024. return [
  2025. 'code' => 0,
  2026. 'msg' => $msg
  2027. ];
  2028. }
  2029. } catch (\Exception $e) {
  2030. $t->rollBack();
  2031. return [
  2032. 'code' => 1,
  2033. 'msg' => $e->getMessage()
  2034. ];
  2035. }
  2036. }
  2037. /**
  2038. * 提交审核的状态
  2039. */
  2040. public function getAuditStatus($mini_id)
  2041. {
  2042. //20240816微信回调老是处理失败 增加主动查询的逻辑
  2043. // $t = \Yii::$app->db->beginTransaction();
  2044. try {
  2045. if ($mini_id) {
  2046. $store = StoreMini::findOne($mini_id);
  2047. }
  2048. if (empty($store)) {
  2049. throw new \Exception("数据错误");
  2050. }
  2051. if (empty($store->audit_id)) {
  2052. throw new \Exception("未查询到审核信息");
  2053. }
  2054. if (!$this->openPlatform) {
  2055. throw new \Exception("参数配置错误");
  2056. }
  2057. $miniProgram = $this->miniProgram($store->appid);
  2058. $result = $miniProgram->code->getAuditStatus($store->audit_id);
  2059. $msg = "返回成功";
  2060. if (empty($result['errcode']) && !empty($result)) {
  2061. switch ($result['status']) {
  2062. case 0:
  2063. $msg = "审核通过";
  2064. if (intval($store->mini_up) === 1) {
  2065. $store->mini_up = 2;
  2066. }
  2067. break;
  2068. case 1:
  2069. $msg = "审核已拒绝:" . $result['reason'];
  2070. $store->mini_up = 3;
  2071. $store->mini_up_error = $result['reason'];
  2072. break;
  2073. case 2:
  2074. $msg = "审核中";
  2075. $store->mini_up = 1;
  2076. break;
  2077. case 3:
  2078. $msg = "未提交审核或已撤回";
  2079. $store->mini_up = 0;
  2080. break;
  2081. }
  2082. if (!$store->save()) {
  2083. throw new \Exception("保存失败");
  2084. }
  2085. // $t->commit();
  2086. return [
  2087. 'code' => 0,
  2088. 'msg' => $msg,
  2089. 'mini_up' => $store->mini_up
  2090. ];
  2091. }
  2092. throw new \Exception(json_encode($result, JSON_UNESCAPED_UNICODE));
  2093. } catch (\Exception $e) {
  2094. // $t->rollBack();
  2095. return [
  2096. 'code' => 1,
  2097. 'msg' => $e->getMessage()
  2098. ];
  2099. }
  2100. }
  2101. /**
  2102. * 审核撤回
  2103. */
  2104. public function unDoCodeAudit()
  2105. {
  2106. $t = \Yii::$app->db->beginTransaction();
  2107. try {
  2108. if (!empty($this->bind_store_id)) {
  2109. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2110. } else {
  2111. $store = StoreMini::findOne($this->mini_id);
  2112. }
  2113. if (empty($store)) {
  2114. throw new \Exception("数据错误,未查询到小程序信息");
  2115. }
  2116. if (!$this->openPlatform) {
  2117. throw new \Exception("参数配置错误");
  2118. }
  2119. $miniProgram = $this->miniProgram($store->appid);
  2120. $res = $miniProgram->code->withdrawAudit();
  2121. if (empty($res['errcode']) && !empty($res)) {
  2122. $store->mini_up = -1;
  2123. $store->save();
  2124. $t->commit();
  2125. return [
  2126. 'code' => 0,
  2127. 'msg' => "成功",
  2128. 'data' => $res
  2129. ];
  2130. } elseif (empty($res)) {
  2131. throw new \Exception("数据错误");
  2132. } else {
  2133. throw new \Exception($this->getZnMsg($res));
  2134. }
  2135. } catch (\Exception $e) {
  2136. $t->rollBack();
  2137. return [
  2138. 'code' => 1,
  2139. 'msg' => $e->getMessage()
  2140. ];
  2141. }
  2142. }
  2143. /**
  2144. * 发布已通过审核的小程序
  2145. */
  2146. public function release()
  2147. {
  2148. $t = \Yii::$app->db->beginTransaction();
  2149. try {
  2150. if (!empty($this->bind_store_id)) {
  2151. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2152. } else {
  2153. $store = StoreMini::findOne($this->mini_id);
  2154. }
  2155. $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id();
  2156. if (empty($store)) {
  2157. throw new \Exception("数据错误,获取不到小程序信息");
  2158. }
  2159. if (!$this->openPlatform) {
  2160. throw new \Exception("参数配置错误");
  2161. }
  2162. $miniProgram = $this->miniProgram($store->appid);
  2163. $this->is_use = 1;
  2164. $this->unbind();
  2165. $res = $miniProgram->code->release();
  2166. if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") {
  2167. $store->mini_up = 5;
  2168. if ($store->save()) {
  2169. $t->commit();
  2170. $res = $this->getMiniQr($store->id);
  2171. if ($res['code'] === 0) {
  2172. return [
  2173. 'code' => 0,
  2174. 'msg' => "成功",
  2175. 'data' => $res['data']
  2176. ];
  2177. } else {
  2178. throw new \Exception("获取二维码数据错误");
  2179. }
  2180. } else {
  2181. throw new \Exception("保存数据错误");
  2182. }
  2183. } elseif (empty($res)) {
  2184. throw new \Exception("数据错误");
  2185. } else {
  2186. throw new \Exception($this->getZnMsg($res));
  2187. }
  2188. } catch (\Exception $e) {
  2189. $t->rollBack();
  2190. return [
  2191. 'code' => 1,
  2192. 'msg' => $e->getMessage()
  2193. ];
  2194. }
  2195. }
  2196. /**
  2197. * 设置小程序用户隐私保护指引
  2198. */
  2199. public function setPrivacySetting()
  2200. {
  2201. $data = [
  2202. "privacy_ver" => $this->privacy_ver,
  2203. "owner_setting" => [
  2204. "contact_phone" => "17090402350",
  2205. "notice_method" => "短信"
  2206. ],
  2207. "setting_list" => [
  2208. [
  2209. "privacy_key" => "UserInfo",
  2210. "privacy_text" => "统计订单"
  2211. ],
  2212. [
  2213. "privacy_key" => "Location",
  2214. "privacy_text" => "显示附近自提点"
  2215. ],
  2216. [
  2217. "privacy_key" => "Address",
  2218. "privacy_text" => "显示附近自提点"
  2219. ],
  2220. [
  2221. "privacy_key" => "RunData",
  2222. "privacy_text" => "消费步数兑换商品"
  2223. ],
  2224. [
  2225. "privacy_key" => "Record",
  2226. "privacy_text" => "售后咨询"
  2227. ],
  2228. [
  2229. "privacy_key" => "Camera",
  2230. "privacy_text" => "提交订单备注资料"
  2231. ],
  2232. [
  2233. "privacy_key" => "AlbumWriteOnly",
  2234. "privacy_text" => "保存海报"
  2235. ],
  2236. [
  2237. "privacy_key" => "PhoneNumber",
  2238. "privacy_text" => "多端口数据统一"
  2239. ],
  2240. [
  2241. "privacy_key" => "Album",
  2242. "privacy_text" => "审核资质"
  2243. ],
  2244. [
  2245. "privacy_key" =>"MessageFile",
  2246. "privacy_text" => "订单评价拍照"
  2247. ],
  2248. [
  2249. "privacy_key" =>"Clipboard",
  2250. "privacy_text" => "支持用户信息复制信息"
  2251. ],
  2252. [
  2253. "privacy_key" =>"ChooseLocation",
  2254. "privacy_text" => "计算距离"
  2255. ],
  2256. [
  2257. "privacy_key" =>"DeviceInfo",
  2258. "privacy_text" => "生成适应页面"
  2259. ]
  2260. ],
  2261. ];
  2262. try {
  2263. $this->apply_privacy_interface();
  2264. if (!empty($this->bind_store_id)) {
  2265. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2266. } else {
  2267. $store = StoreMini::findOne($this->mini_id);
  2268. }
  2269. $contact_tel = Option::get(OptionSetting::STORE_CONTACT_TEL, $store->store_id, '')['value'];
  2270. if (!$contact_tel) {
  2271. throw new \Exception("更新隐私政策失败,请前往'系统-功能设置-基础设置'填写保存手机号信息。");
  2272. }
  2273. $data['owner_setting']['contact_phone'] = $contact_tel;
  2274. if (!$this->openPlatform) {
  2275. throw new \Exception("参数配置错误");
  2276. }
  2277. $client = new BaseClient($this->miniProgram($store->appid));
  2278. $res = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data);
  2279. if (empty($res['errcode']) && !empty($res)) {
  2280. return [
  2281. 'code' => 0,
  2282. 'msg' => "成功",
  2283. 'data' => $res
  2284. ];
  2285. } elseif (empty($res)) {
  2286. return [
  2287. 'code' => 1,
  2288. 'msg' => "数据错误"
  2289. ];
  2290. } else {
  2291. return [
  2292. 'code' => $res['errcode'],
  2293. 'msg' => $this->getZnMsg($res)
  2294. ];
  2295. }
  2296. } catch (\Exception $e) {
  2297. return [
  2298. 'code' => 1,
  2299. 'msg' => $e->getMessage()
  2300. ];
  2301. }
  2302. }
  2303. public function apply_privacy_interface() {
  2304. try {
  2305. if (!empty($this->bind_store_id)) {
  2306. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2307. } else {
  2308. $store = StoreMini::findOne($this->mini_id);
  2309. }
  2310. $client = new BaseClient($this->miniProgram($store->appid));
  2311. $arr = [
  2312. [
  2313. 'api_name' => 'wx.chooseLocation',
  2314. 'content' => '获取用户选择的省市区位置信息,帮助快速生成收货地址'
  2315. ],
  2316. [
  2317. 'api_name' => 'wx.getLocation',
  2318. 'content' => '获取用户位置信息,用户能查看到附近的自提地点'
  2319. ]
  2320. ];
  2321. foreach ($arr as $item) {
  2322. $res = $client->httpPostJson('wxa/security/apply_privacy_interface', $item);
  2323. debug_log('申请——', 'privacy_interface.log');
  2324. debug_log($res, 'privacy_interface.log');
  2325. if ($res['errcode']) {
  2326. return [
  2327. 'code' => $res['errcode'],
  2328. 'msg' => $this->getZnMsg($res)
  2329. ];
  2330. }
  2331. }
  2332. return [
  2333. 'code' => 0,
  2334. 'msg' => '开始申请...'
  2335. ];
  2336. } catch (\Exception $e) {
  2337. debug_log('申请——', 'privacy_interface.log');
  2338. debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log');
  2339. return [
  2340. 'code' => 1,
  2341. 'msg' => $e->getMessage()
  2342. ];
  2343. }
  2344. }
  2345. public function get_privacy_interface() {
  2346. try {
  2347. if (!empty($this->bind_store_id)) {
  2348. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2349. } else {
  2350. $store = StoreMini::findOne($this->mini_id);
  2351. }
  2352. if (!$store->appid) {
  2353. throw new \Exception("参数配置错误");
  2354. }
  2355. $client = new BaseClient($this->miniProgram($store->appid));
  2356. $res = $client->httpGet('wxa/security/get_privacy_interface');
  2357. debug_log('获取——', 'privacy_interface.log');
  2358. debug_log($res, 'privacy_interface.log');
  2359. } catch (\Exception $e) {
  2360. debug_log('获取——', 'privacy_interface.log');
  2361. debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log');
  2362. }
  2363. }
  2364. /**
  2365. * 查询小程序用户隐私保护指引
  2366. */
  2367. public function getPrivacySetting()
  2368. {
  2369. try {
  2370. $data = [
  2371. "privacy_ver" => $this->privacy_ver,
  2372. ];
  2373. if (!empty($this->bind_store_id)) {
  2374. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  2375. } else {
  2376. $store = StoreMini::findOne($this->mini_id);
  2377. }
  2378. if (!$this->openPlatform) {
  2379. throw new \Exception("参数配置错误");
  2380. }
  2381. $client = new BaseClient($this->miniProgram($store->appid));
  2382. $res = $client->httpPostJson('cgi-bin/component/getprivacysetting', $data);
  2383. if (empty($res['errcode']) && !empty($res)) {
  2384. return [
  2385. 'code' => 0,
  2386. 'msg' => "成功",
  2387. 'data' => $res
  2388. ];
  2389. } elseif (empty($res)) {
  2390. return [
  2391. 'code' => 1,
  2392. 'msg' => "数据错误"
  2393. ];
  2394. } else {
  2395. return [
  2396. 'code' => $res['errcode'],
  2397. 'msg' => $this->getZnMsg($res),
  2398. 'data' => $res
  2399. ];
  2400. }
  2401. } catch (\Exception $e) {
  2402. return [
  2403. 'code' => 1,
  2404. 'msg' => $e->getMessage()
  2405. ];
  2406. }
  2407. }
  2408. /**
  2409. * 查询小程序码
  2410. */
  2411. public function getMiniQr($id = 0, $sence = '', $page = '')
  2412. {
  2413. try {
  2414. if (empty($id)) {
  2415. $id = $this->mini_id;
  2416. }
  2417. $store = StoreMini::findOne($id);
  2418. if (!$this->openPlatform) {
  2419. throw new \Exception("参数配置错误");
  2420. }
  2421. $miniProgram = $this->miniProgram($store->appid);
  2422. $response = $miniProgram->app_code->getUnlimit($sence ?: "store=" . $this->store_id, [
  2423. 'page' => $page
  2424. ]);
  2425. $filename = md5(time()) . '.jpg';
  2426. $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
  2427. if (!is_dir($filePath)) {
  2428. mkdir($filePath, 0777, true);
  2429. chmod($filePath , 0777);
  2430. }
  2431. $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
  2432. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  2433. $resfilename = $response->saveAs($filePath, $filename);
  2434. if (!empty($resfilename)) {
  2435. $filePath = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $filePath . '/' . $filename);
  2436. $store->qrcode = $filePath;
  2437. $res = $store->save();
  2438. if ($res) {
  2439. return [
  2440. 'code' => 0,
  2441. 'msg' => "获取成功",
  2442. 'data' => $filePath
  2443. ];
  2444. } else {
  2445. throw new \Exception("添加小程序码失败");
  2446. }
  2447. } else {
  2448. throw new \Exception("数据错误");
  2449. }
  2450. } else {
  2451. throw new \Exception("数据错误");
  2452. }
  2453. } catch (\Exception $e) {
  2454. return [
  2455. 'code' => 1,
  2456. 'msg' => $e->getMessage()
  2457. ];
  2458. }
  2459. }
  2460. /**
  2461. * 查询小程序
  2462. */
  2463. public function getMiniStatus()
  2464. {
  2465. $mini_info = StoreMini::find()->where(['appid' => $this->appid])->select("mini_url,mini_up,is_cancle,appid,status,msg")->asArray()->one();
  2466. return [
  2467. 'code' => 0,
  2468. 'msg' => "获取成功",
  2469. 'data' => $mini_info
  2470. ];
  2471. }
  2472. /**
  2473. * 获取列表
  2474. */
  2475. public function getMiniList()
  2476. {
  2477. $store_id = $this->store_id;
  2478. $query = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0])
  2479. ->select("*");
  2480. if (!empty($this->mini_nickname)) {
  2481. $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]);
  2482. }
  2483. if (isset($this->fuwu_type)) {
  2484. $query->andWhere(['fuwu_type' => $this->fuwu_type]);
  2485. }
  2486. if (!empty($this->status)) {
  2487. switch ($this->status) {
  2488. case 1:
  2489. $query->andWhere(['OR', ['mini_nickname' => ""], ['signature' => ""], ['mini_url' => ""]]);
  2490. break;
  2491. case 2:
  2492. $query->andWhere(['AND', ['<>', 'mini_nickname', ""], ['<>', 'signature', ""], ['<>', 'mini_url', ""]]);
  2493. break;
  2494. case 3:
  2495. $query->andWhere(['status' => 2]);
  2496. break;
  2497. case 4:
  2498. $query->andWhere(['status' => -1]);
  2499. break;
  2500. }
  2501. }
  2502. if ($this->appid) {
  2503. $query->andWhere(['LIKE', 'appid', $this->appid]);
  2504. }
  2505. if ($this->start_time) {
  2506. $start_time = strtotime($this->start_time);
  2507. $query->andWhere(['>=', 'created_at', $start_time]);
  2508. }
  2509. if ($this->end_time) {
  2510. $end_time = strtotime($this->end_time);
  2511. $query->andWhere(['<=', 'created_at', $end_time]);
  2512. }
  2513. if (!empty($this->is_use)) {
  2514. $query->andWhere(['is_use' => $this->is_use]);
  2515. }
  2516. if (!empty($this->mini_nickname)) {
  2517. $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]);
  2518. }
  2519. $query->orderBy('id desc');
  2520. $pagination = pagination_make($query);
  2521. $admins = $pagination['list'];
  2522. foreach ($admins as $index => &$item) {
  2523. //判断是否认证
  2524. $item['is_wxauth'] = 1;
  2525. if ($item['created_at'] > strtotime("2023-12-25") || empty($item['created_at'])) {
  2526. $item['is_wxauth'] = 0;
  2527. $store_mini_auth = StoreMiniAuth::findOne(['mini_id' => $item['id'], 'is_delete' => 0, 'apply_status' => 4]);//已通过
  2528. if ($store_mini_auth) {
  2529. $item['is_wxauth'] = 1;
  2530. }
  2531. }
  2532. $mini_info = json_decode($item['mini_info'], true);
  2533. $item['principal_name'] = $mini_info['principal_name'];
  2534. $item['nickname'] = $mini_info['nickname'];
  2535. // $item['app_secret'] = '';
  2536. // if ($item['audit_id'] && (int)$item['mini_up'] !== 5) {
  2537. // $res = $this->getAuditStatus($item['id']);
  2538. // if ($res['code'] === 0) {
  2539. // $item['mini_up'] = $res['mini_up'];
  2540. // }
  2541. // }
  2542. $item['is_finish'] = 0;
  2543. if (!empty($item['categories'])) {
  2544. $item['categories'] = json_decode($item['categories'], true);
  2545. } elseif (empty($item['categories']) && !empty($item['appid']) && empty($this->fuwu_type)) {
  2546. $this->mini_id = $item['id'];
  2547. $item['categories'] = $this->getCategories()['data'];
  2548. }
  2549. if (!empty($item['mini_info'])) {
  2550. $item['mini_info'] = json_decode($item['mini_info'], true);
  2551. }
  2552. $item['qr'] = false;
  2553. if (!empty($item['qrcode'])) {
  2554. $item['qr'] = $item['qrcode'];
  2555. }
  2556. $item['user_version'] = cyy_version();
  2557. $item['qrcode'] = '';
  2558. $item['created_at'] = $item['created_at'] ? date("Y-m-d H:i:s", $item['created_at']) : '';
  2559. if (!empty($item['appid']) && empty($this->fuwu_type)) {
  2560. // $res = $this->getQrcodeRules($item['appid']);
  2561. // $item['qrcode_rules'] = $res;
  2562. // \Yii::error($res);
  2563. $miniInfo = $this->getMiniInfo("", $item['appid']);
  2564. if ($miniInfo["code"] === 0) {
  2565. \Yii::error($miniInfo);
  2566. if (!empty($item['signature']) && !empty($item['mini_url']) && !empty($item['mini_nickname'] && !empty($item['categories']))) {
  2567. $item['is_finish'] = 1;
  2568. }
  2569. }
  2570. $res = $this->getMiniQrcode($item['id']);
  2571. if ($res['code'] == 0) {
  2572. $item['qrcode'] = $res['data'];
  2573. }
  2574. //20240816微信回调老是处理失败 增加主动查询的逻辑
  2575. $item['audit_result'] = $this->getAuditStatus($item['id']);
  2576. }
  2577. if (!empty($this->fuwu_type)) {
  2578. $item['call_back_url'] = 'https://' . \Yii::$app->request->hostName . '/index.php/wechat/video-shop-order/callback/' . $item['id'];
  2579. $item['token'] = Option::get('token', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
  2580. $item['encodingAesKey'] = Option::get('encodingAesKey', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
  2581. $item['finderUserName'] = Option::get('finderUserName', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
  2582. $WechatMini = WechatMini::getWechatConfig($this->store_id, $item['id'], 1);
  2583. try {
  2584. $client = new BaseClient($WechatMini);
  2585. $res = $client->httpGet('channels/ec/basics/info/get');
  2586. if (!$res['errcode'] && !empty($res)) {
  2587. $store_mini = StoreMini::findOne($item['id']);
  2588. $item['mini_nickname'] = $store_mini->mini_nickname = $res['info']['nickname'];
  2589. $item['mini_url'] = $store_mini->mini_url = $res['info']['headimg_url'];
  2590. $store_mini->save();
  2591. }
  2592. } catch (\Exception $e) {
  2593. }
  2594. }
  2595. }
  2596. return [
  2597. 'code' => 0,
  2598. 'msg' => "获取成功",
  2599. 'data' => $admins,
  2600. 'pageNo' => $pagination['pageNo'],
  2601. 'totalCount' => $pagination['totalCount'],
  2602. 'isSaas' => \Yii::$app->isSaas()
  2603. ];
  2604. }
  2605. /**
  2606. * 获取单个
  2607. */
  2608. public function getMini()
  2609. {
  2610. $query = StoreMini::find()->where(['id' => $this->mini_id])
  2611. ->select("id, name, code, code_type, legal_persona_wechat, legal_persona_name, status, msg, appid,
  2612. mini_up, mini_url, is_cancle, mini_up_error, mini_info, apply_name_status, apply_name_error, is_use,
  2613. signature, categories, license_pic, apply_mini_info, mini_nickname");
  2614. $data = $query->asArray()->one();
  2615. // try {
  2616. // $this->get_privacy_interface();
  2617. // } catch (\Exception $e) {
  2618. //
  2619. // }
  2620. if (!empty(json_decode($data['mini_info']))) {
  2621. $data['principal_name'] = json_decode($data['mini_info'], true)['principal_name'];
  2622. $data['nickname'] = json_decode($data['mini_info'], true)['nickname'] ?: $data['mini_nickname'];
  2623. $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url;
  2624. $data['apply_mini_info'] = $data['apply_mini_info'] ? json_decode($data['apply_mini_info'], true): [];
  2625. $data['categories'] = [];
  2626. if ($this->getCategories()['code'] === 0) {
  2627. $data['categories'] = $this->getCategories()['data'];
  2628. }
  2629. //获取认证资料
  2630. $storeMiniAuth = StoreMiniAuth::findOne(['mini_id' => $this->mini_id]);
  2631. // if ($result['apply_status'] < 2) {
  2632. $data['auth_info'] = [
  2633. 'is_finish' => intval($storeMiniAuth->apply_status) === 4 ? 1 : 0,
  2634. 'message' => intval($storeMiniAuth->task_status) !== -1 ? $storeMiniAuth->task_message : '未提交认证信息'
  2635. ];
  2636. //获取备案资料
  2637. $storeMiniIcp = StoreMiniIcp::findOne(['mini_id' => $this->mini_id, 'is_cancel_icp' => 0]);
  2638. // if ($result['apply_status'] < 2) {
  2639. $is_finish = 0;
  2640. if ($storeMiniIcp) {//人身核验是否通过
  2641. $is_finish = intval($storeMiniIcp->icp_audit_status) == 6 ? 1 : 0;
  2642. if (intval($storeMiniIcp->icp_audit_status) >= 2) {
  2643. $icp_message = StoreMiniIcp::$audit_status_text[$storeMiniIcp->icp_audit_status];
  2644. } else {
  2645. $icp_message = StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniIcp->face_status)];
  2646. }
  2647. } else {
  2648. $icp_message = '未开始人身核验';
  2649. }
  2650. $data['icp_info'] = [
  2651. 'is_finish' => $is_finish,
  2652. 'message' => $icp_message
  2653. ];
  2654. //获取认证以及备案资料
  2655. $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]);
  2656. $wechatThirdAuthAndIcpForm = new \app\modules\admin\models\WechatThirdAuthAndIcpForm(['store_id' => get_store_id(), 'mini_id' => $this->mini_id]);
  2657. if ($storeMiniAuthIcp->task_id) {
  2658. $wechatThirdAuthAndIcpForm->queryIcpVerifyTask();
  2659. }
  2660. if ($storeMiniAuthIcp->procedure_id) {
  2661. $wechatThirdAuthAndIcpForm->queryAuthAndIcp();
  2662. }
  2663. //重新加载数据
  2664. $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]);
  2665. $data['icp_auth_info'] = [
  2666. 'is_finish' => intval($storeMiniAuthIcp->procedure_status) === StoreMiniAuthIcp::PROCEDURE_STATUS_SUCCESS ? 1 : 0,
  2667. 'message' => intval($storeMiniAuthIcp->face_is_finish) === StoreMiniAuthIcp::FACE_FINISH ?
  2668. StoreMiniAuthIcp::PROCEDURE_STATUS_NAME[intval($storeMiniAuthIcp->procedure_status)] :
  2669. StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniAuthIcp->face_status)]
  2670. ];
  2671. }
  2672. return [
  2673. 'code' => 0,
  2674. 'msg' => "获取成功",
  2675. 'data' => $data
  2676. ];
  2677. }
  2678. public static function getUsedStoreMini($store_id) {
  2679. $mini = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'is_use' => 1, 'fuwu_type' => 0])->one();
  2680. return $mini;
  2681. }
  2682. public static function getStoreMiniSelectList($store_id) {
  2683. $list = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'fuwu_type' => 0])->select('id, mini_nickname, mini_url, is_use')->asArray()->all();
  2684. foreach($list as &$item){
  2685. empty($item['mini_nickname']) && $item['mini_nickname'] = '--';
  2686. }
  2687. return [
  2688. 'code' => 0,
  2689. 'msg' => "获取成功",
  2690. 'data' => $list,
  2691. ];
  2692. }
  2693. public static function checkOrBindOpen($store_id) {
  2694. $mini = self::getUsedStoreMini($store_id);
  2695. if(!$mini){
  2696. return [
  2697. 'code' => 1,
  2698. 'msg' => '没有使用中的小程序,请到小程序管理列表查看',
  2699. ];
  2700. }
  2701. $form = new self();
  2702. $form->mini_id = $mini->id;
  2703. $openGet = $form->openGet();
  2704. if($openGet['code'] == 0){
  2705. return $openGet;
  2706. }
  2707. $openCreate = $form->openCreate();
  2708. return $openCreate;
  2709. }
  2710. public function openGet() {
  2711. try {
  2712. $mini = StoreMini::findOne($this->mini_id);
  2713. $client = new BaseClient($this->miniProgram($mini->appid));
  2714. $data = [
  2715. 'appid' => $mini->appid,
  2716. ];
  2717. $res = $client->httpPostJson('cgi-bin/open/get', $data);
  2718. \Yii::error($res);
  2719. if ((empty($res['errcode']) && !empty($res))) {
  2720. return [
  2721. 'code' => 0,
  2722. 'msg' => "成功",
  2723. 'data' => $res,
  2724. ];
  2725. } elseif (empty($res)) {
  2726. throw new \Exception("数据错误");
  2727. } else {
  2728. throw new \Exception($this->getZnMsg($res));
  2729. }
  2730. } catch (\Exception $e) {
  2731. return [
  2732. 'code' => 1,
  2733. 'msg' => $e->getMessage()
  2734. ];
  2735. }
  2736. }
  2737. public function openCreate() {
  2738. try {
  2739. $mini = StoreMini::findOne($this->mini_id);
  2740. $client = new BaseClient($this->miniProgram($mini->appid));
  2741. $data = [
  2742. 'appid' => $mini->appid,
  2743. ];
  2744. $res = $client->httpPostJson('cgi-bin/open/create', $data);
  2745. \Yii::error($res);
  2746. if ((empty($res['errcode']) && !empty($res))) {
  2747. return [
  2748. 'code' => 0,
  2749. 'msg' => "成功",
  2750. 'data' => $res,
  2751. ];
  2752. } elseif (empty($res)) {
  2753. throw new \Exception("数据错误");
  2754. } else {
  2755. throw new \Exception($this->getZnMsg($res));
  2756. }
  2757. } catch (\Exception $e) {
  2758. return [
  2759. 'code' => 1,
  2760. 'msg' => $e->getMessage()
  2761. ];
  2762. }
  2763. }
  2764. public function openBind($open_appid) {
  2765. try {
  2766. $mini = StoreMini::findOne($this->mini_id);
  2767. $client = new BaseClient($this->miniProgram($mini->appid));
  2768. $data = [
  2769. 'appid' => $mini->appid,
  2770. 'open_appid' => $open_appid,
  2771. ];
  2772. $res = $client->httpPostJson('cgi-bin/open/bind', $data);
  2773. \Yii::error($res);
  2774. if ((empty($res['errcode']) && !empty($res))) {
  2775. return [
  2776. 'code' => 0,
  2777. 'msg' => "成功",
  2778. 'data' => $res,
  2779. 'open_appid' => $open_appid,
  2780. ];
  2781. } elseif (empty($res)) {
  2782. throw new \Exception("数据错误");
  2783. } else {
  2784. throw new \Exception($this->getZnMsg($res));
  2785. }
  2786. } catch (\Exception $e) {
  2787. return [
  2788. 'code' => 1,
  2789. 'msg' => $e->getMessage()
  2790. ];
  2791. }
  2792. }
  2793. public function openUnbind() {
  2794. try {
  2795. $get = $this->openGet();
  2796. if($get['code'] == 0 && $get['data']['open_appid']){
  2797. $open_appid = $get['data']['open_appid'];
  2798. }else{
  2799. return $get;
  2800. }
  2801. $mini = StoreMini::findOne($this->mini_id);
  2802. $client = new BaseClient($this->miniProgram($mini->appid));
  2803. $data = [
  2804. 'appid' => $mini->appid,
  2805. 'open_appid' => $open_appid,
  2806. ];
  2807. $res = $client->httpPostJson('cgi-bin/open/unbind', $data);
  2808. \Yii::error($res);
  2809. if ((empty($res['errcode']) && !empty($res))) {
  2810. return [
  2811. 'code' => 0,
  2812. 'msg' => "成功",
  2813. 'data' => $res,
  2814. 'open_appid' => $open_appid,
  2815. ];
  2816. } elseif (empty($res)) {
  2817. throw new \Exception("数据错误");
  2818. } else {
  2819. throw new \Exception($this->getZnMsg($res));
  2820. }
  2821. } catch (\Exception $e) {
  2822. return [
  2823. 'code' => 1,
  2824. 'msg' => $e->getMessage()
  2825. ];
  2826. }
  2827. }
  2828. //添加二维码规则
  2829. public function setQrcodeRules($id, $appid)
  2830. {
  2831. try {
  2832. $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->asArray()->one();
  2833. $data = [
  2834. 'prefix' => $qrcode['param_url'],
  2835. 'permit_sub_rule' => 1,
  2836. 'path' => $qrcode['wx_url'],
  2837. 'open_version' => 2,
  2838. 'debug_url' => [
  2839. $qrcode['param_url']
  2840. ],
  2841. 'is_edit' => 0
  2842. ];
  2843. \Yii::error($data);
  2844. if (!$this->openPlatform) {
  2845. throw new \Exception("参数配置错误");
  2846. }
  2847. $client = new BaseClient($this->miniProgram($appid));
  2848. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
  2849. \Yii::error($res);
  2850. if ((empty($res['errcode']) && !empty($res)) || ($res['errcode'] == "85071")) {
  2851. return [
  2852. 'code' => 0,
  2853. 'msg' => "成功",
  2854. 'data' => $res
  2855. ];
  2856. } elseif (empty($res)) {
  2857. throw new \Exception("数据错误");
  2858. } else {
  2859. throw new \Exception($this->getZnMsg($res));
  2860. }
  2861. } catch (\Exception $e) {
  2862. return [
  2863. 'code' => 1,
  2864. 'msg' => $e->getMessage()
  2865. ];
  2866. }
  2867. }
  2868. //发布二维码规则
  2869. public function submitQrcodeRules($id, $appid)
  2870. {
  2871. try {
  2872. $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one();
  2873. if (empty($qrcode->param_url)) {
  2874. return [
  2875. 'code' => 1,
  2876. 'msg' => '参数错误'
  2877. ];
  2878. }
  2879. $data = [
  2880. 'prefix' => $qrcode->param_url
  2881. ];
  2882. if (!$this->openPlatform) {
  2883. throw new \Exception("参数配置错误");
  2884. }
  2885. $client = new BaseClient($this->miniProgram($appid));
  2886. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
  2887. if (empty($res->errcode) && !empty($res)) {
  2888. $qrcode->wx_status = 1;
  2889. $res = $qrcode->save();
  2890. if (!$res) {
  2891. throw new \Exception('保存数据失败,二维码规则发布生成');
  2892. }
  2893. return [
  2894. 'code' => 0,
  2895. 'msg' => "成功",
  2896. 'data' => $res
  2897. ];
  2898. } elseif (empty($res)) {
  2899. return [
  2900. 'code' => 1,
  2901. 'msg' => "数据错误"
  2902. ];
  2903. } else {
  2904. $res = json_decode($res, true);
  2905. return [
  2906. 'code' => $res['errcode'],
  2907. 'msg' => $this->getZnMsg($res),
  2908. 'data' => $res
  2909. ];
  2910. }
  2911. } catch (\Exception $e) {
  2912. return [
  2913. 'code' => 1,
  2914. 'msg' => $e->getMessage()
  2915. ];
  2916. }
  2917. }
  2918. //获取二维码规则
  2919. public function getQrcodeRules($appid)
  2920. {
  2921. try {
  2922. if (!$this->openPlatform) {
  2923. throw new \Exception("参数配置错误");
  2924. }
  2925. $client = new BaseClient($this->miniProgram($appid));
  2926. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
  2927. if (empty($res->errcode) && !empty($res)) {
  2928. return [
  2929. 'code' => 0,
  2930. 'msg' => "成功",
  2931. 'data' => $res
  2932. ];
  2933. } elseif (empty($res)) {
  2934. return [
  2935. 'code' => 1,
  2936. 'msg' => "数据错误"
  2937. ];
  2938. } else {
  2939. $res = json_decode($res, true);
  2940. return [
  2941. 'code' => $res['errcode'],
  2942. 'msg' => $this->getZnMsg($res),
  2943. 'data' => $res
  2944. ];
  2945. }
  2946. } catch (\Exception $e) {
  2947. return [
  2948. 'code' => 1,
  2949. 'msg' => $e->getMessage()
  2950. ];
  2951. }
  2952. }
  2953. //下载文件
  2954. public function downQrcodeRules($id, $appid)
  2955. {
  2956. try {
  2957. if (!$this->openPlatform) {
  2958. throw new \Exception("参数配置错误");
  2959. }
  2960. $client = new BaseClient($this->miniProgram($appid));
  2961. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
  2962. if (empty($res['errcode']) && !empty($res)) {
  2963. if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
  2964. // dir doesn't exist, make it
  2965. mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
  2966. }
  2967. $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $res['file_name'], $res['file_content']);
  2968. if ($file_result) {
  2969. $resRules = $this->setQrcodeRules($id, $appid);
  2970. if ($resRules['code'] === 0) {
  2971. $ress = $this->submitQrcodeRules($id, $appid);
  2972. \Yii::error($ress);
  2973. if ($ress['code'] === 0) {
  2974. $resGet = $this->getQrcodeRules($appid);
  2975. \Yii::error($resGet);
  2976. } else {
  2977. throw new \Exception($ress['msg']);
  2978. }
  2979. } else {
  2980. throw new \Exception($resRules['msg']);
  2981. }
  2982. } else {
  2983. throw new \Exception("创建文件失败");
  2984. }
  2985. return [
  2986. 'code' => 0,
  2987. 'msg' => "成功",
  2988. 'data' => $res,
  2989. 'file' => $file_result
  2990. ];
  2991. } elseif (empty($res)) {
  2992. throw new \Exception("数据错误");
  2993. } else {
  2994. $res = json_decode($res, true);
  2995. throw new \Exception($res['errmsg']);
  2996. }
  2997. } catch (\Exception $e) {
  2998. return [
  2999. 'code' => 1,
  3000. 'msg' => $e->getMessage()
  3001. ];
  3002. }
  3003. }
  3004. //删除二维码规则
  3005. public function delQrcodeRules()
  3006. {
  3007. try {
  3008. $store = StoreMini::findOne($this->mini_id);
  3009. if ($store->appid) {
  3010. //查询所有普通二维码
  3011. $res = $this->getQrcodeRules($store->appid);
  3012. if ($res['code'] !== 0) {
  3013. return $res;
  3014. }
  3015. $rules_list = $res['data']['rule_list'];
  3016. if (!empty($rules_list)) {
  3017. //全部删除
  3018. foreach ($rules_list as $item) {
  3019. $data = [
  3020. 'prefix' => $item['prefix']
  3021. ];
  3022. $client = new BaseClient($this->miniProgram($store->appid));
  3023. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdelete', $data);
  3024. if (empty($res)) {
  3025. return [
  3026. 'code' => 1,
  3027. 'msg' => "数据错误"
  3028. ];
  3029. } elseif (!empty($res->errcode)) {
  3030. $res = json_decode($res, true);
  3031. return [
  3032. 'code' => $res['errcode'],
  3033. 'msg' => $this->getZnMsg($res),
  3034. 'data' => $res
  3035. ];
  3036. }
  3037. }
  3038. }
  3039. //将商城显示解除绑定
  3040. $store->bind_food_qr = 0;
  3041. if (!$store->save()) {
  3042. throw new \Exception(json_encode($store->errors));
  3043. }
  3044. Option::set('store_home_is_bind_wx', 0, get_store_id(), 'store');
  3045. $store_qrcode = AggregateQrcode::findOne(['store_id' => get_store_id()]);
  3046. if ($store_qrcode) {
  3047. $store_qrcode->wx_status = 0;
  3048. $store_qrcode->wx_mini_id = 0;
  3049. if (!$store_qrcode->save()) {
  3050. throw new \Exception(json_encode($store_qrcode->errors));
  3051. }
  3052. }
  3053. return [
  3054. 'code' => 0,
  3055. 'msg' => '解除成功'
  3056. ];
  3057. }
  3058. throw new \Exception('没有绑定小程序');
  3059. } catch (\Exception $e) {
  3060. return [
  3061. 'code' => 1,
  3062. 'msg' => $e->getMessage()
  3063. ];
  3064. }
  3065. }
  3066. //微信支付进件
  3067. public function mchRegister()
  3068. {
  3069. $mini = StoreMini::find()->where(['id' => $this->mini_id])->select('id,appid')->one();
  3070. if (empty($mini->appid)) {
  3071. return [
  3072. 'code' => 0,
  3073. 'msg' => "未找到对应的小程序信息"
  3074. ];
  3075. }
  3076. $merchant_info = MerchantInfo::find()->where(['bind_store_id' => get_store_id(), 'is_delete' => 0])->one();
  3077. if (empty($merchant_info)) {
  3078. // $StoreSchedule = StoreSchedule::find()->where(['store_id' => get_store_id()])->one();
  3079. // if (!empty($StoreSchedule->merchant_info)) {
  3080. // $merchant_info = json_decode($StoreSchedule->merchant_info, true);
  3081. // $contact_info = $merchant_info['contact_info'];
  3082. // $subject_info = $merchant_info['subject_info'];
  3083. // $business_info = $merchant_info['business_info'];
  3084. // $bank_account_info = $merchant_info['bank_account_info'];
  3085. // } else {
  3086. // return [
  3087. // 'code' => 1,
  3088. // 'msg' => "未找到对应的进件数据"
  3089. // ];
  3090. // }
  3091. return [
  3092. 'code' => 1,
  3093. 'msg' => "未找到对应的进件数据"
  3094. ];
  3095. } else {
  3096. $contact_info = !empty($merchant_info->contact_info) ? json_decode($merchant_info->contact_info, true) : "";
  3097. $subject_info = !empty($merchant_info->subject_info) ? json_decode($merchant_info->subject_info, true) : "";
  3098. $business_info = !empty($merchant_info->business_info) ? json_decode($merchant_info->business_info, true) : "";
  3099. $bank_account_info = !empty($merchant_info->bank_account_info) ? json_decode($merchant_info->bank_account_info, true) : "";
  3100. }
  3101. $Merchant = new Merchant();
  3102. return $Merchant->submit($contact_info, $subject_info, $business_info, $bank_account_info, !empty($merchant_info->id) ? $merchant_info->id : 0, $mini->appid);
  3103. }
  3104. public function servicemarket_service_login_auth($code) {
  3105. try {
  3106. $client = new BaseClient($this->openPlatform);
  3107. $postData = [
  3108. 'code' => $code,
  3109. ];
  3110. $res = $client->httpPostJson('wxa/servicemarket/service/login_auth', $postData);
  3111. \Yii::error([$res, json_encode($postData)]);
  3112. // var_dump($res);die;
  3113. // $res = [
  3114. // 'order_id' => 3118683324757311491,
  3115. // 'openid' => '',
  3116. // 'appid' => 'wxedd15801cce16326',
  3117. // 'service_id' => 3118677029492637699,
  3118. // 'sku_id' => '4711132',
  3119. // 'spec_id' => 'free',
  3120. // 'errcode' => 0,
  3121. // 'errmsg' => 'ok',
  3122. // ];
  3123. \Yii::error([$res, json_encode($postData)]);
  3124. if (empty($res['errcode']) && !empty($res)) {
  3125. return [
  3126. 'code' => 0,
  3127. 'msg' => "成功",
  3128. 'data' => $res,
  3129. ];
  3130. } else {
  3131. throw new \Exception($res['errmsg'] ?? '');
  3132. }
  3133. } catch (\Exception $e) {
  3134. \Yii::error($e);
  3135. return [
  3136. 'code' => 1,
  3137. 'msg' => $e->getMessage()
  3138. ];
  3139. }
  3140. }
  3141. //服务市场appid实例化
  3142. public function fuwu_app() {
  3143. try {
  3144. $appid = Option::get('platform_wechat_fuwu_appid', 0, 'saas', '')['value'];
  3145. $secret = Option::get('platform_wechat_fuwu_secret', 0, 'saas', '')['value'];
  3146. if(empty($appid) || empty($secret)){
  3147. throw new \Exception("平台后台未配置服务市场信息");
  3148. }
  3149. $config =[];
  3150. $config['app_id'] = $appid;
  3151. $config['secret'] = $secret;
  3152. $config['response_type'] = 'array';
  3153. $app = Factory::officialAccount($config);
  3154. return [
  3155. 'code' => 0,
  3156. 'data' => $app,
  3157. ];
  3158. } catch (\Exception $e) {
  3159. \Yii::error($e);
  3160. return [
  3161. 'code' => 1,
  3162. 'msg' => $e->getMessage()
  3163. ];
  3164. }
  3165. }
  3166. //服务市场token获取
  3167. public function fuwu_token() {
  3168. try {
  3169. $fuwu_app = $this->fuwu_app();
  3170. if($fuwu_app['code'] != 0){
  3171. throw new \Exception($fuwu_app['msg']);
  3172. }
  3173. $app = $fuwu_app['data'];
  3174. $accessToken = $app->access_token;
  3175. $token = $accessToken->getToken();
  3176. if(empty($token['access_token'])){
  3177. throw new \Exception("服务市场token信息获取失败");
  3178. }
  3179. return [
  3180. 'code' => 0,
  3181. 'data' => $token['access_token'],
  3182. ];
  3183. } catch (\Exception $e) {
  3184. \Yii::error($e);
  3185. return [
  3186. 'code' => 1,
  3187. 'msg' => $e->getMessage()
  3188. ];
  3189. }
  3190. }
  3191. public function servicemarket_getwxaqrcode($service_id = '', $custom_params = ['a' => 1]) {
  3192. try {
  3193. $fuwu_app = $this->fuwu_app();
  3194. if($fuwu_app['code'] != 0){
  3195. throw new \Exception($fuwu_app['msg']);
  3196. }
  3197. $app = $fuwu_app['data'];
  3198. $client = new BaseClient($app);
  3199. $postData = [
  3200. 'service_id' => (string)$service_id,
  3201. 'custom_params' => http_build_query($custom_params),
  3202. ];
  3203. $res = $client->httpPostJson('wxa/servicemarket/getwxaqrcode', $postData);
  3204. // var_dump($res);die;
  3205. \Yii::error([$res, json_encode($postData)]);
  3206. if (empty($res['errcode']) && !empty($res)) {
  3207. return [
  3208. 'code' => 0,
  3209. 'msg' => "成功",
  3210. 'service_id' => $service_id,
  3211. 'data' => $res,
  3212. ];
  3213. } else {
  3214. throw new \Exception($res['errmsg'] ?? '');
  3215. }
  3216. } catch (\Exception $e) {
  3217. \Yii::error($e);
  3218. return [
  3219. 'code' => 1,
  3220. 'msg' => $e->getMessage(),
  3221. 'service_id' => $service_id,
  3222. ];
  3223. }
  3224. }
  3225. public function servicemarket_get_paid_order_list($appid = '', $service_id = '') {
  3226. try {
  3227. $fuwu_app = $this->fuwu_app();
  3228. if($fuwu_app['code'] != 0){
  3229. throw new \Exception($fuwu_app['msg']);
  3230. }
  3231. $app = $fuwu_app['data'];
  3232. $client = new BaseClient($app);
  3233. $postData = [
  3234. 'appid' => $appid,
  3235. 'service_id' => $service_id,
  3236. 'offset' => 0,
  3237. 'limit' => 10,
  3238. 'buyer_type' => 1,
  3239. ];
  3240. $res = $client->httpPostJson('wxa/servicemarket/get_paid_order_list', $postData);
  3241. // $res = [
  3242. // 'errcode' => 0,
  3243. // 'errmsg' => 'ok',
  3244. // 'order_list' => [
  3245. // [
  3246. // 'order_id' => 3138343822027653126,
  3247. // 'effective_time' => 1696652147,
  3248. // 'expire_time' => 1728188147,
  3249. // 'specification_id' => 'new',
  3250. // 'service_id' => 3118677029492637699,
  3251. // 'total_price' => 100,
  3252. // 'phone' => '',
  3253. // 'sku_id' => 3144177,
  3254. // ],
  3255. // [
  3256. // 'order_id' => 3118683324757311491,
  3257. // 'effective_time' => 1695480283,
  3258. // 'expire_time' => 1696085083,
  3259. // 'specification_id' => 'free',
  3260. // 'service_id' => 3118677029492637699,
  3261. // 'total_price' => 0,
  3262. // 'phone' => '',
  3263. // 'sku_id' => 4711132,
  3264. // ],
  3265. // ],
  3266. // 'total' => 2,
  3267. // ];
  3268. \Yii::error([$res, json_encode($postData)]);
  3269. // var_dump($res, $postData);die;
  3270. if (empty($res['errcode']) && !empty($res)) {
  3271. return [
  3272. 'code' => 0,
  3273. 'msg' => "成功",
  3274. 'data' => $res,
  3275. ];
  3276. } else {
  3277. throw new \Exception($res['errmsg'] ?? '');
  3278. }
  3279. } catch (\Exception $e) {
  3280. \Yii::error($e);
  3281. return [
  3282. 'code' => 1,
  3283. 'msg' => $e->getMessage()
  3284. ];
  3285. }
  3286. }
  3287. public function servicemarket_service_get_service_buyer_list($service_id = '') {
  3288. try {
  3289. $fuwu_app = $this->fuwu_app();
  3290. if($fuwu_app['code'] != 0){
  3291. throw new \Exception($fuwu_app['msg']);
  3292. }
  3293. $app = $fuwu_app['data'];
  3294. $client = new BaseClient($app);
  3295. $postData = [
  3296. 'service_id' => $service_id,
  3297. 'offset' => 0,
  3298. 'limit' => 10,
  3299. 'buyer_type' => 1,
  3300. ];
  3301. $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer_list', $postData);
  3302. // $res = [
  3303. // 'errcode' => 0,
  3304. // 'errmsg' => 'ok',
  3305. // 'buyer_list' => [
  3306. // [
  3307. // 'appid' => 'wxedd15801cce16326',
  3308. // 'service_id' => 3118677029492637699,
  3309. // 'spec_list' => [
  3310. // [
  3311. // 'specification_id' => 'free',
  3312. // 'expire_time' => 1696085083,
  3313. // ],
  3314. // [
  3315. // 'specification_id' => 'new',
  3316. // 'expire_time' => 1728188147,
  3317. // ],
  3318. // ],
  3319. // ],
  3320. // ],
  3321. // 'count' => 1,
  3322. // ];
  3323. \Yii::error([$res, json_encode($postData)]);
  3324. // var_dump($res, $postData);die;
  3325. if (empty($res['errcode']) && !empty($res)) {
  3326. return [
  3327. 'code' => 0,
  3328. 'msg' => "成功",
  3329. 'data' => $res,
  3330. ];
  3331. } else {
  3332. throw new \Exception($res['errmsg'] ?? '');
  3333. }
  3334. } catch (\Exception $e) {
  3335. \Yii::error($e);
  3336. return [
  3337. 'code' => 1,
  3338. 'msg' => $e->getMessage()
  3339. ];
  3340. }
  3341. }
  3342. public function servicemarket_service_get_service_buyer($appid = '', $service_id = '') {
  3343. try {
  3344. $fuwu_app = $this->fuwu_app();
  3345. if($fuwu_app['code'] != 0){
  3346. throw new \Exception($fuwu_app['msg']);
  3347. }
  3348. $app = $fuwu_app['data'];
  3349. $client = new BaseClient($app);
  3350. $postData = [
  3351. 'appid' => $appid,
  3352. 'service_id' => $service_id,
  3353. ];
  3354. $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer', $postData);
  3355. // $res = [
  3356. // 'errcode' => 0,
  3357. // 'errmsg' => 'ok',
  3358. // 'buyer' => [
  3359. // 'appid' => 'wxedd15801cce16326',
  3360. // 'service_id' => 3118677029492637699,
  3361. // 'spec_list' => [
  3362. // [
  3363. // 'specification_id' => 'free',
  3364. // 'expire_time' => 1696085083,
  3365. // ],
  3366. // [
  3367. // 'specification_id' => 'new',
  3368. // 'expire_time' => 1728188147,
  3369. // ],
  3370. // ],
  3371. // ],
  3372. // ];
  3373. \Yii::error([$res, json_encode($postData)]);
  3374. // var_dump($res, $postData);die;
  3375. if (empty($res['errcode']) && !empty($res)) {
  3376. return [
  3377. 'code' => 0,
  3378. 'msg' => "成功",
  3379. 'data' => $res,
  3380. ];
  3381. } else {
  3382. throw new \Exception($res['errmsg'] ?? '');
  3383. }
  3384. } catch (\Exception $e) {
  3385. \Yii::error($e);
  3386. return [
  3387. 'code' => 1,
  3388. 'msg' => $e->getMessage()
  3389. ];
  3390. }
  3391. }
  3392. //从服务市场登陆
  3393. public function servicemarketAuthCode($code = '', $fuwu_type = 1) {
  3394. try {
  3395. $auth = $this->servicemarket_service_login_auth($code);
  3396. if($auth['code'] != 0){
  3397. throw new \Exception($auth['msg']);
  3398. }
  3399. // return $this->servicemarket_getwxaqrcode($auth['data']['service_id']);
  3400. // return $this->servicemarket_get_paid_order_list($auth['data']['appid'], $auth['data']['service_id']);
  3401. // return $this->servicemarket_service_get_service_buyer_list($auth['data']['service_id']);
  3402. // return $this->servicemarket_service_get_service_buyer($auth['data']['appid'], $auth['data']['service_id']);
  3403. return $this->servicemarketAuth($auth['data']['appid'], $auth['data']['service_id'], $fuwu_type);
  3404. } catch (\Exception $e) {
  3405. \Yii::error($e);
  3406. return [
  3407. 'code' => 1,
  3408. 'msg' => $e->getMessage()
  3409. ];
  3410. }
  3411. }
  3412. //从服务市场登陆时(认证、初始化小程序)
  3413. public function servicemarketAuth($appid, $service_id = '', $fuwu_type = 1) {
  3414. try {
  3415. $service_ids = explode(',', Option::get('platform_wechat_fuwu_serviceId', 0, 'saas', '')['value']);
  3416. if(!in_array($service_id, $service_ids)){
  3417. throw new \Exception("服务信息未配置");
  3418. }
  3419. $mini = StoreMini::findOne(['appid' => $appid]);
  3420. if(!$mini){
  3421. $mini = new StoreMini();
  3422. }
  3423. //初始化小程序
  3424. $mini->fuwu_type = $fuwu_type;
  3425. $mini->appid = $appid;
  3426. $mini->is_cancle = 0;
  3427. $hasAuth = 0;
  3428. $res = $this->openPlatform->getAuthorizer($appid);
  3429. if (empty($res['errcode']) && !empty($res)) {
  3430. $hasAuth = 1;
  3431. $mini->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
  3432. }else{
  3433. debug_log([__METHOD__, __LINE__, $res], __CLASS__ . '.log');
  3434. throw new \Exception($res['errmsg']);
  3435. }
  3436. $mini->save();
  3437. if($hasAuth){
  3438. //如果授权过三方,填充小程序信息
  3439. $miniProgram = $this->openPlatform->miniProgram($mini->appid, $mini->authorizer_refresh_token);
  3440. $client = new BaseClient($miniProgram);
  3441. $res = $client->httpGet('channels/ec/basics/info/get');
  3442. // var_dump($res);die;
  3443. if (empty($res['errcode']) && !empty($res)) {
  3444. $mini->mini_nickname = $res['info']['nickname'];
  3445. $mini->mini_url = $res['info']['headimg_url'];
  3446. if(!$mini->save()){
  3447. debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log');
  3448. }
  3449. }
  3450. }
  3451. if($mini->fuwu_type == StoreMini::FUWU_TYPE_VIDEO_SHOP && $hasAuth && $mini->mini_nickname){
  3452. $buyer = $this->servicemarket_service_get_service_buyer($appid, $service_id);
  3453. if($buyer['code'] != 0){
  3454. debug_log([__METHOD__, __LINE__, $buyer], __CLASS__ . '.log');
  3455. throw new \Exception($buyer['msg']);
  3456. }
  3457. $end_time = 0;
  3458. if(isset($buyer['data']['buyer']['spec_list'])){
  3459. foreach($buyer['data']['buyer']['spec_list'] as $item){
  3460. if($item['expire_time'] > $end_time){
  3461. $end_time = $item['expire_time'];
  3462. }
  3463. }
  3464. }
  3465. // var_dump($end_time);die;
  3466. if(!$mini->store_id){
  3467. //初始化店铺
  3468. if($end_time < time()){
  3469. $end_time = time() + 86400 * 7;
  3470. }
  3471. $storeForm = new StoreForm();
  3472. $username = $mini->mini_nickname . time();
  3473. $add = $storeForm->add($username, '', $username, [], 0, $mini->mini_url, $end_time);
  3474. if($add['code'] != 0){
  3475. debug_log([__METHOD__, __LINE__, $add['msg']], __CLASS__ . '.log');
  3476. }else{
  3477. $store = $add['store'];
  3478. $mini->store_id = $store['id'];
  3479. if(!$mini->save()){
  3480. debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log');
  3481. }
  3482. }
  3483. }else{
  3484. //更新店铺到期时间
  3485. $store = Store::findOne($mini->store_id);
  3486. if($end_time > time() && $store->end_time < $end_time){
  3487. $store->end_time = $end_time;
  3488. $store->save();
  3489. }
  3490. }
  3491. }
  3492. // var_dump($store->attributes, $mini->attributes);die;
  3493. return [
  3494. 'code' => 0,
  3495. 'msg' => "成功",
  3496. 'data' => [
  3497. 'mini' => $mini,
  3498. 'store' => $store ?? null,
  3499. ],
  3500. ];
  3501. } catch (\Exception $e) {
  3502. \Yii::error($e);
  3503. return [
  3504. 'code' => 1,
  3505. 'msg' => $e->getMessage()
  3506. ];
  3507. }
  3508. }
  3509. //下载文件
  3510. public function getWebviewFile() {
  3511. $mini_id = $this->mini_id;
  3512. $store_mini = StoreMini::findOne($mini_id);
  3513. if (empty($store_mini)) {
  3514. return [
  3515. 'code' => 1,
  3516. 'msg' => '查询数据失败'
  3517. ];
  3518. }
  3519. if (!$this->openPlatform) {
  3520. return [
  3521. 'code' => 1,
  3522. 'msg' => '参数配置错误'
  3523. ];
  3524. }
  3525. $miniProgram = $this->miniProgram($store_mini->appid);
  3526. $result = $this->addConfirmFile($miniProgram);
  3527. if ($result['code'] === 1) {
  3528. return $result;
  3529. }
  3530. return $this->addFile2Other($result['data']);
  3531. }
  3532. //设置业务域名 (三方平台修改小程序业务域名)
  3533. public function setWebviewDomain() {
  3534. $mini_id = $this->mini_id;
  3535. $store_mini = StoreMini::findOne($mini_id);
  3536. if (empty($store_mini)) {
  3537. return [
  3538. 'code' => 1,
  3539. 'msg' => '查询数据失败'
  3540. ];
  3541. }
  3542. if (\Yii::$app->request->isPost) {
  3543. $webview_domain_name_ = post_params('webview_domain_name');
  3544. $webview_domain_name_ = json_decode($webview_domain_name_, true);
  3545. $webview_domain_name = [];
  3546. foreach ($webview_domain_name_ as $item) {
  3547. if (!empty($item['url'])) {
  3548. $index = strstr($item['url'], 'http');
  3549. if (!$index) {
  3550. $item['url'] = 'https://' . $item['url'];
  3551. }
  3552. $item['url'] = \str_replace('http://', 'https://', $item['url']);
  3553. $webview_domain_name = array_merge($webview_domain_name, [[
  3554. 'url' => $item['url']
  3555. ]]);
  3556. }
  3557. }
  3558. $store_mini->webview_domain_name = json_encode($webview_domain_name);
  3559. if (!$store_mini->save()) {
  3560. return [
  3561. 'code' => 1,
  3562. 'msg' => implode(';', array_values($store_mini->firstErrors))
  3563. ];
  3564. };
  3565. return $this->setDomainName();
  3566. } else {
  3567. $data = [
  3568. 'init_webview_domain_name' => [
  3569. // [
  3570. // 'url' => 'https://' . \Yii::$app->params['ws_url'],
  3571. // ]
  3572. ],
  3573. 'webview_domain_name' => [
  3574. [
  3575. 'url' => ''
  3576. ]
  3577. ]
  3578. ];
  3579. if (!empty($store_mini->webview_domain_name)) {
  3580. $webview_domain_name = json_decode($store_mini->webview_domain_name, true);
  3581. if (!empty($webview_domain_name)) {
  3582. $data['webview_domain_name'] = $webview_domain_name;
  3583. }
  3584. }
  3585. return [
  3586. 'code' => 0,
  3587. 'msg' => '获取成功',
  3588. 'data' => $data
  3589. ];
  3590. }
  3591. }
  3592. //获取小程序短链$app->short_link->getShortLink
  3593. public function getShortLink($path = '', $mini_id = 0, $is_platform = 0, $is_serve = 0) {
  3594. try {
  3595. $app = WechatMini::getWechatConfig($this->store_id, $mini_id, 0, $is_platform, $is_serve);
  3596. if (empty($app)) {
  3597. throw new \Exception('');
  3598. }
  3599. if (empty($path)) {
  3600. throw new \Exception("页面路径不能为空");
  3601. }
  3602. //获取接口缓存
  3603. // $app_id = $app->getConfig()['app_id'];
  3604. // $cache_key = md5($this->store_id . '_'. $app_id . '_' . $path . '_short_link_');
  3605. // if ($cache_key) {
  3606. // return [
  3607. // 'code' => 0,
  3608. // 'msg' => "设置成功",
  3609. // 'data' => $cache_key
  3610. // ];
  3611. // }
  3612. $result = $app->short_link->getShortLink($path, '');
  3613. if (empty($result['errcode']) && !empty($result)) {
  3614. if (empty($result['link'])) {
  3615. throw new \Exception("参数获取失败");
  3616. }
  3617. //增加接口缓存
  3618. // cache()->set($cache_key, $result, 60);
  3619. return [
  3620. 'code' => 0,
  3621. 'msg' => "设置成功",
  3622. 'data' => $result
  3623. ];
  3624. } elseif (empty($result)) {
  3625. throw new \Exception("数据错误");
  3626. } else {
  3627. throw new \Exception($this->getZnMsg($result) );
  3628. }
  3629. } catch (\Exception $e) {
  3630. return [
  3631. 'code' => 1,
  3632. 'msg' => $e->getMessage()
  3633. ];
  3634. }
  3635. }
  3636. //服务市场服务二维码批量获取
  3637. public function serviceQrcode($service_ids = '') {
  3638. if($service_ids && !is_array($service_ids)){
  3639. $service_ids = explode(',', $service_ids);
  3640. }
  3641. $ret = [];
  3642. foreach((array)$service_ids as $id){
  3643. $qr = $this->servicemarket_getwxaqrcode($id);
  3644. $ret[] = $qr;
  3645. }
  3646. return $ret;
  3647. }
  3648. //获取网络图片到临时目录
  3649. private function saveTempImage($url)
  3650. {
  3651. if (strpos($url, 'http') === false) {
  3652. $url = 'http:' . trim($url);
  3653. }
  3654. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  3655. mkdir(\Yii::$app->runtimePath . '/image');
  3656. }
  3657. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  3658. CurlHelper::download($url, $save_path);
  3659. return $save_path;
  3660. }
  3661. //返回中文错误信息
  3662. public function getZnMsg($result)
  3663. {
  3664. \Yii::error([__METHOD__, $result]);
  3665. $ErrorMsg = new ErrorMsg();
  3666. $arr = $ErrorMsg->getArray();
  3667. $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
  3668. return $msg;
  3669. }
  3670. //返回中文错误信息
  3671. public static function staticGetZnMsg($result)
  3672. {
  3673. \Yii::error([__METHOD__, $result]);
  3674. $ErrorMsg = new ErrorMsg();
  3675. $arr = $ErrorMsg->getArray();
  3676. $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
  3677. return $msg;
  3678. }
  3679. }