Controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\user\User as UserModel;
  4. use app\api\model\App as AppModel;
  5. use app\common\exception\BaseException;
  6. use app\common\library\easywechat\AppMp;
  7. use app\JjjController;
  8. use think\facade\Env;
  9. use think\facade\Cache;
  10. /**
  11. * API控制器基类
  12. */
  13. class Controller extends JjjController
  14. {
  15. // app_id
  16. protected $app_id;
  17. /**
  18. * 后台初始化
  19. */
  20. public function initialize()
  21. {
  22. // 当前小程序id
  23. $this->app_id = $this->getAppId();
  24. // 验证当前小程序状态
  25. $this->checkWxapp();
  26. }
  27. /**
  28. * 获取当前应用ID
  29. */
  30. private function getAppId()
  31. {
  32. if (!$app_id = $this->request->param('app_id')) {
  33. throw new BaseException(['msg' => '缺少必要的参数:app_id']);
  34. }
  35. return $app_id;
  36. }
  37. /**
  38. * 验证当前小程序状态
  39. */
  40. private function checkWxapp()
  41. {
  42. $app = AppModel::detail($this->app_id);
  43. if (empty($app)) {
  44. throw new BaseException(['msg' => '当前应用信息不存在']);
  45. }
  46. if ($app['is_recycle'] || $app['is_delete']) {
  47. throw new BaseException(['msg' => '当前应用已删除']);
  48. }
  49. }
  50. /**
  51. * 获取当前用户信息
  52. */
  53. protected function getUser($is_force = true)
  54. {
  55. if (!$token = $this->request->param('token')) {
  56. if ($is_force) {
  57. throw new BaseException(['msg' => '缺少必要的参数:token', 'code' => -1]);
  58. }
  59. return false;
  60. }
  61. if (!$user = UserModel::getUser($token)) {
  62. if ($is_force) {
  63. throw new BaseException(['msg' => '没有找到用户信息', 'code' => -1]);
  64. }
  65. return false;
  66. }
  67. if ($user['is_delete'] == 1) {
  68. throw new BaseException(['msg' => '没有找到用户信息', 'code' => -2]);
  69. Cache::delete($token);
  70. }
  71. return $user;
  72. }
  73. /**
  74. * 获取当前用户信息
  75. */
  76. protected function getSupplierUser($user)
  77. {
  78. if (!$user['supplierUser']) {
  79. throw new BaseException(['msg' => '非法请求', 'code' => -1]);
  80. }
  81. return $user['supplierUser'];
  82. }
  83. protected function getShareParams($url, $title = '', $desc = '', $link = '', $imgUrl = '')
  84. {
  85. $signPackage = '';
  86. $shareParams = '';
  87. if (Env::get('APP_DEBUG')) {
  88. return [
  89. 'signPackage' => $signPackage,
  90. 'shareParams' => $shareParams
  91. ];
  92. }
  93. if ($url != '') {
  94. $app = AppMp::getApp($this->app_id);
  95. $app->jssdk->setUrl($url);
  96. $signPackage = $app->jssdk->buildConfig(array('updateAppMessageShareData', 'updateTimelineShareData'), false);
  97. $shareParams = [
  98. 'title' => $title,
  99. 'desc' => $desc,
  100. 'link' => $link,
  101. 'imgUrl' => $imgUrl,
  102. ];
  103. }
  104. return [
  105. 'signPackage' => $signPackage,
  106. 'shareParams' => $shareParams
  107. ];
  108. }
  109. protected function getScanParams($url)
  110. {
  111. $signPackage = '';
  112. if (Env::get('APP_DEBUG')) {
  113. return [
  114. 'signPackage' => $signPackage
  115. ];
  116. }
  117. if ($url != '') {
  118. $app = AppMp::getApp($this->app_id);
  119. $app->jssdk->setUrl($url);
  120. $signPackage = $app->jssdk->buildConfig(array('scanQRCode'), false);
  121. }
  122. return [
  123. 'signPackage' => $signPackage
  124. ];
  125. }
  126. }