MiniDecryptController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-09-04
  6. * Time: 14:02
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\frontend\modules\member\controllers;
  22. use app\common\components\ApiController;
  23. class MiniDecryptController extends ApiController
  24. {
  25. public function index()
  26. {
  27. include dirname(__FILE__) . "/../vendors/wechat/wxBizDataCrypt.php";
  28. $min_set = \Setting::get('plugin.min_app');
  29. if (is_null($min_set) || 0 == $min_set['switch']) {
  30. return show_json(0, '未开启小程序');
  31. }
  32. //小程序登录后返回的code
  33. $para = request()->para_arr;
  34. $data = '';
  35. $errCode = '';
  36. if (!empty($para['info'])) {
  37. $json_data = $para['info'];
  38. $pc = new \WXBizDataCrypt($min_set['key'], $para['session_key']);
  39. $errCode = $pc->decryptData($json_data['encryptedData'], $json_data['iv'], $data);
  40. }
  41. if ($errCode == 0) {
  42. $json_data = json_decode($data, true);
  43. return $this->successJson('ok', $json_data);
  44. } else {
  45. return $this->errorJson('解密失败', [
  46. 'error_code' => $errCode,
  47. ]);
  48. }
  49. }
  50. public function getSessionKey()
  51. {
  52. $session_id = request()->session_key_id;
  53. $data = $_SESSION['wx_app'][$session_id];
  54. $session_key_data = unserialize($data);
  55. return $this->successJson('ok', [
  56. 'session_key_data' => $session_key_data,
  57. ]);
  58. }
  59. }