Index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Db;
  5. class Index extends Frontend
  6. {
  7. protected $noNeedLogin = '*';
  8. protected $noNeedRight = '*';
  9. protected $layout = '';
  10. public function index()
  11. {
  12. return $this->view->fetch();
  13. }
  14. public function login()
  15. {
  16. $id=input('id',1);
  17. if(!$id){
  18. $this->error(__('邀请码不对'));
  19. }
  20. $appid = 'wx2a2d24fbe2383604';
  21. // $cmurl = "jy.chengwuwa.cn/index/index/gzhop/id/".$id;//接口地址
  22. $cmurl = "ts.shanghairongxuan.top/index/index/gzhop/id/".$id;//接口地址
  23. #你的公众号appid
  24. $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=https%3a%2f%2f".$cmurl."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  25. //$url='/login.html';
  26. #redirect_uri改为你的网页授权域名和刚刚跳转到的显示页面,比如我的是getinfoDetail.php
  27. header('location:'.$url);
  28. }
  29. public function gzhop()
  30. {
  31. $ids=input('id',1);
  32. if(!$ids){
  33. $this->error(__('邀请码不对'));
  34. }
  35. $appid = "wx2a2d24fbe2383604";
  36. $secret = "07b59b4bac2f17edc9e121e3dba6d0b3";
  37. $cmurl = "https://jy.chengwuwa.cn/d/index.html";//前端地址
  38. $code = $_GET["code"];
  39. $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
  40. $ch = curl_init();
  41. curl_setopt($ch,CURLOPT_URL,$get_token_url);
  42. curl_setopt($ch,CURLOPT_HEADER,0);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  44. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  45. $res = curl_exec($ch);
  46. curl_close($ch);
  47. $json_obj = json_decode($res,true);
  48. //根据openid和access_token查询用户信息
  49. $access_token = $json_obj['access_token'];
  50. $openid = $json_obj['openid'];
  51. $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
  52. $ch = curl_init();
  53. curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
  54. curl_setopt($ch,CURLOPT_HEADER,0);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  56. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  57. $res = curl_exec($ch);
  58. curl_close($ch);
  59. //解析json
  60. $user_obj = json_decode($res,true);
  61. $openid=isset($user_obj['openid'])?$user_obj['openid']:null;
  62. $unionid=isset($user_obj['unionid'])?$user_obj['unionid']:null;
  63. $nikname=isset($user_obj['nickname'])?$user_obj['nickname']:null;
  64. $headimgurl=isset($user_obj['headimgurl'])?$user_obj['headimgurl']:null;
  65. $userfxid=Db::name('userfxid')->where('unionid',$unionid)->find();
  66. if($userfxid){
  67. Db::name('userfxid')->where('unionid',$unionid)->update(['fid'=>$ids,'openid'=>$openid,'unionid'=>$unionid]);
  68. }else{
  69. Db::name('userfxid')->insertGetId(['fid'=>$ids,'openid'=>$openid,'unionid'=>$unionid]);
  70. }
  71. $urls=$cmurl.'?iswx=1';
  72. header('location:'.$urls);
  73. }
  74. public function pay()
  75. {
  76. $site=config('site');
  77. require_once $_SERVER['DOCUMENT_ROOT']."/wxpay/lib/WxPay.Api.php";
  78. require_once $_SERVER['DOCUMENT_ROOT']."/wxpay/example/WxPay.JsApiPay.php";
  79. require_once $_SERVER['DOCUMENT_ROOT']."/wxpay/example/WxPay.Config.php";
  80. require_once $_SERVER['DOCUMENT_ROOT']."/wxpay/example/log.php";
  81. //初始化日志
  82. $logHandler= new \CLogFileHandler($_SERVER['DOCUMENT_ROOT']."/wxpay/logs/".date('Y-m-d').'.log');
  83. $log = \Log::Init($logHandler, 15);
  84. //打印输出数组信息
  85. function printf_info($data)
  86. {
  87. foreach($data as $key=>$value){
  88. echo "<font color='#00ff55;'>$key</font> : ".htmlspecialchars($value, ENT_QUOTES)." <br/>";
  89. }
  90. }
  91. $data=[];
  92. $jsApiParameters=[];
  93. $editAddress=[];
  94. if(input('id',0)>0){
  95. $data=Db::name('paylog')->where('id',input('id'))->find();
  96. if($data){
  97. if($data['type']==2){
  98. $this->error(__('已经购买'));
  99. }
  100. }else{
  101. $this->error(__('查询补单订单信息'));
  102. }
  103. $data['member']=Db::name('user')->where('id',$data['uid'])->find();
  104. }
  105. $notifyurl=$site['imgurl'].'/api/paywx/notifwxh5';
  106. $Total=isset($data['amount'])?$data['amount']*100:0.01;
  107. $data['type']=isset($data['type'])?$data['type']:0;
  108. if($data['type']==1){
  109. try{
  110. $tools = new \JsApiPay();
  111. $openId = $tools->GetOpenid();
  112. $jsApiParameters=[];
  113. $editAddress=[];
  114. if($openId){
  115. //②、统一下单
  116. $input = new \WxPayUnifiedOrder();
  117. $input->SetBody("商城订单");
  118. $input->SetAttach("商城订单");
  119. $input->SetOut_trade_no($data['out_trade_no']);
  120. $input->SetTotal_fee($Total);
  121. $input->SetTime_start(date("YmdHis"));
  122. $input->SetTime_expire(date("YmdHis", time() + 600));
  123. $input->SetGoods_tag("商城订单");
  124. $input->SetNotify_url($notifyurl);
  125. $input->SetTrade_type("JSAPI");
  126. $input->SetOpenid($openId);
  127. $config = new \WxPayConfig();
  128. $order = \WxPayApi::unifiedOrder($config, $input);
  129. //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
  130. //printf_info($order);
  131. $jsApiParameters = $tools->GetJsApiParameters($order);
  132. //获取共享收货地址js函数参数
  133. $editAddress = $tools->GetEditAddressParameters();
  134. }
  135. } catch(Exception $e) {
  136. //Log::ERROR(json_encode($e));
  137. }
  138. }else{
  139. }
  140. $zt=['1'=>'未付款','2'=>'已付款'];
  141. $this->assign('jsApiParameters', $jsApiParameters);
  142. $this->assign('editAddress', $editAddress);
  143. $this->assign('data', $data);
  144. $this->assign('url',$site['imgurl']);
  145. $this->assign('zt', $zt);
  146. //var_dump($data);
  147. if($jsApiParameters and $data){
  148. return $this->fetch();
  149. }else{
  150. //header('Location: '.$data['url'].'/index.php/shop/pay/index?id='.input('id').'&');
  151. }
  152. }
  153. }