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