MiniProgram.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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('post_cache_key',$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. dump($userInfoCong);
  38. $session_key = $userInfoCong['session_key'];
  39. $cache_key = md5(time() . $code);
  40. cache('post_cache_key', $session_key, 86400);
  41. } catch (\Exception $e) {
  42. $this->error('',['message' => $e->getMessage(), 'line' => $e->getLine()]);
  43. }
  44. }
  45. $data = UtilService::postMore([
  46. ['spread_spid', 0],
  47. ['spread_code', ''],
  48. ['iv', ''],
  49. ['encryptedData', ''],
  50. ]);//获取前台传的code
  51. dump($data);
  52. echo $this->cid.'---';echo $session_key;
  53. try {
  54. //解密获取用户信息
  55. $userInfo = MiniProgramService::encryptor($this->cid,$session_key, $data['iv'], $data['encryptedData']);
  56. } catch (\Exception $e) {
  57. dump($e);
  58. }
  59. dump($userInfo);
  60. exit();
  61. @file_put_contents("user.txt",json_encode($userInfo).'--'.json_encode($userInfoCong));
  62. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  63. if(!$userInfo['openId']) $userInfo['openId'] = $userInfoCong['openid'];
  64. if(!$userInfo['openId']) $this->error('获取openId失败!');
  65. $userInfo['spid'] = $data['spread_spid'];
  66. $userInfo['code'] = $data['spread_code'];
  67. $userInfo['openId'] = $userInfo['openId'];
  68. $userInfo['unionid'] = $userInfo['unionId'];
  69. $userInfo['session_key'] = $session_key;
  70. $userInfo['login_type'] = $login_type;
  71. if(isset($userInfo['unionId']) && $userInfo['unionId'] != '' && ($uid = UserRelation::where('unionid', $userInfo['unionId'])->where('login_type', '<>', 0)->value('user_id')))
  72. {
  73. UserRelation::where('user_id',$uid)->update($userInfo);
  74. if (!UserRelation::where(['user_id' => $uid])->find()) {
  75. $userInfo = UserRelation::where('user_id', $uid)->find();
  76. UserModel::setRoutineUser($userInfo, 0);
  77. } else {
  78. if ($login_type) $userInfo['login_type'] = 3;
  79. UserModel::updateWechatUser($userInfo, $data['spread_spid']);
  80. }
  81. } else if ($uid = UserRelation::where(['openid' => $userInfo['openId']])->where('login_type', '<>', 0)->value('user_id')) {
  82. UserRelation::where('user_id',$uid)->update($userInfo);
  83. if ($login_type) $userInfo['login_type'] = $login_type;
  84. UserModel::updateWechatUser($userInfo, $uid);
  85. } else {
  86. $userInfo['login_type'] = $login_type;
  87. UserModel::setRoutineUser($userInfo, $data['spread_spid']);
  88. }
  89. $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find();
  90. if (!$user)
  91. $this->error('获取用户失败');
  92. $this->auth->direct($user['id']);
  93. return $this->success('登录成功', $this->auth->getUserinfo());
  94. }
  95. /**
  96. * 小程序支付回调
  97. */
  98. public function notify()
  99. {
  100. MiniProgramService::handleNotify();
  101. }
  102. /**
  103. * 获取小程序订阅消息id
  104. * @return mixed
  105. */
  106. public function teml_ids()
  107. {
  108. $temlIdsName = SubscribeTemplateService::getConstants();
  109. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  110. $temlId = [];
  111. foreach ($temlIdsName as $key => $item) {
  112. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  113. }
  114. return $temlId;
  115. });
  116. return app('json')->success($temlIdsList);
  117. }
  118. }