NewSalesmanForm.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Admin;
  9. use app\models\AggregateQrcode;
  10. use app\models\MerchantInfo;
  11. use app\models\Option;
  12. use app\models\SaasCategory;
  13. use app\models\SaasStoreReferral;
  14. use app\models\SaasUser;
  15. use app\models\Salesman;
  16. use app\models\SharingReceiver;
  17. use app\models\Store;
  18. use app\models\StoreAliMini;
  19. use app\models\StoreAliMiniVersion;
  20. use app\models\StoreAudit;
  21. use app\models\StoreMini;
  22. use app\models\StoreSchedule;
  23. use app\modules\admin\models\AlipayThirdForm;
  24. use app\modules\admin\models\WechatThirdErrorMsgForm;
  25. use app\modules\admin\models\WechatThirdForm;
  26. use app\utils\WechatMerchant\Merchant;
  27. use yii\base\Model;
  28. use EasyWeChat\Factory;
  29. use yii\data\Pagination;
  30. class NewSalesmanForm extends Model
  31. {
  32. public $contact_info;
  33. public $subject_info;
  34. public $business_info;
  35. public $bank_account_info;
  36. public $mini_apply_info;
  37. public $type = 'weixin'; //'weixin'
  38. public $code;
  39. public $id;
  40. public $qrcode_id;
  41. public $page;
  42. public $status;
  43. public $sort;
  44. public $mini_id;
  45. public $category_id;
  46. public $merchant_info_id;
  47. public $store_id;
  48. public $bind_store_id;
  49. public $openPlatform;
  50. public function rules()
  51. {
  52. return [
  53. [["license_pic", "license_no", "license_name", "legal_personal_name", "contact_info", "subject_info", "business_info", "bank_account_info", "type","code","pay_fee"], 'string'],
  54. [["apply_id","id",'qrcode_id', "page", "status", "sort", "category_id",'store_id','merchant_info_id','bind_store_id','mini_id'], "integer"],
  55. [["mini_apply_info"], "array"]
  56. ];
  57. }
  58. public function __construct($config = [])
  59. {
  60. parent::__construct($config);
  61. $config = [
  62. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  63. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  64. 'token' => Option::get("platform_token",0,'saas')['value'],
  65. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  66. ];
  67. $this->store_id = !empty($this->store_id)?$this->store_id:get_store_id();
  68. $this->openPlatform = Factory::openPlatform($config);
  69. }
  70. //返回中文错误信息
  71. public function getZnMsg($result){
  72. $ErrorMsg = new WechatThirdErrorMsgForm();
  73. $arr = $ErrorMsg->getArray();
  74. $msg = !empty($arr[$result['errcode']])?$arr[$result['errcode']]:$result['errmsg'];
  75. return $msg;
  76. }
  77. //TODO 商户逻辑 start
  78. public function saveMerchant(){
  79. //保存商户信息
  80. $merchant_info = MerchantInfo::find()->where(['bind_store_id'=>$this->bind_store_id, 'is_delete' => 0])->one();
  81. $mini = StoreMini::find()->where(['store_id'=>$this->bind_store_id])->orderBy('id desc')->select('appid')->one();
  82. if(empty($merchant_info) || empty($mini->appid)){
  83. return [
  84. 'code' => 1,
  85. 'msg' =>"参数错误"
  86. ];
  87. }
  88. $Merchant = new Merchant();
  89. return $Merchant->submit(json_decode($this->contact_info,true),json_decode($this->subject_info,true),json_decode($this->business_info,true),json_decode($this->bank_account_info,true),$merchant_info->id,$mini->appid);
  90. }
  91. public function getMerchant(){
  92. $StoreSchedule = StoreSchedule::find()->where(['audit_id'=>$this->id])->select('merchant_info')->one();
  93. return [
  94. 'code'=>0,
  95. 'msg'=>"",
  96. 'data'=>$StoreSchedule
  97. ];
  98. }
  99. //申请小程序
  100. public function applyMini(){
  101. $saas_user_id = get_saas_user_id();
  102. $mini_apply_info = $this->mini_apply_info;
  103. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  104. if($this->type == "weixin"){
  105. $form = new WechatThirdForm();
  106. $form->name = $mini_apply_info->license_name;
  107. $form->code = $mini_apply_info->license_no;
  108. $form->code_type = $mini_apply_info->code_type;
  109. $form->legal_persona_wechat = $mini_apply_info->legal_persona_account;
  110. $form->legal_persona_name = $mini_apply_info->legal_persona_name;
  111. $form->license_pic = $mini_apply_info->license_pic;
  112. $form->bind_store_id = $this->bind_store_id;
  113. $res = $form->fastRegisterMini();
  114. if($res['code'] === 0){
  115. if(!empty($this->bind_store_id)){
  116. $qrcode = AggregateQrcode::find()->where(['store_id'=>$this->bind_store_id])->one();
  117. if(!empty($qrcode)){
  118. $qrcode->wx_mini_id = $res['data'];
  119. $res = $qrcode->save();
  120. if($res){
  121. return [
  122. 'code'=>0,
  123. 'msg'=>"开始审核,绑定聚合码成功"
  124. ];
  125. }
  126. }
  127. }
  128. $res = [
  129. 'code'=>0,
  130. 'msg'=>"开始审核,聚合码未绑定"
  131. ];
  132. }
  133. }elseif($this->type == "ali"){
  134. $form = new AlipayThirdForm();
  135. $form->license_name = $mini_apply_info->license_name;
  136. $form->license_no = $mini_apply_info->license_no;
  137. $form->alipay_account = $mini_apply_info->legal_persona_account;
  138. $form->legal_personal_name = $mini_apply_info->legal_persona_name;
  139. $form->AppName = $mini_apply_info->app_name;
  140. $form->ServicePhone = $mini_apply_info->service_phone;
  141. $form->contact_name = $mini_apply_info->contact_name;
  142. $form->license_pic = $mini_apply_info->license_pic;
  143. $form->bind_store_id = $this->bind_store_id;
  144. $res = $form->isvCreateMini();
  145. if($res['code'] === 0){
  146. if(!empty($this->bind_store_id)){
  147. $qrcode = AggregateQrcode::find()->where(['store_id'=>$this->bind_store_id])->one();
  148. if(!empty($qrcode)){
  149. $qrcode->ali_mini_id = $res['id'];
  150. $res = $qrcode->save();
  151. if($res){
  152. return [
  153. 'code'=>0,
  154. 'msg'=>"开始审核,绑定聚合码成功"
  155. ];
  156. }
  157. }
  158. }
  159. $res = [
  160. 'code'=>0,
  161. 'msg'=>"开始审核,聚合码未绑定"
  162. ];
  163. }
  164. }else{
  165. $res = [
  166. 'code'=>1,
  167. 'msg'=>"未知的申请方式"
  168. ];
  169. }
  170. return $res;
  171. }
  172. //绑定收款码
  173. public function bindPayQrcode(){
  174. $bind_store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id();
  175. if (!empty($this->bind_store_id)){
  176. $saas_user_id = get_saas_user_id();
  177. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  178. $AggregateQrcode = AggregateQrcode::find()->where([
  179. 'or',
  180. ['id' => $this->qrcode_id],
  181. ['store_id' => $bind_store_id]
  182. ])->one();
  183. } else{
  184. $AggregateQrcode = AggregateQrcode::find()->where(['store_id' => $bind_store_id])->one();
  185. if(empty($AggregateQrcode->salesman_id)){
  186. return [
  187. 'code'=>1,
  188. 'msg'=>"请绑定业务员"
  189. ];
  190. }
  191. $salesman = Salesman::findOne($AggregateQrcode->salesman_id);
  192. }
  193. // \Yii::error(json_encode($AggregateQrcode));
  194. if (empty($AggregateQrcode)){
  195. return [
  196. 'code'=>1,
  197. 'msg'=>"聚合码不存在"
  198. ];
  199. }
  200. if (empty($AggregateQrcode->ali_status) || empty($AggregateQrcode->wx_status)){
  201. if ($this->type == "weixin" || empty($this->type)){
  202. $form = StoreMini::find()->where(['store_id'=>$bind_store_id])->orderBy('id desc')->select('id,appid')->asArray()->one();
  203. $mini_id = !empty($this->mini_id)?$this->mini_id:$form['id'];
  204. $AggregateQrcode->wx_mini_id = $mini_id;
  205. } elseif($this->type == "ali"){
  206. $form = StoreAliMini::find()->where(['store_id'=>$bind_store_id])->orderBy('id desc')->select('id,auth_app_id appid')->asArray()->one();
  207. $mini_id = !empty($this->mini_id)?$this->mini_id:$form['id'];
  208. $AggregateQrcode->ali_mini_id = $mini_id;
  209. }
  210. if (empty($form['appid'])){
  211. return [
  212. 'code'=>1,
  213. 'msg'=>"没有找到任何的小程序或小程序未创建完成"
  214. ];
  215. }
  216. $AggregateQrcode->store_id = $bind_store_id;
  217. $AggregateQrcode->mini_time = time();
  218. if (empty($AggregateQrcode->salesman_id)){
  219. $AggregateQrcode->salesman_id = $salesman->id;
  220. $AggregateQrcode->admin_id = $salesman->admin_id;
  221. }
  222. $res = $AggregateQrcode->save();
  223. if ($res){
  224. if ($this->type == "ali"){
  225. $form = new AlipayThirdForm();
  226. $res = $form->bindQrcode($AggregateQrcode->ali_mini_id);
  227. if ($res['code'] != 0){
  228. return [
  229. 'code'=>1,
  230. 'msg'=>'绑定失败'
  231. ];
  232. }
  233. $MerchantInfo = MerchantInfo::find()->where([ 'bind_store_id' => $bind_store_id, 'is_delete' => 0])->orderBy('id desc')->one();
  234. if (!empty($MerchantInfo) && !empty($this->bind_store_id)){
  235. $str = $MerchantInfo->app_status;
  236. $str_arr = explode(',', $str);
  237. // 判断当前数组是否包含1
  238. if (in_array(2, $str_arr)) {
  239. $string = count($str_arr) > 1 ? '1' : '0';
  240. $MerchantInfo->app_status = $string;
  241. }
  242. if (!$MerchantInfo->save()){
  243. return [
  244. 'code'=>1,
  245. 'msg'=>json_encode($MerchantInfo->errors)
  246. ];
  247. }
  248. }
  249. return [
  250. 'code'=>0,
  251. 'msg'=>"绑定成功"
  252. ];
  253. }elseif($this->type == "weixin" || empty($this->type)){
  254. $wechat = new WechatThirdForm();
  255. $wechat->bind_store_id = $bind_store_id;
  256. $res = $wechat->downQrcodeRules($form['id'],$form['appid']);
  257. if($res['code'] === 0){
  258. $MerchantInfo = MerchantInfo::find()->where(['bind_store_id' => $bind_store_id, 'is_delete' => 0])->orderBy('id desc')->one();
  259. if(!empty($MerchantInfo) && !empty($this->bind_store_id)){
  260. $str = $MerchantInfo->app_status;
  261. $str_arr = explode(',', $str);
  262. // 判断当前数组是否包含1
  263. if(in_array(1, $str_arr)) {
  264. $string = count($str_arr) > 1 ? '2' : '0';
  265. $MerchantInfo->app_status = $string;
  266. }
  267. if(!$MerchantInfo->save()){
  268. return [
  269. 'code'=>1,
  270. 'msg'=>json_encode($MerchantInfo->errors)
  271. ];
  272. }
  273. }
  274. return [
  275. 'code'=>0,
  276. 'msg'=>"绑定成功"
  277. ];
  278. }else{
  279. return [
  280. 'code'=>1,
  281. 'msg'=>$res['msg']
  282. ];
  283. }
  284. }else{
  285. return [
  286. 'code'=>1,
  287. 'msg'=>'使用未知的系统'
  288. ];
  289. }
  290. }else{
  291. return [
  292. 'code'=>1,
  293. 'msg'=>'绑定失败'
  294. ];
  295. }
  296. }else{
  297. return [
  298. 'code'=>1,
  299. 'msg'=>'已经绑定完成,请勿重复绑定'
  300. ];
  301. }
  302. }
  303. //获取进度信息
  304. public function getProgress(){//注册小程序-进件状态-小程序完善状态-绑定二维码状态
  305. if(empty($this->bind_store_id)){
  306. return [
  307. 'code'=>1,
  308. 'msg'=>"参数错误",
  309. 'data'=>[
  310. 'num'=>2,
  311. 'progress'=>1
  312. ]
  313. ];
  314. }
  315. $MerchantInfo = MerchantInfo::find()->where(['bind_store_id'=>$this->bind_store_id, 'is_delete' => 0])->select('id,status,bind_store_id,refuse_desc,contact_info,subject_info,business_info,bank_account_info,applyment_id')->orderBy('id desc')->asArray()->one();
  316. $StoreSchedule = StoreSchedule::find()->where(['store_id'=>$this->bind_store_id])->one();
  317. $StoreAudit = StoreAudit::find()->where(['merchant_info_id'=>$MerchantInfo['id']])->select("name,logo")->asArray()->one();
  318. $form = new WechatThirdForm();
  319. $form->bind_store_id = $this->bind_store_id;
  320. $all_categories = $form->getAllCategories();
  321. $all_category = [];
  322. $all_old_category = [];
  323. if($all_categories['code'] === 0){
  324. $all_old_category = $all_categories['old_data'];
  325. $all_category = $all_categories['data'][0]['children'];
  326. }
  327. if($this->type == 'ali'){
  328. $form = new AlipayThirdForm();
  329. $form->bind_store_id = $this->bind_store_id;
  330. $all_categories = $form->getCategoryList();
  331. if($all_categories['code'] === 0){
  332. $all_category = $all_categories['data'];
  333. }
  334. }
  335. $merchant_info = [];
  336. if(!empty($StoreSchedule->merchant_info)){
  337. $merchant_info = json_decode($StoreSchedule->merchant_info,true);
  338. $business_license_info = $merchant_info['subject_info']['business_license_info'];
  339. }
  340. if($MerchantInfo['status'] > 0){
  341. $merchant_info = $MerchantInfo;
  342. $merchant_info['subject_info'] = !empty($merchant_info['subject_info'])?json_decode($merchant_info['subject_info'],true):[];
  343. $business_license_info = $merchant_info['subject_info']['business_license_info'];
  344. $merchant_info['business_info'] = !empty($merchant_info['business_info'])?json_decode($merchant_info['business_info'],true):[];
  345. $merchant_info['settlement_info'] = !empty($merchant_info['settlement_info'])?json_decode($merchant_info['settlement_info'],true):[];
  346. $merchant_info['bank_account_info'] = !empty($merchant_info['bank_account_info'])?json_decode($merchant_info['bank_account_info'],true):[];
  347. $merchant_info['contact_info'] = !empty($merchant_info['contact_info'])?json_decode($merchant_info['contact_info'],true):[];
  348. }
  349. // \Yii::error($merchant_info);
  350. if($this->type == "weixin"){
  351. $mini = StoreMini::find()->where(['store_id'=>$this->bind_store_id])
  352. ->orderBy('id desc')
  353. ->select('id,status,msg,mini_up,mini_up_error,name license_name,apply_name_status,apply_name_error,code license_no,code_type,legal_persona_wechat legal_persona_account,legal_persona_name,license_pic,mini_url,signature,mini_nickname,categories')
  354. ->asArray()->one();
  355. $mini_data = [
  356. 'mini_url'=>!empty($mini['mini_url'])?$mini['mini_url']:$StoreAudit['logo'],
  357. 'signature'=>$mini['signature'],
  358. 'mini_nickname'=>!empty($mini['mini_nickname'])?$mini['mini_nickname']:$StoreAudit['name'],
  359. 'categories'=>!empty($mini['categories'])?json_decode($mini['categories'],true):[]
  360. ];
  361. if(!empty($mini)){//是否注册小程序
  362. if($mini['status'] == -1){ //小程序注册审核中
  363. $res = $this->openPlatform
  364. ->component
  365. ->getRegistrationStatus($mini['license_name'], $mini['legal_persona_account'], $mini['legal_persona_name']);
  366. return [
  367. 'code'=>0,
  368. 'msg'=>"小程序注册审核中",
  369. 'data'=>[
  370. 'mini_info'=>[
  371. 'id'=>$mini['id'],
  372. 'status'=>$mini['status'],
  373. 'license_name'=>$mini['license_name'],
  374. 'license_no'=>$mini['license_no'],
  375. 'code_type'=>$mini['code_type'],
  376. 'legal_persona_account'=>$mini['legal_persona_account'],
  377. 'legal_persona_name'=>$mini['legal_persona_name'],
  378. 'license_pic'=>$mini['license_pic'],
  379. ],
  380. 'alert_msg' => $this->getZnMsg($res),
  381. 'num'=>$res['errcode'] == 89250 ? 2 : 3, // 如果查询未找到该任务,重新提交
  382. 'progress'=>1,
  383. 'bind_store_id'=>$this->bind_store_id,
  384. ]
  385. ];
  386. }elseif($mini['status'] == 2){ //小程序注册审核失败
  387. return [
  388. 'code'=>0,
  389. 'msg'=>"小程序注册审核失败".$mini['msg'],
  390. 'data'=>[
  391. 'mini_info'=>[
  392. 'id'=>$mini['id'],
  393. 'status'=>$mini['status'],
  394. 'license_name'=>$mini['license_name'],
  395. 'license_no'=>$mini['license_no'],
  396. 'code_type'=>$mini['code_type'],
  397. 'legal_persona_account'=>$mini['legal_persona_account'],
  398. 'legal_persona_name'=>$mini['legal_persona_name'],
  399. 'license_pic'=>$mini['license_pic'],
  400. "msg"=>$mini['msg']
  401. ],
  402. 'alert_msg'=>"小程序注册审核失败".$mini['msg'],
  403. 'num'=>2,
  404. 'progress'=>1,
  405. 'bind_store_id'=>$this->bind_store_id,
  406. ]
  407. ];
  408. }elseif($mini['status'] == 0 || $mini['status'] == 1){//小程序注册成功
  409. // 支付进件状态
  410. if(!empty($MerchantInfo)){//判断进件是否提交资料
  411. if($MerchantInfo['status'] == 1){//判断审核状态
  412. $merchant = new Merchant();
  413. $res = $merchant->queryApplyment($MerchantInfo['applyment_id']);
  414. if ($res['applyment_state_msg']) {
  415. $alert_msg = $res['applyment_state_msg'];
  416. }
  417. if ($res['sign_url']) {
  418. $alert_url = $res['sign_url'];
  419. }
  420. return [
  421. 'code'=>0,
  422. 'msg'=>"进件材料审核中",
  423. 'data'=>[
  424. 'num'=>1,
  425. "progress"=>2,
  426. 'bind_store_id'=> $this->bind_store_id,
  427. "merchant_info"=> $merchant_info,
  428. 'alert_msg' => $alert_msg,
  429. 'alert_url' => isset($alert_url) ? $alert_url : ''
  430. ]
  431. ];
  432. }elseif($MerchantInfo['status'] == 2){
  433. return [
  434. 'code'=>0,
  435. 'msg'=>"进件材料审核失败".$MerchantInfo['refuse_desc'],
  436. 'data'=>[
  437. 'num'=>2,
  438. "progress"=>2,
  439. 'bind_store_id'=>$this->bind_store_id,
  440. "merchant_info"=>$merchant_info,
  441. 'msg'=>$MerchantInfo['refuse_desc'],
  442. 'alert_msg'=>"进件材料审核失败".$MerchantInfo['refuse_desc'],
  443. ]
  444. ];
  445. }elseif($MerchantInfo['status'] == 3){
  446. // 小程序完善资料
  447. if(empty($mini['mini_url'])||empty($mini['signature'])||empty($mini['mini_nickname'])||empty($mini['categories'])){
  448. return [
  449. 'code'=>0,
  450. 'msg'=>"小程序资料不全",
  451. 'data'=>[
  452. 'num'=>1,
  453. "progress"=>3,
  454. 'bind_store_id'=>$this->bind_store_id,
  455. 'mini_data'=>$mini_data,
  456. 'all_categories'=>$all_category,
  457. 'all_old_category' => $all_old_category
  458. ]
  459. ];
  460. }
  461. $form = new WechatThirdForm();
  462. $form->mini_id = $mini['id'];
  463. $res = $form->getNicknameAuditStatus();
  464. if (isset($res['status']) && $res['status'] == 3 && $mini['apply_name_status'] != 3) {
  465. $mini['apply_name_status'] = 3;
  466. }
  467. if($mini['apply_name_status'] == 2){
  468. return [
  469. 'code'=>0,
  470. 'msg'=>"小程序资料审核失败",
  471. 'data'=>[
  472. 'num'=>8,
  473. "progress"=>3,
  474. 'bind_store_id'=>$this->bind_store_id,
  475. 'mini_data'=>$mini_data,
  476. 'msg'=>$mini['apply_name_error'],
  477. 'all_categories'=>$all_category,
  478. 'alert_msg' => $mini['apply_name_error'],
  479. ]
  480. ];
  481. }
  482. if($mini['apply_name_status'] == 1){
  483. return [
  484. 'code'=>0,
  485. 'msg'=>"小程序资料开始审核",
  486. 'data'=>[
  487. 'num'=>7,
  488. "progress"=>3,
  489. 'bind_store_id'=>$this->bind_store_id,
  490. 'mini_data'=>$mini_data,
  491. 'all_categories'=>$all_category,
  492. 'alert_msg' => '小程序资料开始审核'
  493. ]
  494. ];
  495. }
  496. if($mini['mini_up'] == 5){ //判断是否完善资料
  497. $AggregateQrcode = AggregateQrcode::findOne(['store_id'=>$this->bind_store_id]);
  498. if(!empty($AggregateQrcode->wx_status)){
  499. return [
  500. 'code'=>0,
  501. 'msg'=>"完成",
  502. 'data'=>[
  503. 'num'=>0,
  504. 'bind_store_id'=>$this->bind_store_id,
  505. "progress"=>5,
  506. 'qrcode_id'=>$AggregateQrcode->id
  507. ]
  508. ];
  509. }else{
  510. return [
  511. 'code'=>0,
  512. 'msg'=>"没有绑定二维码",
  513. 'data'=>[
  514. 'num'=>1,
  515. 'bind_store_id'=>$this->bind_store_id,
  516. "progress"=>4,
  517. 'qrcode_id'=>$AggregateQrcode->id,
  518. 'alert_msg' => '没有绑定二维码'
  519. ]
  520. ];
  521. }
  522. }elseif($mini['mini_up'] == 4){
  523. return [
  524. 'code'=>0,
  525. 'msg'=>"小程序代码审核延迟",
  526. 'data'=>[
  527. 'num'=>4,
  528. 'bind_store_id'=>$this->bind_store_id,
  529. "progress"=>3,
  530. 'mini_data'=>$mini_data,
  531. ]
  532. ];
  533. }elseif($mini['mini_up'] == 2){
  534. return [
  535. 'code'=>0,
  536. 'msg'=>"小程序代码审核通过未发布",
  537. 'data'=>[
  538. 'num'=>6,
  539. 'bind_store_id'=>$this->bind_store_id,
  540. "progress"=>3,
  541. 'mini_data'=>$mini_data,
  542. ]
  543. ];
  544. }elseif($mini['mini_up'] == 3){
  545. return [
  546. 'code'=>0,
  547. 'msg'=>"小程序代码审核失败,".$mini['mini_up_error'],
  548. 'data'=>[
  549. 'msg'=>$mini['mini_up_error'],
  550. 'num'=>5,
  551. "progress"=>3,
  552. 'bind_store_id'=>$this->bind_store_id,
  553. 'mini_data'=>$mini_data,
  554. 'alert_msg' => $mini['mini_up_error']
  555. ]
  556. ];
  557. }elseif($mini['mini_up'] == 1){
  558. return [
  559. 'code'=>0,
  560. 'msg'=>"小程序代码审核中",
  561. 'data'=>[
  562. 'num'=>4,
  563. 'bind_store_id'=>$this->bind_store_id,
  564. "progress"=>3,
  565. 'mini_data'=>$mini_data,
  566. 'alert_msg' => '小程序代码审核中'
  567. ]
  568. ];
  569. }elseif($mini['mini_up'] == -1){
  570. return [
  571. 'code'=>0,
  572. 'msg'=>"小程序代码未审核",
  573. 'data'=>[
  574. 'num'=>3,
  575. 'bind_store_id'=>$this->bind_store_id,
  576. "progress"=>3,
  577. 'mini_data'=>$mini_data,
  578. ]
  579. ];
  580. }elseif($mini['mini_up'] == 0){
  581. return [
  582. 'code'=>0,
  583. 'msg'=>"小程序代码未上传",
  584. 'data'=>[
  585. 'num'=>2,
  586. 'bind_store_id'=>$this->bind_store_id,
  587. "progress"=>3,
  588. 'mini_data'=>$mini_data,
  589. ]
  590. ];
  591. }else{
  592. return [
  593. 'code'=>0,
  594. 'msg'=>"未知状态",
  595. 'data'=>[
  596. 'num'=>13,
  597. 'bind_store_id'=>$this->bind_store_id,
  598. "progress"=>3,
  599. 'mini_data'=>$mini_data,
  600. ]
  601. ];
  602. }
  603. }else{
  604. return [
  605. 'code'=>0,
  606. 'msg'=>"未提交进件资料",
  607. 'data'=>[
  608. 'num'=>3,
  609. "progress"=>2,
  610. 'bind_store_id'=>$this->bind_store_id,
  611. "merchant_info"=>$merchant_info,
  612. ]
  613. ];
  614. }
  615. }else{
  616. return [
  617. 'code'=>0,
  618. 'msg'=>"未提交进件资料",
  619. "data"=>[
  620. 'merchant_info'=>$merchant_info,
  621. 'bind_store_id'=>$this->bind_store_id,
  622. 'num'=>3,
  623. "progress"=>2,
  624. ]
  625. ];
  626. }
  627. }else{
  628. return [
  629. 'code'=>0,
  630. 'msg'=>"未知状态",
  631. 'data'=>[
  632. 'num'=>13,
  633. "progress"=>1
  634. ]
  635. ];
  636. }
  637. }else{
  638. return [
  639. 'code'=>0,
  640. 'msg'=>"小程序未注册",
  641. 'data'=>[
  642. 'num'=>1,
  643. "progress"=>1,
  644. "merchant_info"=>$merchant_info['subject_info'],
  645. 'mini_info'=>[
  646. 'license_name'=>$business_license_info['merchant_name'],
  647. 'license_no'=>$business_license_info['license_number'],
  648. 'code_type'=>1,
  649. 'legal_persona_name'=>$business_license_info['legal_person'],
  650. 'license_pic'=>$business_license_info['license_copy'],
  651. ],
  652. ]
  653. ];
  654. }
  655. }elseif($this->type=="ali"){
  656. //查询签约状态
  657. $mini = StoreAliMini::find()->alias('sam')->where(['sam.store_id'=>$this->bind_store_id,'sam.is_cancel'=>0])
  658. ->leftJoin(['mv'=>StoreAliMiniVersion::tableName()],'mv.mini_id = sam.id')
  659. ->orderBy('sam.id desc')
  660. ->select("sam.batch_status,sam.store_face_photo,mv.id m_id,mv.is_up,mv.is_submit,sam.id,sam.store_id,sam.category_data,sam.license_no,sam.license_pic,sam.app_name,sam.app_english_name,sam.app_slogan,sam.app_logo,sam.app_desc,sam.service_phone,sam.service_email,sam.mini_categoryIds,sam.license_date,sam.license_name,sam.category_name,sam.is_long_effective,sam.alipay_account,sam.legal_personal_name,sam.contact_name,sam.order_cancel")
  661. ->asArray()
  662. ->one();
  663. $mini_re_info = [
  664. "license_name"=>$mini['license_name'],
  665. "license_no"=>$mini['license_no'],
  666. "legal_persona_account"=>$mini['alipay_account'],
  667. "legal_persona_name"=>$mini['legal_personal_name'],
  668. "app_name"=>$mini['app_name'],
  669. "service_phone"=>$mini['service_phone'],
  670. "contact_name"=>$mini['contact_name'],
  671. "license_pic"=>$mini['license_pic'],
  672. ];
  673. $mini_categoryIds = [];
  674. if(!empty($mini['category_data'])){
  675. $mini_categoryIds = json_decode($mini['category_data'],true);
  676. }
  677. $mini_info = [
  678. "app_name"=>!empty($mini['app_name'])?$mini['app_name']:$StoreAudit['name'],
  679. "app_english_name"=>$mini['app_english_name'],
  680. "app_slogan"=>$mini['app_slogan'],
  681. "license_no"=>$mini['license_no'],
  682. "license_pic"=>$mini['license_pic'],
  683. "license_name"=>$mini['license_name'],
  684. "license_date"=>$mini['license_date'],
  685. "is_long_effective"=>$mini['is_long_effective'],
  686. "app_logo"=>!empty($mini['app_logo'])?$mini['app_logo']:$StoreAudit['logo'],
  687. "app_desc"=>$mini['app_desc'],
  688. "service_phone"=>$mini['service_phone'],
  689. "service_email"=>$mini['service_email'],
  690. "mini_categoryIds"=>$mini_categoryIds,
  691. ];
  692. if(empty($mini)){
  693. return [
  694. 'code'=>0,
  695. 'msg'=>"没有注册小程序",
  696. 'data'=>[
  697. 'bind_store_id'=>$this->bind_store_id,
  698. 'merchant_info'=>$merchant_info,
  699. 'progress'=>1,
  700. 'num'=>1,
  701. 'mini_info'=>[
  702. 'license_name'=>$business_license_info['merchant_name'],
  703. 'license_no'=>$business_license_info['license_number'],
  704. 'legal_persona_name'=>$business_license_info['legal_person'],
  705. 'license_pic'=>$business_license_info['license_copy'],
  706. "service_phone"=>$merchant_info['contact_info']['contact_name'],
  707. "contact_name"=> $merchant_info['contact_info']['mobile_phone'],
  708. "app_name"=>$StoreAudit['name']
  709. ]
  710. ]
  711. ];
  712. }else{//进度 提交 审核 () 完善信息
  713. // 获取当前审核状态
  714. // $res = (new AlipayThirdForm())->getMiniShenheStatus($mini['id']);
  715. // if (isset($res['status'])) {
  716. // }
  717. if(empty($mini['m_id'])){
  718. return [
  719. 'code'=>0,
  720. 'msg'=>"小程序注册待确认或签约审核中",
  721. 'data'=>[
  722. 'bind_store_id'=>$this->bind_store_id,
  723. 'progress'=>1,
  724. 'num'=>2,
  725. 'mini_info'=>$mini_re_info
  726. ],
  727. ];
  728. }
  729. if($mini['order_cancel'] == 1){
  730. return [
  731. 'code'=>0,
  732. 'msg'=>"小程序注册已拒绝",
  733. 'data'=>[
  734. 'bind_store_id'=>$this->bind_store_id,
  735. 'progress'=>1,
  736. 'num'=>3,
  737. 'mini_info'=>$mini_re_info
  738. ]
  739. ];
  740. }
  741. if($mini['is_submit'] == 0 && (
  742. empty($mini['app_name']) ||
  743. empty($mini['app_english_name']) ||
  744. empty($mini['app_slogan']) ||
  745. empty($mini['app_logo']) ||
  746. empty($mini['app_desc']) ||
  747. empty($mini['service_phone']) ||
  748. empty($mini['service_email']) ||
  749. empty($mini['mini_categoryIds'])
  750. )){
  751. return [
  752. 'code'=>0,
  753. 'msg'=>"小程序资料需要完善",
  754. 'data'=>[
  755. 'bind_store_id'=>$this->bind_store_id,
  756. 'progress'=>2,
  757. 'num'=>1,
  758. 'mini_info'=>$mini_info,
  759. 'all_categories'=>$all_category
  760. ]
  761. ];
  762. }elseif($mini['is_submit'] == 0){
  763. return [
  764. 'code'=>0,
  765. 'msg'=>"小程序资料完善完成",//可以审核
  766. 'data'=>[
  767. 'bind_store_id'=>$this->bind_store_id,
  768. 'progress'=>2,
  769. 'num'=>2,
  770. 'mini_info'=>$mini_info
  771. ]
  772. ];
  773. }
  774. if($mini['is_submit'] == 1){
  775. return [
  776. 'code'=>0,
  777. 'msg'=>"小程序审核中",
  778. 'data'=>[
  779. 'bind_store_id'=>$this->bind_store_id,
  780. 'progress'=>2,
  781. 'num'=>3,
  782. 'mini_info'=>$mini_info
  783. ]
  784. ];
  785. }elseif($mini['is_submit'] == 2){
  786. return [
  787. 'code'=>0,
  788. 'msg'=>"小程序审核通过",//可以发布
  789. 'data'=>[
  790. 'bind_store_id'=>$this->bind_store_id,
  791. 'progress'=>2,
  792. 'num'=>4,
  793. 'mini_info'=>$mini_info
  794. ]
  795. ];
  796. }elseif($mini['is_submit'] == 3){
  797. return [
  798. 'code'=>0,
  799. 'msg'=>"小程序审核失败",
  800. 'data'=>[
  801. 'bind_store_id'=>$this->bind_store_id,
  802. 'progress'=>2,
  803. 'num'=>5,
  804. 'mini_info'=>$mini_info
  805. ]
  806. ];
  807. }elseif($mini['is_submit'] == 4){
  808. if($mini['batch_status'] == 0 || $mini['batch_status'] == 7){//没有创建事务
  809. return [
  810. 'code'=>0,
  811. 'msg'=>"需要创建事务",
  812. 'data'=>[
  813. 'bind_store_id'=>$this->bind_store_id,
  814. 'progress'=>3,
  815. 'num'=>1
  816. ]
  817. ];
  818. }elseif($mini['batch_status'] == 1 || $mini['batch_status'] == 4){//创建事务成功
  819. return [
  820. 'code'=>0,
  821. 'msg'=>"需要签约当面付",
  822. 'data'=>[
  823. 'bind_store_id'=>$this->bind_store_id,
  824. 'progress'=>3,
  825. 'num'=>2
  826. ]
  827. ];
  828. }elseif($mini['batch_status'] == 2){//签约当面付成功
  829. return [
  830. 'code'=>0,
  831. 'msg'=>"审核当面付",
  832. 'data'=>[
  833. 'bind_store_id'=>$this->bind_store_id,
  834. 'progress'=>3,
  835. 'num'=>3
  836. ]
  837. ];
  838. }elseif($mini['batch_status'] == 3){//审核中
  839. return [
  840. 'code'=>0,
  841. 'msg'=>"审核中",
  842. 'data'=>[
  843. 'bind_store_id'=>$this->bind_store_id,
  844. 'progress'=>3,
  845. 'num'=>4
  846. ]
  847. ];
  848. }elseif($mini['batch_status'] == 4){
  849. return [
  850. 'code'=>0,
  851. 'msg'=>"小程序签约已拒绝",
  852. 'data'=>[
  853. 'bind_store_id'=>$this->bind_store_id,
  854. 'progress'=>3,
  855. 'num'=>5
  856. ]
  857. ];
  858. }elseif($mini['batch_status'] == 5){//等待商户确认
  859. return [
  860. 'code'=>0,
  861. 'msg'=>"等待商户确认",
  862. 'data'=>[
  863. 'bind_store_id'=>$this->bind_store_id,
  864. 'progress'=>3,
  865. 'num'=>6
  866. ]
  867. ];
  868. }elseif($mini['batch_status'] == 6){//确认成功
  869. $AggregateQrcode = AggregateQrcode::find()->where(['and',['store_id'=>$this->bind_store_id],["<>",'ali_mini_id',""]])->one();
  870. if(!empty($AggregateQrcode->ali_status)){
  871. return [
  872. 'code'=>0,
  873. 'msg'=>"完成",
  874. 'data'=>[
  875. 'bind_store_id'=>$this->bind_store_id,
  876. 'progress'=>5,
  877. 'num'=>1,
  878. 'qrcode_id'=>$AggregateQrcode->id
  879. ]
  880. ];
  881. }else{
  882. return [
  883. 'code'=>0,
  884. 'msg'=>"未绑定聚合码",
  885. 'data'=>[
  886. 'bind_store_id'=>$this->bind_store_id,
  887. 'progress'=>4,
  888. 'num'=>1,
  889. 'qrcode_id'=>$AggregateQrcode->id
  890. ]
  891. ];
  892. }
  893. }elseif($mini['batch_status'] == 8){//审核失败
  894. return [
  895. 'code'=>0,
  896. 'msg'=>"审核失败",
  897. 'data'=>[
  898. 'bind_store_id'=>$this->bind_store_id,
  899. 'progress'=>3,
  900. 'num'=>7,
  901. 'store_face_photo'=>$mini['store_face_photo']
  902. ]
  903. ];
  904. }
  905. }
  906. }
  907. }else{
  908. return [
  909. 'code'=>0,
  910. 'msg'=>"未识别的操作系统",
  911. 'data'=>[
  912. 'num'=>13,
  913. "progress"=>1
  914. ]
  915. ];
  916. }
  917. }
  918. //小程序列表基础数据
  919. public function getUserInfo(){
  920. $saas_user_id = get_saas_user_id();
  921. //佣金
  922. //已到帐
  923. $query = SharingReceiver::find()->where(['user_id' => $saas_user_id, 'is_pay' => 1, 'from' => SharingReceiver::FROM_RECOMMEND,'is_delete'=>0])->andWhere(['like', 'order_no', 'ML']);//->sum('amount');
  924. $is_pay_money['all'] = $query->sum('amount')??'0';
  925. $is_pay_money['prev'] = $query->andWhere(['and',['<','created_at',strtotime(date('Y-m-d'))],['>','created_at',(strtotime(date('Y-m-d')) - 60*60*24)]])->sum('amount')??'0';
  926. //未到账
  927. $query = SharingReceiver::find()->where(['user_id' => $saas_user_id, 'is_pay' => 0, 'from' => SharingReceiver::FROM_RECOMMEND,'is_delete'=>0])->andWhere(['like', 'order_no', 'ML']);
  928. $no_pay_money['all'] = $query->sum('amount')??'0';
  929. $no_pay_money['prev'] = $query->andWhere(['and',['<','created_at',strtotime(date('Y-m-d'))],['>','created_at',(strtotime(date('Y-m-d')) - 60*60*24)]])->sum('amount')??'0';
  930. //总金额
  931. $query = SharingReceiver::find()->where(['user_id' => $saas_user_id, 'from' => SharingReceiver::FROM_RECOMMEND,'is_delete'=>0])->andWhere(['like', 'order_no', 'ML']);
  932. $all_pay_money['all'] = $query->sum('amount')??'0';
  933. $all_pay_money['prev'] = $query->andWhere(['and',['<','created_at',strtotime(date('Y-m-d'))],['>','created_at',(strtotime(date('Y-m-d')) - 60*60*24)]])->sum('amount')??'0';
  934. //分销订单数量
  935. $query = SharingReceiver::find()->where(['user_id' => $saas_user_id, 'from' => SharingReceiver::FROM_RECOMMEND])->andWhere(['like', 'order_no', 'ML']);
  936. $count['all'] = $query->count();
  937. $count['prev'] = $query->andWhere(['and',['<','created_at',strtotime(date('Y-m-d'))],['>','created_at',(strtotime(date('Y-m-d')) - 60*60*24)]])->count();
  938. //头像
  939. $avatar = get_saas_user()->avatar;
  940. //名称
  941. $name = get_saas_user()->name;
  942. //所属店铺
  943. $up_name = SaasUser::findOne(Admin::findOne(Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0])->admin_id)->saas_user_id)->name;
  944. //下级店铺数量
  945. $query = SaasStoreReferral::find()->alias('ssr')
  946. ->leftJoin(['s' => Store::tableName()], 's.id=ssr.store_id')
  947. ->andWhere(['ssr.referral_id' => $saas_user_id])->groupBy(['ssr.store_id'])->andWhere([
  948. '>',
  949. 's.id',
  950. 0
  951. ]);
  952. $sub_merchant_count['all'] = $query->count();
  953. $sub_merchant_count['prev'] = $query->andWhere(['and',['<','s.created_at',strtotime(date('Y-m-d'))],['>','s.created_at',(strtotime(date('Y-m-d')) - 60*60*24)]])->count();
  954. //(店铺待审核/待完善/待完成/待处理)
  955. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  956. $query = StoreAudit::find()->alias('sa')->leftJoin(['mi' => MerchantInfo::tableName()], 'mi.id=sa.merchant_info_id')->where(['sa.salesman_id' => $salesman->id, 'mi.is_delete' => 0]);
  957. //待审核
  958. $examineQuery = clone $query;
  959. $examineQuery->andWhere(['sa.status' => 0]);
  960. $examine['all'] = $examineQuery->count();
  961. $examine['prev'] = $examineQuery->andWhere(['OR',['and',['<','sa.created_at',strtotime(date('Y-m-d'))],['>','sa.created_at',(strtotime(date('Y-m-d')) - 60*60*24)]],['and',['<','sa.updated_at',strtotime(date('Y-m-d'))],['>','sa.updated_at',(strtotime(date('Y-m-d')) - 60*60*24)]]])->count();
  962. //待完善
  963. $repairQuery = clone $query;
  964. $repairQuery->andWhere(['and',['sa.status' => 1],['!=', 'mi.status', 3],['<>','mi.app_status','']]);
  965. $repair['all'] = $repairQuery->count();
  966. $repair['prev'] = $repairQuery->andWhere(['OR',['and',['<','sa.created_at',strtotime(date('Y-m-d'))],['>','sa.created_at',(strtotime(date('Y-m-d')) - 60*60*24)]],['and',['<','sa.updated_at',strtotime(date('Y-m-d'))],['>','sa.updated_at',(strtotime(date('Y-m-d')) - 60*60*24)]]])->count();
  967. //待完成
  968. $finishQuery = clone $query;
  969. $finishQuery->andWhere(['sa.status' => 1, 'mi.status' => 3,'mi.app_status'=>'12']);
  970. $finish['all'] = $finishQuery->count();
  971. $finish['prev'] = $finishQuery->andWhere(['OR',['and',['<','sa.created_at',strtotime(date('Y-m-d'))],['>','sa.created_at',(strtotime(date('Y-m-d')) - 60*60*24)]],['and',['<','sa.updated_at',strtotime(date('Y-m-d'))],['>','sa.updated_at',(strtotime(date('Y-m-d')) - 60*60*24)]]])->count();
  972. //待处理
  973. $query->andWhere(['OR',['sa.status' => 0],['and',['sa.status' => 1],['or',['!=', 'mi.status', 3],['<>','mi.app_status','']]]]);
  974. $handle_count['all'] = $query->count();
  975. $handle_count['prev'] = $query->andWhere(['OR',['and',['<','sa.created_at',strtotime(date('Y-m-d'))],['>','sa.created_at',(strtotime(date('Y-m-d')) - 60*60*24)]],['and',['<','sa.updated_at',strtotime(date('Y-m-d'))],['>','sa.updated_at',(strtotime(date('Y-m-d')) - 60*60*24)]]])->count();
  976. //展示微信审核失败信息
  977. $qrcode = AggregateQrcode::find()->where(['salesman_id'=>$salesman->id])->select('id,wx_mini_id,ali_mini_id,salesman_id,store_id')->asArray()->all();
  978. $mini = [];
  979. //拟定通知消息
  980. $notice = [];
  981. foreach($qrcode as $value){
  982. if(!empty($value['wx_mini_id'])){
  983. $data = StoreMini::find()->where(['AND',['id'=>$value['wx_mini_id']],['OR',['status'=>2,'mini_up'=>3]]])->select('id,store_id,msg,status,mini_up_error fail_reason,,mini_up,fail_time')->asArray()->one();
  984. $store = Store::find()->where(['id'=>$value['store_id']])->one();
  985. $data['name'] = !empty($store->name)?$store->name:'';
  986. $data['bind_store_id'] = $value['store_id'];
  987. $data['msg'] = "";
  988. if(!empty($data)){
  989. $data['msg'] = !empty($data['status'] == 2)?$data['msg']:$data['fail_reason'];
  990. }
  991. $data['type'] = 'weixin';
  992. if($data['status'] == 2){
  993. $notice[] = $store->name.'审核失败'.$data['msg'];
  994. }
  995. if($data['mini_up'] == 3){
  996. $notice[] = $store->name.'审核失败'.$data['fail_reason'];
  997. }
  998. $mini[] = $data;
  999. }
  1000. if(!empty($value['ali_mini_id'])){
  1001. $data = StoreAliMiniVersion::find()->where(['mini_id'=>$value['ali_mini_id'],'is_submit'=>3])->orderBy('id DESC')->select('id,fail_reason,is_submit mini_up,fail_time')->asArray()->one();
  1002. $store = Store::find()->where(['id'=>$value['store_id']])->one();
  1003. $data['name'] = !empty($store->name)?$store->name:'';
  1004. $data['bind_store_id'] = $value['store_id'];
  1005. $data['type'] = 'ali';
  1006. $data['msg'] = "";
  1007. if(!empty($data)){
  1008. $data['msg'] = !empty($data['is_submit'] == 3)?$data['fail_reason']:'';
  1009. }
  1010. if($data['mini_up'] == 3){
  1011. $notice[] = $store->name.'审核失败'.$data['ali']['fail_reason'];
  1012. }
  1013. $mini[] = $data;
  1014. }
  1015. }
  1016. return [
  1017. 'code' => 0,
  1018. 'msg' => 'success',
  1019. 'data' => [
  1020. 'is_pay_money' => $is_pay_money,//已到账佣金
  1021. 'no_pay_money' => $no_pay_money,//未到账佣金
  1022. 'all_pay_money'=>$all_pay_money,//到账总金额
  1023. 'count' => $count,//分销订单数量
  1024. 'avatar' => $avatar,//头像
  1025. 'name' => $name,//名称
  1026. 'up_name' => $up_name ?: '',//总店
  1027. 'sub_merchant_count'=>$sub_merchant_count, //下级店铺数量
  1028. 'handle_count'=>$handle_count,//待处理数量
  1029. 'mini'=>$mini,//有错误的小程序
  1030. 'examine'=>$examine,//待审核数量
  1031. 'repair'=>$repair,//待完成数量
  1032. 'finish'=>$finish,//已完成数量
  1033. 'notice'=>$notice,
  1034. "id"=>$salesman->id
  1035. ]
  1036. ];
  1037. }
  1038. //获取顶部数量
  1039. public function topNumber($query){
  1040. //待审核
  1041. $examine = clone $query;
  1042. $examine = $examine->andWhere(['sa.status' => 0])->count();
  1043. //待完善
  1044. $repair = clone $query;
  1045. $repair = $repair->andWhere(['and',['sa.status' => 1],['or',['!=', 'mi.status', 0],['<>','mi.app_status','']]])->count();
  1046. //待完成
  1047. $finish = clone $query;
  1048. $finish = $finish->andWhere(['sa.status' => 1, 'mi.status' => 3,'mi.app_status'=>'12'])->count();
  1049. return [
  1050. 'examine'=>$examine,//待审核数量
  1051. 'repair'=>$repair,//待完成数量
  1052. 'finish'=>$finish,//已完成数量
  1053. ];
  1054. }
  1055. //店铺列表
  1056. public function merchantList(){
  1057. ///分类数据
  1058. $category = SaasCategory::find()->where(['is_delete'=>0])->orderBy('created_at DESC,sort DESC')->all();
  1059. ///列表数据
  1060. $page = $this->page;
  1061. $status = $this->status;
  1062. $sort = $this->sort;
  1063. $category_id = $this->category_id;
  1064. $type = $this->type == "weixin" ? 1 : 2;
  1065. $saas_user_id = get_saas_user_id();
  1066. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  1067. $query = StoreAudit::find()->alias('sa')->leftJoin(['mi' => MerchantInfo::tableName()], 'mi.id=sa.merchant_info_id')->where(['sa.salesman_id' => $salesman->id, 'mi.is_delete' => 0]);
  1068. $query->leftJoin(['sc'=>SaasCategory::tableName()],'sa.cat_id = sc.id')->andWhere(['sc.is_delete'=>0]);
  1069. $res = $this->topNumber($query);
  1070. if ($status == 0) {
  1071. // 待审核
  1072. $query->andWhere(['sa.status' => 0]);
  1073. } else {
  1074. // 审核通过
  1075. $query->andWhere(['sa.status' => 1]);
  1076. }
  1077. if ($status == 1) {
  1078. // 待完善
  1079. // $query->andWhere(['sa.status' => 1]);
  1080. // $query->andWhere([
  1081. // 'OR',['!=', 'mi.status', 3],
  1082. // ['!=','mi.app_status','']
  1083. // ]);
  1084. $query->andWhere([
  1085. 'and',
  1086. ['!=','mi.app_status',''],
  1087. ['!=','mi.app_status','0']
  1088. ]);
  1089. }
  1090. if ($status == 2) {
  1091. // 已完成
  1092. $query->andWhere([
  1093. 'or',
  1094. ['=','mi.app_status',''],
  1095. ['=','mi.app_status','0']
  1096. ]);
  1097. }
  1098. //排序
  1099. if($sort == 0){
  1100. $query->orderBy('sa.created_at DESC');
  1101. }elseif($sort == 1 || $sort == 2){
  1102. $query->orderBy('sa.updated_at DESC');
  1103. }
  1104. //分类
  1105. if(!empty($category_id)){
  1106. $query->andWhere(['sc.id'=>$category_id]);
  1107. }
  1108. //小程序类型
  1109. // if(!empty($type) && $status != 0){
  1110. // switch ($status){
  1111. // case 2:
  1112. // $query->andWhere(['NOT LIKE','mi.app_status',$type]);
  1113. // break;
  1114. // default:
  1115. // $query->andWhere(['LIKE','mi.app_status',$type]);
  1116. // break;
  1117. // }
  1118. // }
  1119. //$query->select('sa.id,sa.saas_user_id,sa.cat_id,sa.salesman_id,sa.name,sa.logo,sa.mobile, mi.id merchant_info_id, mi.status merchant_status, mi.state state, mi.from from,mi.app_status,mi.bind_store_id store_id');
  1120. //var_dump($query->createCommand()->getRawSql());die;
  1121. $count = $query->count();
  1122. $pagination = new Pagination(['totalCount' => $count, 'page' => $page - 1, 'pageSize' => 20]);
  1123. $list = $query->select('sa.id,sa.saas_user_id,sa.cat_id,sa.salesman_id,sa.name,sa.logo,sa.mobile, mi.id merchant_info_id, mi.status merchant_status, mi.state state, mi.from from,mi.app_status,mi.bind_store_id store_id')->limit($pagination->limit)->offset($pagination->offset)->asArray()->all();
  1124. foreach ($list as &$value) {
  1125. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  1126. $value['cat_name'] = SaasCategory::findOne($value['cat_id'])->name;
  1127. $value['app_status'] = !empty($value['app_status'])?explode(',',$value['app_status']):[];
  1128. $StoreSchedule = StoreSchedule::find()->where(['audit_id'=>$value['id']])->one();
  1129. $value['merchant_info'] = [];
  1130. $license_copy = json_decode($StoreSchedule['merchant_info'],true)['subject_info']['business_license_info']['license_copy'];
  1131. if(!empty($license_copy)){
  1132. array_push($value['merchant_info'],$license_copy);
  1133. }
  1134. //审核状态展示审核资料
  1135. }
  1136. $result = [
  1137. 'code' => 0,
  1138. 'msg' => 'success',
  1139. 'data' => [
  1140. 'row_count' => $count,
  1141. 'page_count' => $pagination->pageCount,
  1142. 'list' => $list,
  1143. 'examine'=>$res["examine"],//待审核数量
  1144. 'repair'=>$res["repair"],//待完成数量
  1145. 'finish'=>$res["finish"],//已完成数量
  1146. 'category'=>$category
  1147. ],
  1148. ];
  1149. return $result;
  1150. }
  1151. //审核店铺
  1152. public function audit(){
  1153. $id = $this->id;
  1154. $status = $this->status;
  1155. $audit = StoreAudit::findOne(['id' => $id, 'status' => 0]);
  1156. if (!$audit) {
  1157. return [
  1158. 'code' => 1,
  1159. 'msg' => '数据不存在'
  1160. ];
  1161. }
  1162. $t = \Yii::$app->db->beginTransaction();
  1163. $saas_user_id = get_saas_user_id();
  1164. if ($status == 1) {
  1165. // 自动创建admin账号
  1166. $admin = new Admin();
  1167. $admin->access_token = \Yii::$app->security->generateRandomString();
  1168. $admin->username = $audit->name;
  1169. // $admin->password = \Yii::$app->security->generatePasswordHash($audit->mobile);
  1170. // 默认密码改为cyyvipcom 与后台添加商城统一
  1171. $admin->password = \Yii::$app->security->generatePasswordHash('123456');
  1172. $admin->mobile = $audit->mobile;
  1173. $admin->name = $audit->name;
  1174. $admin->type = 'store';
  1175. if ($admin->save()) {
  1176. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  1177. $store = new Store();
  1178. $store->admin_id = $admin->id;
  1179. $store->logo = $audit->logo;
  1180. $store->created_at = time();
  1181. $store->name = $audit->name;
  1182. $store->salesman_id = $salesman->id;
  1183. $store->open_status = 0;
  1184. $store->end_time = strtotime("+1 year");
  1185. if ($store->save()) {
  1186. $admin->type_id = $store->id;
  1187. $admin->save();
  1188. // // 绑定关系
  1189. // $saas_store_referral = new SaasStoreReferral();
  1190. // $saas_store_referral->store_id = $store->id;
  1191. // $saas_store_referral->referral_id = $saas_user_id;
  1192. // if (!$saas_store_referral->save()) {
  1193. // $t->rollBack();
  1194. // \Yii::error("绑定关系失败");
  1195. // \Yii::error($saas_store_referral->errors);
  1196. // return [
  1197. // 'code' => 1,
  1198. // 'msg' => "绑定关系失败"
  1199. // ];
  1200. // } else {
  1201. //绑定二维码
  1202. $AggregateQrcode = AggregateQrcode::find()->where(['audit_id'=>$id])->one();
  1203. if(!empty($AggregateQrcode)){
  1204. $AggregateQrcode->store_id = $store->id;
  1205. $res = $AggregateQrcode->save();
  1206. if(!$res){
  1207. \Yii::error("聚合码绑定失败");
  1208. \Yii::error($AggregateQrcode->errors);
  1209. return [
  1210. 'code' => 1,
  1211. 'msg' => "聚合码绑定失败"
  1212. ];
  1213. }
  1214. }
  1215. //
  1216. // $sales_admin = Admin::findOne($salesman->admin_id);
  1217. // $sales_saas_user_id = $sales_admin->saas_user_id;
  1218. // if ($sales_saas_user_id) {
  1219. // // 代理商绑定
  1220. // $saas_store_referral = new SaasStoreReferral();
  1221. // $saas_store_referral->store_id = $store->id;
  1222. // $saas_store_referral->referral_id = $sales_saas_user_id;
  1223. // if (!$saas_store_referral->save()) {
  1224. // $t->rollBack();
  1225. // \Yii::error("代理商失败");
  1226. // \Yii::error($saas_store_referral->errors);
  1227. // return [
  1228. // 'code' => 1,
  1229. // 'msg' => "代理商绑定失败"
  1230. // ];
  1231. // }
  1232. // }
  1233. // }
  1234. // 绑定上级关系
  1235. if ($audit->saas_user_id) {
  1236. // 绑定上级关系
  1237. // if(!SaasStoreReferral::storeBindParent($audit->saas_user_id, $store->id)){
  1238. // $t->rollBack();
  1239. // \Yii::error("绑定上级关系失败");
  1240. // throw new \Exception("绑定上级关系失败");
  1241. // }
  1242. }
  1243. try {
  1244. $merchant = new Merchant();
  1245. } catch (\Exception $e) {
  1246. return [
  1247. 'code' => 1,
  1248. 'msg' => $e->getMessage()
  1249. ];
  1250. }
  1251. $merchant_info = new MerchantInfo();
  1252. $merchant_info->store_id = get_store_id();
  1253. $merchant_info->saas_user_id = $saas_user_id;
  1254. $merchant_info->business_code = $merchant->getBusinessCode();
  1255. $merchant_info->bind_store_id = $store->id;
  1256. $merchant_info->app_status = "1,2";
  1257. $merchant_info->from = 1;
  1258. //保存进件数据
  1259. $StoreSchedule = StoreSchedule::findOne(['audit_id'=>$id]);
  1260. $StoreSchedule->store_id = $store->id;
  1261. $res = $StoreSchedule->save();
  1262. if(!$res){
  1263. \Yii::error($StoreSchedule->errors);
  1264. return [
  1265. 'code' => 1,
  1266. 'msg' => "保存数据信息失败"
  1267. ];
  1268. }
  1269. if(!empty($StoreSchedule->merchant_info)){
  1270. $store_merchant_info = json_decode($StoreSchedule->merchant_info,true);
  1271. $merchant_info->contact_info = json_encode($store_merchant_info['contact_info']);
  1272. $merchant_info->subject_info = json_encode($store_merchant_info['subject_info']);
  1273. $merchant_info->business_info = json_encode($store_merchant_info['business_info']);
  1274. $merchant_info->bank_account_info = json_encode($store_merchant_info['bank_account_info']);
  1275. }
  1276. if (!$merchant_info->save()) {
  1277. $t->rollBack();
  1278. \Yii::error($merchant_info->errors);
  1279. return [
  1280. 'code' => 1,
  1281. 'msg' => "保存进件信息失败"
  1282. ];
  1283. } else {
  1284. $audit->merchant_info_id = $merchant_info->id;
  1285. if (!$audit->save()) {
  1286. $t->rollBack();
  1287. \Yii::error($audit->errors);
  1288. return [
  1289. 'code' => 1,
  1290. 'msg' => "申请单修改失败"
  1291. ];
  1292. }
  1293. }
  1294. } else {
  1295. $t->rollBack();
  1296. \Yii::error($store->errors);
  1297. return [
  1298. 'code' => 1,
  1299. 'msg' => "店铺新增失败"
  1300. ];
  1301. }
  1302. } else {
  1303. $t->rollBack();
  1304. \Yii::error($admin->errors);
  1305. return [
  1306. 'code' => 1,
  1307. 'msg' => "新增管理员失败"
  1308. ];
  1309. }
  1310. }
  1311. $audit->status = $status;
  1312. if (!$audit->save()) {
  1313. $t->rollBack();
  1314. \Yii::error($audit->errors);
  1315. return [
  1316. 'code' => 1,
  1317. 'msg' => "申请单修改失败。"
  1318. ];
  1319. } else {
  1320. $t->commit();
  1321. return [
  1322. 'code' => 0,
  1323. 'msg' => '操作成功'
  1324. ];
  1325. }
  1326. }
  1327. //获取进件信息
  1328. public function getInfo(){
  1329. $StoreSchedule = StoreSchedule::find()->where(['store_id'=>$this->bind_store_id])->select("merchant_info")->one();
  1330. $merchant_info = [];
  1331. if(!empty($StoreSchedule)){
  1332. $merchant_info = json_decode($StoreSchedule->$StoreSchedule);
  1333. }
  1334. return $merchant_info;
  1335. }
  1336. /**
  1337. * 生成业务编号
  1338. * @return string
  1339. */
  1340. public function getBusinessCode()
  1341. {
  1342. return date('Ymd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
  1343. }
  1344. }