123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\utils\Easywechat\MiniAppService;
- use app\utils\PayUtil;
- use think\Cache;
- /**
- * 通行证
- */
- class Passport extends Api
- {
- protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay'];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 微信小程序手机号授权
- * @return void
- */
- public function loginWxMiniPhone()
- {
- // 获取参数
- $params = $this->request->param();
- if (empty($params['code'])) {
- $this->error('The code is required.');
- }
- $wxMiniApp = new MiniAppService();
- $wx = $wxMiniApp->getUserPhone($params['code']);
- if (empty($wx['phone_info']['purePhoneNumber'])) {
- $this->error('手机号授权失败.', $wx);
- }
- $mobile = $wx['phone_info']['purePhoneNumber'];
- $extend = [];
- if (!empty($params['openid']) && $wxInfo = Cache::get($params['openid'])){
- $extend['mini_openid'] = $wxInfo['openid'] ?? '';
- $extend['mini_sessionkey'] = $wxInfo['session_key'] ?? '';
- }
- // 校验手机号登录信息
- list($exists, $user_id) = UserModel::checkExists('', $mobile);
- if (!$exists) {
- // 手机号不存在 创建账号
- $ret = $this->auth->register('1', '1', '', $mobile, $extend);
- if (!$ret) {
- $this->error("注册失败!");
- }
- $user_id = $this->auth->id;
- }
- // 写入登录Cookies和Token
- if (!$this->auth->direct($user_id,$extend)) {
- $this->error($this->auth->getError());
- }
- $userInfo = $this->auth->getUserinfo();
- $result = [
- 'token' => $userInfo['token'],
- // 'userInfo' => $userInfo
- ];
- $this->success('登录成功', $result);
- }
- /**
- * 微信小程序授权
- *
- * @return void
- */
- public function loginWxMini()
- {
- // 获取参数
- $params = $this->request->param();
- if (empty($params['code'])) {
- $this->error('The code is required.');
- }
- $wxMiniApp = new MiniAppService();
- $res = $wxMiniApp->login($params['code']);
- if (empty($res['openid'])) {
- $this->error('授权失败,请重试');
- }
- $sign = md5($res['openid']);
- Cache::set($sign, $res, 3600);
- $this->success('success', [
- 'openid' => $sign
- ]);
- }
- /**
- * 测试 汇付 支付
- */
- public function testPay()
- {
- $params = \request()->post();
- $order_no = $params['order_no'] ?? '';
- if ($params['openid'] != 9696){
- $wxInfo = Cache::get($params['openid'] ?? '');
- $openid = $wxInfo['openid'] ?? '';
- // $sessionKey = $wxInfo['session_key'] ?? '';
- }else{
- $openid = 'ol8qS68vKSgWJ3Unrgfyi3rkakcQ';
- }
- $order_no = !empty($order_no) ? $order_no : time() . rand(1, 200);
- $pay = new PayUtil();
- if (!$pay->jsPay($openid, "D0{$order_no}", '0.01', '开通会员')){
- $this->error($pay->getMessage());
- }
- $res = $pay->getData();
- if (empty($res['data']['pay_info']) || !$pay_info = json_decode($res['data']['pay_info'],true)){
- $this->error('支付信息有误');
- }
- $this->success('success', [
- 'pay_info' => $pay_info,
- 'order_no' => $order_no
- ]);
- }
- /**
- * 支付回调
- * @return void
- */
- public function pay_notify()
- {
- // $params = \request()->post();
- //
- // $params = json_encode($params,JSON_UNESCAPED_UNICODE);
- $this->notify_log_start('hf_pay');
- }
- //异步日志
- private function notify_log_start($paytype = 'wechat')
- {
- //记录支付回调数据
- ignore_user_abort(); // run script in background
- set_time_limit(30);
- // 日志文件 start
- $log_base_dir = '../paylog/' . $paytype . '/';
- if (!is_dir($log_base_dir)) {
- mkdir($log_base_dir, 0770, true);
- @chmod($log_base_dir, 0770);
- }
- $notify_file = $log_base_dir . 'notify.txt';
- if (!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- if (filesize($notify_file) > 5242880)//大于5M自动切换
- {
- rename($notify_file, $log_base_dir . 'notify_' . date('Y_m_d_H_i_s') . '.txt');
- }
- if (!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- // 日志文件 end
- //开始写入
- $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
- if ($_REQUEST && $paytype == 'alipay') {
- file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收request]" . json_encode($_REQUEST), FILE_APPEND);
- } else {
- $xml = file_get_contents("php://input");
- file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流原始数据] \n" . $xml, FILE_APPEND);
- $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流] " . json_encode($xmlObj), FILE_APPEND);
- }
- ini_set('display_errors', 'On');
- return $notify_file;
- }
- }
|