MiniProgram.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $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('签名无效,重新刷新'.$e->getMessage());
  55. }
  56. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  57. if(!isset($userInfo['openId']) && isset($userInfoCong['openid'])) $userInfo['openId'] = $userInfoCong['openid'];
  58. if(!isset($userInfo['openId'])) $this->error('获取openId失败!');
  59. User::routineOauth($userInfo);
  60. $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find();
  61. if (!$user)
  62. $this->error('获取用户失败');
  63. $this->auth->direct($user['id']);
  64. return $this->success('登录成功', $this->auth->getUserinfo());
  65. }
  66. /**
  67. * 小程序支付回调
  68. */
  69. public function notify()
  70. {
  71. MiniProgramService::handleNotify();
  72. }
  73. /**
  74. * 获取小程序订阅消息id
  75. * @return mixed
  76. */
  77. public function teml_ids()
  78. {
  79. $temlIdsName = SubscribeTemplateService::getConstants();
  80. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  81. $temlId = [];
  82. foreach ($temlIdsName as $key => $item) {
  83. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  84. }
  85. return $temlId;
  86. });
  87. return app('json')->success($temlIdsList);
  88. }
  89. }