Controller.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $sid = $this->request->param('sid');
  80. $qrcode = $app->app_code->getQrCode('/pages/user/my_shop/supplier_qrcode?sid='.$sid);
  81. $user['supplierUser']['qrcode'] = $qrcode;
  82. // $user['supplierUser']['qrcode'] = getPreview('/image/agent/qrcode.jpg');
  83. $user['supplierUser']['pay_bg'] = getPreview('/image/agent/pay.png');
  84. }
  85. return $user;
  86. }
  87. /**
  88. * 获取当前用户信息
  89. */
  90. protected function getSupplierUser($user)
  91. {
  92. if (!$user['supplierUser']) {
  93. throw new BaseException(['msg' => '非法请求', 'code' => -1]);
  94. }
  95. return $user['supplierUser'];
  96. }
  97. protected function getShareParams($url, $title = '', $desc = '', $link = '', $imgUrl = '')
  98. {
  99. $signPackage = '';
  100. $shareParams = '';
  101. if (Env::get('APP_DEBUG')) {
  102. return [
  103. 'signPackage' => $signPackage,
  104. 'shareParams' => $shareParams
  105. ];
  106. }
  107. if ($url != '') {
  108. $app = AppMp::getApp($this->app_id);
  109. $app->jssdk->setUrl($url);
  110. $signPackage = $app->jssdk->buildConfig(array('updateAppMessageShareData', 'updateTimelineShareData'), false);
  111. $shareParams = [
  112. 'title' => $title,
  113. 'desc' => $desc,
  114. 'link' => $link,
  115. 'imgUrl' => $imgUrl,
  116. ];
  117. }
  118. return [
  119. 'signPackage' => $signPackage,
  120. 'shareParams' => $shareParams
  121. ];
  122. }
  123. protected function getScanParams($url)
  124. {
  125. $signPackage = '';
  126. if (Env::get('APP_DEBUG')) {
  127. return [
  128. 'signPackage' => $signPackage
  129. ];
  130. }
  131. if ($url != '') {
  132. $app = AppMp::getApp($this->app_id);
  133. $app->jssdk->setUrl($url);
  134. $signPackage = $app->jssdk->buildConfig(array('scanQRCode'), false);
  135. }
  136. return [
  137. 'signPackage' => $signPackage
  138. ];
  139. }
  140. }