Passport.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\utils\Easywechat\MiniAppService;
  5. use app\utils\PayUtil;
  6. use think\Cache;
  7. /**
  8. * 通行证
  9. */
  10. class Passport extends Api
  11. {
  12. protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay'];
  13. protected $noNeedRight = '*';
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 微信小程序手机号授权
  21. * @return void
  22. */
  23. public function loginWxMiniPhone()
  24. {
  25. // 获取参数
  26. $params = $this->request->param();
  27. if (empty($params['code'])) {
  28. $this->error('The code is required.');
  29. }
  30. $wxMiniApp = new MiniAppService();
  31. $wx = $wxMiniApp->getUserPhone($params['code']);
  32. if (empty($wx['phone_info']['purePhoneNumber'])) {
  33. $this->error('手机号授权失败.', $wx);
  34. }
  35. $mobile = $wx['phone_info']['purePhoneNumber'];
  36. $extend = [];
  37. if (!empty($params['openid']) && $wxInfo = Cache::get($params['openid'])){
  38. $extend['mini_openid'] = $wxInfo['openid'] ?? '';
  39. $extend['mini_sessionkey'] = $wxInfo['session_key'] ?? '';
  40. }
  41. // 校验手机号登录信息
  42. list($exists, $user_id) = UserModel::checkExists('', $mobile);
  43. if (!$exists) {
  44. // 手机号不存在 创建账号
  45. $ret = $this->auth->register('1', '1', '', $mobile, $extend);
  46. if (!$ret) {
  47. $this->error("注册失败!");
  48. }
  49. $user_id = $this->auth->id;
  50. }
  51. // 写入登录Cookies和Token
  52. if (!$this->auth->direct($user_id,$extend)) {
  53. $this->error($this->auth->getError());
  54. }
  55. $userInfo = $this->auth->getUserinfo();
  56. $result = [
  57. 'token' => $userInfo['token'],
  58. // 'userInfo' => $userInfo
  59. ];
  60. $this->success('登录成功', $result);
  61. }
  62. /**
  63. * 微信小程序授权
  64. *
  65. * @return void
  66. */
  67. public function loginWxMini()
  68. {
  69. // 获取参数
  70. $params = $this->request->param();
  71. if (empty($params['code'])) {
  72. $this->error('The code is required.');
  73. }
  74. $wxMiniApp = new MiniAppService();
  75. $res = $wxMiniApp->login($params['code']);
  76. if (empty($res['openid'])) {
  77. $this->error('授权失败,请重试');
  78. }
  79. $sign = md5($res['openid']);
  80. Cache::set($sign, $res, 3600);
  81. $this->success('success', [
  82. 'openid' => $sign
  83. ]);
  84. }
  85. /**
  86. * 测试 汇付 支付
  87. */
  88. public function testPay()
  89. {
  90. $params = \request()->post();
  91. $order_no = $params['order_no'] ?? '';
  92. if ($params['openid'] != 9696){
  93. $wxInfo = Cache::get($params['openid'] ?? '');
  94. $openid = $wxInfo['openid'] ?? '';
  95. // $sessionKey = $wxInfo['session_key'] ?? '';
  96. }else{
  97. $openid = 'ol8qS68vKSgWJ3Unrgfyi3rkakcQ';
  98. }
  99. $order_no = !empty($order_no) ? $order_no : time() . rand(1, 200);
  100. $pay = new PayUtil();
  101. if (!$pay->jsPay($openid, "D0{$order_no}", '0.01', '开通会员')){
  102. $this->error($pay->getMessage());
  103. }
  104. $res = $pay->getData();
  105. if (empty($res['data']['pay_info']) || !$pay_info = json_decode($res['data']['pay_info'],true)){
  106. $this->error('支付信息有误');
  107. }
  108. $this->success('success', [
  109. 'pay_info' => $pay_info,
  110. 'order_no' => $order_no
  111. ]);
  112. }
  113. /**
  114. * 支付回调
  115. * @return void
  116. */
  117. public function pay_notify()
  118. {
  119. // $params = \request()->post();
  120. //
  121. // $params = json_encode($params,JSON_UNESCAPED_UNICODE);
  122. $this->notify_log_start('hf_pay');
  123. }
  124. //异步日志
  125. private function notify_log_start($paytype = 'wechat')
  126. {
  127. //记录支付回调数据
  128. ignore_user_abort(); // run script in background
  129. set_time_limit(30);
  130. // 日志文件 start
  131. $log_base_dir = '../paylog/' . $paytype . '/';
  132. if (!is_dir($log_base_dir)) {
  133. mkdir($log_base_dir, 0770, true);
  134. @chmod($log_base_dir, 0770);
  135. }
  136. $notify_file = $log_base_dir . 'notify.txt';
  137. if (!file_exists($notify_file)) {
  138. @touch($notify_file);
  139. @chmod($notify_file, 0770);
  140. }
  141. if (filesize($notify_file) > 5242880)//大于5M自动切换
  142. {
  143. rename($notify_file, $log_base_dir . 'notify_' . date('Y_m_d_H_i_s') . '.txt');
  144. }
  145. if (!file_exists($notify_file)) {
  146. @touch($notify_file);
  147. @chmod($notify_file, 0770);
  148. }
  149. // 日志文件 end
  150. //开始写入
  151. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  152. if ($_REQUEST && $paytype == 'alipay') {
  153. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收request]" . json_encode($_REQUEST), FILE_APPEND);
  154. } else {
  155. $xml = file_get_contents("php://input");
  156. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流原始数据] \n" . $xml, FILE_APPEND);
  157. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  158. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流] " . json_encode($xmlObj), FILE_APPEND);
  159. }
  160. ini_set('display_errors', 'On');
  161. return $notify_file;
  162. }
  163. }