Passport.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $pay->jsPay($openid, "D0{$order_no}", '0.01', '开通会员');
  102. if (!$pay->jsPay($openid, "D0{$order_no}", '0.01', '开通会员')){
  103. $this->error($pay->getMessage());
  104. }
  105. $res = $pay->getData();
  106. if (empty($res['data']['pay_info']) || !$pay_info = json_decode($res['data']['pay_info'],true)){
  107. $this->error('支付信息有误');
  108. return;
  109. }
  110. $this->success('success', [
  111. 'pay_info' => $pay_info,
  112. 'order_no' => $order_no
  113. ]);
  114. }
  115. }