MemberMobileSelect.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\web\Response;
  5. use common\enums\StatusEnum;
  6. use common\models\member\Member;
  7. use common\enums\MemberTypeEnum;
  8. /**
  9. * Trait MemberMobileSelect
  10. * @package common\traits
  11. */
  12. trait MemberMobileSelect
  13. {
  14. /**
  15. * select2 查询
  16. *
  17. * @param null $q
  18. * @param null $id
  19. * @return array
  20. */
  21. public function actionMobileSelect($q = null, $id = null, $field = 'mobile')
  22. {
  23. Yii::$app->response->format = Response::FORMAT_JSON;
  24. $out = [
  25. 'results' => [
  26. 'id' => '',
  27. 'text' => ''
  28. ]
  29. ];
  30. $defaultCondition = ['like', 'mobile', $q];
  31. $condition = ['merchant_id' => Yii::$app->services->merchant->getNotNullId()];
  32. if (Yii::$app->services->devPattern->isB2B2C()) {
  33. $condition = [];
  34. }
  35. $field == 'id' && $defaultCondition = ['like', 'id', $q];
  36. $field == 'nickname' && $defaultCondition = ['like', 'nickname', $q];
  37. if (!is_null($q)) {
  38. $data = Member::find()
  39. ->select('id, nickname as text')
  40. ->where($defaultCondition)
  41. ->andWhere(['status' => StatusEnum::ENABLED])
  42. ->andWhere(['type' => MemberTypeEnum::MEMBER])
  43. ->andFilterWhere($condition)
  44. ->limit(10)
  45. ->asArray()
  46. ->all();
  47. $out['results'] = array_values($data);
  48. array_unshift($out['results'], [
  49. 'id' => 0,
  50. 'text' => '不选择'
  51. ]);
  52. } elseif ($id > 0) {
  53. $out['results'] = ['id' => $id, 'text' => Member::findOne($id)->mobile];
  54. }
  55. return $out;
  56. }
  57. }
粤ICP备19079148号