| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\Admin;
- use app\models\AggregateQrcode;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\StoreMedia;
- use app\models\StoreMini;
- use app\models\StoreMiniAuth;
- use app\models\StoreMiniAuthIcp;
- use app\models\StoreMiniIcp;
- use app\models\StoreSchedule;
- use app\models\WechatConfig;
- use app\modules\admin\models\file\FileForm;
- use app\utils\CurlHelper;
- use app\utils\Wechat\WechatMini;
- use app\utils\WechatMerchant\Merchant;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use EasyWeChat\BasicService\Media\Client;
- use yii\base\Model;
- use yii\log\Logger;
- use app\modules\admin\models\WechatThirdErrorMsgForm as ErrorMsg;
- use Exception;
- use app\models\OrderUnion;
- use app\models\Store;
- use app\models\Order;
- use app\utils\Wechat\Wechat;
- use app\utils\Wechat\WechatProfit;
- use app\models\ReOrder;
- class WechatThirdForm extends Model
- {
- /**
- * @var mixed
- */
- public $fuwu_type;
- public $openPlatform;
- public $config;
- public $appid;
- public $data = [];
- public $base_url = "https://api.weixin.qq.com/cgi-bin/";
- public $name; // 企业名
- public $code; // 企业代码
- public $code_type; // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
- public $legal_persona_wechat; // 法人微信
- public $legal_persona_name; // 法人姓名
- public $component_phone; //第三方联系电话
- public $authorization_code;
- public $action; // 0获取 1添加 2删除 3覆盖
- public $requestdomain;
- public $wsrequestdomain;
- public $uploaddomain;
- public $downloaddomain;
- public $udpdomain;
- public $tcpdomai;
- public $webviewdomain;
- public $media_image;
- public $mini_nickname;
- public $id_card;
- public $license;
- public $naming_other_stuff_1;
- public $naming_other_stuff_2;
- public $naming_other_stuff_3;
- public $naming_other_stuff_4;
- public $naming_other_stuff_5;
- public $head_img_media_id; //上传头像 头像素材 media_id
- public $x1 = 0; //上传头像 裁剪框左上角 x 坐标(取值范围:[0, 1])
- public $y1 = 0; //上传头像 裁剪框左上角 y 坐标(取值范围:[0, 1])
- public $x2 = 1; //上传头像 裁剪框右下角 x 坐标(取值范围:[0, 1])
- public $y2 = 1; //上传头像 裁剪框右下角 y 坐标(取值范围:[0, 1])
- public $signature; //简介
- public $categories;
- public $cate_first;
- public $cate_second;
- public $user_version; //代码版本号
- public $user_desc = "小程序新版本上传"; //代码描述
- public $is_fast_audit = 0;
- public $privacy_ver = 2; //用户隐私保护指引的版本,1表示现网版本;2表示开发版。默认是2开发版。
- public $owner_setting;
- public $setting_list;
- public $file_url;
- public $file;
- public $mini_id;
- public $mini_path;
- public $item_list;
- public $feedback_info;
- public $feedback_stuff;
- public $is_use;
- public $license_pic;
- public $wechatId;
- public $status;
- public $merchant_id = 0;
- public $store_id;
- public $bind_store_id;
- public $start_time;
- public $end_time;
- public function rules()
- {
- return [
- [[
- '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'
- , 'start_time', 'end_time'], 'string'],
- [['code_type', 'mini_id', 'merchant_id', 'store_id', 'bind_store_id', 'fuwu_type'], 'integer'],
- [['file'], 'file'],
- [['item_list', 'feedback_stuff'], 'array']
- ];
- }
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->store_id = $this->store_id ?: get_store_id();
- $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
- if (empty($verify_ticket)) {
- $this->openPlatform = null;
- }else {
- $this->config = [
- 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
- 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
- 'token' => Option::get("platform_token", 0, 'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
- ];
- $this->config['http'] = [
- 'max_retries' => 0,
- 'retry_delay' => 500,
- 'timeout' => 50.0,
- 'read_timeout' => 50,
- // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
- ];
- $this->openPlatform = Factory::openPlatform($this->config);
- }
- }
- public function is_trade_managed($app) {
- if (!$app) {
- return;
- }
- $appid = $app->getConfig()['app_id'];
- $key = 'is_trade_managed' . (string)$appid;
- $cache = cache()->get($key);
- if($cache > 0){
- return true;
- }
- if($cache < 0){
- return false;
- }
- $postData = [
- 'appid' => (string)$appid,
- ];
- $client = new BaseClient($app);
- $result = $client->httpPostJson('wxa/sec/order/is_trade_managed', $postData);
- if (!empty($result)) {
- if ($result['errcode'] == '0') {
- if($result['is_trade_managed']){
- cache()->set($key, 1, 86400);
- return true;
- }else{
- cache()->set($key, -1, 86400);
- }
- }else{
- cache()->set($key, -1, 86400);
- }
- \Yii::error(['--------is_trade_managed-error---- ', $result]);
- }
- return false;
- }
- public function storeWxOrderShipping($store_id) {
- try {
- Wechat::init($store_id);
- //$app = Wechat::$wechat_mini;
- $app = WechatMini::getWechatConfig($store_id);
- if (!$app) {
- return;
- }
- $is_trade_managed = $this->is_trade_managed($app);
- if(!$is_trade_managed){
- // \Yii::error(['--------$is_trade_managed=false---- ', $store_id]);
- return;
- }
- //收货
- $query = Order::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'pay_type' => Order::PAY_TYPE_WECHAT, 'is_pay' => 1]);
- $query->andWhere(['trade_status' => Order::ORDER_FLOW_SEND]);
- $orders = $query->limit(50)->all();
- // debug_log([$store_id, count($orders)], __CLASS__);
- foreach ($orders as $order) {
- if($this->wxOrderStateIsConfirm($app, $order)){
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- $order->confirm_time = time();
- $order->save();
- }
- }
- //发货
- $wxOrderGetNoShippingList = $this->wxOrderGetNoShippingList($app);
- // debug_log([$store_id, $wxOrderGetNoShippingList], __CLASS__);
- if($wxOrderGetNoShippingList['code'] != 0){
- return;
- }
- if(empty($wxOrderGetNoShippingList['data']['order_list'])){
- return;
- }
- $max = 300;
- foreach($wxOrderGetNoShippingList['data']['order_list'] as $item){
- $order_no = (string)$item['merchant_trade_no'];
- $order = Order::findOne(['order_no' => $order_no]);
- //商城订单需要看发货状态,其他订单直接发货
- if($order && !$order['send_time']){
- continue;
- }
- //有些订单会延时写order表,所以支付后3分钟再处理发货逻辑
- if(!$order && ($item['pay_time'] > (time() - 60 * 3))){
- continue;
- }
- //拼团订单,没写order表时,跳出
- if((!$order) && \app\utils\OrderNo::checkOrderNo($order_no, \app\utils\OrderNo::ORDER_PT)){
- continue;
- }
- $client = new BaseClient($app);
- $postData = [
- 'order_key' => [
- 'order_number_type' => 2,
- 'transaction_id' => (string)$item['transaction_id'],
- ],
- 'logistics_type' => Order::getWxOrderLocalType($store_id, (string)$item['merchant_trade_no'], $order),
- 'delivery_mode' => 1,
- 'shipping_list' => [
- [
- 'item_desc' => '微信支付',
- ],
- ],
- 'upload_time' => date(DATE_RFC3339),
- 'payer' => [
- 'openid' => (string)$item['openid'],
- ],
- ];
- //快递发货
- if($postData['logistics_type'] == 1){
- $tracking_no = $order['express_no'] ?: $order['order_no'];
- $express_company = \app\models\Express::getExpressCode($order['express']);
- $receiver_contact = substr_replace($order['mobile'], '****', 3, 4);
- $postData['shipping_list'] = [
- [
- 'item_desc' => '微信支付',
- 'tracking_no' => $tracking_no,
- 'express_company' => $express_company,
- 'contact' => [
- 'receiver_contact' => $receiver_contact,
- ],
- ]
- ];
- }
- $result = $client->httpPostJson('wxa/sec/order/upload_shipping_info', $postData);
- \Yii::error([$result, json_encode($postData)]);
- if($max-- <= 0){
- break;
- }
- }
- } catch (\Exception $e) {
- }
- }
- //订单是否已收货状态
- public function wxOrderStateIsConfirm($app, $order) {
- if(!$order){
- return false;
- }
- if ($order['pay_time'] > 0 && $order['pay_time'] < strtotime('-30 DAY')) {
- return true;
- }
- $orderRes = $this->wxOrderGet($app, $order['transaction_id']);
- if(!empty($orderRes['data']) && !empty($orderRes['data']['order'])){
- return in_array($orderRes['data']['order']['order_state'], [3, 4]);
- }
- return false;
- }
- //查询订单
- public function wxOrderGet($app, $transaction_id) {
- try {
- $postData = [
- 'transaction_id' => (string)$transaction_id,
- ];
- $client = new BaseClient($app);
- $result = $client->httpPostJson('wxa/sec/order/get_order', $postData);
- if (!empty($result)) {
- if ($result['errcode'] == '0') {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result,
- ];
- }
- if ($result['errcode'] == "10060001") {//支付单不存在
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'order' => [
- 'order_state' => 4
- ]
- ],
- ];
- }
- }
- \Yii::warning(['--------wxOrderGet-error---- ', $result]);
- return [
- 'code' => 1,
- 'msg' => "获取失败" . $result['errmsg'],
- 'data' => $result,
- ];
- } catch(\Exception $e) {
- \Yii::warning(['--------wxOrderGet-error---- ', $e]);
- }
- return [
- 'code' => 1,
- 'msg' => "获取失败",
- ];
- }
- //查询订单
- public function wxOrderGetNoShippingList($app, $last_index = null) {
- try {
- $postData = [
- 'order_state' => 1,
- ];
- $last_index && $postData['last_index'] = $last_index;
- $client = new BaseClient($app);
- $result = $client->httpPostJson('wxa/sec/order/get_order_list', $postData);
- if (!empty($result)) {
- if ($result['errcode'] == '0') {
- if($result['has_more']){
- $more = $this->wxOrderGetNoShippingList($app, $result['last_index']);
- if(!$more['code'] && $more['data']['order_list']){
- $result['order_list'] = array_merge($result['order_list'], $more['data']['order_list']);
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result,
- ];
- }
- }
- \Yii::warning(['--------wxOrderGet-error---- ', $result]);
- return [
- 'code' => 1,
- 'msg' => "获取失败" . $result['errmsg'],
- 'data' => $result,
- ];
- } catch(\Exception $e) {
- \Yii::warning(['--------wxOrderGet-error---- ', $e]);
- }
- return [
- 'code' => 1,
- 'msg' => "获取失败",
- ];
- }
- /**
- * mini业务
- */
- public function miniProgram($appid = "")
- {
- $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : $this->store_id;
- $store = StoreMini::find()->where(['appid' => $appid, 'store_id' => $store_id ?: get_store_id()])->one();
- if (empty($store)) {
- $store = StoreMini::find()->where(['or', ['id' => $this->mini_id], ['store_id' => $store_id ?: get_store_id()]])->orderBy("id desc")->one();
- }
-
-
- return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
- }
- /**
- * 获取授权方的帐号基本信息
- */
- public function api_get_authorizer_info()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store = StoreMini::findOne($this->mini_id);
- if (empty($store->appid)) {
- throw new \Exception("当前没有绑定小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->getAuthorizer($store->appid);
- if (empty($res->errcode) && !empty($res)) {
- $store = StoreMini::find()->where(['store_id' => $this->store_id])->one();
- if (!empty($res->authorizer_info->qrcode_url)) {
- $store->mini_url = $res->authorizer_info->qrcode_url;
- $store->save();
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($res->errmsg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 快速注册小程序
- */
- public function fastRegisterMini($refresh = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = [
- 'name' => $this->name,
- 'code' => $this->code,
- 'code_type' => $this->code_type,
- 'legal_persona_wechat' => $this->legal_persona_wechat,
- 'legal_persona_name' => $this->legal_persona_name,
- 'component_phone' => Option::get("platform_phone", 0, 'saas')['value']
- ];
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- } else {
- $store_id = $this->store_id;
- }
- if(!$refresh){
- $store = StoreMini::find()->where(['AND',
- ['code' => $this->code, 'status' => -1, 'store_id' => $store_id],
- ['IS', 'appid', NULL]
- ])->one();
- }
- if ($refresh || empty($store)) {
- $store = new StoreMini();
- $store->store_id = $store_id;
- }
- $store->name = $this->name;
- $store->code = $this->code;
- $store->code_type = $this->code_type * 1;
- $store->legal_persona_wechat = $this->legal_persona_wechat;
- $store->legal_persona_name = $this->legal_persona_name;
- $store->license_pic = $this->license_pic;
- $store->merchant_id = $this->merchant_id;
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->component->registerMiniProgram($data);
- // if ($res['errcode'] == '89249') {
- // debug_log([__METHOD__, $res], __CLASS__ . '.log');
- // $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name);
- // if ($res['errcode'] == 89251) {
- // debug_log([__METHOD__, $res], __CLASS__ . '.log');
- // $store->status = -1;
- // $store->save();
- // }
- // return [
- // 'code' => $res['errcode'],
- // 'msg' => $this->getZnMsg($res),
- // ];
- // }
- if (empty($res['errcode']) && !empty($res)) {
- $store->status = -1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "申请成功,请等待审核",
- 'data' => $store->id
- ];
- } elseif (empty($res)) {
- $store->save();
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $store->save();
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- // $weChatUrl = $this->base_url."component/fastregisterweapp?action=create&component_access_token=".$this->data['component_access_token'];
- }
- /**
- * 手动查询快速创建任务状态
- */
- public function getRegisterStatus($id = 0)
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- } else {
- $store_id = $this->store_id;
- }
- if($id){
- $store = StoreMini::findOne($id);
- }else{
- $store = StoreMini::findOne(['store_id' => $store_id]);
- }
- if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name);
- \Yii::error($res);
- return $res;
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 返回授权需要的参数
- */
- public function backAuthInfo()
- {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]);
- return [
- 'code' => 0,
- 'msg' => "请访问链接依次来授权",
- 'url' => $res
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 存储授权码
- */
- // public function setAuthorization_code(){
- // try {
- // $store = new StoreMini();
- // $store->store_id = get_store_id();
- // $store->auth_code = json_encode([
- // 'authorization_code'=>$this->authorization_code,
- // 'end_time'=>time()*1 + 3600*1
- // ]);
- // $store->save();
- // return $this->getAuthorization_info($this->authorization_code);
- // }catch (\Exception $e){
- // return [
- // 'code'=>1,
- // 'msg'=>$e->getMessage()
- // ];
- // }
- //
- //
- // }
- public function getAuthorizers()
- {
- $res = $this->openPlatform->getAuthorizers();
- return [
- 'code' => 0,
- 'data' => $res,
- ];
- }
- /**
- * 使用授权码获取授权信息
- */
- public function getAuthorization_info($authorization_code)
- {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->handleAuthorize($authorization_code);
- \Yii::error($res);
- if (empty($res['errcode']) && !empty($res)) {
- $store_id = $this->store_id;
- if ($store_id * 1 > 0 || !empty($this->mini_id)) {
- $store = StoreMini::find()->where(['appid' => $res['authorization_info']['authorizer_appid'], 'store_id' => $store_id])->one();
- if (empty($store)) {
- $store = new StoreMini();
- $store->store_id = $store_id;
- }
- $store->appid = $res['authorization_info']['authorizer_appid'];
- $store->is_cancle = 0;
- $store->status = 1;
- $store->auth_code = json_encode([
- 'authorization_code' => $authorization_code,
- 'end_time' => time() * 1 + 3600 * 1
- ]);
- $store->authorizer_access_token = json_encode([
- 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
- 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
- ]);
- $store->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
- $result = $store->save();
- \Yii::error($result);
- if ($result) {
- return $this->getMiniInfo($res['authorization_info']['authorizer_access_token'], $res['authorization_info']['authorizer_appid']);
- }
- }
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取小程序基础信息
- */
- public function getMiniInfo($authorizer_access_token = "", $appid = "")
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $store = StoreMini::findOne(['appid' => $appid, 'store_id' => $store_id]);
- if (empty($authorizer_access_token) && empty($store)) {
- throw new \Exception("暂未授权");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($appid);
- $res = $miniProgram->account->getBasicInfo();
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_info = json_encode($res, JSON_UNESCAPED_UNICODE);
- if ($res['nickname']) {
- $store->mini_nickname = $res['nickname'];
- }
- if ($res['head_image_info']['head_image_url']) {
- $store->mini_url = $res['head_image_info']['head_image_url'];
- }
- $store->signature = $res['signature_info']['signature'];
- //$store->mini_url = $res['head_image_info']['head_image_url'];
- $saveRes = $store->save();
- if ($saveRes) {
- switch ($res['account_type']) {
- case 1:
- $res['account_type'] = "订阅号";
- break;
- case 2:
- $res['account_type'] = "服务号";
- break;
- case 3:
- $res['account_type'] = "小程序";
- break;
- }
- $t->commit();
- //将Appid填充系统设置
- if ((int)$store->is_use === 1) {
- $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
- if (empty($WechatConfig)) {
- $WechatConfig = new WechatConfig();
- $WechatConfig->store_id = $store_id;
- $WechatConfig->type = 1;
- }
- $WechatConfig->app_id = $store->appid;
- $WechatConfig->app_secret = "0";
- // $WechatConfig->name = ($res['principal_name'] ?: $store->name) ?: '';
- $WechatConfig->updated_at = time();
- if (!$WechatConfig->save()) {
- throw new \Exception(json_encode($WechatConfig->errors));
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res,
- 'appid' => $appid,
- 'store_mini' => $store,
- ];
- }
- throw new \Exception(json_encode($store->errors));
- } else {
- $t->rollBack();
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 设置获取修改服务器域名
- */
- public function setDomainName()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $res = $this->setWebviewDomainName();
- if ($res['code'] == 1) {
- throw new \Exception($res['msg']);
- }
- $DomainData = $res;
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $webview_domain_name = $store->webview_domain_name;
- $requestdomain = [\Yii::$app->request->hostName];
- if (!empty($webview_domain_name)) {
- $webview_domain_name = json_decode($webview_domain_name, true);
- if (!empty($webview_domain_name)) {
- foreach ($webview_domain_name as $url_item) {
- if (!empty($url_item['url'])) {
- $index = strstr($url_item['url'], 'https');
- if ($index) {
- $url_item['url'] = \str_replace('https://', '', $url_item['url']);
- } else {
- $index = strstr($url_item['url'], 'http');
- if ($index) {
- $url_item['url'] = \str_replace('http://', '', $url_item['url']);
- }
- }
- array_push($requestdomain, $url_item['url']);
- }
- }
- }
- }
- $uploaddomain = $downloaddomain = $udpdomain = $tcpdomain = $wsrequestdomain = $requestdomain;
- array_walk($requestdomain, function(&$item) {
- $item = "https://" . $item;
- });
- array_walk($wsrequestdomain, function(&$item) {
- $item = "wss://" . $item;
- });
- array_walk($uploaddomain, function(&$item) {
- $item = "https://" . $item;
- });
- array_walk($downloaddomain, function(&$item) {
- $item = "https://" . $item;
- });
- array_walk($udpdomain, function(&$item) {
- $item = "udp://" . $item;
- });
- array_walk($tcpdomain, function(&$item) {
- $item = "tcp://" . $item;
- });
- $miniProgram = $this->miniProgram($store->appid);
- $data = [
- 'action' => 'set',
- 'requestdomain' => $requestdomain,
- "wsrequestdomain" => $wsrequestdomain,
- "uploaddomain" => $uploaddomain,
- "downloaddomain" => $downloaddomain,
- "udpdomain" => $udpdomain,
- "tcpdomain" => $tcpdomain
- ];
- $res = $miniProgram->domain->modify($data);
- if (empty($res['errcode']) && !empty($res)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res,
- 'DomainData' => $DomainData
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '服务器域名错误' . $e->getMessage(),
- ];
- }
- }
- /**
- * 下载校验文件并提交至客服系统
- * @param mixed $app
- * @return array
- * @throws \Exception
- * @date 2023-02-20
- */
- public function addConfirmFile($app)
- {
- try {
- $access_token = $app->access_token->getToken()['authorizer_access_token'];
- $res = http_post('https://api.weixin.qq.com/wxa/get_webviewdomain_confirmfile?access_token=' . $access_token, [
- 'body' => '{}'
- ]);
- if ($res->getStatusCode() != 200) {
- throw new \Exception('下载校验文件出错');
- }
- $result = \json_decode((string)$res->getBody(), true);
- if ($result['errcode'] != 0) {
- throw new \Exception($result['errmsg']);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => $result
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function addFile2Ws($result) {
- try {
- $res = http_post('https://ws2.cyyvip.com/web/index.php?r=common/default/add-confirm-file', [
- 'form_params' => [
- 'token' => 'tianxin100',
- 'file_name' => $result['file_name'],
- 'file_content' => $result['file_content'],
- ]
- ]);
- if ($res->getStatusCode() != 200) {
- throw new \Exception('添加校验文件出错');
- }
- return [
- 'code' => 0,
- 'msg' => '完成'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function addFile2Other($result) {
- try {
- if (!is_dir(\Yii::$app->runtimePath . '/store/mini/')) {
- mkdir(\Yii::$app->runtimePath . '/store/mini/', 0777, true);
- }
- $file_name = \Yii::$app->runtimePath . '/store/mini/' . $result['file_name'];
- file_put_contents($file_name, $result['file_content']);
- return [
- 'code' => 0,
- 'msg' => '完成',
- 'data' => [
- 'file' => 'https://' . \Yii::$app->request->hostName . '/runtime/store/mini/' . $result['file_name']
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @return array
- * 设置业务域名
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function setWebviewDomainName()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $this->addConfirmFile($miniProgram);
- if ($result['code'] === 1) {
- return $result;
- }
- $result_ws = $this->addFile2Ws($result['data']);
- if ($result_ws['code'] === 1) {
- return $result_ws;
- }
- $webview_domain_name = $store->webview_domain_name;
- $url_arr = [];
- if (!empty($webview_domain_name)) {
- $webview_domain_name = json_decode($webview_domain_name, true);
- if (!empty($webview_domain_name)) {
- foreach ($webview_domain_name as $url_item) {
- if (!empty($url_item['url'])) {
- array_push($url_arr, $url_item['url']);
- }
- }
- }
- }
- $res = $miniProgram->domain->setWebviewDomain($url_arr, 'set');
- if (empty($res['errcode']) && !empty($res)) {
- $store->is_set_web = 1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '业务域名错误' . $e->getMessage()
- ];
- }
- }
- /**
- * 暂停/开始使用
- */
- public function unbind()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- if ($this->is_use == 1 || empty($this->is_use)) {
- StoreMini::updateAll(['is_use' => 2], ['store_id' => $this->store_id]);
- $store->is_use = 1;
- } elseif ($this->is_use == 2) {
- $store->is_use = 2;
- }
- $res = $store->save();
- if ($res) {
- //设置店铺的appid
- if($store->is_use == 1){
- $wechat = WechatConfig::find()->where(['store_id'=>$this->store_id,'type'=>1,'is_delete'=>0])->one();
- if (!$wechat) {
- $wechat = new WechatConfig();
- $wechat->store_id = $this->store_id;
- $wechat->type = 1;
- }
- $wechat->app_id = $store->appid;
- $wechat->save();
- }
- //$store->save();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } else {
- throw new \Exception("数据库添加失败");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 注:前端提交微信临时素材接口,获取素材ID
- * 设置小程序名称
- */
- public function setMiniNianname()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $media = new Client($miniProgram);
- $data = [
- 'id_card' => $this->id_card ? $this->id_card : '',
- 'license' => $this->license ? $this->license : $store->license_pic,
- 'naming_other_stuff_1' => $this->naming_other_stuff_1,
- 'naming_other_stuff_2' => $this->naming_other_stuff_2,
- 'naming_other_stuff_3' => $this->naming_other_stuff_3,
- 'naming_other_stuff_4' => $this->naming_other_stuff_4,
- 'naming_other_stuff_5' => $this->naming_other_stuff_5,
- ];
- $store->apply_mini_info = json_encode($data);
- // $this->addMedia($data);
- foreach ($data as $index => $item) {
- if (!empty($item)) {
- $head_img_media_id = $media->uploadImage($this->saveTempImage($item));
- if (empty($head_img_media_id['media_id'])) {
- throw new \Exception($this->getZnMsg($head_img_media_id));
- }
- $data[$index] = $head_img_media_id['media_id'];
- }
- }
- $id_card = $data['id_card'] ?? "";
- $license = $data['license'] ?? "";
- $res = $miniProgram->setting->setNickname($this->mini_nickname, $id_card, $license, [
- $data['naming_other_stuff_1'],
- $data['naming_other_stuff_2'],
- $data['naming_other_stuff_3'],
- $data['naming_other_stuff_4'],
- $data['naming_other_stuff_5']
- ]);
- //self::addCategory();
- $store->mini_nickname = $this->mini_nickname;
- if (empty($res['errcode']) && !empty($res)) {
- $store->nickname_audit_id = $res['audit_id'];
- $store->apply_name_status = 1;
- if (empty($res['audit_id'])) {
- $store->apply_name_status = 0;
- }
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "开始审核",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- $store->save();
- throw new \Exception("数据错误");
- } else {
- if ($res['errcode'] == 91019) {
- $store->apply_name_status = 1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "小程序审核中"
- ];
- }
-
- throw new \Exception($this->getZnMsg($res).'|'.$res['errcode']);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询名称审核状态
- */
- public function getNicknameAuditStatus()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store) || empty($store->nickname_audit_id)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '数据错误或审核已经通过',
- 'status' => 3
- ];
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $miniProgram->setting->getNicknameAuditStatus($store->nickname_audit_id);
- if (empty($result['errcode']) && !empty($result)) {
- $msg = "获取成功";
- switch ($result['audit_stat']) {
- case 0:
- $msg = "名称审核中";
- $store->apply_name_status = 1;
- break;
- case 1:
- $msg = "名称审核完成";
- $store->apply_name_status = 3;
- break;
- case 2:
- $msg = "名称审核失败";
- $store->apply_name_status = 2;
- $store->apply_name_error = $result['fail_reason'];
- break;
- }
- if (!$store->save()) {
- throw new \Exception("保存失败");
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'status' => $store->apply_name_status
- ];
- }
- throw new \Exception("查询失败");
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 增加Media事件
- */
- public function addMedia($data)
- {
- foreach ($data as $index => $item) {
- $mediaForm = StoreMedia::find()->where(['media_id' => $item])->one();
- if (empty($mediaForm)) {
- $mediaForm = new StoreMedia();
- $mediaForm->store_id = $this->store_id;
- $mediaForm->media_id = $item;
- $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
- }
- $mediaForm->event = $index;
- $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
- switch ($index) {
- case 'id_card':
- $mediaForm->desc = "身份证照片";
- break;
- case 'license':
- $mediaForm->desc = "组织机构代码证或营业执照";
- break;
- case 'naming_other_stuff_1':
- $mediaForm->desc = "其他证明材料1";
- break;
- case 'naming_other_stuff_2':
- $mediaForm->desc = "其他证明材料2";
- break;
- case 'naming_other_stuff_3':
- $mediaForm->desc = "其他证明材料3";
- break;
- case 'naming_other_stuff_4':
- $mediaForm->desc = "其他证明材料4";
- break;
- case 'naming_other_stuff_5':
- $mediaForm->desc = "其他证明材料5";
- break;
- case 'head_img_media_id':
- $mediaForm->desc = "头像素材";
- break;
- case "ext_file_media_id":
- $mediaForm->desc = "用户隐私指引";
- }
- $result = $mediaForm->save();
- }
- }
- /**
- * 微信认证名称检测
- */
- public function checkWxVerifyNickname()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->isAvailableNickname($this->mini_nickname);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 修改头像
- */
- public function setHeadImage()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $media = new Client($miniProgram);
- $head_img_media_id = $media->uploadImage($this->saveTempImage($this->head_img_media_id));
- if (empty($head_img_media_id['media_id'])) {
- throw new \Exception($this->getZnMsg($head_img_media_id));
- }
- \Yii::error($head_img_media_id['thumb_media_id']);
- $res = $miniProgram->account->updateAvatar($head_img_media_id['media_id'], $this->x1, $this->y1, $this->x2, $this->y2);
- if ($res['errcode'] == '53202') {
- $store->mini_url = $this->head_img_media_id;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- ];
- }
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_url = $this->head_img_media_id;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 修改简介
- */
- public function setSignature()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息错误");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->account->updateSignature($this->signature);
- if ($res['errcode'] == 53200) {
- $store->signature = $this->signature;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- }
- if (empty($res['errcode']) && !empty($res)) {
- $store->signature = $this->signature;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getdata($data, $id = [0])
- {
- foreach ($data as $k => $v) {
- if (in_array($v['id'], $id)) {
- $v['children'] = $this->getdata($data, $v['children']);
- $arr[] = $v;
- }
- }
- return $arr;
- }
- /**
- * @return array
- * 获取全部可选分类
- */
- public function getAllCategories()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store->authorizer_access_token)) {
- return [
- 'code' => 1,
- 'msg' => "没有授权数据",
- 'data' => []
- ];
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->getAllCategories();
- if (empty($res['errcode']) && !empty($res)) {
- $data = $res['categories_list']['categories'];
- $data = $this->getdata($data);
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $data,
- 'old_data' => $res['categories_list']['categories']
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @return array
- * 获取已有分类
- */
- public function getCategories()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,查询小程序信息失败");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- if (!$store->appid) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->getCategories();
- if (empty($res['errcode']) && !empty($res)) {
- $store->categories = !empty($res['categories']) ? json_encode($res['categories']) : "";
- $store->save();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res['categories']
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 添加类目
- */
- public function addCategory()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,查找不到小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $data = $this->categories;
- $media = new Client($miniProgram);
- \Yii::error($data);
- foreach ($data as $index => &$item) {
- if(!empty($item['certicates'])){
- if(isset($item['certicates']['key'])){
- $item['certicates'] = [$item['certicates']];
- }
- foreach ($item['certicates'] as $cateIndex => &$cate) {
- $url = $this->saveTempImage($cate['value']);
- $cate['value'] = $media->uploadImage($url)['media_id'];
- }
- }
- }
- \Yii::error($data);
- // $data = [
- // [
- // "first" => 304,
- // "second"=> 1309
- // ]
- // ];
- $res = $miniProgram->setting->addCategories($data);
- if (empty($res['errcode']) && !empty($res)) {
- $cat_res = $this->getCategories();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res,
- 'cat_data' => $cat_res['data']
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage(),
- 'e' => $e,
- ];
- }
- }
- /**
- * 删除类目
- */
- public function delCategory()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->deleteCategories($this->cate_first, $this->cate_second);
- if (empty($res['errcode']) && !empty($res)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "删除成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取代码模版列表
- */
- public function getTemplateList()
- {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $res = $this->openPlatform->code_template->list(0);
- if (empty($res['errcode']) && !empty($res)) {
- $templateList = $res['template_list'];
- foreach ($templateList as &$item) {
- $item['c_time'] = date('Y-m-d H:i:s', $item['create_time']);
- $item['template_id'] = (string)$item['template_id'];
- }
- $last_names = array_column($templateList, 'create_time');
- array_multisort($last_names, SORT_DESC, $templateList);
- if (!empty($templateList) && count($templateList) > 15) {
- $last_template = end($templateList);
- if (isset($last_template['template_id'])) {
- $result = $this->delLastTemplate($last_template['template_id']);
- if ($result['code'] === 0) {
- array_pop($templateList);
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'template_list' => $templateList
- ]
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function delLastTemplate($templateId = 0) {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- if (empty(trim($templateId))) {
- throw new \Exception("缺失关键参数");
- }
- $res = $this->openPlatform->code_template->delete($templateId);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "操作成功",
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function pluginList($miniProgram) {
- return $miniProgram->plugin->list();
- }
- //具备某个插件权限
- public function hasPlugin($miniProgram, $appid = 'wx2b03c6e691cd7370') {
- $res = $this->pluginList($miniProgram);
- if (empty($res['errcode']) && !empty($res)) {
- if (!empty($res['plugin_list'])) {
- foreach($res['plugin_list'] as $item){
- //直播
- if($item['appid'] == $appid && $item['status'] == 2){
- return true;
- }
- }
- } else {
- throw new \Exception("权限数据失败");
- }
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- return false;
- }
- //具备某个三方授权权限
- public function hasAuthorizationId($id = 52) {
- $res = $this->api_get_authorizer_info();
- if($res['code'] == 0){
- $items = $res['data']['authorization_info']['func_info'];
- foreach($items as $item){
- if($item['funcscope_category']['id'] == $id){
- return true;
- }
- }
- }
- return false;
- }
- public function canUsePlugin($appid = 'wx2b03c6e691cd7370', $aid = 52) {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取不到小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $hasPlugin = $this->hasPlugin($miniProgram, $appid);
- $hasAuthorizationId = $this->hasAuthorizationId($aid);
- $canUse = $hasPlugin && $hasAuthorizationId;
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'canUse' => intval($canUse),
- 'hasPlugin' => intval($hasPlugin),
- 'hasAuthorizationId' => intval($hasAuthorizationId),
- ];
- }
- /**
- * 上传小程序
- */
- public function upMini($useLive = 0, $useStudent = 0, $useB2b = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取不到小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $self_mini = Option::get('self_mini', $store->store_id, 'store', 0)['value'];
- $ext = [
- 'extEnable' => true,
- 'extAppid' => $store->appid,
- 'ext' => [
- 'self_mini' => $self_mini ? 1 : 0,
- "apiurl" => "https://" . \Yii::$app->request->hostName,
- "store_id" => $store->store_id,
- "mini_id" => $store->id
- ],
- "window" => [
- "navigationBarTitleText" => "0000"
- ]
- ];
- // 此方法是把插件放主包,暂时不用
- // $plugins = [];
- // if($useLive){
- // $plugins['live-player-plugin'] = [
- // 'version' => '1.3.2',
- // 'provider' => 'wx2b03c6e691cd7370',
- // ];
- // }
- // if($useB2b){
- // $plugins['B2bPlugin'] = [
- // 'version' => 'latest',
- // 'provider' => 'wx69b7451feb427f0e',
- // ];
- // }
- // if ($useLive || $useB2b) {
- // $ext['plugins'] = $plugins;
- // }
- if($useLive){
- $ext['plugins'] = [
- 'live-player-plugin' => [
- 'version' => '1.3.2',
- 'provider' => 'wx2b03c6e691cd7370',
- ],
- ];
- }
- // 此方法是把插件放分包,每次更新需要更新config/ext.php
- if ($useB2b) {
- // 获取config/ext.php内容
- $extSub = require \Yii::getAlias('@app/config/ext.php');
- $extSub = \json_decode($extSub, true);
- $ext['subPackages'] = $extSub;
- }
- if ($useStudent) {
- $ext['usingComponents'] = [
- 'student' => '/wxcomponents/student/index',
- 'single-header' => '/components/diySystemPage/components/singleHeader/index',
- 'foot-nav' => '/components/diyNew/footNav/index',
- 'transition-page' => '/components/transitionPage/index',
- 'get-success' => '/components/coupon/getSuccessPopup',
- 'shenhe' => '/components/shenhe/shenhe',
- ];
- }
- $template = Option::get("platform_template_id", 0, 'saas');//
- $template_id = $template['value'];
- if (empty($template) || $template_id === "-1") {
- throw new \Exception("未选择模板");
- }
- $res = $this->setDomainName();
- if ($res['code'] == 1) {
- throw new \Exception($res['msg']);
- }
- $DomainData = $res;
- $res = $miniProgram->code->commit($template_id, json_encode($ext), cyy_version(), $this->user_desc);
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_up = -1;
- $store->template_id = $template_id;
- $store->user_version = cyy_version();
- $store->user_desc = $this->user_desc;
- $store->ext = json_encode($ext);
- $res = $store->save();
- if ($res) {
- $res = $this->getMiniQrcode($store->id);
- if ($res['code'] === 0) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res['data'],
- 'datas' => $DomainData
- ];
- } else {
- throw new \Exception("获取体验二维码失败");
- }
- } else {
- throw new \Exception("存储数据失败");
- }
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- if (strpos($e->getMessage(), 'cURL error 28') !== false) {
- return $this->upMini();
- }
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 添加体验者
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function addTester()
- {
- try {
- $store = StoreMini::findOne($this->mini_id);
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->tester->bind($this->wechatId);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取体验版二维码
- */
- public function getMiniQrcode($id = 0)
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $id = $id !== 0 ? $id : $this->mini_id;
- $store = StoreMini::findOne($id);
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $qrcode = \Yii::$app->basePath . "/web/temp/" . $this->store_id . "_" . date('YmdHis') . ".jpg";
- $res = $miniProgram->code->getQrCode($this->mini_path);
- if (empty(json_decode($res, true)['errcode'])) {
- file_put_contents($qrcode, $res);
- $url = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $qrcode);
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $url
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res, true);
- return [
- 'code' => $res->errcode,
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 提交审核
- */
- public function submitAudit()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $result = $this->setPrivacySetting();
- if ($result['code'] === 0) {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,未找到对应的小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $data = [
- 'item_list' => $this->item_list ?? [],
- 'order_path' => 'order/order/order',
- ];
- $res = $miniProgram->code->submitAudit($data, '', '');
- if (empty($res['errcode']) && !empty($res)) {
- //是否加急审核
- if ($this->is_fast_audit === 1) {
- $res = $miniProgram->code->speedupAudit($res['auditid']);
- }
- $store->mini_up = 1;
- $store->audit_id = (string)$res['auditid'];
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } else {
- throw new \Exception($result['msg']);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- if (strpos($e->getMessage(), 'cURL error 28') !== false) {
- return $this->submitAudit();
- }
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询最后一次提交审核的状态
- */
- public function lastAuditStatus()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $miniProgram->code->getLatestAuditStatus();
- $msg = "返回成功";
- if (empty($result['errcode']) && !empty($result)) {
- switch ($result['status']) {
- case 0:
- $msg = "审核通过";
- $store->mini_up = 2;
- break;
- case 1:
- $msg = "审核已拒绝:" . $result['reason'];
- $store->mini_up = 3;
- $store->mini_up_error = $result['reason'];
- break;
- case 2:
- $msg = "审核中";
- $store->mini_up = 1;
- break;
- case 3:
- $msg = "未提交审核或已撤回";
- $store->mini_up = 0;
- break;
- }
- if (!$store->save()) {
- throw new \Exception("保存失败");
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $msg
- ];
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 提交审核的状态
- */
- public function getAuditStatus($mini_id)
- {
- //20240816微信回调老是处理失败 增加主动查询的逻辑
- // $t = \Yii::$app->db->beginTransaction();
- try {
- if ($mini_id) {
- $store = StoreMini::findOne($mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误");
- }
- if (empty($store->audit_id)) {
- throw new \Exception("未查询到审核信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $miniProgram->code->getAuditStatus($store->audit_id);
- $msg = "返回成功";
- if (empty($result['errcode']) && !empty($result)) {
- switch ($result['status']) {
- case 0:
- $msg = "审核通过";
- if (intval($store->mini_up) === 1) {
- $store->mini_up = 2;
- }
- break;
- case 1:
- $msg = "审核已拒绝:" . $result['reason'];
- $store->mini_up = 3;
- $store->mini_up_error = $result['reason'];
- break;
- case 2:
- $msg = "审核中";
- $store->mini_up = 1;
- break;
- case 3:
- $msg = "未提交审核或已撤回";
- $store->mini_up = 0;
- break;
- }
- if (!$store->save()) {
- throw new \Exception("保存失败");
- }
- // $t->commit();
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'mini_up' => $store->mini_up
- ];
- }
- throw new \Exception(json_encode($result, JSON_UNESCAPED_UNICODE));
- } catch (\Exception $e) {
- // $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 审核撤回
- */
- public function unDoCodeAudit()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,未查询到小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->code->withdrawAudit();
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_up = -1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 发布已通过审核的小程序
- */
- public function release()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id();
- if (empty($store)) {
- throw new \Exception("数据错误,获取不到小程序信息");
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $this->is_use = 1;
- $this->unbind();
- $res = $miniProgram->code->release();
- if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") {
- $store->mini_up = 5;
- if ($store->save()) {
- $t->commit();
- $res = $this->getMiniQr($store->id);
- if ($res['code'] === 0) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res['data']
- ];
- } else {
- throw new \Exception("获取二维码数据错误");
- }
- } else {
- throw new \Exception("保存数据错误");
- }
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 设置小程序用户隐私保护指引
- */
- public function setPrivacySetting()
- {
- $data = [
- "privacy_ver" => $this->privacy_ver,
- "owner_setting" => [
- "contact_phone" => "17090402350",
- "notice_method" => "短信"
- ],
- "setting_list" => [
- [
- "privacy_key" => "UserInfo",
- "privacy_text" => "统计订单"
- ],
- [
- "privacy_key" => "Location",
- "privacy_text" => "显示附近自提点"
- ],
- [
- "privacy_key" => "Address",
- "privacy_text" => "显示附近自提点"
- ],
- [
- "privacy_key" => "RunData",
- "privacy_text" => "消费步数兑换商品"
- ],
- [
- "privacy_key" => "Record",
- "privacy_text" => "售后咨询"
- ],
- [
- "privacy_key" => "Camera",
- "privacy_text" => "提交订单备注资料"
- ],
- [
- "privacy_key" => "AlbumWriteOnly",
- "privacy_text" => "保存海报"
- ],
- [
- "privacy_key" => "PhoneNumber",
- "privacy_text" => "多端口数据统一"
- ],
- [
- "privacy_key" => "Album",
- "privacy_text" => "审核资质"
- ],
- [
- "privacy_key" =>"MessageFile",
- "privacy_text" => "订单评价拍照"
- ],
- [
- "privacy_key" =>"Clipboard",
- "privacy_text" => "支持用户信息复制信息"
- ],
- [
- "privacy_key" =>"ChooseLocation",
- "privacy_text" => "计算距离"
- ],
- [
- "privacy_key" =>"DeviceInfo",
- "privacy_text" => "生成适应页面"
- ]
- ],
- ];
- try {
- $this->apply_privacy_interface();
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $contact_tel = Option::get(OptionSetting::STORE_CONTACT_TEL, $store->store_id, '')['value'];
- if (!$contact_tel) {
- throw new \Exception("更新隐私政策失败,请前往'系统-功能设置-基础设置'填写保存手机号信息。");
- }
- $data['owner_setting']['contact_phone'] = $contact_tel;
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res)
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function apply_privacy_interface() {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $arr = [
- [
- 'api_name' => 'wx.chooseLocation',
- 'content' => '获取用户选择的省市区位置信息,帮助快速生成收货地址'
- ],
- [
- 'api_name' => 'wx.getLocation',
- 'content' => '获取用户位置信息,用户能查看到附近的自提地点'
- ]
- ];
- foreach ($arr as $item) {
- $res = $client->httpPostJson('wxa/security/apply_privacy_interface', $item);
- debug_log('申请——', 'privacy_interface.log');
- debug_log($res, 'privacy_interface.log');
- if ($res['errcode']) {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res)
- ];
- }
- }
- return [
- 'code' => 0,
- 'msg' => '开始申请...'
- ];
- } catch (\Exception $e) {
- debug_log('申请——', 'privacy_interface.log');
- debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log');
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function get_privacy_interface() {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (!$store->appid) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpGet('wxa/security/get_privacy_interface');
- debug_log('获取——', 'privacy_interface.log');
- debug_log($res, 'privacy_interface.log');
- } catch (\Exception $e) {
- debug_log('获取——', 'privacy_interface.log');
- debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log');
- }
- }
- /**
- * 查询小程序用户隐私保护指引
- */
- public function getPrivacySetting()
- {
- try {
- $data = [
- "privacy_ver" => $this->privacy_ver,
- ];
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpPostJson('cgi-bin/component/getprivacysetting', $data);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询小程序码
- */
- public function getMiniQr($id = 0, $sence = '', $page = '')
- {
- try {
- if (empty($id)) {
- $id = $this->mini_id;
- }
- $store = StoreMini::findOne($id);
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $response = $miniProgram->app_code->getUnlimit($sence ?: "store=" . $this->store_id, [
- 'page' => $page
- ]);
- $filename = md5(time()) . '.jpg';
- $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
- if (!is_dir($filePath)) {
- mkdir($filePath, 0777, true);
- chmod($filePath , 0777);
- }
- $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $resfilename = $response->saveAs($filePath, $filename);
- if (!empty($resfilename)) {
- $filePath = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $filePath . '/' . $filename);
- $store->qrcode = $filePath;
- $res = $store->save();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $filePath
- ];
- } else {
- throw new \Exception("添加小程序码失败");
- }
- } else {
- throw new \Exception("数据错误");
- }
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询小程序
- */
- public function getMiniStatus()
- {
- $mini_info = StoreMini::find()->where(['appid' => $this->appid])->select("mini_url,mini_up,is_cancle,appid,status,msg")->asArray()->one();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $mini_info
- ];
- }
- /**
- * 获取列表
- */
- public function getMiniList()
- {
- $store_id = $this->store_id;
- $query = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0])
- ->select("*");
- if (!empty($this->mini_nickname)) {
- $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]);
- }
- if (isset($this->fuwu_type)) {
- $query->andWhere(['fuwu_type' => $this->fuwu_type]);
- }
- if (!empty($this->status)) {
- switch ($this->status) {
- case 1:
- $query->andWhere(['OR', ['mini_nickname' => ""], ['signature' => ""], ['mini_url' => ""]]);
- break;
- case 2:
- $query->andWhere(['AND', ['<>', 'mini_nickname', ""], ['<>', 'signature', ""], ['<>', 'mini_url', ""]]);
- break;
- case 3:
- $query->andWhere(['status' => 2]);
- break;
- case 4:
- $query->andWhere(['status' => -1]);
- break;
- }
- }
- if ($this->appid) {
- $query->andWhere(['LIKE', 'appid', $this->appid]);
- }
- if ($this->start_time) {
- $start_time = strtotime($this->start_time);
- $query->andWhere(['>=', 'created_at', $start_time]);
- }
- if ($this->end_time) {
- $end_time = strtotime($this->end_time);
- $query->andWhere(['<=', 'created_at', $end_time]);
- }
- if (!empty($this->is_use)) {
- $query->andWhere(['is_use' => $this->is_use]);
- }
- if (!empty($this->mini_nickname)) {
- $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]);
- }
- $query->orderBy('id desc');
- $pagination = pagination_make($query);
- $admins = $pagination['list'];
- foreach ($admins as $index => &$item) {
- //判断是否认证
- $item['is_wxauth'] = 1;
- if ($item['created_at'] > strtotime("2023-12-25") || empty($item['created_at'])) {
- $item['is_wxauth'] = 0;
- $store_mini_auth = StoreMiniAuth::findOne(['mini_id' => $item['id'], 'is_delete' => 0, 'apply_status' => 4]);//已通过
- if ($store_mini_auth) {
- $item['is_wxauth'] = 1;
- }
- }
- $mini_info = json_decode($item['mini_info'], true);
- $item['principal_name'] = $mini_info['principal_name'];
- $item['nickname'] = $mini_info['nickname'];
- // $item['app_secret'] = '';
- // if ($item['audit_id'] && (int)$item['mini_up'] !== 5) {
- // $res = $this->getAuditStatus($item['id']);
- // if ($res['code'] === 0) {
- // $item['mini_up'] = $res['mini_up'];
- // }
- // }
- $item['is_finish'] = 0;
- if (!empty($item['categories'])) {
- $item['categories'] = json_decode($item['categories'], true);
- } elseif (empty($item['categories']) && !empty($item['appid']) && empty($this->fuwu_type)) {
- $this->mini_id = $item['id'];
- $item['categories'] = $this->getCategories()['data'];
- }
- if (!empty($item['mini_info'])) {
- $item['mini_info'] = json_decode($item['mini_info'], true);
- }
- $item['qr'] = false;
- if (!empty($item['qrcode'])) {
- $item['qr'] = $item['qrcode'];
- }
- $item['user_version'] = cyy_version();
- $item['qrcode'] = '';
- $item['created_at'] = $item['created_at'] ? date("Y-m-d H:i:s", $item['created_at']) : '';
- if (!empty($item['appid']) && empty($this->fuwu_type)) {
- // $res = $this->getQrcodeRules($item['appid']);
- // $item['qrcode_rules'] = $res;
- // \Yii::error($res);
- $miniInfo = $this->getMiniInfo("", $item['appid']);
- if ($miniInfo["code"] === 0) {
- \Yii::error($miniInfo);
- if (!empty($item['signature']) && !empty($item['mini_url']) && !empty($item['mini_nickname'] && !empty($item['categories']))) {
- $item['is_finish'] = 1;
- }
- }
- $res = $this->getMiniQrcode($item['id']);
- if ($res['code'] == 0) {
- $item['qrcode'] = $res['data'];
- }
- //20240816微信回调老是处理失败 增加主动查询的逻辑
- $item['audit_result'] = $this->getAuditStatus($item['id']);
- }
- if (!empty($this->fuwu_type)) {
- $item['call_back_url'] = 'https://' . \Yii::$app->request->hostName . '/index.php/wechat/video-shop-order/callback/' . $item['id'];
- $item['token'] = Option::get('token', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
- $item['encodingAesKey'] = Option::get('encodingAesKey', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
- $item['finderUserName'] = Option::get('finderUserName', $store_id, 'video_shop_config_' . $item['id'], '')['value'];
- $WechatMini = WechatMini::getWechatConfig($this->store_id, $item['id'], 1);
- try {
- $client = new BaseClient($WechatMini);
- $res = $client->httpGet('channels/ec/basics/info/get');
- if (!$res['errcode'] && !empty($res)) {
- $store_mini = StoreMini::findOne($item['id']);
- $item['mini_nickname'] = $store_mini->mini_nickname = $res['info']['nickname'];
- $item['mini_url'] = $store_mini->mini_url = $res['info']['headimg_url'];
- $store_mini->save();
- }
- } catch (\Exception $e) {
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $admins,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- 'isSaas' => \Yii::$app->isSaas()
- ];
- }
- /**
- * 获取单个
- */
- public function getMini()
- {
- $query = StoreMini::find()->where(['id' => $this->mini_id])
- ->select("id, name, code, code_type, legal_persona_wechat, legal_persona_name, status, msg, appid,
- mini_up, mini_url, is_cancle, mini_up_error, mini_info, apply_name_status, apply_name_error, is_use,
- signature, categories, license_pic, apply_mini_info, mini_nickname");
- $data = $query->asArray()->one();
- // try {
- // $this->get_privacy_interface();
- // } catch (\Exception $e) {
- //
- // }
- if (!empty(json_decode($data['mini_info']))) {
- $data['principal_name'] = json_decode($data['mini_info'], true)['principal_name'];
- $data['nickname'] = json_decode($data['mini_info'], true)['nickname'] ?: $data['mini_nickname'];
- $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url;
- $data['apply_mini_info'] = $data['apply_mini_info'] ? json_decode($data['apply_mini_info'], true): [];
- $data['categories'] = [];
- if ($this->getCategories()['code'] === 0) {
- $data['categories'] = $this->getCategories()['data'];
- }
- //获取认证资料
- $storeMiniAuth = StoreMiniAuth::findOne(['mini_id' => $this->mini_id]);
- // if ($result['apply_status'] < 2) {
- $data['auth_info'] = [
- 'is_finish' => intval($storeMiniAuth->apply_status) === 4 ? 1 : 0,
- 'message' => intval($storeMiniAuth->task_status) !== -1 ? $storeMiniAuth->task_message : '未提交认证信息'
- ];
- //获取备案资料
- $storeMiniIcp = StoreMiniIcp::findOne(['mini_id' => $this->mini_id, 'is_cancel_icp' => 0]);
- // if ($result['apply_status'] < 2) {
- $is_finish = 0;
- if ($storeMiniIcp) {//人身核验是否通过
- $is_finish = intval($storeMiniIcp->icp_audit_status) == 6 ? 1 : 0;
- if (intval($storeMiniIcp->icp_audit_status) >= 2) {
- $icp_message = StoreMiniIcp::$audit_status_text[$storeMiniIcp->icp_audit_status];
- } else {
- $icp_message = StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniIcp->face_status)];
- }
- } else {
- $icp_message = '未开始人身核验';
- }
- $data['icp_info'] = [
- 'is_finish' => $is_finish,
- 'message' => $icp_message
- ];
- //获取认证以及备案资料
- $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]);
- $wechatThirdAuthAndIcpForm = new \app\modules\admin\models\WechatThirdAuthAndIcpForm(['store_id' => get_store_id(), 'mini_id' => $this->mini_id]);
- if ($storeMiniAuthIcp->task_id) {
- $wechatThirdAuthAndIcpForm->queryIcpVerifyTask();
- }
- if ($storeMiniAuthIcp->procedure_id) {
- $wechatThirdAuthAndIcpForm->queryAuthAndIcp();
- }
- //重新加载数据
- $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]);
- $data['icp_auth_info'] = [
- 'is_finish' => intval($storeMiniAuthIcp->procedure_status) === StoreMiniAuthIcp::PROCEDURE_STATUS_SUCCESS ? 1 : 0,
- 'message' => intval($storeMiniAuthIcp->face_is_finish) === StoreMiniAuthIcp::FACE_FINISH ?
- StoreMiniAuthIcp::PROCEDURE_STATUS_NAME[intval($storeMiniAuthIcp->procedure_status)] :
- StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniAuthIcp->face_status)]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $data
- ];
- }
-
- public static function getUsedStoreMini($store_id) {
- $mini = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'is_use' => 1, 'fuwu_type' => 0])->one();
- return $mini;
- }
-
- public static function getStoreMiniSelectList($store_id) {
- $list = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'fuwu_type' => 0])->select('id, mini_nickname, mini_url, is_use')->asArray()->all();
- foreach($list as &$item){
- empty($item['mini_nickname']) && $item['mini_nickname'] = '--';
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $list,
- ];
- }
-
- public static function checkOrBindOpen($store_id) {
- $mini = self::getUsedStoreMini($store_id);
- if(!$mini){
- return [
- 'code' => 1,
- 'msg' => '没有使用中的小程序,请到小程序管理列表查看',
- ];
- }
- $form = new self();
- $form->mini_id = $mini->id;
- $openGet = $form->openGet();
- if($openGet['code'] == 0){
- return $openGet;
- }
- $openCreate = $form->openCreate();
- return $openCreate;
- }
-
- public function openGet() {
- try {
- $mini = StoreMini::findOne($this->mini_id);
- $client = new BaseClient($this->miniProgram($mini->appid));
- $data = [
- 'appid' => $mini->appid,
- ];
- $res = $client->httpPostJson('cgi-bin/open/get', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res))) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function openCreate() {
- try {
- $mini = StoreMini::findOne($this->mini_id);
- $client = new BaseClient($this->miniProgram($mini->appid));
- $data = [
- 'appid' => $mini->appid,
- ];
- $res = $client->httpPostJson('cgi-bin/open/create', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res))) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function openBind($open_appid) {
- try {
- $mini = StoreMini::findOne($this->mini_id);
- $client = new BaseClient($this->miniProgram($mini->appid));
- $data = [
- 'appid' => $mini->appid,
- 'open_appid' => $open_appid,
- ];
- $res = $client->httpPostJson('cgi-bin/open/bind', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res))) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- 'open_appid' => $open_appid,
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function openUnbind() {
- try {
- $get = $this->openGet();
- if($get['code'] == 0 && $get['data']['open_appid']){
- $open_appid = $get['data']['open_appid'];
- }else{
- return $get;
- }
- $mini = StoreMini::findOne($this->mini_id);
- $client = new BaseClient($this->miniProgram($mini->appid));
- $data = [
- 'appid' => $mini->appid,
- 'open_appid' => $open_appid,
- ];
- $res = $client->httpPostJson('cgi-bin/open/unbind', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res))) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- 'open_appid' => $open_appid,
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //添加二维码规则
- public function setQrcodeRules($id, $appid)
- {
- try {
- $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->asArray()->one();
- $data = [
- 'prefix' => $qrcode['param_url'],
- 'permit_sub_rule' => 1,
- 'path' => $qrcode['wx_url'],
- 'open_version' => 2,
- 'debug_url' => [
- $qrcode['param_url']
- ],
- 'is_edit' => 0
- ];
- \Yii::error($data);
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res)) || ($res['errcode'] == "85071")) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //发布二维码规则
- public function submitQrcodeRules($id, $appid)
- {
- try {
- $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one();
- if (empty($qrcode->param_url)) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $data = [
- 'prefix' => $qrcode->param_url
- ];
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
- if (empty($res->errcode) && !empty($res)) {
- $qrcode->wx_status = 1;
- $res = $qrcode->save();
- if (!$res) {
- throw new \Exception('保存数据失败,二维码规则发布生成');
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res, true);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取二维码规则
- public function getQrcodeRules($appid)
- {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
- if (empty($res->errcode) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res, true);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //下载文件
- public function downQrcodeRules($id, $appid)
- {
- try {
- if (!$this->openPlatform) {
- throw new \Exception("参数配置错误");
- }
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
- if (empty($res['errcode']) && !empty($res)) {
- if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
- // dir doesn't exist, make it
- mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
- }
- $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $res['file_name'], $res['file_content']);
- if ($file_result) {
- $resRules = $this->setQrcodeRules($id, $appid);
- if ($resRules['code'] === 0) {
- $ress = $this->submitQrcodeRules($id, $appid);
- \Yii::error($ress);
- if ($ress['code'] === 0) {
- $resGet = $this->getQrcodeRules($appid);
- \Yii::error($resGet);
- } else {
- throw new \Exception($ress['msg']);
- }
- } else {
- throw new \Exception($resRules['msg']);
- }
- } else {
- throw new \Exception("创建文件失败");
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- 'file' => $file_result
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- $res = json_decode($res, true);
- throw new \Exception($res['errmsg']);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //删除二维码规则
- public function delQrcodeRules()
- {
- try {
- $store = StoreMini::findOne($this->mini_id);
- if ($store->appid) {
- //查询所有普通二维码
- $res = $this->getQrcodeRules($store->appid);
- if ($res['code'] !== 0) {
- return $res;
- }
- $rules_list = $res['data']['rule_list'];
- if (!empty($rules_list)) {
- //全部删除
- foreach ($rules_list as $item) {
- $data = [
- 'prefix' => $item['prefix']
- ];
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdelete', $data);
- if (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } elseif (!empty($res->errcode)) {
- $res = json_decode($res, true);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- }
- //将商城显示解除绑定
- $store->bind_food_qr = 0;
- if (!$store->save()) {
- throw new \Exception(json_encode($store->errors));
- }
- Option::set('store_home_is_bind_wx', 0, get_store_id(), 'store');
- $store_qrcode = AggregateQrcode::findOne(['store_id' => get_store_id()]);
- if ($store_qrcode) {
- $store_qrcode->wx_status = 0;
- $store_qrcode->wx_mini_id = 0;
- if (!$store_qrcode->save()) {
- throw new \Exception(json_encode($store_qrcode->errors));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '解除成功'
- ];
- }
- throw new \Exception('没有绑定小程序');
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //微信支付进件
- public function mchRegister()
- {
- $mini = StoreMini::find()->where(['id' => $this->mini_id])->select('id,appid')->one();
- if (empty($mini->appid)) {
- return [
- 'code' => 0,
- 'msg' => "未找到对应的小程序信息"
- ];
- }
- $merchant_info = MerchantInfo::find()->where(['bind_store_id' => get_store_id(), 'is_delete' => 0])->one();
- if (empty($merchant_info)) {
- // $StoreSchedule = StoreSchedule::find()->where(['store_id' => get_store_id()])->one();
- // if (!empty($StoreSchedule->merchant_info)) {
- // $merchant_info = json_decode($StoreSchedule->merchant_info, true);
- // $contact_info = $merchant_info['contact_info'];
- // $subject_info = $merchant_info['subject_info'];
- // $business_info = $merchant_info['business_info'];
- // $bank_account_info = $merchant_info['bank_account_info'];
- // } else {
- // return [
- // 'code' => 1,
- // 'msg' => "未找到对应的进件数据"
- // ];
- // }
- return [
- 'code' => 1,
- 'msg' => "未找到对应的进件数据"
- ];
- } else {
- $contact_info = !empty($merchant_info->contact_info) ? json_decode($merchant_info->contact_info, true) : "";
- $subject_info = !empty($merchant_info->subject_info) ? json_decode($merchant_info->subject_info, true) : "";
- $business_info = !empty($merchant_info->business_info) ? json_decode($merchant_info->business_info, true) : "";
- $bank_account_info = !empty($merchant_info->bank_account_info) ? json_decode($merchant_info->bank_account_info, true) : "";
- }
- $Merchant = new Merchant();
- return $Merchant->submit($contact_info, $subject_info, $business_info, $bank_account_info, !empty($merchant_info->id) ? $merchant_info->id : 0, $mini->appid);
- }
-
- public function servicemarket_service_login_auth($code) {
- try {
- $client = new BaseClient($this->openPlatform);
- $postData = [
- 'code' => $code,
- ];
- $res = $client->httpPostJson('wxa/servicemarket/service/login_auth', $postData);
- \Yii::error([$res, json_encode($postData)]);
- // var_dump($res);die;
- // $res = [
- // 'order_id' => 3118683324757311491,
- // 'openid' => '',
- // 'appid' => 'wxedd15801cce16326',
- // 'service_id' => 3118677029492637699,
- // 'sku_id' => '4711132',
- // 'spec_id' => 'free',
- // 'errcode' => 0,
- // 'errmsg' => 'ok',
- // ];
- \Yii::error([$res, json_encode($postData)]);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } else {
- throw new \Exception($res['errmsg'] ?? '');
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- //服务市场appid实例化
- public function fuwu_app() {
- try {
- $appid = Option::get('platform_wechat_fuwu_appid', 0, 'saas', '')['value'];
- $secret = Option::get('platform_wechat_fuwu_secret', 0, 'saas', '')['value'];
- if(empty($appid) || empty($secret)){
- throw new \Exception("平台后台未配置服务市场信息");
- }
- $config =[];
- $config['app_id'] = $appid;
- $config['secret'] = $secret;
- $config['response_type'] = 'array';
- $app = Factory::officialAccount($config);
- return [
- 'code' => 0,
- 'data' => $app,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- //服务市场token获取
- public function fuwu_token() {
- try {
- $fuwu_app = $this->fuwu_app();
- if($fuwu_app['code'] != 0){
- throw new \Exception($fuwu_app['msg']);
- }
- $app = $fuwu_app['data'];
- $accessToken = $app->access_token;
- $token = $accessToken->getToken();
- if(empty($token['access_token'])){
- throw new \Exception("服务市场token信息获取失败");
- }
- return [
- 'code' => 0,
- 'data' => $token['access_token'],
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function servicemarket_getwxaqrcode($service_id = '', $custom_params = ['a' => 1]) {
- try {
- $fuwu_app = $this->fuwu_app();
- if($fuwu_app['code'] != 0){
- throw new \Exception($fuwu_app['msg']);
- }
- $app = $fuwu_app['data'];
- $client = new BaseClient($app);
- $postData = [
- 'service_id' => (string)$service_id,
- 'custom_params' => http_build_query($custom_params),
- ];
- $res = $client->httpPostJson('wxa/servicemarket/getwxaqrcode', $postData);
- // var_dump($res);die;
- \Yii::error([$res, json_encode($postData)]);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'service_id' => $service_id,
- 'data' => $res,
- ];
- } else {
- throw new \Exception($res['errmsg'] ?? '');
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- 'service_id' => $service_id,
- ];
- }
- }
-
- public function servicemarket_get_paid_order_list($appid = '', $service_id = '') {
- try {
- $fuwu_app = $this->fuwu_app();
- if($fuwu_app['code'] != 0){
- throw new \Exception($fuwu_app['msg']);
- }
- $app = $fuwu_app['data'];
- $client = new BaseClient($app);
- $postData = [
- 'appid' => $appid,
- 'service_id' => $service_id,
- 'offset' => 0,
- 'limit' => 10,
- 'buyer_type' => 1,
- ];
- $res = $client->httpPostJson('wxa/servicemarket/get_paid_order_list', $postData);
- // $res = [
- // 'errcode' => 0,
- // 'errmsg' => 'ok',
- // 'order_list' => [
- // [
- // 'order_id' => 3138343822027653126,
- // 'effective_time' => 1696652147,
- // 'expire_time' => 1728188147,
- // 'specification_id' => 'new',
- // 'service_id' => 3118677029492637699,
- // 'total_price' => 100,
- // 'phone' => '',
- // 'sku_id' => 3144177,
- // ],
- // [
- // 'order_id' => 3118683324757311491,
- // 'effective_time' => 1695480283,
- // 'expire_time' => 1696085083,
- // 'specification_id' => 'free',
- // 'service_id' => 3118677029492637699,
- // 'total_price' => 0,
- // 'phone' => '',
- // 'sku_id' => 4711132,
- // ],
- // ],
- // 'total' => 2,
- // ];
- \Yii::error([$res, json_encode($postData)]);
- // var_dump($res, $postData);die;
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } else {
- throw new \Exception($res['errmsg'] ?? '');
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function servicemarket_service_get_service_buyer_list($service_id = '') {
- try {
- $fuwu_app = $this->fuwu_app();
- if($fuwu_app['code'] != 0){
- throw new \Exception($fuwu_app['msg']);
- }
- $app = $fuwu_app['data'];
- $client = new BaseClient($app);
- $postData = [
- 'service_id' => $service_id,
- 'offset' => 0,
- 'limit' => 10,
- 'buyer_type' => 1,
- ];
- $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer_list', $postData);
- // $res = [
- // 'errcode' => 0,
- // 'errmsg' => 'ok',
- // 'buyer_list' => [
- // [
- // 'appid' => 'wxedd15801cce16326',
- // 'service_id' => 3118677029492637699,
- // 'spec_list' => [
- // [
- // 'specification_id' => 'free',
- // 'expire_time' => 1696085083,
- // ],
- // [
- // 'specification_id' => 'new',
- // 'expire_time' => 1728188147,
- // ],
- // ],
- // ],
- // ],
- // 'count' => 1,
- // ];
- \Yii::error([$res, json_encode($postData)]);
- // var_dump($res, $postData);die;
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } else {
- throw new \Exception($res['errmsg'] ?? '');
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- public function servicemarket_service_get_service_buyer($appid = '', $service_id = '') {
- try {
- $fuwu_app = $this->fuwu_app();
- if($fuwu_app['code'] != 0){
- throw new \Exception($fuwu_app['msg']);
- }
- $app = $fuwu_app['data'];
- $client = new BaseClient($app);
- $postData = [
- 'appid' => $appid,
- 'service_id' => $service_id,
- ];
- $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer', $postData);
- // $res = [
- // 'errcode' => 0,
- // 'errmsg' => 'ok',
- // 'buyer' => [
- // 'appid' => 'wxedd15801cce16326',
- // 'service_id' => 3118677029492637699,
- // 'spec_list' => [
- // [
- // 'specification_id' => 'free',
- // 'expire_time' => 1696085083,
- // ],
- // [
- // 'specification_id' => 'new',
- // 'expire_time' => 1728188147,
- // ],
- // ],
- // ],
- // ];
- \Yii::error([$res, json_encode($postData)]);
- // var_dump($res, $postData);die;
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- ];
- } else {
- throw new \Exception($res['errmsg'] ?? '');
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- //从服务市场登陆
- public function servicemarketAuthCode($code = '', $fuwu_type = 1) {
- try {
- $auth = $this->servicemarket_service_login_auth($code);
- if($auth['code'] != 0){
- throw new \Exception($auth['msg']);
- }
- // return $this->servicemarket_getwxaqrcode($auth['data']['service_id']);
- // return $this->servicemarket_get_paid_order_list($auth['data']['appid'], $auth['data']['service_id']);
- // return $this->servicemarket_service_get_service_buyer_list($auth['data']['service_id']);
- // return $this->servicemarket_service_get_service_buyer($auth['data']['appid'], $auth['data']['service_id']);
- return $this->servicemarketAuth($auth['data']['appid'], $auth['data']['service_id'], $fuwu_type);
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- //从服务市场登陆时(认证、初始化小程序)
- public function servicemarketAuth($appid, $service_id = '', $fuwu_type = 1) {
- try {
- $service_ids = explode(',', Option::get('platform_wechat_fuwu_serviceId', 0, 'saas', '')['value']);
- if(!in_array($service_id, $service_ids)){
- throw new \Exception("服务信息未配置");
- }
- $mini = StoreMini::findOne(['appid' => $appid]);
- if(!$mini){
- $mini = new StoreMini();
- }
- //初始化小程序
- $mini->fuwu_type = $fuwu_type;
- $mini->appid = $appid;
- $mini->is_cancle = 0;
- $hasAuth = 0;
- $res = $this->openPlatform->getAuthorizer($appid);
- if (empty($res['errcode']) && !empty($res)) {
- $hasAuth = 1;
- $mini->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
- }else{
- debug_log([__METHOD__, __LINE__, $res], __CLASS__ . '.log');
- throw new \Exception($res['errmsg']);
- }
- $mini->save();
- if($hasAuth){
- //如果授权过三方,填充小程序信息
- $miniProgram = $this->openPlatform->miniProgram($mini->appid, $mini->authorizer_refresh_token);
- $client = new BaseClient($miniProgram);
- $res = $client->httpGet('channels/ec/basics/info/get');
- // var_dump($res);die;
- if (empty($res['errcode']) && !empty($res)) {
- $mini->mini_nickname = $res['info']['nickname'];
- $mini->mini_url = $res['info']['headimg_url'];
- if(!$mini->save()){
- debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log');
- }
- }
- }
- if($mini->fuwu_type == StoreMini::FUWU_TYPE_VIDEO_SHOP && $hasAuth && $mini->mini_nickname){
- $buyer = $this->servicemarket_service_get_service_buyer($appid, $service_id);
- if($buyer['code'] != 0){
- debug_log([__METHOD__, __LINE__, $buyer], __CLASS__ . '.log');
- throw new \Exception($buyer['msg']);
- }
- $end_time = 0;
- if(isset($buyer['data']['buyer']['spec_list'])){
- foreach($buyer['data']['buyer']['spec_list'] as $item){
- if($item['expire_time'] > $end_time){
- $end_time = $item['expire_time'];
- }
- }
- }
- // var_dump($end_time);die;
- if(!$mini->store_id){
- //初始化店铺
- if($end_time < time()){
- $end_time = time() + 86400 * 7;
- }
- $storeForm = new StoreForm();
- $username = $mini->mini_nickname . time();
- $add = $storeForm->add($username, '', $username, [], 0, $mini->mini_url, $end_time);
- if($add['code'] != 0){
- debug_log([__METHOD__, __LINE__, $add['msg']], __CLASS__ . '.log');
- }else{
- $store = $add['store'];
- $mini->store_id = $store['id'];
- if(!$mini->save()){
- debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log');
- }
- }
- }else{
- //更新店铺到期时间
- $store = Store::findOne($mini->store_id);
- if($end_time > time() && $store->end_time < $end_time){
- $store->end_time = $end_time;
- $store->save();
- }
- }
- }
- // var_dump($store->attributes, $mini->attributes);die;
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => [
- 'mini' => $mini,
- 'store' => $store ?? null,
- ],
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //下载文件
- public function getWebviewFile() {
- $mini_id = $this->mini_id;
- $store_mini = StoreMini::findOne($mini_id);
- if (empty($store_mini)) {
- return [
- 'code' => 1,
- 'msg' => '查询数据失败'
- ];
- }
- if (!$this->openPlatform) {
- return [
- 'code' => 1,
- 'msg' => '参数配置错误'
- ];
- }
- $miniProgram = $this->miniProgram($store_mini->appid);
- $result = $this->addConfirmFile($miniProgram);
- if ($result['code'] === 1) {
- return $result;
- }
- return $this->addFile2Other($result['data']);
- }
- //设置业务域名 (三方平台修改小程序业务域名)
- public function setWebviewDomain() {
- $mini_id = $this->mini_id;
- $store_mini = StoreMini::findOne($mini_id);
- if (empty($store_mini)) {
- return [
- 'code' => 1,
- 'msg' => '查询数据失败'
- ];
- }
- if (\Yii::$app->request->isPost) {
- $webview_domain_name_ = post_params('webview_domain_name');
- $webview_domain_name_ = json_decode($webview_domain_name_, true);
- $webview_domain_name = [];
- foreach ($webview_domain_name_ as $item) {
- if (!empty($item['url'])) {
- $index = strstr($item['url'], 'http');
- if (!$index) {
- $item['url'] = 'https://' . $item['url'];
- }
- $item['url'] = \str_replace('http://', 'https://', $item['url']);
- $webview_domain_name = array_merge($webview_domain_name, [[
- 'url' => $item['url']
- ]]);
- }
- }
- $store_mini->webview_domain_name = json_encode($webview_domain_name);
- if (!$store_mini->save()) {
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($store_mini->firstErrors))
- ];
- };
- return $this->setDomainName();
- } else {
- $data = [
- 'init_webview_domain_name' => [
- // [
- // 'url' => 'https://' . \Yii::$app->params['ws_url'],
- // ]
- ],
- 'webview_domain_name' => [
- [
- 'url' => ''
- ]
- ]
- ];
- if (!empty($store_mini->webview_domain_name)) {
- $webview_domain_name = json_decode($store_mini->webview_domain_name, true);
- if (!empty($webview_domain_name)) {
- $data['webview_domain_name'] = $webview_domain_name;
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ];
- }
- }
- //获取小程序短链$app->short_link->getShortLink
- public function getShortLink($path = '', $mini_id = 0, $is_platform = 0, $is_serve = 0) {
- try {
- $app = WechatMini::getWechatConfig($this->store_id, $mini_id, 0, $is_platform, $is_serve);
- if (empty($app)) {
- throw new \Exception('');
- }
- if (empty($path)) {
- throw new \Exception("页面路径不能为空");
- }
- //获取接口缓存
- // $app_id = $app->getConfig()['app_id'];
- // $cache_key = md5($this->store_id . '_'. $app_id . '_' . $path . '_short_link_');
- // if ($cache_key) {
- // return [
- // 'code' => 0,
- // 'msg' => "设置成功",
- // 'data' => $cache_key
- // ];
- // }
- $result = $app->short_link->getShortLink($path, '');
- if (empty($result['errcode']) && !empty($result)) {
- if (empty($result['link'])) {
- throw new \Exception("参数获取失败");
- }
- //增加接口缓存
- // cache()->set($cache_key, $result, 60);
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $result
- ];
- } elseif (empty($result)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($result) );
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- //服务市场服务二维码批量获取
- public function serviceQrcode($service_ids = '') {
- if($service_ids && !is_array($service_ids)){
- $service_ids = explode(',', $service_ids);
- }
- $ret = [];
- foreach((array)$service_ids as $id){
- $qr = $this->servicemarket_getwxaqrcode($id);
- $ret[] = $qr;
- }
- return $ret;
- }
- //获取网络图片到临时目录
- private function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- //返回中文错误信息
- public function getZnMsg($result)
- {
- \Yii::error([__METHOD__, $result]);
- $ErrorMsg = new ErrorMsg();
- $arr = $ErrorMsg->getArray();
- $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
- return $msg;
- }
- //返回中文错误信息
- public static function staticGetZnMsg($result)
- {
- \Yii::error([__METHOD__, $result]);
- $ErrorMsg = new ErrorMsg();
- $arr = $ErrorMsg->getArray();
- $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
- return $msg;
- }
- }
|