DefaultController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\Certificate\api\modules\v1\controllers;
  3. use addons\Certificate\common\enums\CertificateEnum;
  4. use addons\Certificate\common\models\Certificate;
  5. use addons\Certificate\common\models\CertificateClaim;
  6. use common\enums\StatusEnum;
  7. use common\models\user\UserLevel;
  8. use common\enums\DiyEnum;
  9. use Yii;
  10. use yii\db\Exception;
  11. use api\controllers\OnAuthController;
  12. use yii\db\Expression;
  13. /**
  14. * 默认控制器
  15. *
  16. * Class DefaultController
  17. * @package addons\Certificate\api\modules\v1\controllers
  18. */
  19. class DefaultController extends OnAuthController
  20. {
  21. public $modelClass = '';
  22. /**
  23. * 不用进行登录验证的方法
  24. *
  25. * 例如: ['index', 'update', 'create', 'view', 'delete']
  26. * 默认全部需要验证
  27. *
  28. * @var array
  29. */
  30. protected $authOptional = ['index'];
  31. /**
  32. * 首页
  33. *
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. return 'Hello world';
  39. }
  40. /**
  41. * 获取证书列表
  42. * @Author: ltm
  43. * @DateTime: 2022/5/8 0021 11:29
  44. * @Copyright: copyright (c) 2021 广东七件事集团
  45. * @return mixed|string|void
  46. */
  47. public function actionList()
  48. {
  49. $retuest = Yii::$app->request;
  50. if ($retuest->isGet) {
  51. $where = [];
  52. $where[] = ['=', 'mall_id', $this->mall_id];
  53. $where[] = ['=', 'status', StatusEnum::ENABLED];
  54. $countWhere = compact('where');
  55. $where[] = new Expression("FIND_IN_SET({$this->user['level']}, member_level)");
  56. $order = 'id desc';
  57. $params = array('where' => $where, 'order'=>$order, 'limit' => 5);
  58. $list = Certificate::listPage($params, false, true);
  59. // 如果后台有新建证书,但是用户的等级一个都看不了,则给提示。如果后台没证书则不理
  60. if($list['row_count'] == 0) {
  61. $countQuery = Certificate::find();
  62. Certificate::paramPack($countWhere, $countQuery);
  63. if ($countQuery->count() > 0) {
  64. return $this->error('授权证书目前仅对指定等级开放,请升级后重试');
  65. }
  66. }
  67. $user['number'] = $this->user_id.substr($this->user['mobile'],-4);
  68. $level = UserLevel::getOne([['mall_id' => $this->mall_id,'status' =>StatusEnum::ENABLED,'level'=>$this->user['level'] ]],true);
  69. $user['level'] = $level['name'];
  70. $list['user'] = $user;
  71. $sign = $this->mall['mall_sign'];
  72. if($list['list']){
  73. foreach($list['list'] as $k => $v){
  74. $list['list'][$k]['qrcode_url'] = DiyEnum::HOME_PAGE_PATH . "?sign={$sign}&pid=".$this->user_id;
  75. }
  76. }
  77. $list['qrcode_id'] = CertificateEnum::SHARE_TYPE;
  78. $this->success('获取成功', $list);
  79. }
  80. }
  81. /**
  82. * 认领证书
  83. * @Author: ltm
  84. * @DateTime: 2022/5/8 0021 11:29
  85. * @Copyright: copyright (c) 2021 广东七件事集团
  86. * @return mixed|string|void
  87. */
  88. public function actionCodeDetail()
  89. {
  90. $retuest = Yii::$app->request;
  91. if ($retuest->isGet) {
  92. $params = Yii::$app->request->get();
  93. // 检查提交的参数
  94. $res = Yii::$app->services->validate->validate($params,[
  95. [['id'], 'safe']
  96. ]);
  97. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  98. $params['user_id'] = $this->user_id;
  99. $params['mall_id'] = $this->mall_id;
  100. $params['mobile'] = $this->user['mobile'];
  101. $res = CertificateClaim::setData($params);
  102. if ($res === false) throw new Exception(CertificateClaim::getStaticError());
  103. return $this->success('操作成功');
  104. }
  105. }
  106. /**
  107. * 认领证书
  108. * @Author: ltm
  109. * @DateTime: 2022/5/8 0021 11:29
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return mixed|string|void
  112. */
  113. public function actionClaim()
  114. {
  115. $retuest = Yii::$app->request;
  116. if ($retuest->isGet) {
  117. $params = Yii::$app->request->get();
  118. // 检查提交的参数
  119. $res = Yii::$app->services->validate->validate($params,[
  120. [['id'], 'safe']
  121. ]);
  122. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  123. $params['user_id'] = $this->user_id;
  124. $params['mall_id'] = $this->mall_id;
  125. $params['mobile'] = $this->user['mobile'];
  126. $res = CertificateClaim::setData($params);
  127. if ($res === false) throw new Exception(CertificateClaim::getStaticError());
  128. return $this->success('操作成功');
  129. }
  130. }
  131. }