Passport.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\utils\Easywechat\MiniAppService;
  5. use app\utils\LogUtil;
  6. use app\utils\PayUtil;
  7. use think\Cache;
  8. use think\Request;
  9. /**
  10. * 通行证
  11. */
  12. class Passport extends Api
  13. {
  14. // 日志模块名称
  15. const LOG_MODULE = 'Passport';
  16. protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay', 'pay_notify'];
  17. protected $noNeedRight = '*';
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. }
  23. public function __construct(Request $request = null)
  24. {
  25. parent::__construct($request);
  26. //日志统一写入
  27. register_shutdown_function([new LogUtil, 'close']);
  28. LogUtil::getInstance('Api/'); //设置日志存入通道
  29. }
  30. /**
  31. * 微信小程序手机号授权
  32. * @return void
  33. */
  34. public function loginWxMiniPhone()
  35. {
  36. // 获取参数
  37. $params = $this->request->param();
  38. if (empty($params['code'])) {
  39. $this->error('The code is required.');
  40. }
  41. $wxMiniApp = new MiniAppService();
  42. $wx = $wxMiniApp->getUserPhone($params['code']);
  43. if (empty($wx['phone_info']['purePhoneNumber'])) {
  44. $this->error('手机号授权失败.', $wx);
  45. }
  46. $mobile = $wx['phone_info']['purePhoneNumber'];
  47. $extend = [];
  48. if (!empty($params['openid']) && $wxInfo = Cache::get($params['openid'])){
  49. $extend['mini_openid'] = $wxInfo['openid'] ?? '';
  50. $extend['mini_sessionkey'] = $wxInfo['session_key'] ?? '';
  51. }
  52. // 校验手机号登录信息
  53. list($exists, $user_id) = UserModel::checkExists('', $mobile);
  54. if (!$exists) {
  55. // 手机号不存在 创建账号
  56. $ret = $this->auth->register('1', '1', '', $mobile, $extend);
  57. if (!$ret) {
  58. $this->error("注册失败!");
  59. }
  60. $user_id = $this->auth->id;
  61. }
  62. // 写入登录Cookies和Token
  63. if (!$this->auth->direct($user_id,$extend)) {
  64. $this->error($this->auth->getError());
  65. }
  66. $userInfo = $this->auth->getUserinfo();
  67. $result = [
  68. 'token' => $userInfo['token'],
  69. // 'userInfo' => $userInfo
  70. ];
  71. $this->success('登录成功', $result);
  72. }
  73. /**
  74. * 微信小程序授权
  75. *
  76. * @return void
  77. */
  78. public function loginWxMini()
  79. {
  80. // 获取参数
  81. $params = $this->request->param();
  82. if (empty($params['code'])) {
  83. $this->error('The code is required.');
  84. }
  85. $wxMiniApp = new MiniAppService();
  86. $res = $wxMiniApp->login($params['code']);
  87. if (empty($res['openid'])) {
  88. $this->error('授权失败,请重试');
  89. }
  90. $sign = md5($res['openid']);
  91. Cache::set($sign, $res, 3600);
  92. $this->success('success', [
  93. 'openid' => $sign
  94. ]);
  95. }
  96. /**
  97. * 测试 汇付 支付
  98. */
  99. /*public function testPay()
  100. {
  101. $params = \request()->post();
  102. $order_no = $params['order_no'] ?? '';
  103. if ($params['openid'] != 9696){
  104. $wxInfo = Cache::get($params['openid'] ?? '');
  105. $openid = $wxInfo['openid'] ?? '';
  106. // $sessionKey = $wxInfo['session_key'] ?? '';
  107. }else{
  108. $openid = 'ol8qS68vKSgWJ3Unrgfyi3rkakcQ';
  109. }
  110. $order_no = !empty($order_no) ? $order_no : time() . rand(1, 200);
  111. $pay = new PayUtil();
  112. $notify_url = 'http://app.zhiyinvip001.com/api/passport/pay_notify';
  113. if (!$pay->jsPay($openid, "D0{$order_no}", '0.01', '开通会员', $notify_url)){
  114. $this->error($pay->getMessage());
  115. }
  116. $res = $pay->getData();
  117. if (empty($res['data']['pay_info']) || !$pay_info = json_decode($res['data']['pay_info'],true)){
  118. $this->error('支付信息有误');
  119. }
  120. $this->success('success', [
  121. 'pay_info' => $pay_info,
  122. 'order_no' => $order_no
  123. ]);
  124. }*/
  125. /**
  126. * 支付回调
  127. * @return void
  128. */
  129. public function pay_notify(Request $request)
  130. {
  131. $params = $request->param();
  132. // 消息主体信息
  133. $resp_data = json_decode(stripslashes(htmlspecialchars_decode($params['resp_data'] ?? '')),true);
  134. unset($params['resp_data']);
  135. LogUtil::info('支付回调参数', self::LOG_MODULE, __FUNCTION__,[
  136. 'params' => $params,
  137. 'resp_data' => $resp_data,
  138. ]);
  139. if (empty($params['resp_code']) || $params['resp_code'] != '00000000' || empty($resp_data)){
  140. LogUtil::info('回调信息有误', self::LOG_MODULE, __FUNCTION__,"resp_code error");
  141. $this->error('支付信息有误');
  142. }
  143. $this->success();
  144. }
  145. }