MiniProgram.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\UserRelation;
  5. use app\common\model\User;
  6. use liuniu\MiniProgramService;
  7. use liuniu\UtilService;
  8. use think\Hook;
  9. use think\Request;
  10. class MiniProgram extends Api
  11. {
  12. /**
  13. * 小程序授权登录
  14. * @param Request $request
  15. * @return mixed
  16. * @throws \Psr\SimpleCache\InvalidArgumentException
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function mp_auth(Request $request)
  22. {
  23. $cache_key = '';
  24. list($code, $post_cache_key, $login_type) = UtilService::postMore([
  25. ['code', ''],
  26. ['cache_key', ''],
  27. ['login_type', '']
  28. ], $request, true);
  29. $session_key = cache('post_cache_key',$post_cache_key);
  30. if (!$code && !$session_key)
  31. $this->error('授权失败,参数有误');
  32. if ($code && !$session_key) {
  33. try {
  34. $userInfoCong = MiniProgramService::getUserInfo($this->cid,$code);
  35. $session_key = $userInfoCong['session_key'];
  36. $cache_key = md5(time() . $code);
  37. cache('post_cache_key', $session_key, 86400);
  38. } catch (\Exception $e) {
  39. $this->error('',['message' => $e->getMessage(), 'line' => $e->getLine()]);
  40. }
  41. }
  42. $data = UtilService::postMore([
  43. ['spread_spid', 0],
  44. ['spread_code', ''],
  45. ['iv', ''],
  46. ['encryptedData', ''],
  47. ]);//获取前台传的code
  48. try {
  49. //解密获取用户信息
  50. $userInfo = MiniProgramService::encryptor($session_key, $data['iv'], $data['encryptedData']);
  51. } catch (\Exception $e) {
  52. if ($e->getCode() == '-41003') $this->error('获取会话密匙失败');
  53. }
  54. if (!isset($userInfoCong['openid'])) $this->error('openid获取失败');
  55. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  56. $userInfo['openId'] = $userInfoCong['openid'];
  57. $userInfo['spid'] = $data['spread_spid'];
  58. $userInfo['code'] = $data['spread_code'];
  59. $userInfo['session_key'] = $session_key;
  60. $userInfo['login_type'] = $login_type;
  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. }