Controller.php 4.3 KB

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