MiniProgram.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\UserRelation;
  6. use app\common\model\User;
  7. use liuniu\MiniProgramService;
  8. use liuniu\UtilService;
  9. use think\Hook;
  10. use think\Request;
  11. class MiniProgram extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. /**
  15. * 小程序授权登录
  16. * @param Request $request
  17. * @return mixed
  18. * @throws \Psr\SimpleCache\InvalidArgumentException
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function mp_auth(Request $request)
  24. {
  25. $cache_key = '';
  26. list($code, $post_cache_key, $login_type) = UtilService::postMore([
  27. ['code', ''],
  28. ['cache_key', ''],
  29. ['login_type', '2']
  30. ], $request, true);
  31. $session_key = cache('post_cache_key',$post_cache_key);
  32. if (!$code && !$session_key)
  33. $this->error('授权失败,参数有误');
  34. if ($code && !$session_key) {
  35. try {
  36. $userInfoCong = MiniProgramService::getUserInfo($this->cid,$code);
  37. $session_key = $userInfoCong['session_key'];
  38. $cache_key = md5(time() . $code);
  39. cache('post_cache_key', $session_key, 86400);
  40. } catch (\Exception $e) {
  41. $this->error('',['message' => $e->getMessage(), 'line' => $e->getLine()]);
  42. }
  43. }
  44. $data = UtilService::postMore([
  45. ['spread_spid', 0],
  46. ['spread_code', ''],
  47. ['iv', ''],
  48. ['encryptedData', ''],
  49. ]);//获取前台传的code
  50. try {
  51. //解密获取用户信息
  52. $userInfo = MiniProgramService::encryptor($this->cid,$session_key, $data['iv'], $data['encryptedData']);
  53. } catch (\Exception $e) {
  54. $this->error('签名无效,重新刷新');
  55. }
  56. @file_put_contents("user.txt",json_encode($userInfo).'--'.json_encode($userInfoCong));
  57. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  58. if(!$userInfo['openId']) $userInfo['openId'] = $userInfoCong['openid'];
  59. if(!$userInfo['openId']) $this->error('获取openId失败!');
  60. User::routineOauth($userInfo);
  61. $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find();
  62. if (!$user)
  63. $this->error('获取用户失败');
  64. $this->auth->direct($user['id']);
  65. return $this->success('登录成功', $this->auth->getUserinfo());
  66. }
  67. /**
  68. * 小程序支付回调
  69. */
  70. public function notify()
  71. {
  72. MiniProgramService::handleNotify();
  73. }
  74. /**
  75. * 获取小程序订阅消息id
  76. * @return mixed
  77. */
  78. public function teml_ids()
  79. {
  80. $temlIdsName = SubscribeTemplateService::getConstants();
  81. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  82. $temlId = [];
  83. foreach ($temlIdsName as $key => $item) {
  84. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  85. }
  86. return $temlId;
  87. });
  88. return app('json')->success($temlIdsList);
  89. }
  90. }