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. @file_put_contents("user.txt",json_encode($userInfo).'--'.json_encode($userInfoCong));
  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. }