MemberController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use api\controllers\OnAuthController;
  6. use common\models\member\Member;
  7. use common\forms\MemberForm;
  8. use common\helpers\ResultHelper;
  9. use common\enums\StatusEnum;
  10. use common\helpers\ArrayHelper;
  11. /**
  12. * 会员接口
  13. *
  14. * Class MemberController
  15. * @package api\modules\v1\controllers\member
  16. * @property \yii\db\ActiveRecord $modelClass
  17. * @author jianyan74 <751393839@qq.com>
  18. */
  19. class MemberController extends OnAuthController
  20. {
  21. /**
  22. * @var Member
  23. */
  24. public $modelClass = MemberForm::class;
  25. /**
  26. * 个人中心
  27. *
  28. * @return array|null|\yii\data\ActiveDataProvider|\yii\db\ActiveRecord
  29. */
  30. public function actionIndex()
  31. {
  32. $member_id = Yii::$app->user->identity->member_id;
  33. $member = $this->modelClass::find()
  34. ->where(['id' => $member_id])
  35. ->with(['account', 'memberLevel'])
  36. ->asArray()
  37. ->one();
  38. return $member;
  39. }
  40. /**
  41. * 更新
  42. *
  43. * @param $id
  44. * @return bool|mixed
  45. * @throws NotFoundHttpException
  46. */
  47. public function actionUpdate($id)
  48. {
  49. $data = Yii::$app->request->post();
  50. $data = ArrayHelper::filter($data, [
  51. 'nickname',
  52. 'head_portrait',
  53. 'realname',
  54. 'birthday',
  55. 'province_id',
  56. 'city_id',
  57. 'area_id',
  58. 'address',
  59. 'qq',
  60. 'email',
  61. 'gender',
  62. ]);
  63. $model = $this->findModel($id);
  64. $model->attributes = $data;
  65. if (!$model->save()) {
  66. return ResultHelper::json(422, $this->getError($model));
  67. }
  68. return 'ok';
  69. }
  70. /**
  71. * @param $id
  72. * @return \yii\db\ActiveRecord
  73. * @throws NotFoundHttpException
  74. */
  75. protected function findModel($id)
  76. {
  77. /* @var $model \yii\db\ActiveRecord */
  78. if (empty($id) || !($model = $this->modelClass::find()->where([
  79. 'id' => Yii::$app->user->identity->member_id,
  80. 'status' => StatusEnum::ENABLED,
  81. ])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  82. throw new NotFoundHttpException('请求的数据不存在');
  83. }
  84. return $model;
  85. }
  86. /**
  87. * 权限验证
  88. *
  89. * @param string $action 当前的方法
  90. * @param null $model 当前的模型类
  91. * @param array $params $_GET变量
  92. * @throws \yii\web\BadRequestHttpException
  93. */
  94. public function checkAccess($action, $model = null, $params = [])
  95. {
  96. // 方法名称
  97. if (in_array($action, ['delete', 'view'])) {
  98. throw new \yii\web\BadRequestHttpException('权限不足');
  99. }
  100. }
  101. }
粤ICP备19079148号