Controller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // 格式化头像
  68. if(isset($user['avatarUrl']) && $user['avatarUrl']){
  69. $user['avatarUrl'] = getPreview($user['avatarUrl']);
  70. }
  71. if ($user['is_delete'] == 1) {
  72. throw new BaseException(['msg' => '没有找到用户信息', 'code' => -2]);
  73. Cache::delete($token);
  74. }
  75. return $user;
  76. }
  77. /**
  78. * 获取当前用户信息
  79. */
  80. protected function getSupplierUser($user)
  81. {
  82. if (!$user['supplierUser']) {
  83. throw new BaseException(['msg' => '非法请求', 'code' => -1]);
  84. }
  85. return $user['supplierUser'];
  86. }
  87. protected function getShareParams($url, $title = '', $desc = '', $link = '', $imgUrl = '')
  88. {
  89. $signPackage = '';
  90. $shareParams = '';
  91. if (Env::get('APP_DEBUG')) {
  92. return [
  93. 'signPackage' => $signPackage,
  94. 'shareParams' => $shareParams
  95. ];
  96. }
  97. if ($url != '') {
  98. $app = AppMp::getApp($this->app_id);
  99. $app->jssdk->setUrl($url);
  100. $signPackage = $app->jssdk->buildConfig(array('updateAppMessageShareData', 'updateTimelineShareData'), false);
  101. $shareParams = [
  102. 'title' => $title,
  103. 'desc' => $desc,
  104. 'link' => $link,
  105. 'imgUrl' => $imgUrl,
  106. ];
  107. }
  108. return [
  109. 'signPackage' => $signPackage,
  110. 'shareParams' => $shareParams
  111. ];
  112. }
  113. protected function getScanParams($url)
  114. {
  115. $signPackage = '';
  116. if (Env::get('APP_DEBUG')) {
  117. return [
  118. 'signPackage' => $signPackage
  119. ];
  120. }
  121. if ($url != '') {
  122. $app = AppMp::getApp($this->app_id);
  123. $app->jssdk->setUrl($url);
  124. $signPackage = $app->jssdk->buildConfig(array('scanQRCode'), false);
  125. }
  126. return [
  127. 'signPackage' => $signPackage
  128. ];
  129. }
  130. }