MiniProgram.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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('eb_api_code_',$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('eb_api_code_'.$cache_key,$session_key, 86400);
  40. } catch (\Exception $e) {
  41. $this->error('',['message' => $e->getMessage(), 'line' => $e->getLine()]);
  42. }
  43. }
  44. dump($userInfoCong);
  45. $data = UtilService::postMore([
  46. ['spread_spid', 0],
  47. ['spread_code', ''],
  48. ['iv', ''],
  49. ['encryptedData', ''],
  50. ]);//获取前台传的code
  51. try {
  52. //解密获取用户信息
  53. $userInfo = MiniProgramService::encryptor($this->cid,$session_key, $data['iv'], $data['encryptedData']);
  54. } catch (\Exception $e) {
  55. $this->error('签名无效,重新刷新'.$e->getMessage());
  56. }
  57. dump($userInfo);
  58. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  59. if(!$userInfo['openId'] && isset($userInfoCong['openid'])) $userInfo['openId'] = $userInfoCong['openid'];
  60. if(!$userInfo['openId']) $this->error('获取openId失败!');
  61. User::routineOauth($userInfo);
  62. $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find();
  63. if (!$user)
  64. $this->error('获取用户失败');
  65. $this->auth->direct($user['id']);
  66. return $this->success('登录成功', $this->auth->getUserinfo());
  67. }
  68. /**
  69. * 小程序支付回调
  70. */
  71. public function notify()
  72. {
  73. MiniProgramService::handleNotify();
  74. }
  75. /**
  76. * 获取小程序订阅消息id
  77. * @return mixed
  78. */
  79. public function teml_ids()
  80. {
  81. $temlIdsName = SubscribeTemplateService::getConstants();
  82. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  83. $temlId = [];
  84. foreach ($temlIdsName as $key => $item) {
  85. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  86. }
  87. return $temlId;
  88. });
  89. return app('json')->success($temlIdsList);
  90. }
  91. }