<?php namespace app\api\controller; use app\common\controller\Api; use app\common\model\User as UserModel; use app\common\model\UserRelation; use app\common\model\User; use liuniu\MiniProgramService; use liuniu\UtilService; use think\Hook; use think\Request; class MiniProgram extends Api { protected $noNeedLogin = ['*']; /** * 小程序授权登录 * @param Request $request * @return mixed * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function mp_auth(Request $request) { $cache_key = ''; list($code, $post_cache_key, $login_type) = UtilService::postMore([ ['code', ''], ['cache_key', ''], ['login_type', '2'] ], $request, true); $session_key = cache('post_cache_key',$post_cache_key); if (!$code && !$session_key) $this->error('授权失败,参数有误'); if ($code && !$session_key) { try { $userInfoCong = MiniProgramService::getUserInfo($this->cid,$code); dump($userInfoCong); $session_key = $userInfoCong['session_key']; $cache_key = md5(time() . $code); cache('post_cache_key', $session_key, 86400); } catch (\Exception $e) { $this->error('',['message' => $e->getMessage(), 'line' => $e->getLine()]); } } $data = UtilService::postMore([ ['spread_spid', 0], ['spread_code', ''], ['iv', ''], ['encryptedData', ''], ]);//获取前台传的code dump($data); echo $this->cid.'---';echo $session_key.'--'.$data['iv'].'--'.$data['encryptedData']; try { //解密获取用户信息 $userInfo = MiniProgramService::encryptor($this->cid,$session_key, $data['iv'], $data['encryptedData']); dump($userInfo); } catch (\Exception $e) { dump($e->getFile().'--'.$e->getLine().'--'.$e->getMessage()); } exit(); @file_put_contents("user.txt",json_encode($userInfo).'--'.json_encode($userInfoCong)); if (!isset($userInfo['unionId'])) $userInfo['unionId'] = ''; if(!$userInfo['openId']) $userInfo['openId'] = $userInfoCong['openid']; if(!$userInfo['openId']) $this->error('获取openId失败!'); $userInfo['spid'] = $data['spread_spid']; $userInfo['code'] = $data['spread_code']; $userInfo['openId'] = $userInfo['openId']; $userInfo['unionid'] = $userInfo['unionId']; $userInfo['session_key'] = $session_key; $userInfo['login_type'] = $login_type; if(isset($userInfo['unionId']) && $userInfo['unionId'] != '' && ($uid = UserRelation::where('unionid', $userInfo['unionId'])->where('login_type', '<>', 0)->value('user_id'))) { UserRelation::where('user_id',$uid)->update($userInfo); if (!UserRelation::where(['user_id' => $uid])->find()) { $userInfo = UserRelation::where('user_id', $uid)->find(); UserModel::setRoutineUser($userInfo, 0); } else { if ($login_type) $userInfo['login_type'] = 3; UserModel::updateWechatUser($userInfo, $data['spread_spid']); } } else if ($uid = UserRelation::where(['openid' => $userInfo['openId']])->where('login_type', '<>', 0)->value('user_id')) { UserRelation::where('user_id',$uid)->update($userInfo); if ($login_type) $userInfo['login_type'] = $login_type; UserModel::updateWechatUser($userInfo, $uid); } else { $userInfo['login_type'] = $login_type; UserModel::setRoutineUser($userInfo, $data['spread_spid']); } $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find(); if (!$user) $this->error('获取用户失败'); $this->auth->direct($user['id']); return $this->success('登录成功', $this->auth->getUserinfo()); } /** * 小程序支付回调 */ public function notify() { MiniProgramService::handleNotify(); } /** * 获取小程序订阅消息id * @return mixed */ public function teml_ids() { $temlIdsName = SubscribeTemplateService::getConstants(); $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) { $temlId = []; foreach ($temlIdsName as $key => $item) { $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item); } return $temlId; }); return app('json')->success($temlIdsList); } }