MyhouseController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace frontend\controllers;
  3. use common\models\UserHouse;
  4. use Yii;
  5. use yii\web\Controller;
  6. use yii\filters\AccessControl;
  7. /**
  8. * red controller
  9. */
  10. class MyhouseController extends BasewechatController
  11. {
  12. static $pagenum = 20;
  13. public function behaviors()
  14. {
  15. return [
  16. 'access' => [
  17. 'class' => AccessControl::className(),
  18. 'rules' => [
  19. [
  20. 'actions' => [],
  21. 'allow' => true,
  22. 'roles' => ['@'],
  23. ],
  24. ],
  25. ],
  26. ];
  27. }
  28. /**
  29. * 我的房屋
  30. */
  31. public function actionList()
  32. {
  33. $mod = UserHouse::findBySql("SELECT * FROM {{%user_house}} WHERE uid = :uid ORDER BY id DESC",[':uid'=>Yii::$app->user->id]);
  34. $list = $mod->all();
  35. Yii::$app->view->title="我的房屋";
  36. return $this->render('list',['datas'=>$list]);
  37. }
  38. /**
  39. * @return mixed|string
  40. * 添加房屋
  41. */
  42. public function actionAdd()
  43. {
  44. if(Yii::$app->request->isAjax)
  45. {
  46. $result=['sign'=>1,'msg'=>'ok'];
  47. $post = Yii::$app->request->post();
  48. $mod = new UserHouse();
  49. $mod->uid = Yii::$app->user->id;
  50. $mod->compound = $post['compound'];
  51. $mod->layout = $post['layout'];
  52. $mod->acreage = $post['acreage'];
  53. $mod->amount = $post['amount'];
  54. $mod->province = $post['province'];
  55. $mod->city = $post['city'];
  56. $mod->district = $post['district'];
  57. $mod->address = $post['address2'];
  58. $mod->created_at = time();
  59. $mod->status = 0;
  60. $mod->style = $post['style'];
  61. $mod->updated_at = time();
  62. if($mod->save()&&$mod->validate())
  63. {
  64. $result=['sign'=>1,'msg'=>'添加成功'];
  65. }else{
  66. $result=['sign'=>0,'msg'=>'添加失败'];
  67. }
  68. return json_encode($result);
  69. }
  70. Yii::$app->view->title="添加房屋";
  71. return $this->render('add');
  72. }
  73. /**
  74. *修改房屋
  75. */
  76. public function actionEdit()
  77. {
  78. if(Yii::$app->request->isAjax)
  79. {
  80. $result=['sign'=>1,'msg'=>'ok'];
  81. $post = Yii::$app->request->post();
  82. $mod = UserHouse::find()->where(['uid'=>Yii::$app->user->id,'id'=>$post['id']])->one();
  83. $mod->uid = Yii::$app->user->id;
  84. $mod->compound = $post['compound'];
  85. $mod->layout = $post['layout'];
  86. $mod->acreage = $post['acreage'];
  87. $mod->amount = $post['amount'];
  88. $mod->province = $post['province'];
  89. $mod->city = $post['city'];
  90. $mod->district = $post['district'];
  91. $mod->address = $post['address2'];
  92. $mod->style = $post['style'];
  93. if($mod->save()&&$mod->validate())
  94. {
  95. $result=['sign'=>1,'msg'=>'修改成功'];
  96. }else{
  97. $result=['sign'=>0,'msg'=>'修改失败'];
  98. }
  99. return json_encode($result);
  100. }
  101. Yii::$app->view->title="修改房屋";
  102. $id = Yii::$app->request->get('id');
  103. $info = UserHouse::find()->where(['id'=>$id,'uid'=>Yii::$app->user->id])->one();
  104. return $this->render('edit',['info'=>$info]);
  105. }
  106. }