123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\utils\Easywechat\MiniAppService;
- use app\utils\LogUtil;
- use app\utils\PayUtil;
- use think\Cache;
- use think\Request;
- class Passport extends Api
- {
-
- const LOG_MODULE = 'Passport';
- protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay', 'pay_notify'];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- }
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
-
- register_shutdown_function([new LogUtil, 'close']);
- LogUtil::getInstance('Api/');
- }
-
- 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;
- }
-
- if (!$this->auth->direct($user_id,$extend)) {
- $this->error($this->auth->getError());
- }
- $userInfo = $this->auth->getUserinfo();
- $result = [
- 'token' => $userInfo['token'],
- ];
- $this->success('登录成功', $result);
- }
-
- 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 pay_notify(Request $request)
- {
- $params = $request->param();
-
- $resp_data = json_decode(stripslashes(htmlspecialchars_decode($params['resp_data'] ?? '')),true);
- unset($params['resp_data']);
- LogUtil::info('支付回调参数', self::LOG_MODULE, __FUNCTION__,[
- 'params' => $params,
- 'resp_data' => $resp_data,
- ]);
- if (empty($params['resp_code']) || $params['resp_code'] != '00000000' || empty($resp_data)){
- LogUtil::info('回调信息有误', self::LOG_MODULE, __FUNCTION__,"resp_code error");
- $this->error('支付信息有误');
- }
- $this->success();
- }
- }
|