UserExtractRepository.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-06-16
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\user;
  11. use alipay\aop\AlipayConfig;
  12. use alipay\aop\AopCertClient;
  13. use alipay\aop\request\AlipayFundTransUniTransferRequest;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\dao\user\UserExtractDao as dao;
  16. use app\common\repositories\merchant\MerchantRepository;
  17. use crmeb\jobs\SendTemplateMessageJob;
  18. use crmeb\services\SwooleTaskService;
  19. use think\Exception;
  20. use think\facade\Db;
  21. use think\facade\App;
  22. use think\facade\Queue;
  23. class UserExtractRepository extends BaseRepository
  24. {
  25. /**
  26. * @var dao
  27. */
  28. protected $dao;
  29. protected $wxapp = array(
  30. 'app_id'=>'wxbf753066ac8686ba',
  31. 'applet_app_id'=>'wx9082e5ac2fdb513f',
  32. 'mchid'=>'1605734712',
  33. 'certPem'=>'/pkey/cert/apiclient_cert.pem',
  34. 'keyPem'=>'/pkey/cert/apiclient_key.pem'
  35. );
  36. /**
  37. * UserExtractRepository constructor.
  38. * @param dao $dao
  39. */
  40. public function __construct(dao $dao)
  41. {
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * TODO
  46. * @param $id
  47. * @return bool
  48. * @author Qinii
  49. * @day 2020-06-16
  50. */
  51. public function getWhereCount($id)
  52. {
  53. $where['extract_id'] = $id;
  54. $where['status'] = 0;
  55. return $this->dao->getWhereCount($where) > 0;
  56. }
  57. /**
  58. * TODO
  59. * @param array $where
  60. * @param $page
  61. * @param $limit
  62. * @return array
  63. * @author Qinii
  64. * @day 2020-06-16
  65. */
  66. public function serach(array $where,$page,$limit)
  67. {
  68. if(isset($where['mer_id'])){
  69. $where['mer_id']=isset($where['mer_id'])?$where['mer_id']:"";
  70. }
  71. $mer_ids=[];
  72. if(isset($where['mer_name']) && $where['mer_name']!=''){
  73. $mer_ids=Db::name('merchant')->whereLike('mer_name','%'.$where['mer_name'].'%')->column('mer_id');
  74. }
  75. unset($where['mer_name']);
  76. $uids[]=0;
  77. $keyword='';
  78. if($where['keyword']!=''){
  79. $keyword=$where['keyword'];
  80. $uids=Db::name('user_certification')
  81. ->alias('uc')
  82. ->join('user_extract ue','uc.uid = ue.uid')
  83. ->join('user u','uc.uid = u.uid')
  84. ->whereLike('uc.user_name','%'.$where['keyword'].'%')
  85. ->whereOr('u.account','like','%'.$where['keyword'].'%')
  86. ->group('uc.uid')
  87. ->column('uc.uid');
  88. unset($where['keyword']);
  89. }
  90. $query = $this->dao->serach($where)
  91. ->when($keyword,function($query)use($keyword,$uids){
  92. $query->where(function($query)use($keyword,$uids){
  93. $query->where('real_name','like','%'.$keyword.'%')
  94. ->whereOr('bank_code','like','%'.$keyword.'%')
  95. ->whereOr('alipay_code','like','%'.$keyword.'%')
  96. ->whereOr('wechat','like','%'.$keyword.'%')
  97. ->whereOr('uid','in',$uids);
  98. });
  99. })
  100. ->when(($where['identity'] ?? '') == 'user', function ($query) {
  101. $query->where('mer_id is null');
  102. })
  103. ->when(($where['identity'] ?? '') == 'mer', function ($query) {
  104. $query->where('mer_id is not null');
  105. })
  106. ->when($mer_ids,function ($q) use ($mer_ids){$q->where('mer_id','in',$mer_ids);});
  107. $query_sql= clone $query;
  108. $count = $query->count();
  109. $list = $query->page($page,$limit)->order("extract_id desc")->select();
  110. if($list){
  111. foreach($list as $key => &$value){
  112. $value['real_name'] = $value['real_name'].'['.Db::name('user_certification') -> where('uid',$value['uid']) -> value('user_name').']';
  113. $value['phone'] =Db::name('user') -> where('uid',$value['uid']) -> value('phone');
  114. $value['mer_name'] ='平台';
  115. if($value['mer_id']){
  116. $value['mer_name'] = Db::name('merchant')->where('mer_id',$value['mer_id'])->value('mer_name');
  117. }
  118. }
  119. }
  120. if(isset($where['mer_id']) && $where['mer_id']!=''){
  121. $where2['mer_id']=isset($where['mer_id'])?$where['mer_id']:'';
  122. $mer_id = $where['mer_id'];
  123. $bill_number = Db::name("user_bill")->where('mer_id', $mer_id)->where('status', 1)->whereIn('commission_type', [7, 17, 19])->sum('number');
  124. $uids2=$query_sql->group('uid')->column('uid');
  125. //总额
  126. $zonge_1=$bill_number;
  127. $zonge_2=sprintf("%.2f", $bill_number * 0.06);
  128. //已提现
  129. $tixian_1=Db::name("user_extract") ->where($where2)->where('status',1)->whereIn('uid',$uids2)->sum("extract_price");
  130. $tixian_2=Db::name("user_extract") ->where($where2)->where('status',1)->whereIn('uid',$uids2)->sum("service_money");
  131. //审核中
  132. $shenhezhong_1=Db::name("user_extract") ->where($where2)->where('status',0)->whereIn('uid',$uids2)->sum("extract_price");
  133. $shenhezhong_2=Db::name("user_extract") ->where($where2)->where('status',0)->whereIn('uid',$uids2)->sum("service_money");
  134. //增加【实际提现】模块,实际提现金额=已提现金额*(1-综合服务费率)
  135. $countmoyer=[
  136. //总额
  137. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_1, 'field'=>'元', 'name'=> '收益总额' ],
  138. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_2, 'field'=>'元', 'name'=> '综合服务费总额' ],
  139. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($zonge_1-$zonge_2)), 'field'=>'元', 'name'=> '实际到账总额' ],
  140. //已提现
  141. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_1, 'field'=>'元', 'name'=> '已提现总额' ],
  142. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_2, 'field'=>'元', 'name'=> '综合服务费' ],
  143. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($tixian_1-$tixian_2)), 'field'=>'元', 'name'=> '实际到账总额' ],
  144. //审核中
  145. ['className'=> 'el-icon-s-goods', 'count'=>$shenhezhong_1, 'field'=>'元', 'name'=> '审核中总额' ],
  146. ['className'=> 'el-icon-s-goods', 'count'=>$shenhezhong_2, 'field'=>'元', 'name'=> '综合服务费' ],
  147. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($shenhezhong_1-$shenhezhong_2)), 'field'=>'元', 'name'=> '实际到账总额' ]
  148. ];
  149. }else{
  150. if (($where['identity'] ?? '') == 'user') {
  151. $bill_number = Db::name("user_bill")->where('status', 1)->whereNotIn('commission_type', [5, 7, 17, 19])->sum('number');
  152. }
  153. if (($where['identity'] ?? '') == 'mer') {
  154. $bill_number = Db::name("user_bill")->where('status', 1)->whereIn('commission_type', [7, 17, 19])->sum('number');
  155. }
  156. $zonge_1=$bill_number;
  157. $tixian_1=Db::name("user_extract")->when(($where['identity'] ?? '') == 'user', function ($query) {
  158. $query->where('mer_id is null');
  159. })
  160. ->when(($where['identity'] ?? '') == 'mer', function ($query) {
  161. $query->where('mer_id is not null');
  162. })->where('status',1)->sum("extract_price");
  163. $tixian_2=Db::name("user_extract")->when(($where['identity'] ?? '') == 'user', function ($query) {
  164. $query->where('mer_id is null');
  165. })
  166. ->when(($where['identity'] ?? '') == 'mer', function ($query) {
  167. $query->where('mer_id is not null');
  168. })->where('status',1)->sum("service_money");
  169. $shenhezhong_1=Db::name("user_extract")->when(($where['identity'] ?? '') == 'user', function ($query) {
  170. $query->where('mer_id is null');
  171. })
  172. ->when(($where['identity'] ?? '') == 'mer', function ($query) {
  173. $query->where('mer_id is not null');
  174. })->where('status',0)->sum("extract_price");
  175. $count1 = $query->where("status",1)->sum("extract_price");
  176. $count2 = Db::name("user")->sum("brokerage_price");
  177. if (($where['identity'] ?? '') == 'mer') {
  178. $count2 = Db::name("merchant")->sum("brokerage_price");
  179. }
  180. $countmoyer=[
  181. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_1, 'field'=>'元', 'name'=> '奖励总额' ],
  182. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_1, 'field'=>'元', 'name'=> '已提现' ],
  183. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_1 - $tixian_2, 'field'=>'元', 'name'=> '实际提现' ],
  184. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_1 - $tixian_1 - $shenhezhong_1, 'field'=>'元', 'name'=> '未提现' ],
  185. ['className'=> 'el-icon-s-goods', 'count'=>$shenhezhong_1, 'field'=>'元', 'name'=> '审核中' ],
  186. ];
  187. }
  188. return compact('count','list',"countmoyer");
  189. }
  190. /**
  191. * TODO
  192. * @param array $where
  193. * @param $page
  194. * @param $limit
  195. * @return array
  196. * @author Qinii
  197. * @day 2020-06-16
  198. */
  199. public function new_search(array $where,$page,$limit)
  200. {
  201. if(isset($where['mer_id'])){
  202. $where['mer_id']=isset($where['mer_id'])?$where['mer_id']:"";
  203. }
  204. $mer_ids=[];
  205. if(isset($where['mer_name']) && $where['mer_name']!=''){
  206. $mer_ids=Db::name('merchant')->whereLike('mer_name','%'.$where['mer_name'].'%')->column('mer_id');
  207. }
  208. unset($where['mer_name']);
  209. $uids[]=0;
  210. $keyword='';
  211. if($where['keyword']!=''){
  212. $keyword=$where['keyword'];
  213. $uids=Db::name('user_certification')
  214. ->alias('uc')
  215. ->join('user_extract ue','uc.uid = ue.uid')
  216. ->join('user u','uc.uid = u.uid')
  217. ->whereLike('uc.user_name','%'.$where['keyword'].'%')
  218. ->whereOr('u.account','like','%'.$where['keyword'].'%')
  219. ->group('uc.uid')
  220. ->column('uc.uid');
  221. unset($where['keyword']);
  222. }
  223. $query = $this->dao->serach($where)
  224. ->when($keyword,function($query)use($keyword,$uids){
  225. $query->where(function($query)use($keyword,$uids){
  226. $query->where('real_name','like','%'.$keyword.'%')
  227. ->whereOr('bank_code','like','%'.$keyword.'%')
  228. ->whereOr('alipay_code','like','%'.$keyword.'%')
  229. ->whereOr('wechat','like','%'.$keyword.'%')
  230. ->whereOr('uid','in',$uids);
  231. });
  232. })
  233. // ->when($where['identity'] == 'user', function ($query) {
  234. // $query->where('mer_id is null');
  235. // })
  236. // ->when($where['identity'] == 'mer', function ($query) {
  237. // $query->where('mer_id is not null');
  238. // })
  239. ->when($mer_ids,function ($q) use ($mer_ids){$q->where('mer_id','in',$mer_ids);});
  240. $query_sql= clone $query;
  241. $count = $query->count();
  242. $list = $query->page($page,$limit)->order("extract_id desc")->select();
  243. if($list){
  244. foreach($list as $key => &$value){
  245. $value['real_name'] = $value['real_name'].'['.Db::name('user_certification') -> where('uid',$value['uid']) -> value('user_name').']';
  246. $value['phone'] =Db::name('user') -> where('uid',$value['uid']) -> value('phone');
  247. $value['mer_name'] ='平台';
  248. if($value['mer_id']){
  249. $value['mer_name'] = Db::name('merchant')->where('mer_id',$value['mer_id'])->value('mer_name');
  250. }
  251. }
  252. }
  253. $where2['mer_id']=isset($where['mer_id'])?$where['mer_id']:'';
  254. $uids2=$query_sql->group('uid')->column('uid');
  255. //总额
  256. $zonge_1=Db::name("user_extract")->whereIn('uid',$uids2)->sum("extract_price");
  257. $zonge_2=Db::name("user_extract")->whereIn('uid',$uids2)->sum("service_money");
  258. //已提现
  259. $tixian_1=Db::name("user_extract")->where('status',1)->whereIn('uid',$uids2)->sum("extract_price");
  260. $tixian_2=Db::name("user_extract")->where('status',1)->whereIn('uid',$uids2)->sum("service_money");
  261. //审核中
  262. $shenhezhong_1=Db::name("user_extract")->where('status',0)->whereIn('uid',$uids2)->sum("extract_price");
  263. $shenhezhong_2=Db::name("user_extract")->where('status',0)->whereIn('uid',$uids2)->sum("service_money");
  264. //增加【实际提现】模块,实际提现金额=已提现金额*(1-综合服务费率)
  265. $countmoyer=[
  266. //总额
  267. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_1, 'field'=>'元', 'name'=> '收益总额' ],
  268. ['className'=> 'el-icon-s-goods', 'count'=>$zonge_2, 'field'=>'元', 'name'=> '综合服务费总额' ],
  269. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($zonge_1-$zonge_2)), 'field'=>'元', 'name'=> '实际到账总额' ],
  270. //已提现
  271. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_1, 'field'=>'元', 'name'=> '已提现总额' ],
  272. ['className'=> 'el-icon-s-goods', 'count'=>$tixian_2, 'field'=>'元', 'name'=> '综合服务费' ],
  273. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($tixian_1-$tixian_2)), 'field'=>'元', 'name'=> '实际到账总额' ],
  274. //审核中
  275. ['className'=> 'el-icon-s-goods', 'count'=>$shenhezhong_1, 'field'=>'元', 'name'=> '审核中总额' ],
  276. ['className'=> 'el-icon-s-goods', 'count'=>$shenhezhong_2, 'field'=>'元', 'name'=> '综合服务费' ],
  277. ['className'=> 'el-icon-s-goods', 'count'=>set_price_rtrim(decimal2($shenhezhong_1-$shenhezhong_2)), 'field'=>'元', 'name'=> '实际到账总额' ]
  278. ];
  279. return compact('count','list',"countmoyer");
  280. }
  281. public function get_user_extract_item($extract_id)
  282. {
  283. return Db::name('user_extract_item')->where('extract_id',$extract_id)->select();
  284. }
  285. /**
  286. * @param $uid
  287. * @return mixed
  288. * @author xaboy
  289. * @day 2020/6/22
  290. */
  291. public function userTotalExtract($uid)
  292. {
  293. return $this->dao->serach(['status' => 1, 'uid' => $uid])->sum('extract_price');
  294. }
  295. /**
  296. * TODO
  297. * @param $user
  298. * @param $data
  299. * @author Qinii
  300. * @day 2020-06-16
  301. */
  302. public function create($user,$data,$uid,$type=1)
  303. {
  304. $userData = Db::name('user') -> where('uid',$uid) -> find();
  305. if ($data["extract_type"]==1){
  306. $data["wechat"]=$userData['account'];
  307. $data["extract_pic"]='';
  308. $data["real_name"]=$userData['nickname'];
  309. $data['order_sn'] = 'wx'.time().rand(100000, 999999);
  310. }else if($data["extract_type"]==2) {
  311. if ($userData['ali_user_id'] == '0' || $userData['ali_user_id'] == '') {
  312. return false;
  313. }
  314. $data["alipay_code"] = $userData["account"];
  315. $data["real_name"] = $userData["nickname"];
  316. $data['order_sn'] = 'al' . time() . rand(100000, 999999);
  317. }else if($data["extract_type"]==3){
  318. $account_info=Db::name("user_withdraw_account")->where(["account_type"=>$data["extract_type"],"user_id"=>$uid])->find();
  319. $data["bank_code"]=$account_info["account"];
  320. $data["real_name"]=$account_info['info'];
  321. $data["bank_address"]=$account_info['bank_name'];
  322. $data['order_sn'] = 'tx'.time().rand(100000, 999999);
  323. $data["bank_info"]= json_encode($account_info, true);
  324. unset($data['bank_id']);
  325. }else{
  326. return false;
  327. }
  328. if ($data["extract_type"]==3){
  329. $data = Db::transaction(function()use($user,$data, $type){
  330. if($type==1){
  331. $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2);
  332. $user->brokerage_price = $brokerage_price;
  333. }else if ($type == 2) {
  334. $brokerage_price = bcsub($user['member_brokerage_price'],$data['extract_price'],2);
  335. $user->member_brokerage_price = $brokerage_price;
  336. }
  337. $user->save();
  338. $service_money=bcmul($data['extract_price'],'0.06',2);
  339. $data['service_money']=$service_money;
  340. $data['status'] = 0;
  341. $data['uid'] = $user['uid'];
  342. $data['type']=$type;
  343. $data['withdrawal_type'] =3;
  344. return $this->dao->create($data);
  345. });
  346. }else{
  347. $data = Db::transaction(function()use($user,$data,$uid,$type){
  348. if($type==1){
  349. $brokerage_price = bcsub($user['brokerage_price'],$data['extract_price'],2);
  350. $user->brokerage_price = $brokerage_price;
  351. }else if ($type == 2) {
  352. $brokerage_price = bcsub($user['member_brokerage_price'],$data['extract_price'],2);
  353. $user->member_brokerage_price = $brokerage_price;
  354. }
  355. $user->save();
  356. $service_money=bcmul($data['extract_price'],'0.06',2);
  357. $data['service_money']=$service_money;
  358. $data['status'] = 0;
  359. $data['uid'] = $user['uid'];
  360. $data['types'] = $type;
  361. $data['statbalanceus'] = $brokerage_price;
  362. $extract=$this->dao->create($data);
  363. $tea_price=Db::name('user_rich')->where('uid', $uid)->value('tea_price');
  364. $extract_price_yihu=0;
  365. $service_money_yihu=0;
  366. if($tea_price && $tea_price>0){
  367. if($tea_price>=$data['extract_price']){
  368. $extract_price_yihu= $data['extract_price'];
  369. $service_money_yihu= $data['service_money'];
  370. }else{
  371. $extract_price_yihu= $tea_price;
  372. $service_money_yihu= bcmul($tea_price,'0.06',2);
  373. }
  374. }
  375. // if($extract_price_yihu>0){
  376. $insertAll[]=array(
  377. 'extract_id'=>$extract->extract_id,
  378. 'uid'=> $data['uid'],
  379. 'extract_type'=> $data["extract_type"],
  380. 'extract_price'=> $extract_price_yihu,
  381. 'service_money'=> $service_money_yihu,
  382. 'order_sn'=> $data['order_sn'],
  383. 'type'=> 1,//0- 储蓄保 1-一壶茶
  384. );
  385. // }
  386. $insertAll[]=array(
  387. 'extract_id'=>$extract->extract_id,
  388. 'uid'=> $data['uid'],
  389. 'extract_type'=> $data["extract_type"],
  390. 'extract_price'=> ($data['extract_price']-$extract_price_yihu),
  391. 'service_money'=> ($data['service_money']-$service_money_yihu),
  392. 'order_sn'=> $data['order_sn'],
  393. 'type'=> 0,//0- 储蓄保 1-一壶茶
  394. );
  395. DB::name('user_extract_item')->insertAll($insertAll);
  396. return $extract;
  397. });
  398. }
  399. SwooleTaskService::admin('notice', [
  400. 'type' => 'extract',
  401. 'title' => '您有一条新的提醒申请',
  402. 'id' => $data->extract_id
  403. ]);
  404. return true;
  405. }
  406. public function create_yhc($user, $merchant, $data)
  407. {
  408. Db::name("merchant")->where("mer_id", $merchant['mer_id'])->dec('brokerage_price', $data['extract_price'])->update();
  409. $data["bank_code"]=$merchant["bank_account"];
  410. $data["bank_address"]=$merchant["bank_deposit"];
  411. $data['order_sn'] = 'tx'.time().rand(100000, 999999);
  412. $data["real_name"]=$user['nickname'];
  413. $data['status'] = 0;
  414. $data['uid'] = $user['uid'];
  415. $data['types'] = 1;
  416. $data['type'] = 1;
  417. $data['mer_id'] = $merchant['mer_id'];
  418. $data['withdrawal_type'] =3;
  419. return $this->dao->create($data);
  420. }
  421. public function switchStatus($id,$data)
  422. {
  423. Db::transaction(function()use($id,$data){
  424. if($data['status'] == '-1'){
  425. $extract = $this->dao->getWhere(['extract_id' => $id]);
  426. $user = app()->make(UserRepository::class)->get($extract['uid']);
  427. if (!empty($extract['mer_id'])) {
  428. //商户提现逻辑处理
  429. $mer = app()->make(MerchantRepository::class)->get($extract['mer_id']);
  430. $brokerage_price = bcadd($mer['brokerage_price'] ,$extract['extract_price'],2);
  431. $mer->brokerage_price = $brokerage_price;
  432. $mer->save();
  433. } else {
  434. //用户提现逻辑处理
  435. if($extract['types']==1){
  436. $brokerage_price = bcadd($user['brokerage_price'] ,$extract['extract_price'],2);
  437. //返回余额
  438. $user->brokerage_price = $brokerage_price;
  439. }else{
  440. $member_brokerage_price = bcadd($user['member_brokerage_price'] ,$extract['extract_price'],2);
  441. //返回余额
  442. $user->member_brokerage_price = $member_brokerage_price;
  443. Db::name('user_rich')->where('uid',$extract['uid'])->inc('red', $extract['extract_price'])->update();
  444. Db::name('user_rich_log')->insert([
  445. 'uid' => $extract['uid'],
  446. 'red' => $extract['extract_price'],
  447. 'pm' => 1,
  448. 'time' => time(),
  449. 'order_id' => 0,
  450. 'mark' => '提现失败返还红积分'
  451. ]);
  452. }
  453. $user->save();
  454. }
  455. // //返回积分
  456. // $user->score = bcadd($user['score'] ,$extract['extract_price'],2);
  457. // $user->save();
  458. // Db::name('user_sign_score')->where("order_type","extract") -> where("order_id",$id) -> update(['status'=>0]);
  459. }else{
  460. //积分扣除
  461. // Db::name('user_sign_score')->where("order_type","extract") -> where("order_id",$id) -> update(['status'=>1]);
  462. }
  463. $this->dao->update($id,$data);
  464. });
  465. Queue::push(SendTemplateMessageJob::class,[
  466. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  467. 'id' =>$id
  468. ]);
  469. }
  470. /**
  471. * @remarks 微信商家转账到零钱
  472. * @author Tang
  473. * @created 2023/10/31
  474. */
  475. public function WechatTransfers($id)
  476. {
  477. $userdata=Db::name('user_extract')
  478. ->alias('u_e')
  479. ->leftJoin('user_extract_openid u_e_o','u_e.uid=u_e_o.uid')
  480. ->where(['extract_id' => $id,'extract_type'=>1])
  481. ->field('u_e.extract_price,u_e.uid,u_e_o.openid,u_e_o.type as u_e_o_type,u_e.service_money,u_e.withdrawal_type,u_e.order_sn,u_e.mer_id')
  482. ->find();
  483. if($userdata and !empty($userdata)){
  484. if($userdata['withdrawal_type']==2){
  485. $money= bcsub($userdata['extract_price'],$userdata['service_money'],2);
  486. }else{
  487. $money= bcsub($userdata['extract_price'],$userdata['service_money'],2);
  488. }
  489. // $money=$userdata['extract_price'];//测试不加手续手续费
  490. $res=$this->transfer('提现到微信零钱', $userdata['order_sn'], $money, $userdata['openid'],$userdata['u_e_o_type']);
  491. if(isset($res['code']) && isset($res['message'])) {
  492. return ['code'=>500,'msg'=>$res['message']];
  493. }else{
  494. Db::name('user_extract')->where('extract_id',$id)->update(['wx_status'=>1]);
  495. return ['code'=>201,'msg'=>'微信正在处理'];
  496. }
  497. // else if($res['batch_status']=='ACCEPTED' || $res['batch_status']=='PROCESSING'){
  498. // Db::name('user_extract')->where('extract_id',$id)->update(['wx_status'=>1]);
  499. // return ['code'=>201,'msg'=>'微信正在处理'];
  500. // }else if($res['batch_status']=='FINISHED'){
  501. // //FINISHED:已完成。批次内的所有转账明细单都已处理完成
  502. // Db::name('entropy_barter_bill')->where('order_no', $userdata['order_sn'])->where('pm',2)->update(['status'=>2,'remarks'=>'已到账']);
  503. // Db::name('user_extract')->where('extract_id',$id)->update(['status'=>1]);
  504. // return ['code'=>200,'msg'=>'审核成功'];
  505. // }
  506. // return ['code'=>500,'msg'=>'提现错误'];
  507. }else{
  508. return ['code'=>200,'msg'=>'未找到数据'];
  509. }
  510. }
  511. /**
  512. * 定时任务3分钟查询一次审核中的微信待处理的订单
  513. * @return array|bool
  514. * @throws \think\db\exception\DataNotFoundException
  515. * @throws \think\db\exception\DbException
  516. * @throws \think\db\exception\ModelNotFoundException
  517. */
  518. public function wechat_extract(){
  519. $user_extract_data=Db::name('user_extract')->where('wx_status',1)->where('status',0)->select();
  520. foreach ($user_extract_data as $v){
  521. $data=$this->batch_id($v['order_sn']);
  522. Db::name('log')->insert(['data'=>'微信审核------'.json_encode($data)]);
  523. if(isset($data['transfer_batch']['batch_status']) && $data['transfer_batch']['batch_status']=='FINISHED' && $data['transfer_batch']['success_num']==1){
  524. Db::name('entropy_barter_bill')->where('order_no', $v['order_sn'])->where('pm',2)->update(['status'=>2,'remarks'=>'已到账']);
  525. Db::name('user_extract')->where('extract_id',$v['extract_id'])->update(['status'=>1]);
  526. }else if(isset($data['transfer_batch']['batch_status']) && $data['transfer_batch']['batch_status']=='FINISHED' && $data['transfer_batch']['success_num']<1){
  527. $fail_msg='微信二次审核已驳回';
  528. Db::name('user_extract')->where('order_sn',$v['order_sn'])->update(['fail_msg'=>$fail_msg,'status_time'=>date('Y-m-d H:i:s'),'status'=>'-1']);
  529. if($v['withdrawal_type']==1){
  530. Db::name('user')->where('uid',$v['uid'])->inc('brokerage_price',$v['extract_price'])->update();
  531. }else{
  532. $eb_bill=Db::name('entropy_barter_bill')->where('order_no',$v['order_sn'])->where('pm',2)->find();
  533. Db::name('entropy_barter_bill')->where('id',$eb_bill['id'])->where('pm',2)->update(['status'=>3,'remarks'=>$fail_msg]);
  534. Db::name('entropy_barter_user_'.$v['mer_id'])->where('eb_id',$eb_bill['to_id'])->inc('wallet',$v['extract_price'])->update();
  535. }
  536. }else if(isset($data['transfer_batch']['batch_status']) && $data['transfer_batch']['batch_status']=='CLOSED'){
  537. $fail_msg=(isset($data['transfer_batch']['close_reason']) && $data['transfer_batch']['close_reason']!='')?$data['transfer_batch']['close_reason']:'系统超时未审核,已取消';
  538. Db::name('user_extract')->where('order_sn',$v['order_sn'])->update(['fail_msg'=>$fail_msg,'status_time'=>date('Y-m-d H:i:s'),'status'=>'-1']);
  539. if($v['withdrawal_type']==1){
  540. Db::name('user')->where('uid',$v['uid'])->inc('brokerage_price',$v['extract_price'])->update();
  541. }else{
  542. $eb_bill=Db::name('entropy_barter_bill')->where('order_no',$v['order_sn'])->where('pm',2)->find();
  543. Db::name('entropy_barter_bill')->where('id',$eb_bill['id'])->where('pm',2)->update(['status'=>3,'remarks'=>$fail_msg]);
  544. Db::name('entropy_barter_user_'.$v['mer_id'])->where('eb_id',$eb_bill['to_id'])->inc('wallet',$v['extract_price'])->update();
  545. }
  546. }
  547. }
  548. return true;
  549. }
  550. /**
  551. * 商家转账到零钱(公众号:程序员在囧途)
  552. * @param $batch_name
  553. * @param $out_trade_no
  554. * @param $money
  555. * @param $openid
  556. */
  557. private function transfer($batch_name, $out_trade_no, $money, $openid,$type=0)
  558. {
  559. $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
  560. $pars = [];
  561. if($type==1){
  562. $pars['appid'] = $this->wxapp['applet_app_id'];//直连商户的appid 小程序的
  563. }else{
  564. $pars['appid'] = $this->wxapp['app_id'];//直连商户的appid APP的
  565. }
  566. $pars['out_batch_no'] = $out_trade_no;//商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
  567. $pars['batch_name'] = $batch_name;//该笔批量转账的名称
  568. $pars['batch_remark'] = $batch_name;//转账说明,UTF8编码,最多允许32个字符
  569. $pars['total_amount'] = intval($money * 100);//转账总金额 单位为“分”
  570. $pars['total_num'] = 1;//转账总笔数
  571. $pars['transfer_detail_list'][0] = [
  572. 'out_detail_no' => 'transfer' . $out_trade_no,
  573. 'transfer_amount' => $pars['total_amount'],
  574. 'transfer_remark' => $batch_name,
  575. 'openid' => $openid
  576. ];//转账明细列表
  577. $token = $this->getToken($url,$pars);//获取token
  578. $res = $this->https_request($url, json_encode($pars), $token);//发送请求
  579. return json_decode($res, true);
  580. }
  581. /**
  582. * 通过商家批次单号查询批次单
  583. * @param $order_sn
  584. * @return mixed
  585. *
  586. * 【批次状态】 ACCEPTED:已受理。批次已受理成功,若发起批量转账的30分钟后,转账批次单仍处于该状态,可能原因是商户账户余额不足等。商户可查询账户资金流水,若该笔转账批次单的扣款已经发生,则表示批次已经进入转账中,请再次查单确认
  587. * PROCESSING:转账中。已开始处理批次内的转账明细单
  588. * FINISHED:已完成。批次内的所有转账明细单都已处理完成
  589. * CLOSED:已关闭。可查询具体的批次关闭原因确认
  590. */
  591. private function batch_id($order_sn)
  592. {
  593. $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/'.$order_sn.'?need_query_detail=false';
  594. $token = $this->getToken($url,null,'GET');//获取token
  595. $res = $this->https_request($url, null, $token);//发送请求
  596. return json_decode($res, true);
  597. }
  598. private function https_request($url, $data = null, $token)
  599. {
  600. $curl = curl_init();
  601. curl_setopt($curl, CURLOPT_URL, (string)$url);
  602. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  603. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  604. if (!empty($data)) {
  605. curl_setopt($curl, CURLOPT_POST, 1);
  606. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  607. }
  608. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  609. //添加请求头
  610. $headers = [
  611. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $token,
  612. 'Accept: application/json',
  613. 'Content-Type: application/json; charset=utf-8',
  614. 'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
  615. ];
  616. if (!empty($headers)) {
  617. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  618. }
  619. $output = curl_exec($curl);
  620. curl_close($curl);
  621. return $output;
  622. }
  623. private function getToken($url,$pars,$http_method='POST')
  624. {
  625. // $http_method = $http_method;//请求方法(GET,POST,PUT)
  626. $timestamp = time();//请求时间戳
  627. $url_parts = parse_url($url);//获取请求的绝对URL
  628. $nonce = $timestamp . rand('10000', '99999');//请求随机串
  629. if($pars){
  630. $body = json_encode((object)$pars);//请求报文主体
  631. }else{
  632. $body = '';
  633. }
  634. $stream_opts = [
  635. "ssl" => [
  636. "verify_peer" => false,
  637. "verify_peer_name" => false,
  638. ]
  639. ];
  640. // try {
  641. $filePath = App::getRootPath();
  642. $certPem= [
  643. 'certPem' => $filePath . $this->wxapp['certPem'],
  644. 'keyPem' => $filePath . $this->wxapp['keyPem'],
  645. ];
  646. $apiclient_cert_path = $certPem['certPem'];
  647. $apiclient_key_path = $certPem['keyPem'];
  648. $apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path, false, stream_context_create($stream_opts)));
  649. $serial_no = $apiclient_cert_arr['serialNumberHex'];//证书序列号
  650. $mch_private_key = file_get_contents($apiclient_key_path, false, stream_context_create($stream_opts));//密钥
  651. $merchant_id = $this->wxapp['mchid'];
  652. $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
  653. $message = $http_method . "\n" .
  654. $canonical_url . "\n" .
  655. $timestamp . "\n" .
  656. $nonce . "\n" .
  657. $body . "\n";
  658. openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
  659. $sign = base64_encode($raw_sign);//签名
  660. // $schema = 'WECHATPAY2-SHA256-RSA2048';
  661. $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
  662. $merchant_id, $nonce, $timestamp, $serial_no, $sign);//微信返回token
  663. // } catch (\Exception $e) {
  664. // throw new \Exception(['msg' => $e->getMessage()]);
  665. // }
  666. return $token;
  667. }
  668. /**
  669. * 企业付款到零钱
  670. * @param $id
  671. * @return array
  672. * @throws \think\db\exception\DataNotFoundException
  673. * @throws \think\db\exception\DbException
  674. * @throws \think\db\exception\ModelNotFoundException
  675. */
  676. public function WechatTransfers_qiye($id)
  677. {
  678. // $extract = $this->dao->getWhere(['extract_id' => $id]);
  679. $userdata=Db::name('user_extract')
  680. ->alias('u_e')
  681. ->leftJoin('user_extract_openid u_e_o','u_e.uid=u_e_o.uid')
  682. ->where(['extract_id' => $id,'extract_type'=>1])
  683. ->field('u_e.extract_price,u_e.uid,u_e_o.openid,u_e.service_money,u_e.withdrawal_type,u_e.order_sn')
  684. ->find();
  685. if($userdata and !empty($userdata)){
  686. $params["mch_appid"]='wxbf753066ac8686ba';
  687. $params["mchid"] = '1605734712';
  688. $params["nonce_str"]= mt_rand(100000000,999999999);
  689. $params["partner_trade_no"] = $userdata['order_sn'];
  690. if($userdata['withdrawal_type']==2){
  691. $params["amount"]= bcmul(bcsub($userdata['extract_price'],$userdata['service_money'],2),100);
  692. }else{
  693. $params["amount"]= bcmul(bcsub($userdata['extract_price'],$userdata['service_money'],2),100);
  694. }
  695. $params["desc"]= '提现到微信零钱';
  696. $params["openid"]= $userdata['openid'];
  697. $params["check_name"]= 'NO_CHECK';
  698. $sign =$this->getSign($params);
  699. //生成签名
  700. $params['sign'] = $sign;
  701. //构造XML数据
  702. $xmldata = $this->ToXml($params); //数组转XML
  703. $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
  704. //发送post请求
  705. $res = $this->curl_post_ssl($url, $xmldata,[]);
  706. // Db::name('log')->insert(['data'=>json_encode($res)]);
  707. if(!$res){
  708. return ['code'=>500,'msg'=>'请求未能发送成功'];
  709. }
  710. //付款结果分析
  711. $content = $this->FromXml($res); //xml转数组
  712. Db::name('log')->insert(['data'=>'【微信提现返回】'.json_encode($content)]);
  713. if($content['result_code']=="SUCCESS"){
  714. $this->WechatTransfers_query($userdata['order_sn']);
  715. return ['code'=>200,'msg'=>'成功'];
  716. }else{
  717. // Db::name('log')->insert(['data'=>'else进来了']);
  718. Db::name('user_extract')->where('order_sn',$userdata['order_sn'])->update(['fail_msg'=>$content['err_code_des'],'status_time'=>date('Y-m-d H:i:s'),'status'=>'-1']);
  719. $extract_data=Db::name('user_extract')->where('order_sn',$userdata['order_sn'])->find();
  720. if($extract_data['withdrawal_type']==1){
  721. Db::name('user')->where('uid',$extract_data['uid'])->inc('brokerage_price',$extract_data['extract_price'])->update();
  722. }else{
  723. $eb_bill=Db::name('entropy_barter_bill')->where('order_no',$extract_data['order_sn'])->where('pm',2)->find();
  724. Db::name('entropy_barter_bill')->where('id',$eb_bill['id'])->where('pm',2)->update(['status'=>3,'remarks'=>$content['err_code_des']]);
  725. Db::name('entropy_barter_user_'.$extract_data['mer_id'])->where('eb_id',$eb_bill['to_id'])->inc('wallet',$extract_data['extract_price'])->update();
  726. }
  727. return ['code'=>201,'msg'=>$content['err_code_des']];
  728. }
  729. }else{
  730. return ['code'=>200,'msg'=>'未找到数据'];
  731. }
  732. }
  733. //付款到零钱查询
  734. public function WechatTransfers_query($order_sn)
  735. {
  736. // Db::name('log')->insert(['data'=>'微信提现查询走进来了']);
  737. $params["appid"]='wxbf753066ac8686ba';
  738. $params["mch_id"] = '1605734712';
  739. $params["nonce_str"]= mt_rand(100000000,999999999);
  740. $params["partner_trade_no"]= $order_sn;
  741. $sign =$this->getSign($params);
  742. //生成签名
  743. $params['sign'] = $sign;
  744. //构造XML数据
  745. $xmldata = $this->ToXml($params); //数组转XML
  746. $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
  747. //发送post请求
  748. $res = $this->curl_post_ssl($url, $xmldata,[]);
  749. if(!$res){
  750. return ['code'=>500,'msg'=>'请求未能发送成功'];
  751. }
  752. //付款结果分析
  753. $content = $this->FromXml($res); //xml转数组
  754. // Db::name('log')->insert(['data'=>'查询'.json_encode($content)]);
  755. try {
  756. if($content['status']=='SUCCESS'){
  757. Db::name('entropy_barter_bill')->where('order_no',$content['partner_trade_no'])->where('pm',2)->update(['status'=>2,'remarks'=>'已到账']);
  758. Db::name('user_extract')->where('order_sn',$content['partner_trade_no'])->update(['status'=>1]);
  759. //$bill=Db::name('entropy_barter_bill')->where('order_no',$content['partner_trade_no'])->where('pm',2)->find();
  760. //$mer_id=Db::name('user_extract')->where('order_sn',$content['partner_trade_no'])->value('mer_id');
  761. //Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$bill['to_id'])->dec('wallet',$bill['number'])->update();
  762. }
  763. }catch (\Exception $e) {
  764. Db::name('log')->insert(['data'=>'提现到微信查询出错:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  765. }
  766. }
  767. /**
  768. * @remarks 支付宝付款到零钱
  769. * @author Tang
  770. * @created 2021/9/9 12:59
  771. */
  772. public function AliTransfers($id)
  773. {
  774. try {
  775. $userdata=Db::name('user_extract')
  776. ->where(['extract_id'=>$id])
  777. ->find();
  778. $identity=Db::name('user')->where('uid',$userdata['uid'])->value('ali_user_id');
  779. if($userdata and !empty($userdata)){
  780. $app_cert_path=dirname(__FILE__). '/../../../../extend/alipay/aop/cert/appCertPublicKey.crt';
  781. $alipay_cert_path=dirname(__FILE__). '/../../../../extend/alipay/aop/cert/alipayCertPublicKey.crt';
  782. $alipay_root_cert_path=dirname(__FILE__). '/../../../../extend/alipay/aop/cert/alipayRootCert.crt';
  783. $url = "https://openapi.alipay.com/gateway.do";
  784. $app_id="2021002194632049";
  785. $privateKey="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDGxceDFVT6VTYClinyAdY4PxE6MadNYLZ3sbLOLV14e39kuu1lkKDBZ/KPRNruPlSPGdiRVe+L8v4rsVk6bthsS/vMk5aH1wQyt58/HvHmZ5QVo3U5UD7cgqqefTcRJjKhjjM1rU/OrOkOqyssaLRukW3jeKRlBccUgtaGsXXrbMxWYpzl1OMDhKRRUVxu3LTfqlMpHghOasQrgyD92q9wlUN7U6hBYzGUocO/N3sz97PZwptwWAW0r3CFdlLCpgnMr+xb/Zh8td1OKGl3wEghiGavgDDlc2+w/xtk1SZ3pJbnGAU+D+oyJ6r76po6vkyGIlLrKp/zQ50az7fObzgNAgMBAAECggEBAK60szERlMkaEAih92gQGTsCo5+keMnX0UjO1LvGv7rfwQ7GBVYZklGrjVRDTrHL8wJ70w+amptFDgVVMXncZzh4aG5c4heu8q+pGLyf9DlwnIWQT736O+YQqun2YGogSqwYkgYBuH2NGKKd66pytPPhutYN+WAZfesXPHrSizRJtZMfuZvvGzm6ZJLZ1LmhvkOuLrX4xrJGRs1nAwZOmOfi4MY5VASblfPj59fUim9hErocNaX4uiMVwmhQl2QuOM/EbTd1UkW+dkV/gyQMOFzpcfN+SEi+hdLN+JiR9TVEcwity6ihOG5+3X33GKzfY4WFxeHHUaqA0SQWSkKofMECgYEA5OF5Imtq8/6JjWJ0jvYCpUnj9742McGJZTWBoTn9Hp0d3bwyE3YbH8pBVJyvzlfPKB9TUjE2FzVZvQtxEu+ZxToccp/LPqlgITWNHCmEtzAgXW7ww2PafgDZJ2QRSyjLUeRIWSAkeilj64XMCEC/bEs/nb5nmUb2gnsNTvvpMLUCgYEA3lMMtaIZH1fMI4XMNaPFzeRiKc0FIcLEem/efnPetPuOCujIWQJ/x9cCDCcfqO59I1FXQuiC/E1dFvme2qs/H7h2bFu8RG0M/ZQWd9twg31Sh+R7gCQakFERag8HXc4AWfBGXCvMu1SmSovU49KecSLxYGOAUdp77j1GxtLiePkCgYAQjc45ExPVOd+w/iH4BQsD8Wgvju5Ugu7xBwGtK4iHIk36r5XfmnczwWkgOYTto6Qz/rAEDdMfspge+CjhebrhbOaJ08zbZ1ywijL+inIy3aQ5ncCEIUC4RE9wbehiSz0JWz1wVMbOnxe+/RpYrnYp2vdq7U6I430IyuWXhNjE0QKBgEtJUmd7oghEIgPxfiq37/b+HOPM4q0BGKnhkjW+JS+idAJru9SwjPbub2oQIZ+CS2wXUnZ86EZqKFjsZQflr7WdMLUXB5UgXKLOcCFcdSiB2t3b2JqXH0u5uCzzM+pnJSVPwXPa/TR+wOVDnu8bVr43OAgNCdouI4OUyo/vv/N5AoGATJaIvpsZ9v1zmhbtJbgPBM9dhD2PIp2ufZ7yvhGGzA02FqAbyAeVsI2d+W+uA6jJ1X87x27pQv//mFoFXStxX3/bhdgTWbUYEWobAhw1jBS1rMQfLuBGGI/LsHGhzau3U+59erC5sYhGgZKSdXDyjOduWQlPWAv6ufkNZ+I3FCU=";
  786. // 证书模式的测试
  787. $alipayConfig = new AlipayConfig();
  788. $alipayConfig->setAppId($app_id);
  789. $alipayConfig->setCharset("utf-8");
  790. $alipayConfig->setFormat("json");
  791. $alipayConfig->setSignType("RSA2");
  792. $alipayConfig->setServerUrl($url);
  793. $alipayConfig->setPrivateKey($privateKey);
  794. // content 和 path 只需要设置一个即可
  795. $alipayConfig->setAppCertPath($app_cert_path);
  796. //$alipayConfig->setAppCertContent()
  797. $alipayConfig->setAlipayPublicCertPath($alipay_cert_path);
  798. //$alipayConfig->setAlipayPublicCertContent()
  799. $alipayConfig->setRootCertPath($alipay_root_cert_path);
  800. //$alipayConfig->setRootCertContent()
  801. $aop = new AopCertClient($alipayConfig);
  802. $aop->isCheckAlipayPublicCert = true;
  803. $order_title='提现';
  804. if($userdata['withdrawal_type']==1){
  805. $order_title='储蓄保提现';
  806. $userdata['extract_price']=bcsub($userdata['extract_price'],$userdata['service_money']);
  807. }elseif($userdata['withdrawal_type']==2){
  808. $mer_name=Db::name('merchant')->where('mer_id',$userdata['mer_id'])->value('mer_name');
  809. $order_title='储蓄保-'.$mer_name.'提现';
  810. $userdata['extract_price']=bcsub($userdata['extract_price'],$userdata['service_money']);
  811. }
  812. // Db::name('log')->insert(['data'=>'AopCertClient']);
  813. $parameter = "{".
  814. "\"out_biz_no\":\"".$userdata['order_sn']."\",".
  815. "\"remark\":\"提现到支付宝\",".
  816. // "\"business_params\"":.json_encode(['payer_show_name_use_alias'=>'北京天地博爱网络科技有限公司']).
  817. "\"business_params\":\"{'payer_show_name_use_alias':'北京天地博爱网络科技有限公司'}\",".
  818. "\"biz_scene\":\"DIRECT_TRANSFER\",".
  819. "\"payee_info\":{".
  820. "\"identity\":\"".$identity."\",".
  821. "\"identity_type\":\"ALIPAY_USER_ID\",".
  822. // "\"name\":\"".$userdata['real_name']."\"".
  823. "},".
  824. "\"trans_amount\":\"".$userdata['extract_price']."\",".
  825. "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
  826. "\"order_title\":\"".$order_title."\"".
  827. "}";
  828. $request = new AlipayFundTransUniTransferRequest ();
  829. $request->setBizContent($parameter);
  830. $response = $aop->execute($request);
  831. Db::name('log')->insert(['data'=>'【支付宝提现返回结果】'.$response]);
  832. $response=json_decode($response,true);
  833. if($response['alipay_fund_trans_uni_transfer_response']['code']=='10000'){
  834. return ['code'=>200,'msg'=>'成功'];
  835. }else{
  836. return ['code'=>201,'msg'=>$response['alipay_fund_trans_uni_transfer_response']['sub_msg']];
  837. }
  838. }else{
  839. return ['code'=>200,'msg'=>'未找到数据'];
  840. }
  841. }catch (Exception $exception){
  842. Db::name("log")->insert(array('data'=>$exception->getFile().':'.$exception->getLine().'【'.$exception->getMessage().'】'));//日志记录
  843. }
  844. }
  845. /*
  846. * Effect 微信生成签名
  847. * author tangshiwei
  848. * time 2021-09-09
  849. * */
  850. public function getSign($params) {
  851. ksort($params); //将参数数组按照参数名ASCII码从小到大排序
  852. foreach ($params as $key => $item) {
  853. if (!empty($item)) {
  854. //剔除参数值为空的参数
  855. $newArr[] = $key.'='.$item; // 整合新的参数数组
  856. }
  857. }
  858. $stringA = implode("&", $newArr); //使用 & 符号连接参数
  859. $stringSignTemp = $stringA."&key=70yfchPMifQCESGSFr53vOZmUJZ8Srql";//拼接key
  860. // key是在商户平台API安全里自己设置的
  861. $stringSignTemp = md5($stringSignTemp); //将字符串进行MD5加密
  862. $sign = strtoupper($stringSignTemp); //将所有字符转换为大写
  863. return $sign;
  864. }
  865. /**
  866. * @remarks curl 请求
  867. * @author Tang
  868. * @created 2021/9/9 13:14
  869. */
  870. function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
  871. $dirs= App::getRootPath();
  872. $ch = curl_init();//初始化curl
  873. curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
  874. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  875. curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
  876. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
  877. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
  878. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
  879. curl_setopt($ch, CURLOPT_SSLCERT, $dirs.'pkey/cert/apiclient_cert.pem');//证书位置
  880. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
  881. curl_setopt($ch, CURLOPT_SSLKEY, $dirs.'pkey/cert/apiclient_key.pem');//证书位置
  882. if (count($aHeader) >= 1) {
  883. curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
  884. }
  885. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  886. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送
  887. $data = curl_exec($ch);//执行回话
  888. if ($data) {
  889. curl_close($ch);
  890. return $data;
  891. } else {
  892. $error = curl_errno($ch);
  893. echo "call faild, errorCode:$error\n";
  894. curl_close($ch);
  895. return false;
  896. }
  897. }
  898. /**
  899. * @remarks 数组转xml
  900. * @author Tang
  901. * @created 2021/9/9 13:14
  902. */
  903. public function ToXml($data=array())
  904. {
  905. if(!is_array($data) || count($data) <= 0)
  906. {
  907. return '数组异常';
  908. }
  909. $xml = "<xml>";
  910. foreach ($data as $key=>$val)
  911. {
  912. if (is_numeric($val)){
  913. $xml.="<".$key.">".$val."</".$key.">";
  914. }else{
  915. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  916. }
  917. }
  918. $xml.="</xml>";
  919. return $xml;
  920. }
  921. /**
  922. * @remarks xml转数组
  923. * @author Tang
  924. * @created 2021/9/9 13:15
  925. */
  926. public function FromXml($xml)
  927. {
  928. if(!$xml){
  929. echo "xml数据异常!";
  930. }
  931. //将XML转为array
  932. //禁止引用外部xml实体
  933. libxml_disable_entity_loader(true);
  934. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  935. return $data;
  936. }
  937. public function band_account($type,$account,$info,$uid){
  938. $accountinfo=Db::name("user_withdraw_account")->where(["account_type"=>$type,"user_id"=>$uid])->find();
  939. $data=[
  940. "account_type"=>$type,
  941. "account"=>$account,
  942. "info"=>$info,
  943. 'update_time'=>date("Y-m-d H:i:s"),
  944. ];
  945. if ($accountinfo){
  946. Db::name("user_withdraw_account")->where("id",$accountinfo["id"])->update($data);
  947. }else{
  948. $data["user_id"]=$uid;
  949. $data["create_time"]=date("Y-m-d H:i:s");
  950. Db::name("user_withdraw_account")->insert($data);
  951. }
  952. return ["msg"=>'绑定成功'];
  953. }
  954. public function band_openid($openid,$uid){
  955. $user_data["wechat_user_id"]=$openid;
  956. $user_count=Db::name('user')->where($user_data)->count();
  957. if($user_count>0){
  958. return true;
  959. }
  960. $data["openid"]=$openid;
  961. $count=Db::name("user_extract_openid")->where($data)->count();
  962. if($count>0){
  963. return false;
  964. }
  965. $data["uid"]=$uid;
  966. $wx_type=input("wx_type/d",0);
  967. $data["type"]=$wx_type==1?1:0;
  968. Db::name("user_extract_openid")->insert($data);
  969. return true;
  970. // return ["msg"=>'绑定成功'];
  971. }
  972. /*获取绑卡账户*/
  973. public function band_account_info($type,$uid){
  974. if($type=='1'){
  975. $res=Db::name('user_extract_openid')->where('uid',$uid)->value('openid');
  976. if(!$res){
  977. return false;
  978. }
  979. return true;
  980. }
  981. $res = Db::name('user') -> where('uid',$uid) -> value('ali_user_id');
  982. if($res && $res != '0' && $res != ''){
  983. return true;
  984. }
  985. return false;
  986. // $account=Db::name("user_withdraw_account")->where(["account_type"=>$type,"user_id"=>$uid])->find();//
  987. //
  988. // if ($account){
  989. // if($account['account_type'] ==3){
  990. // $account['icon'] = '';
  991. // }
  992. // return $account;
  993. // }else{
  994. // return false;
  995. // }
  996. }
  997. public function listBank($where)
  998. {
  999. return Db::name("user_withdraw_account")->where($where)->order('id desc')->select();
  1000. }
  1001. /*添加银行卡*/
  1002. public function createBank($data){
  1003. $data["account_type"] = 3;
  1004. $data["info"] = $data['user_name'];
  1005. $real_name = $this->real_info($data['user_id']);
  1006. if(!$real_name){
  1007. return ['code'=>400,'msg'=>'请先实名认证'];
  1008. }
  1009. if($real_name['user_name'] != $data['user_name']){
  1010. return ['code'=>400,'msg'=>'认证信息与银行卡绑定用户不符'];
  1011. }
  1012. if(Db::name("user_withdraw_account")->where(['user_id'=>$data['user_id'],'account_type'=>3,'status'=>0])->value("id")){
  1013. //操作修改
  1014. $data['update_time'] = date("Y-m-d H:i:s");
  1015. Db::name("user_withdraw_account")->where(['user_id'=>$data['user_id'],'account_type'=>3,'status'=>0])->update($data);
  1016. return Db::name("user_withdraw_account")->where(['user_id'=>$data['user_id'],'account_type'=>3,'status'=>0])->value("id");
  1017. // return ['code'=>400,'msg'=>'只能绑定一张银行卡'];
  1018. }
  1019. $data['create_time'] = date("Y-m-d H:i:s");
  1020. return Db::name("user_withdraw_account")->insertGetId($data);
  1021. }
  1022. /*修改银行卡*/
  1023. public function saveBank($where=[],$data=[]){
  1024. $data['update_time'] = date("Y-m-d H:i:s");
  1025. $res = Db::name("user_withdraw_account")->where($where)->update($data);
  1026. return $res;
  1027. }
  1028. /*银行卡列表*/
  1029. public function list($where=[]){
  1030. $res = Db::name("user_withdraw_account")->where($where)->select();
  1031. return $res;
  1032. }
  1033. /*用户认证信息*/
  1034. public function real_info($uid){
  1035. $data = Db::name("user_certification")->where(["uid"=>$uid,'status'=>1])->find();
  1036. return $data;
  1037. }
  1038. }