MiniProgram.php 4.7 KB

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