MiniProgram.php 3.4 KB

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