Wap.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\index\controller;
  14. use controller\BasicWechat;
  15. use service\DataService;
  16. use service\PayService;
  17. class Wap extends BasicWechat
  18. {
  19. /**
  20. * 网页授权DEMO
  21. */
  22. public function index()
  23. {
  24. dump($this->oAuth(true)); // 获取用户详情信息
  25. dump($this->fansinfo); // 输出用户信息
  26. }
  27. /**
  28. * 微信二维码支付DEMO
  29. * @return \think\response\Json|\think\response\View
  30. */
  31. public function payqrc()
  32. {
  33. $method = '_payqrc_' . strtolower($this->request->get('action'));
  34. if (method_exists($this, $method)) {
  35. return $this->$method();
  36. }
  37. return view();
  38. }
  39. /**
  40. * 获取二维码支付
  41. * @return \think\response\Json
  42. */
  43. protected function _payqrc_payqrc()
  44. {
  45. list($pay, $order_no) = [load_wechat('pay'), session('pay-test-order-no')];
  46. if (empty($order_no)) {
  47. $order_no = DataService::createSequence(10, 'wechat-pay-test');
  48. session('pay-test-order-no', $order_no);
  49. }
  50. if (PayService::isPay($order_no)) {
  51. return json(['code' => 2, 'order_no' => $order_no]);
  52. }
  53. $url = PayService::createWechatPayQrc($pay, $order_no, 1, '微信扫码支付测试!');
  54. if ($url !== false) {
  55. return json(['code' => 1, 'url' => $url, 'order_no' => $order_no]);
  56. }
  57. $this->error("生成支付二维码失败,{$pay->errMsg}[{$pay->errCode}]");
  58. }
  59. /**
  60. * 重置测试订单号
  61. */
  62. protected function _payqrc_reset()
  63. {
  64. session('pay-test-order-no', null);
  65. }
  66. /**
  67. * 微信JSAPI支付DEMO
  68. * @return \think\response\Json|\think\response\View
  69. */
  70. public function payjs()
  71. {
  72. $this->openid = $this->oAuth(false);
  73. $method = '_payjs_' . strtolower($this->request->get('action'));
  74. if (method_exists($this, $method)) {
  75. return $this->$method();
  76. }
  77. return view();
  78. }
  79. /**
  80. * 获取JSAPI支付参数
  81. * @return \think\response\Json
  82. */
  83. protected function _payjs_options()
  84. {
  85. $order_no = session('pay-test-order-no');
  86. if (empty($order_no)) {
  87. $order_no = DataService::createSequence(10, 'wechat-pay-test');
  88. session('pay-test-order-no', $order_no);
  89. }
  90. if (PayService::isPay($order_no)) {
  91. return json(['code' => 2, 'order_no' => $order_no]);
  92. }
  93. $pay = load_wechat('pay');
  94. $options = PayService::createWechatPayJsPicker($pay, $this->openid, $order_no, 1, 'JSAPI支付测试');
  95. if ($options === false) {
  96. $options = ['code' => 3, 'msg' => "创建支付失败,{$pay->errMsg}[$pay->errCode]"];
  97. }
  98. $options['order_no'] = $order_no;
  99. return json($options);
  100. }
  101. /**
  102. * 重置测试订单号
  103. */
  104. protected function _payjs_reset()
  105. {
  106. session('pay-test-order-no', null);
  107. }
  108. }