MerchantIntentionRepository.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. <?php
  2. /**
  3. * @package crmeb_merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-07-18
  7. *
  8. * Copyright (c) http://crmeb.net
  9. */
  10. namespace app\common\repositories\system\merchant;
  11. use AdaPaySdk\CorpMember;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\repositories\merchant\apply\MerchantHaikeApplyRepository;
  14. use app\controller\admin\system\merchant\MerchantIntention;
  15. use app\traits\HaikeApiRequest;
  16. use app\traits\HuiPay;
  17. use app\traits\ShouxinyiApiRequest;
  18. use FormBuilder\Factory\Elm;
  19. use app\common\dao\system\merchant\MerchantIntentionDao;
  20. use FormBuilder\Form;
  21. use think\Exception;
  22. use think\exception\ValidateException;
  23. use think\facade\Db;
  24. use think\facade\Log;
  25. use think\facade\Route;
  26. use think\route\Url;
  27. class MerchantIntentionRepository extends BaseRepository
  28. {
  29. use HaikeApiRequest;
  30. use HuiPay;
  31. public function __construct(MerchantIntentionDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. public function getList(array $where,$page,$limit)
  36. {
  37. $query = $this->dao->search($where);
  38. $count = $query->count();
  39. $list = $query->page($page,$limit)
  40. ->order('mer_intention_id', 'desc')
  41. ->select();
  42. $haikeApplyRepository = app()->make(MerchantHaikeApplyRepository::class);
  43. foreach ($list as &$item) {
  44. $item->haike_status = 0;
  45. if (!$item->merch_no) {
  46. $item->haike_status = '暂无申请';
  47. $item->haike_status_msg = '';
  48. } else {
  49. try {
  50. $apply = $haikeApplyRepository->getWhere(['apply_id' => $item->mer_intention_id, 'apply_type' => 1]);
  51. if ($apply) {
  52. $item->haike_status = $apply->status;
  53. $item->haike_status_msg = '-'.$apply->check_result;
  54. }
  55. $apply2 = $haikeApplyRepository->getWhere(['apply_id' => $item->mer_intention_id, 'apply_type' => 2]);
  56. if ($apply2) {
  57. $str = substr($apply2->check_result, strpos($apply2->check_result, '{'));
  58. $array = json_decode($str, true);
  59. $item->haike_apply_wx_status = $apply2->status;
  60. $item->haike_apply_wx_status_msg = '-' . $array['result_msg'] ?? '';
  61. }
  62. $apply3 = $haikeApplyRepository->getWhere(['apply_id' => $item->mer_intention_id, 'apply_type' => 3]);
  63. if ($apply3) {
  64. $str = substr($apply2->check_result, strpos($apply2->check_result, '{'));
  65. $array = json_decode($str, true);
  66. $item->haike_apply_ali_status = $apply3->status;
  67. $item->haike_apply_ali_status_msg = '-' . $array['result_msg'] ?? '';
  68. }
  69. } catch (\Throwable $e) {
  70. $item->haike_status = $item->agent_apply_no;
  71. $item->haike_status_msg = '';
  72. $item->haike_apply_wx_status = 0;
  73. $item->haike_apply_wx_status_msg = '';
  74. $item->haike_apply_ali_status = 0;
  75. $item->haike_apply_ali_status_msg = '';
  76. }
  77. }
  78. $item = $this->wxAuthStatus($item, $haikeApplyRepository);
  79. $item->haike_wx_auth_status = 0;
  80. $sxy_status = $this->sxyStatus($item->mer_intention_id);
  81. $item->sxy_status = $sxy_status['msg'];
  82. $item->sys_status_code = $sxy_status['code'];
  83. }
  84. unset($item);
  85. // $list = $query->page($page,$limit)->select();
  86. // $count1 = $query->where("status",1)->count();
  87. // $count2 = $query->where("status",0)->count();
  88. // $count3 = $query->where("status",2)->count();
  89. $count1 =$count3 =$count2=0;
  90. $lists = $query->select();
  91. foreach ($lists as $item) {
  92. if($item["status"]==1){
  93. $count1+=1;
  94. }elseif ($item["status"]==0){
  95. $count2+=1;
  96. }else{
  97. $count3+=1;
  98. }
  99. }
  100. $cardLists=[
  101. ['className'=> 'el-icon-s-goods', 'count'=>$count1, 'field'=>'个', 'name'=> '已通过' ],
  102. ['className'=> 'el-icon-s-goods', 'count'=>$count2, 'field'=>'个', 'name'=> '审核中' ],
  103. ['className'=> 'el-icon-s-goods', 'count'=>$count3, 'field'=>'个', 'name'=> '未通过' ],
  104. ];
  105. return compact('count','list','cardLists');
  106. }
  107. public function sxyStatus($id){
  108. $res = Db::name("declare_log") -> where("m_id",$id) -> order('id','desc') -> find();
  109. //declare==1:审核提交成功
  110. //status==SUCCESS:审核成功
  111. $result['code'] = 2;//1:不展示审核按钮。2:展示审核按钮
  112. if($res['declare'] == 1){
  113. $result['msg'] = '审核中';
  114. $result['code'] = 1;
  115. }
  116. switch ($res['status']){
  117. case 'SUCCESS':
  118. $result['msg'] = '已开通';
  119. $result['code'] = 1;
  120. break;
  121. case 'ESIGN_SUCCESS':
  122. $result['msg'] = '电子签签约成功';
  123. $result['code'] = 1;
  124. break;
  125. case 'ESIGN_FAIL':
  126. $result['msg'] = '电子签签约失败';
  127. $result['code'] = 2;
  128. break;
  129. }
  130. if($res['declare'] != 1 || $res['status'] == 'NO_PASS' || $res['status'] == 'REFUSE'){
  131. $result['msg'] = $res['message'];
  132. }
  133. $result['status']=$res['status'];
  134. return $result;
  135. }
  136. public function markForm($id)
  137. {
  138. $intention = $this->dao->get($id);
  139. $intention->mer_intention_id = (string)$intention->mer_intention_id;
  140. $where=[
  141. Elm::input('mer_intention_id', '申请 ID', '')->disabled(true),
  142. Elm::input('merch_no', '海科商户号')->disabled(true),
  143. Elm::input('phone', '手机号'),
  144. Elm::input('mer_name', '商户名称'),
  145. Elm::input('mer_short_name', '商户简称'),
  146. Elm::input('addr', '详细地址'),
  147. Elm::frameImage('doorhead', '门头照', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=doorhead&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  148. Elm::frameImage('business_license', '营业执照', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=business_license&type=1')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false])->info('小微非必填'),
  149. Elm::frameImage('card_positive_person', '身份证正面(法人)', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=card_positive_person&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  150. Elm::frameImage('card_back_person', '身份证反面(法人)', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=card_back_person&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  151. Elm::input('identification_valid_date_start', '身份证有效期起始时间')->info('例:2000-01-01'),
  152. Elm::input('identification_valid_date_end', '身份证有效期结束时间')->info('例:2030-01-01'),
  153. Elm::input('name', '法人姓名'),
  154. Elm::input('idcard_no', '法人身份证号'),
  155. Elm::input('email', '邮箱'),
  156. Elm::input('acc_type', '结算方式(10A:对公,10B:对私)'),
  157. Elm::input('bank_account', '开户账号'),
  158. Elm::input('bank_deposit', '开户行'),
  159. Elm::input('bank_branch', '开户支行'),
  160. Elm::frameImage('open_account', '开户许可证', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=open_account&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal'])->info('小微上传结算卡照片'),
  161. Elm::frameImage('logo', '店铺LOGO', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=logo&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  162. Elm::input('business_addr', '营业执照地址')->info('小微请填写实际经营地址'),
  163. Elm::input('house_number', '门牌号'),
  164. Elm::input('type', '1企业2个体3个人'),
  165. Elm::input('spread_account', '推荐人手机号'),
  166. Elm::input('credit_code', '社会信用代码'),
  167. Elm::frameImage('business_premises2', '营业场所室内照图片1', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=business_premises2&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  168. Elm::frameImage('business_premises3', '营业场所室内照图片2', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=business_premises3&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  169. Elm::frameImage('business_premises4', '协议盖章', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=business_premises4&type=1')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal'])->info('小微必传'),
  170. Elm::input('haike_wx_type', '海科微信营业范围'),
  171. Elm::input('haike_wx_appid', '海科微信APPID')->info('配置微信公众号'),
  172. Elm::input('haike_wxsub_merch_no', '海科子商户号', '')->info('用于二次认证'),
  173. Elm::input('haike_ali_type', '海科支付宝营业范围'),
  174. Elm::input('agent_apply_no', '海科申请单号')->disabled(true),
  175. Elm::textarea('mark', '备注'),
  176. ];
  177. return Elm::createForm(Route::buildUrl('systemMerchantIntentionMark', compact('id'))->build(), $where)->setTitle('编辑')->formData($intention->toArray());
  178. }
  179. public function statusForm($id)
  180. {
  181. $form = Elm::createForm(Route::buildUrl('systemMerchantIntentionStatus', ['id' => $id])->build());
  182. $form->setRule([
  183. Elm::select('status', '审核状态', 1)->options([
  184. ['value' => 1, 'label' => '同意'],
  185. ['value' => 2, 'label' => '拒绝'],
  186. ]),
  187. Elm::textarea('check_result', '审核结果'),
  188. ]);
  189. return $form->setTitle('修改审核状态');
  190. }
  191. public function createMer($id){
  192. $data = Db::name("merchant_intention")
  193. ->where("mer_intention_id",$id)
  194. ->find();
  195. $user = Db::name("user")->where("uid",$data['uid'])->field('account,pwd,identity')->find();
  196. if(in_array($user['identity'],[1,2,3])){
  197. return ['code'=> 500,'msg'=>'该用户已是代理请拒绝'];
  198. }
  199. $count= Db::name("merchant_admin")
  200. ->where(['account' => $user['account'], 'is_del'=>0])
  201. ->count();
  202. if ($count > 0) {
  203. return ['code'=> 500,'msg' => '该用户已创建过商户账号,请先删除原有账户'];
  204. }
  205. $admin= Db::name("merchant_admin")
  206. ->where(['uid' => $data['uid'], 'is_del'=>0])
  207. ->find();
  208. if($admin){
  209. return ['code'=> 500,'msg'=>'该用户已是商户'];
  210. }
  211. if($user['account'] == $admin['account']){
  212. return ['code'=> 500,'msg'=>'该手机号已是商户账号'];
  213. }
  214. $expire_time=strtotime(date('Y-m-d'))+86400*365+86400-1;
  215. $spread = Db::name("user")
  216. ->where(["account"=>$data['spread_account']])
  217. ->field("uid,account,spread_count")
  218. ->find();
  219. $insdata=[
  220. 'uid'=>$data['uid'],
  221. 'merch_no' => $data['merch_no'],
  222. 'type'=>$data['type']??3,
  223. "mer_name"=>$data["mer_name"]??"",
  224. "mer_phone"=>$data["phone"]??"",
  225. "mer_address"=>$data["addr"]??"",
  226. "house_number"=>$data["house_number"]??"",
  227. "category_id"=>$data['cate_id']?$data['cate_id']:0,
  228. "doorhead"=>$data["doorhead"]??"",
  229. "mer_banner"=>$data["doorhead"]??"",
  230. "status"=>1,
  231. "lat"=>$data["lat"],
  232. "lng"=>$data["lng"],
  233. "business_license"=>$data["business_license"]??"",
  234. "service_phone"=>$data["phone"]??"",
  235. 'open_account'=>$data['open_account']??"",
  236. 'bank_account'=>$data['bank_account']??"",
  237. 'bank_location'=>$data['bank_location']??"",
  238. 'bank_deposit'=>$data['bank_deposit']??"",
  239. 'bank_branch'=>$data['bank_branch']??"",
  240. 'name'=>$data['name']??"",
  241. 'email'=>$data['email']??"",
  242. 'card_positive_person'=>$data['card_positive_person']??"",
  243. 'card_back_person'=>$data['card_back_person']??"",
  244. 'logo'=>$data['logo']??"",
  245. 'mer_avatar' => $data['logo']??"",
  246. 'mer_info'=>$data['desc']??"",
  247. 'district_addr'=>$data['district_addr']??"",
  248. 'province_id'=>$data['province_id']??"",
  249. 'city_id'=>$data['city_id']??"",
  250. 'area_id'=>$data['area_id']??"",
  251. 'street_id'=>$data['street_id']??"",
  252. "create_time"=>date("Y-m-d H:i:s"),
  253. "update_time"=>date("Y-m-d H:i:s"),
  254. "expire_time"=>$expire_time
  255. ];
  256. try{
  257. $merchant_id = Db::transaction(function () use ($spread, $data, $user, $insdata){
  258. if ($spread) {
  259. $insdata['recommender'] = $spread['uid'];
  260. //推荐人推荐数量加1
  261. Db::name("user")->where(['uid' => $spread['uid']])->update(['spread_count' => ($spread['spread_count'] + 1)]);
  262. //修改用户推荐人信息
  263. Db::name("user")->where(['uid' => $data['uid']])->update(['spread_uid' => $spread['uid']]);
  264. //保存用户的推荐记录
  265. $log = [
  266. 'uid' => $spread['uid'],
  267. 'link_id' => $data['uid'],
  268. 'identity' => 4,
  269. 'create_time' => date("Y-m-d H:i:s"),
  270. ];
  271. Db::name("user_identity_log")->insert($log);
  272. }
  273. $merchant_id = Db::name("merchant")->insertGetId($insdata);
  274. /*添加账户*/
  275. $merchant_admin_array = [
  276. 'uid' => $data['uid'],
  277. "mer_id" => $merchant_id,
  278. "account" => $user["account"],
  279. "pwd" => $user["pwd"],
  280. "real_name" => $data["mer_name"],
  281. "phone" => $data["phone"],
  282. "create_time" => date("Y-m-d H:i:s"),
  283. 'level' => 0
  284. ];
  285. Db::name("merchant_admin")->insert($merchant_admin_array);
  286. //用户更改成商家
  287. Db::name("user")->where(['uid' => $data['uid']])->update(['identity' => 5]);
  288. //更改商户收款码
  289. Db::name("MerchantQrcode") -> where("uid",$data['uid']) -> update(['mer_id'=>$merchant_id]);
  290. return $merchant_id;
  291. });
  292. if (!$merchant_id)
  293. return ['code' => 500, 'msg' => '创建商户失败'];
  294. return ['code' => 200, 'data' => $merchant_id];
  295. } catch (\Throwable $e) {
  296. return ['code' => 500, 'msg' => $e->getMessage()];
  297. }
  298. }
  299. /*详情*/
  300. public function getDetail($where)
  301. {
  302. $data = Db::name("merchant_intention")
  303. ->where($where)
  304. ->find();
  305. if ($data) {
  306. $data['cate_name'] = Db::name("merchant_category")
  307. ->where(['merchant_category_id' => $data['cate_id']])
  308. ->value('category_name');
  309. $data['province_name'] = Db::name("user_province")
  310. ->where(['province_id' => $data['province_id']])
  311. ->value('province_name');
  312. $data['city_name'] = Db::name("user_city")
  313. ->where(['city_id' => $data['city_id']])
  314. ->value('city_name');
  315. $data['area_name'] = Db::name("user_area")
  316. ->where(['area_id' => $data['area_id']])
  317. ->value('area_name');
  318. $data['street_name'] = Db::name("user_street")
  319. ->where(['street_id' => $data['street_id']])
  320. ->value('street_name');
  321. }
  322. return $data;
  323. }
  324. /**
  325. * 信息回显
  326. * @return array|bool
  327. * @author php迷途小书童
  328. * @created 2021/2/22 16:16
  329. */
  330. public function intentionInfo($where){
  331. try {
  332. $intention = $this->dao->getWhere($where);
  333. $haikeApplyRepository = app()->make(MerchantHaikeApplyRepository::class);
  334. $haikeMerchantApply = $haikeApplyRepository->getWhere(['apply_id' => $intention->mer_intention_id,'trade_type' => 'WX,ALI']);
  335. $haikeWxApply = $haikeApplyRepository->getWhere(['apply_id' => $intention->mer_intention_id,'apply_type' => 2]);
  336. $haikeAliApply = $haikeApplyRepository->getWhere(['apply_id' => $intention->mer_intention_id,'apply_type' => 3]);
  337. $merchantInfo = [];
  338. if (!$haikeMerchantApply) {
  339. $merchantInfo['code'] = 0;
  340. $merchantInfo['msg'] = '暂无申请信息';
  341. } else {
  342. //如果海科审核拒绝
  343. if (in_array($haikeMerchantApply->status, [1,5,6])) {
  344. $intention->status = 2;
  345. $intention->check_result = $haikeMerchantApply->check_result;
  346. $intention->save();
  347. } else if ($haikeWxApply->status == 2) {
  348. $str = substr($haikeWxApply->check_result, strpos($haikeWxApply->check_result, '{'));
  349. $array = json_decode($str, true);
  350. $intention->status = 2;
  351. $intention->check_result = $array['result_msg'] ?? '';
  352. $intention->save();
  353. } else if ($haikeAliApply->status == 2) {
  354. $str = substr($haikeAliApply->check_result, strpos($haikeAliApply->check_result, '{'));
  355. $array = json_decode($str, true);
  356. $intention->status = 2;
  357. $intention->check_result = $array['result_msg'] ?? '';
  358. $intention->save();
  359. } else {
  360. if ($intention->status != 1) {
  361. $intention->status = 0;
  362. $intention->check_result = '';
  363. $intention->save();
  364. }
  365. $merchantInfo['code'] = 0;
  366. $merchantInfo['msg'] = '暂无申请信息';
  367. }
  368. }
  369. $intention = $this->wxAuthStatus($intention, $haikeApplyRepository, $width = '200px');
  370. if (in_array($intention->haike_wx_auth_result_code, ['未授权'])) {
  371. $intention->haike_wx_auth_result_code = '';
  372. $intention->haike_wx_auth_result_msg = '';
  373. }
  374. if (in_array($intention->haike_wx_auth_result_code, ['已授权'])) {
  375. $intention->haike_wx_auth_result_code = '';
  376. $intention->haike_wx_auth_result_msg = '';
  377. $return = $this->createMer($intention->mer_intention_id);
  378. if ($return['code'] == 200) {
  379. $this->dao->update($intention->mer_intention_id, ['status' => 1,'check_result' => '已授权', 'mer_id' => $return['data']]);
  380. } else {
  381. Log::info('创建商户失败:'.$return['msg']);
  382. }
  383. }
  384. if ($intention->haike_wx_auth_result_state == 'APPLYMENT_STATE_REJECTED') {
  385. $intention->status = 2;
  386. $intention->check_result = $intention->haike_wx_auth_result_msg;
  387. $intention->save();
  388. $intention->haike_wx_auth_result_msg = '';
  389. }
  390. $sxy_msg=Db::name('declare_log')->where('m_id',$intention->mer_intention_id??0)->order('id','desc')->find();
  391. if($sxy_msg['status']=='SUCCESS' || $sxy_msg['status']=='ESIGN_SUCCESS'){
  392. $sxy_msg='线上已授权';
  393. }else{
  394. $sxy_msg=$sxy_msg['message'];
  395. }
  396. $intention->check_result=$intention->check_result.' || '. $sxy_msg;
  397. return [
  398. 'intention' => $intention,
  399. 'haikeMerchantInfo' => $merchantInfo,
  400. 'haikeWxAuthInfo' => [
  401. 'haike_wx_auth_result_code' => $intention->haike_wx_auth_result_code,
  402. 'haike_wx_auth_result_msg' => $intention->haike_wx_auth_result_msg,
  403. ],
  404. ];
  405. } catch (\Throwable $e) {
  406. Log::info('获取回显信息失败'.$e->getMessage());
  407. return false;
  408. }
  409. }
  410. /*获取省份*/
  411. public function getProvince(){
  412. $data = Db::name("user_province")
  413. ->where(['status'=>1])
  414. ->field("province_id,province_code,province_name")
  415. ->select();
  416. return $data;
  417. }
  418. /*获取城市*/
  419. public function getCity($province_code){
  420. $data = Db::name("user_city")
  421. ->where(['status'=>1,'province_code'=>$province_code])
  422. ->field("city_id,city_code,city_name")
  423. ->select();
  424. return $data;
  425. }
  426. /*获取区域*/
  427. public function getArea($city_code){
  428. $data = Db::name("user_area")
  429. ->where(['status'=>1,'city_code'=>$city_code])
  430. ->field("area_id,area_code,area_name")
  431. ->select();
  432. return $data;
  433. }
  434. /*获取街道*/
  435. public function getStreet($area_code){
  436. $data = Db::name("user_street")
  437. ->where(['status'=>1,'area_code'=>$area_code])
  438. ->field("street_id,street_code,street_name")
  439. ->select();
  440. return $data;
  441. }
  442. /*获取用户身份*/
  443. public function getIdentity($uid){
  444. $data = Db::name("user")->where(["uid"=>$uid])->field("identity,account,uid")->find();
  445. return $data;
  446. }
  447. /*实名认证判断*/
  448. public function real($uid){
  449. $data = Db::name("user_certification")->where(["uid"=>$uid,'status'=>1])->count();
  450. return $data;
  451. }
  452. public function wxAuthStatus($intention, $haikeApplyRepository, $width = '100px')
  453. {
  454. if ($intention->haike_wx_auth_apply_status == 1) {
  455. $intention->haike_wx_auth_result_code = '已授权';
  456. $intention->haike_wx_auth_result_msg ='';
  457. return $intention;
  458. }
  459. $authAstatusResult = $this->queryMerchantAuthStatus(['merch_no' => $intention->merch_no, 'wx_merch_no' => $intention->haike_wxsub_merch_no]);
  460. $intention->haike_wx_auth_result_code = '未授权';
  461. $intention->haike_wx_auth_result_msg ='';
  462. if ($authAstatusResult['return_code'] === 10000) {
  463. if ($authAstatusResult['authorize_state'] == "AUTHORIZE_STATE_AUTHORIZED") {
  464. $intention->haike_wx_auth_apply_status = 1;
  465. $intention->save();
  466. $intention->haike_wx_auth_result_code = '已授权';
  467. $intention->haike_wx_auth_result_msg ='';
  468. return $intention;
  469. }
  470. }
  471. if ($intention->haike_wx_auth_status === 1) {
  472. $haikeApply = $haikeApplyRepository->getWhere(['apply_id' => $intention->mer_intention_id,'trade_type' => 'WXAUTHAPPLY']);
  473. if ($haikeApply) {
  474. try {
  475. $responseApply = $this->queryMerchantApplyStatus($haikeApply->agent_apply_no);
  476. $intention->haike_wx_auth_result_state = '';
  477. if ($responseApply['return_code'] !== 10000) {
  478. $intention->haike_wx_auth_result_code = $responseApply['return_code'];
  479. $intention->haike_wx_auth_result_msg = $responseApply['return_msg'];
  480. } else {
  481. $intention->haike_wx_auth_result_state = $responseApply['applyment_state'];
  482. $intention->haike_wx_auth_result_code = $responseApply['applyment_state'];
  483. if (in_array($responseApply['applyment_state'],['APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT','APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON','APPLYMENT_STATE_PASSED','APPLYMENT_STATE_FREEZED'])) {
  484. $intention->haike_wx_auth_result_msg = "<img src='data:image/png;base64,{$responseApply['qrcode_data']}' style='width: {$width}' alt=''>";
  485. } else {
  486. if (in_array($responseApply['applyment_state'],['APPLYMENT_STATE_REJECTED'])) {
  487. $intention->haike_wx_auth_result_msg = $responseApply['reject_reason'];
  488. } else {
  489. $intention->haike_wx_auth_result_msg = '';
  490. }
  491. }
  492. $intention->haike_wx_auth_result_code = $this->getAuthCodeMsg($responseApply['applyment_state'], $intention->haike_wxsub_merch_no);
  493. }
  494. } catch (\Throwable $e) {
  495. $intention->haike_wx_auth_result_state = '';
  496. $intention->haike_wx_auth_result_code = $e->getMessage();
  497. $intention->haike_wx_auth_result_msg = '';
  498. }
  499. }
  500. }
  501. return $intention;
  502. }
  503. public function getAuthCodeMsg($applyment_state, $haike_wxsub_merch_no)
  504. {
  505. switch ($applyment_state) {
  506. case 'APPLYMENT_STATE_WAITTING_FOR_AUDIT':
  507. $msg = '【审核中】:请耐心等待1~2个工作日,微信支付将会完成审核。';
  508. break;
  509. case 'APPLYMENT_STATE_EDITTING':
  510. $msg = '【编辑中】:可能提交申请发生了错误导致,可用同一个业务申请编号重新提交。';
  511. break;
  512. case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT':
  513. $msg = '【待确认联系信息】:请扫描微信支付返回的小程序码确认联系信息。';
  514. break;
  515. case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON':
  516. $msg = '【待账户验证】:请扫描微信支付返回的小程序码在小程序端完成账户验证。';
  517. break;
  518. case 'APPLYMENT_STATE_PASSED':
  519. $msg = '【审核通过】:请扫描微信支付返回的小程序码在小程序端完成授权流程。';
  520. break;
  521. case 'APPLYMENT_STATE_REJECTED':
  522. $msg = '【审核驳回】:请按照驳回原因修改申请资料,并更换业务申请编码,重新提交申请。';
  523. break;
  524. case 'APPLYMENT_STATE_FREEZED':
  525. $msg = '【已冻结】:可能是该主体已完成过入驻,请查看驳回原因,并通知驳回原因中指定的联系人扫描微信支付返回的小程序码在小程序端完成授权流程。';
  526. break;
  527. case 'APPLYMENT_STATE_CANCELED':
  528. $msg = '【已作废】:表示申请单已被撤销,无需再对其进行操作。';
  529. break;
  530. default:
  531. $msg = '未知。';
  532. break;
  533. }
  534. return $msg . '微信子商户号:' .$haike_wxsub_merch_no;
  535. }
  536. public function createSXY($id){
  537. $intention = $this->getDetail(['mer_intention_id'=>$id]);
  538. //签约名称
  539. $arr['signedName'] = $intention['name'];
  540. if($intention['type'] != 3){
  541. $arr['signedName'] = $intention['mer_name'];
  542. }
  543. if($intention['acc_type'] != "10A"){//对私全部以自然人身份入网
  544. $arr['signedName'] = $intention['name'];
  545. }
  546. // if($intention['acc_type'] == "10A" || $intention['type'] == 1){//10A对公10B对私 对公或者企业填公司名称
  547. // $arr['signedName'] = $intention['mer_name'];
  548. // }
  549. // if($intention['acc_type'] == '10A' && $intention['type'] == 2){//个体可以对公
  550. // $arr['signedName'] = $intention['mer_name'];
  551. // }
  552. // if($intention['acc_type'] == '10B' && $intention['type'] == 2){//个体可以对私
  553. // $arr['signedName'] = $intention['name'];
  554. // }
  555. //end
  556. if($intention['type'] == 1){//企业
  557. $arr['registerRole'] = 'ENTERPRISE_LEGAL_PERSON';
  558. }elseif ($intention['type'] == 2){//个体
  559. $arr['registerRole'] = 'INDIVIDUAL_BUSINESS';
  560. }elseif ($intention['type'] == 3){//个人
  561. $arr['registerRole'] = 'NATURAL_PERSON';
  562. }
  563. if($intention['acc_type'] != "10A"){//对私全部以自然人身份入网
  564. $arr['registerRole'] = 'NATURAL_PERSON';
  565. }
  566. $arr['cerType'] = 'ID_CARD';
  567. // //个人传其他 个体跟企业传营业执照
  568. // if($intention['type'] == 3){
  569. // $arr['cerType'] = 'OTHERS';
  570. // }
  571. // if($intention['type'] == 1 || $intention['type'] == 2){
  572. // $arr['cerType'] = 'BUSINESS_LICENSE';
  573. // }
  574. $arr['signedShorthand'] = $arr['signedName'];
  575. $arr['businessAddressProvince'] = $this -> getAddress($intention['province_id'],'province');
  576. $arr['businessAddressCity'] = $this -> getAddress($intention['city_id'],'city');
  577. $arr['businessAddressArea'] = $this -> getAddress($intention['area_id'],'area');
  578. $arr['businessAddress'] = $intention['addr'];
  579. $arr['contactName'] = $intention['name'];
  580. $arr['contactEmail'] = $intention['email'];
  581. $arr['contactPhone'] = $intention['phone'];
  582. $arr['businessClassification'] = 'ELECTRONIC_BUSINESS_16';//电商_其他
  583. //银行
  584. $arr['bankCode'] = $this -> getSXYBankCode($intention['bank_deposit']);
  585. $arr['bankBranchName'] =$intention['bank_branch'];
  586. //开户名称
  587. $arr['accountName'] = $intention['name'];//开户名称
  588. if($intention['acc_type'] == "10B"){
  589. $arr['accountName'] = $intention['name'];
  590. }
  591. if($intention['acc_type'] == "10A"){//10A对公10B对私 对公或者企业填公司名称
  592. $arr['accountName'] = $intention['mer_name'];
  593. }
  594. if($intention['acc_type'] == '10A'){//个体可以对公
  595. $arr['accountName'] = $intention['mer_name'];
  596. }
  597. // if($intention['acc_type'] == '10B' && $intention['type'] == 2){//个体可以对私
  598. // $arr['accountName'] = $intention['name'];
  599. // }
  600. //end
  601. str_replace(' ', '', $intention['bank_account']);
  602. $arr['bankCardNo'] = str_replace(' ', '', $intention['bank_account']);;
  603. $arr['cityCode'] = Db::name("user_city")
  604. ->where(['city_id' => $intention['city_id']])
  605. ->value('city_code');
  606. $arr['provinceCode'] = Db::name("user_province")
  607. ->where(['province_id' => $intention['province_id']])
  608. ->value('province_code');
  609. if($intention['acc_type'] == '10A'){//10A对公10B对私
  610. $arr['accountType'] = 'PUBLIC';
  611. }else{
  612. $arr['accountType'] = 'PRIVATE';
  613. }
  614. $arr['legalPersonName'] = $intention['name'];
  615. $arr['legalPersonIdNo'] = $intention['idcard_no'];
  616. $arr['legalPersonPhoneNo'] = $intention['phone'];
  617. //registerRole类型为NATURAL_PERSON必填
  618. $arr['profession'] = $arr['registerRole'] == 'NATURAL_PERSON' ? 6 : '';
  619. //end
  620. $arr['legalPersonIdType'] = 'IDCARD';
  621. $arr['legalPersonIdNoSup'] = $intention['idcard_no'];
  622. $arr['idEffectiveDateStart'] = $intention['identification_valid_date_start'];
  623. $arr['idEffectiveDateEnd'] = $intention['identification_valid_date_end'] == 'forever' ? '2099-12-31' : $intention['identification_valid_date_end'];
  624. //registerRole类型不为NATURAL_PERSON必填
  625. $arr['cerNoType'] = $arr['registerRole'] != 'NATURAL_PERSON' ? 'SOCIAL_CREDIT_NO' : '';
  626. $arr['cerNo'] = $arr['registerRole'] != 'NATURAL_PERSON' ? $intention['credit_code'] : '';
  627. $arr['sellingArea'] = 'B';
  628. $arr['staffSize'] = 'B';
  629. $arr['businessLicensePath'] = $arr['registerRole'] != 'NATURAL_PERSON' ? $this -> sxyPath($intention['business_license']) : '';
  630. //end
  631. $arr['legalIdCardProsPath'] = $this -> sxyPath($intention['card_positive_person']);
  632. $arr['legalIdCardConsPath'] = $this -> sxyPath($intention['card_back_person']);
  633. //openAccountPath 商户类型不为自然人及个体工商户时必传
  634. //legalPersonBankCardPath 商户类型为自然人时必传,为个体工商户时此项和openAccountPath二选一进行传递
  635. //
  636. $arr['openAccountPath'] = '';
  637. $arr['legalPersonBankCardPath'] = '';
  638. //非个人非个体
  639. if ($intention['type'] != 3 && $intention['type'] != 2) {
  640. $arr['openAccountPath'] = $this -> sxyPath($intention['open_account']) ?: '';
  641. }
  642. //个人
  643. if ($intention['type'] == 3) {
  644. $arr['legalPersonBankCardPath'] = $this -> sxyPath($intention['open_account']) ?: '';
  645. }
  646. //个体对公
  647. if ($intention['type'] == 2 && $intention['acc_type'] != '10B') {
  648. $arr['openAccountPath'] = $this -> sxyPath($intention['open_account']) ?: '';
  649. }
  650. //对私
  651. if ($intention['acc_type'] != '10A') {
  652. $arr['legalPersonBankCardPath'] = $this -> sxyPath($intention['open_account']) ?: '';
  653. }
  654. //对公
  655. if ($intention['type'] == 1 && $intention['acc_type'] != '10B') {
  656. $arr['legalPersonBankCardPath'] = $this -> sxyPath($intention['open_account']) ?: '';
  657. }
  658. $arr['m_id'] = $intention['mer_intention_id'];
  659. $res = ShouxinyiApiRequest::merchantApply($arr);
  660. Db::name("log")->insert(array('data'=>json_encode($res)));//日志记录
  661. return true;
  662. }
  663. public function sxyPath($file1){
  664. if ($file1 == '')
  665. return false;
  666. $path = "/serviceprovider/chuxubao/".date('Ymd')."/";
  667. //本地路径
  668. $file1 = str_replace('http://app.bjtdba.com','',$file1);
  669. $file1 = str_replace('https://app.bjtdba.com','',$file1);
  670. //远程路径含文件名
  671. $f = explode("/",$file1);
  672. $file = $path.$f[count($f)-1];
  673. $sxy = ShouxinyiApiRequest::uploadSFtp($path,$file,"./public".$file1);
  674. return $file;
  675. }
  676. public function getAddress($id,$type){
  677. if($type == 'province'){
  678. $res = Db::name("user_province")
  679. ->where(['province_id' => $id])
  680. ->field('province_code,short_name')
  681. ->find();
  682. $code = $res['province_code'];
  683. $check = Db::name("sxy_business_address") -> where('code',$code) -> find();
  684. if(!$check){
  685. $code = Db::name("sxy_business_address") -> where("province like '%".$res['short_name']."%'") -> value('code');
  686. }
  687. return $code;
  688. }
  689. if($type == 'city'){
  690. $res = Db::name("user_city")
  691. ->where(['city_id' => $id])
  692. ->field('city_code,short_name')
  693. ->find();
  694. $code = $res['city_code'];
  695. $check = Db::name("sxy_business_address") -> where('code',$code) -> find();
  696. if(!$check){
  697. $code = Db::name("sxy_business_address") -> where("city like '%".$res['short_name']."%'") -> value('code');
  698. }
  699. return $code;
  700. }
  701. if($type == 'area'){
  702. $res = Db::name("user_area")
  703. ->where(['area_id' => $id])
  704. ->field('area_code,short_name')
  705. ->find();
  706. $code = $res['area_code'];
  707. $check = Db::name("sxy_business_address") -> where('code',$code) -> find();
  708. if(!$check){
  709. $code = Db::name("sxy_business_address") -> where("area like '%".$res['short_name']."%'") -> value('code');
  710. }
  711. return $code;
  712. }
  713. return '';
  714. }
  715. public function getHfAddress($id,$type){
  716. if($type == 'province'){
  717. $res = Db::name("user_province")
  718. ->where(['province_id' => $id])
  719. ->field('province_code,short_name')
  720. ->find();
  721. $code = $res['province_code'];
  722. $check = Db::name("hf_business_address") -> where('code',$code) -> find();
  723. if(!$check){
  724. $code = Db::name("hf_business_address") -> where("province like '%".$res['short_name']."%'") -> value('code');
  725. }
  726. return $code;
  727. }
  728. if($type == 'city'){
  729. $res = Db::name("user_city")
  730. ->where(['city_id' => $id])
  731. ->field('city_code,short_name')
  732. ->find();
  733. $code = $res['city_code'];
  734. $check = Db::name("hf_business_address") -> where('code',$code) -> find();
  735. if(!$check){
  736. $code = Db::name("hf_business_address") -> where("city like '%".$res['short_name']."%'") -> value('code');
  737. }
  738. return $code;
  739. }
  740. if($type == 'area'){
  741. $res = Db::name("user_area")
  742. ->where(['area_id' => $id])
  743. ->field('area_code,short_name')
  744. ->find();
  745. $code = $res['area_code'];
  746. $check = Db::name("hf_business_address") -> where('code',$code) -> find();
  747. if(!$check){
  748. $code = Db::name("hf_business_address") -> where("area like '%".$res['short_name']."%'") -> value('code');
  749. }
  750. return $code;
  751. }
  752. return '';
  753. }
  754. public function getSXYBankCode($bank_deposit){
  755. $count=strpos($bank_deposit,"中国");
  756. $bank_deposit=substr_replace($bank_deposit,"",$count,6);
  757. $code = Db::name("sxy_settlement_bank")
  758. ->where("bank_name like '%".$bank_deposit."%'")
  759. ->value('code');
  760. if($code)
  761. return $code;
  762. $bank_deposit = mb_substr($bank_deposit,0,2);
  763. $code = Db::name("sxy_settlement_bank")
  764. ->where("bank_name like '%".$bank_deposit."%'")
  765. ->value('code');
  766. if($code)
  767. return $code;
  768. $bank_deposit = mb_substr($bank_deposit,2,2);
  769. $code = Db::name("sxy_settlement_bank")
  770. ->where("bank_name like '%".$bank_deposit."%'")
  771. ->value('code');
  772. if($code)
  773. return $code;
  774. $bank_deposit = mb_substr($bank_deposit,4,2);
  775. $code = Db::name("sxy_settlement_bank")
  776. ->where("bank_name like '%".$bank_deposit."%'")
  777. ->value('code');
  778. if($code)
  779. return $code;
  780. return '';
  781. }
  782. public function huifu_create($id,$type,$key='',$app_id='',$mer_id='')
  783. {
  784. try {
  785. $checkMerchat=Db::name('merchant')->where('mer_id',$mer_id)->find();
  786. if(!$checkMerchat) return '错误';
  787. $app_id='app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  788. $key='api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  789. //拿到商户的信息
  790. $merchat=Db::name('merchant_intention')->where('mer_intention_id',$id)->find();
  791. //商户类型为个人走此
  792. if($merchat['type']==3){
  793. //用户在汇付的ID
  794. $member_id='member_personal_'.time().rand(1111,9999);
  795. $params=[
  796. 'app_id'=>$app_id,
  797. 'member_id'=>$member_id,//商户下的用户id,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
  798. 'nickname' => $checkMerchat['mer_name']
  799. ];
  800. //创建用户对象
  801. $res=$this->crate_merber_personal_traits($params,$type,$key);
  802. if(isset($res['data'])){
  803. $result = json_decode($res['data'],true);
  804. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['status'=>3,'check_result'=>$result['error_msg']]);
  805. return $result['error_msg'];
  806. }
  807. if(!empty($res['error_type'])){
  808. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['status'=>3,'check_result'=>$res['error_msg']]);
  809. return $res['error_msg'];
  810. }
  811. //个人创建 没有生成结算账户需要手动创建结算账户
  812. $jiesuandata=[
  813. 'app_id'=>$app_id,//应用ID
  814. 'member_id'=>$member_id,//商户下的用户id,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
  815. 'channel'=>'bank_account',//结算类型 目前仅支持银行卡 固定值 bank_account
  816. 'account_info'=>[
  817. 'card_id'=>str_replace(" ",'',$merchat['bank_account']),//银行卡号
  818. 'card_name'=>$merchat['name'],//银行卡对应的户名
  819. 'cert_id'=>$merchat['idcard_no'],//证件号,银行账户类型为对私时,必填
  820. 'cert_type'=>'00',//证件类型,仅支持:00-身份证,银行账户类型为对私时,必填
  821. 'tel_no'=>$merchat['phone'],//手机号
  822. 'bank_name'=>$merchat['bank_deposit'],//开户银行名称
  823. 'bank_acct_type'=>2//银行账户类型:1-对公;2-对私
  824. ]
  825. ];
  826. //创建该用户的结算账户
  827. $res=$this->create_settleAccounttraits($jiesuandata,$type,$key);
  828. if(isset($res['data'])){
  829. $result = json_decode($res['data'],true);
  830. //创建失败
  831. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['status'=>3,'check_result'=>$result['error_msg'],'hf_status'=>3]);
  832. return $result['error_msg'];
  833. }
  834. if(!empty($res['error_type'])){
  835. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['status'=>3,'check_result'=>$res['error_msg'],'hf_status'=>3]);
  836. return $res['error_msg'];
  837. }
  838. //将生成的id 存入数据库
  839. $insert = [
  840. [
  841. 'mer_id' => $checkMerchat['mer_id'],
  842. 'type' => 1,
  843. 'member_id' => $checkMerchat['hf_member_id'],
  844. 'settle_account_id' => $res['id']
  845. ],
  846. [
  847. 'mer_id' => $checkMerchat['mer_id'],
  848. 'type' => 2,
  849. 'member_id' => $checkMerchat['hf_member_id_wx'],
  850. 'settle_account_id' => $res['id']
  851. ],
  852. [
  853. 'mer_id' => $checkMerchat['mer_id'],
  854. 'type' => 3,
  855. 'member_id' => $member_id,
  856. 'settle_account_id' => $res['id']
  857. ],
  858. ];
  859. Db::name('merchant_member')->where('mer_id', $merchat['mer_id'])->insertAll($insert);
  860. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['status'=>1,'check_result'=>'线下支付已授权','hf_status'=>2]);
  861. }else{
  862. //压缩包
  863. $imgs=[
  864. $merchat['business_license'],
  865. $merchat['card_positive_person'],
  866. $merchat['card_back_person'],
  867. $merchat['open_account'],
  868. ];
  869. $res=$this->addzip($imgs,'attach_file');
  870. if (!$res){
  871. Db::name('merchant_intention')->where('mer_intention_id',$merchat['mer_intention_id'])->update(['status'=>2,'check_result'=>'图片不存在']);
  872. return "图片不存在";
  873. }
  874. $file_real_path = realpath($res);
  875. $fileInfo = pathinfo($file_real_path);
  876. //压缩包end
  877. //银行卡编码
  878. $bank_code=Db::name('hf_bank_code')->whereLike('name','%'.$merchat['bank_deposit'].'%')->value('code');
  879. if(!$bank_code){
  880. Db::name('merchant_intention')->where('mer_intention_id',$merchat['mer_intention_id'])->update(['status'=>2,'check_result'=>'银行卡开户行填写不正确']);
  881. return "银行卡开户行填写不正确";
  882. }
  883. //结算卡类型
  884. if($merchat['acc_type']=='10A'){
  885. $bank_acct_type=1;
  886. $card_name=$merchat['mer_name'];
  887. }else{
  888. $bank_acct_type=2;
  889. $card_name=$merchat['name'];
  890. }
  891. //有效期为永久 默认值为 20991231
  892. if($merchat['identification_valid_date_end']=='forever'){
  893. $legal_cert_id_expires='20991231';
  894. }else{
  895. $legal_cert_id_expires=date('Ymd',strtotime($merchat['identification_valid_date_end']));
  896. }
  897. //有效期为长期 默认值为 20991231
  898. if($merchat['merc_end_time']=='长期'){
  899. $merc_end_time='20991231';
  900. }else{
  901. $merc_end_time=date('Ymd',strtotime($merchat['merc_end_time']));
  902. if($merc_end_time<date('Ymd',time())){
  903. $merc_end_time='20991231';
  904. }
  905. }
  906. //请求订单编号1
  907. $order_no=time().rand(10000,99999);
  908. //商户下的用户id,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
  909. $member_id='member_enterprise_'.time().rand(1111,9999);
  910. $params=[
  911. 'app_id'=>$app_id,//控制台 主页面应用的app_id
  912. 'order_no'=>$order_no,//请求订单号,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
  913. 'member_id'=>$member_id,//商户下的用户id,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
  914. 'name'=>$merchat['mer_name'],//企业名称
  915. 'prov_code'=>$this->getHfAddress($merchat['province_id'],'province'),//省份编码 (省市编码)
  916. 'area_code'=>$this->getHfAddress($merchat['city_id'],'city'),//地区编码 (省市编码)
  917. 'social_credit_code'=>$merchat['credit_code'],//统一社会信用码
  918. 'social_credit_code_expires'=>$merc_end_time,//统一社会信用证有效期
  919. 'business_scope'=>'零售',//经营范围
  920. 'legal_person'=>$merchat['name'],//法人姓名
  921. 'legal_cert_id'=>$merchat['idcard_no'],//法人身份证号码
  922. 'legal_cert_id_expires'=>$legal_cert_id_expires,//法人身份证有效期(格式:YYYYMMDD,例如:20190909)
  923. 'legal_mp'=>$merchat['phone'],//法人手机号
  924. 'address'=>$merchat['business_addr'],//企业地址
  925. 'bank_code'=>$bank_code,//银行代码,如果需要自动开结算账户,本字段必填(详见附录 银行代码)
  926. 'bank_acct_type'=>$bank_acct_type,//银行账户类型:1-对公;2-对私,如果需要自动开结算账户,本字段必填
  927. 'card_no'=>$merchat['bank_account'],//银行卡号,如果需要自动开结算账户,本字段必填
  928. 'card_name'=>$card_name,//银行卡对应的户名,如果需要自动开结算账户,本字段必填;若银行账户类型是对公,必须与企业名称一致
  929. 'notify_url'=>env('APP_URL').'/sys/HuiPay/notify_url/'.'crate_merber_enterprise',//异步通知地址,url为http/https路径,服务器POST回调,URL 上请勿附带参数
  930. ];
  931. //上传附件,传入的中文文件名称为 UTF-8 字符集 URLEncode 编码后的字符串。内容须包含三证合一证件照、法人身份证正面照、法人身份证反面照、开户银行许可证照。 压缩 zip包后上传,最大限制为 9 M。
  932. $params['attach_file']=new \CURLFile($file_real_path,'', $fileInfo['basename']);
  933. //增加记录为异步通知做准备
  934. Db::name('hf_user_order')->insert(['mer_intention_id'=>$id,'order_no'=>$order_no]);
  935. //创建企业用户并增加结算卡账户
  936. $res=$this->crate_merber_enterprise_traits($params,$type,$key);
  937. if (isset($res['data'])){
  938. $result = json_decode($res['data'],true);
  939. if(!empty($result['error_type'])){
  940. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['check_result'=>$result['error_msg']]);
  941. return $result['error_msg'];
  942. }
  943. }
  944. if(!empty($res['error_type'])){
  945. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['check_result'=>$res['error_msg']]);
  946. return $res['error_msg'];
  947. }
  948. //更改汇付状态
  949. Db::name('merchant_intention')->where('mer_intention_id',$id)->update(['hf_status'=>1,'check_result'=>'审核中']);
  950. }
  951. Db::name('user')->where('uid',$merchat['uid'])->update([
  952. 'user_group'=>4
  953. ]);
  954. return "success";
  955. }catch (\Exception $exception){
  956. Db::name('log')->insert(['data'=>$exception->getFile().':'.$exception->getLine().'【'.$exception->getMessage().'】']);
  957. return ['data'=>$exception->getFile().':'.$exception->getLine().'【'.$exception->getMessage().'】'];
  958. }
  959. }
  960. /**
  961. * 将优化后的二维码添加到压缩包
  962. */
  963. public function addzip($show_src,$name='attach_file')
  964. {
  965. try {
  966. $tmpDir = './public/zip/'; //
  967. if (!file_exists($tmpDir)) {
  968. //创建文件夹
  969. mkdir($tmpDir, 0777, true);
  970. }
  971. // $phone=Db::name('user_agent')
  972. // ->where('u_a.user_id',$uid)
  973. // ->alias('u_a')
  974. // ->leftJoin('user u','u_a.user_id=u.uid')
  975. // ->field('u.phone')
  976. // ->find();
  977. $zipName = date('YmdHis') . mt_rand(1000, 9999) . '.zip'; // 压缩包文件名
  978. if($name > 0){
  979. $rend=md5(mt_rand(100000000, 999999999));
  980. $zipName = $name . '_' . $rend . '.zip'; // 压缩包文件名
  981. }
  982. $zipNameUrl = $tmpDir . $zipName; // 文件路径
  983. //生成文件
  984. $zip = new \ZipArchive();
  985. if ($zip->open($zipNameUrl, \ZipArchive::OVERWRITE) !== true) {
  986. //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
  987. if ($zip->open($zipNameUrl, \ZipArchive::CREATE) !== true) {
  988. // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  989. return '下载失败,文件夹不存在';
  990. }
  991. }
  992. foreach ($show_src as $file) {
  993. //判断图片是否存在
  994. $isFile = $this->checkFileExists($file);
  995. if (!$isFile) {
  996. continue;
  997. }
  998. //抓取图片内容
  999. $fileContent = file_get_contents($file);
  1000. //添加图片
  1001. $zip->addFromString(basename($file), $fileContent);
  1002. }
  1003. // 关闭
  1004. $zip->close();
  1005. $qrcodeData=[
  1006. 'url'=>'./public/zip/'.$zipName,
  1007. 'ctime'=>time()
  1008. ];
  1009. return $qrcodeData['url'];
  1010. }catch(\Exception $e){
  1011. log::info($e->getMessage());
  1012. return false;
  1013. }
  1014. }
  1015. /**
  1016. * 判断文件是否存在,支持本地及远程文件
  1017. * @param String $file 文件路径
  1018. * @return Boolean
  1019. */
  1020. private function checkFileExists($file)
  1021. {
  1022. // 远程文件
  1023. if (strtolower(substr($file, 0, 5)) == 'https') {
  1024. // 远程文件
  1025. $header = get_headers($file, true);
  1026. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  1027. } elseif (strtolower(substr($file, 0, 4)) == 'http') {
  1028. // 远程文件
  1029. $header = get_headers($file, true);
  1030. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  1031. } else {
  1032. // 本地文件
  1033. return file_exists($file);
  1034. }
  1035. }
  1036. //进件 汇付
  1037. public function UpgradeToMerchants($merchant_params,$name)
  1038. {
  1039. $merchant_params['fee_rate_list']='[{"rate_channel": "wx_pub_online","fee_rate": "0.008"}, {"rate_channel": "wx_pub_offline","fee_rate": "0.0038"}, {"rate_channel": "wx_lite_online","fee_rate": "0.007"}, {"rate_channel": "wx_lite_offline","fee_rate": "0.0038"}, {"rate_channel": "wx_scan","fee_rate": "0.0038"}, {"rate_channel": "alipay_qr_online","fee_rate": "0.007"}, {"rate_channel": "alipay_qr_offline","fee_rate": "0.0038"}, {"rate_channel": "alipay_scan","fee_rate": "0.0038"}, {"rate_channel": "alipay_lite_online","fee_rate": "0.007"}, {"rate_channel": "alipay_lite_offline","fee_rate": "0.0038"}, {"rate_channel": "alipay_call","fee_rate": "0.007"}, {"rate_channel": "union_qr_under","fee_rate": "0.0038"}, {"rate_channel": "union_qr_beyond","fee_rate": "0.007"}, {"rate_channel": "union","fee_rate": "0.007"}, {"rate_channel": "alipay_pub","fee_rate": "0.0038"}]';
  1040. Db::name('log')->insert(['data'=>json_encode($merchant_params)]);
  1041. return $this->incoming_parts($merchant_params,$name);
  1042. }
  1043. //进件 汇付
  1044. public function incoming_select($merchant_params,$name)
  1045. {
  1046. return $this->incoming_parts_select($merchant_params,$name);
  1047. }
  1048. public function ruzhu($ruzhu_data,$name)
  1049. {
  1050. return $this->Merchant_settlement($ruzhu_data,$name);
  1051. }
  1052. public function ruzhu_query($ruzhu_query_data,$name)
  1053. {
  1054. return $this->Merchant_settlement_select($ruzhu_query_data,$name);
  1055. }
  1056. public function merProfilePictures($data,$name)
  1057. {
  1058. $up_dir = '/uploads/users/huifu_upload/';
  1059. $new_file = (int)(microtime(true) * 1000);
  1060. $res=$this->uploadImg($data['file'],$up_dir,$new_file);
  1061. Db::name('log')->insert(['data'=>json_encode($res)]);
  1062. $file_real_path = realpath($res['path']);
  1063. $fileInfo = pathinfo($file_real_path);
  1064. $data['file']=new \CURLFile($file_real_path,'', $fileInfo['basename']) ;
  1065. $data=$this->merProfilePicture($data,$name);
  1066. if(empty($data['error_type'])){
  1067. $data=['file_type'=>$data['file_type'],'pic_id'=>$data['pic_id']];
  1068. }else{
  1069. $data=['errro_msg'=>$data['error_msg']];
  1070. }
  1071. return $data;
  1072. }
  1073. public function uploadImg(string $image, string $up_dir,$new_file)
  1074. {
  1075. $base64_img = trim($image);
  1076. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
  1077. $type = $result[2];
  1078. if (in_array($type, array('pjpeg', 'jpeg', 'jpg','JPG', 'gif', 'bmp', 'png'))) {
  1079. if (!is_dir('./public' .$up_dir)) {
  1080. mkdir('./public' .$up_dir,0777,true);
  1081. }
  1082. // $new_file = (int)(microtime(true) * 1000);
  1083. $imgName = $up_dir . "{$new_file}." . $type;
  1084. $new_file = './public'.$imgName;
  1085. if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))) {
  1086. // 完整的图片地址
  1087. return ["path"=>$new_file,"suffix"=>$type];
  1088. } else {
  1089. return false;
  1090. }
  1091. }
  1092. }
  1093. return false;
  1094. }
  1095. public function commit_res($data,$name)
  1096. {
  1097. return $this->merProfileForAudit($data,$name);
  1098. }
  1099. public function commit_res_query($data)
  1100. {
  1101. return $this->merProfileAuditStatus($data);
  1102. }
  1103. }