UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace frontend\controllers;
  3. use common\library\ZM_Geohash;
  4. use common\models\Appointment;
  5. use common\models\Building;
  6. use common\models\Comment;
  7. use common\models\Feedback;
  8. use common\models\ImageSource;
  9. use common\models\Information;
  10. use common\models\Manager;
  11. use common\models\Reply;
  12. use common\models\SortMessage;
  13. use common\models\User;
  14. use common\models\UserCompany;
  15. use common\models\UserInfo;
  16. use Yii;
  17. use yii\filters\AccessControl;
  18. use yii\helpers\Url;
  19. use yii\web\UploadedFile;
  20. use common\library\LMUploadFile;
  21. class UserController extends BasewechatController
  22. {
  23. static $pagenum = 20;
  24. public function behaviors()
  25. {
  26. return [
  27. 'access' => [
  28. 'class' => AccessControl::className(),
  29. 'rules' => [
  30. [
  31. 'actions' => [],
  32. 'allow' => true,
  33. 'roles' => ['@'],
  34. ],
  35. ],
  36. ],
  37. ];
  38. }
  39. public function actions()
  40. {
  41. return [
  42. // 'error' => [
  43. // 'class' => 'yii\web\ErrorAction',
  44. // ],
  45. 'captcha' => [
  46. 'class' => 'yii\captcha\CaptchaAction',
  47. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  48. 'maxLength' => 4, //生成的验证码最大长度
  49. 'minLength' => 4 //生成的验证码最短长度
  50. ],
  51. ];
  52. }
  53. //个人中心
  54. public function actionIndex()
  55. {
  56. $userid = YII::$app->user->id;
  57. $datas = UserInfo::find()->where('uid=:uid',[':uid'=>$userid])->one();
  58. return $this->render('index',['datas'=>$datas]);
  59. }
  60. //用户详情
  61. public function actionUserinfo()
  62. {
  63. $userid = YII::$app->user->id;
  64. $tel = User::find()->where('id=:uid',[':uid'=>$userid])->one();
  65. $datas = UserInfo::find()->where('uid=:uid',[':uid'=>$userid])->one();
  66. $picture_form = new LMUploadFile();
  67. return $this->render('userinfo',['datas'=>$datas,'tel'=>$tel,"picture_form" => $picture_form]);
  68. }
  69. /**
  70. * 上传头像
  71. */
  72. public function actionPortraitimgurl()
  73. {
  74. if (Yii::$app->request->isPost) {
  75. $picture_form = new LMUploadFile();
  76. $picture_form->imageFile = UploadedFile::getInstance($picture_form, 'imageFile');
  77. if ($picture_form->upload('portrait_picture',false,160,160)) {
  78. return "<script language='JavaScript'>window.parent.callback('上传成功!',1,'" . $picture_form->filepath . "');</script>";
  79. } else {
  80. if(isset($picture_form->getErrors()['imageFile'][0])){
  81. $error = $picture_form->getErrors()['imageFile'][0];
  82. }else{
  83. $error = '图片上传失败';
  84. }
  85. return "<script language='JavaScript'>window.parent.callback('上传失败!',0,'" . $error . "');</script>";
  86. }
  87. }
  88. }
  89. //修改头像
  90. public function actionEdit_portrait()
  91. {
  92. $userid = YII::$app->user->id;
  93. $portrait = Yii::$app->request->post('portrait');
  94. $model = UserInfo::find()->where('uid=:id',[':id'=>$userid])->one();
  95. if(!empty($portrait)){
  96. if(!empty($model)){
  97. $model->updated_at = time();
  98. $model->portrait = $portrait;
  99. if($model->validate() && $model->save()){
  100. $result=['sign'=>1,'msg'=>'ok'];
  101. }else{
  102. // var_dump($model->getErrors());exit;
  103. $result=['sign'=>0,'msg'=>'error'];
  104. }
  105. }else{
  106. $result=['sign'=>0,'msg'=>'找不到该规格'];
  107. }
  108. }else{
  109. $result=['sign'=>0,'msg'=>'头像路径不能为空'];
  110. }
  111. return json_encode($result);
  112. }
  113. //修改昵称
  114. public function actionEdit_nickname()
  115. {
  116. $userid = YII::$app->user->id;
  117. $nickname = Yii::$app->request->post('nickname');
  118. $model = UserInfo::find()->where('uid=:id',[':id'=>$userid])->one();
  119. if(!empty($nickname)){
  120. if(!empty($model)){
  121. $model->updated_at = time();
  122. $model->nickname = $nickname;
  123. if($model->validate() && $model->save()){
  124. $result=['sign'=>1,'msg'=>'ok'];
  125. }else{
  126. // var_dump($model->getErrors());exit;
  127. $result=['sign'=>0,'msg'=>'error'];
  128. }
  129. }else{
  130. $result=['sign'=>0,'msg'=>'找不到该规格'];
  131. }
  132. }else{
  133. $result=['sign'=>0,'msg'=>'昵称不能为空'];
  134. }
  135. return json_encode($result);
  136. }
  137. //修改电话
  138. public function actionEdit_tel()
  139. {
  140. $userid = YII::$app->user->id;
  141. $tel = Yii::$app->request->post('tel');
  142. $model = UserInfo::find()->where('uid=:id',[':id'=>$userid])->one();
  143. if(!empty($tel)){
  144. if(!empty($model)){
  145. $model->updated_at = time();
  146. $model->tel = $tel;
  147. if($model->validate() && $model->save()){
  148. $result=['sign'=>1,'msg'=>'ok'];
  149. }else{
  150. // var_dump($model->getErrors());exit;
  151. $result=['sign'=>0,'msg'=>'error'];
  152. }
  153. }else{
  154. $result=['sign'=>0,'msg'=>'找不到该规格'];
  155. }
  156. }else{
  157. $result=['sign'=>0,'msg'=>'昵称不能为空'];
  158. }
  159. return json_encode($result);
  160. }
  161. //设置页面
  162. public function actionAccountmanage()
  163. {
  164. return $this->render('accountmanage');
  165. }
  166. //意见反馈页面
  167. public function actionFeekback()
  168. {
  169. return $this->render('feekback');
  170. }
  171. /**首页意见提交*/
  172. public function actionAdvice(){
  173. $contact = Yii::$app->request->post('contact');
  174. $describe = Yii::$app->request->post('describe');
  175. if(!empty($describe)&&!empty($contact)){
  176. if(preg_match("/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/", $contact)){
  177. $data = new Feedback();
  178. $data->contact = $contact;
  179. $data->content = $describe;
  180. $data->uid = Yii::$app->user->id;
  181. $data->type = Feedback::TYPE_CLIENT;
  182. $data->created_at = time();
  183. $data->updated_at = time();
  184. $data->state = Feedback::STATE_WAIT;
  185. $data->status = Feedback::STATUS_ACTIVE;
  186. if($data->validate() && $data->save()){
  187. $result=['sign'=>1,'msg'=>'已提交意见,待审核'];
  188. }else{
  189. $result=['sign'=>0,'msg'=>'提交失败,请重新提交'];
  190. }
  191. }else{
  192. $result=['sign'=>0,'msg'=>'邮箱格式不正确'];
  193. }
  194. }else{
  195. $result=['sign'=>0,'msg'=>'内容和联系方式不能为空'];
  196. }
  197. return json_encode($result);
  198. }
  199. //关于我们页面
  200. public function actionAboutwe()
  201. {
  202. $model = Information::find()->where(['status'=>Information::STATUS_ACTIVE ,'type'=>Information::ABOUTWE])->offset(0)->limit(1)->orderBy('updated_at DESC')->one();
  203. return $this->render('aboutwe',['model'=>$model]);
  204. }
  205. //我的评价页面
  206. public function actionComment()
  207. {
  208. $userid = YII::$app->user->id;
  209. $datas = Comment::find()->where('uid=:id',[':id'=>$userid])->orderBy('created_at desc')->all();
  210. return $this->render('comment',['datas'=>$datas]);
  211. }
  212. /**回复评价*/
  213. public function actionReply(){
  214. $content = Yii::$app->request->post('content');
  215. $id = Yii::$app->request->post('id');
  216. $pid = Yii::$app->request->post('pid');
  217. $result=['sign'=>0,'msg'=>''];
  218. if(!empty($content)){
  219. if(!empty($id)){
  220. if(!empty($pid)){
  221. $data = Building::find()->where('id=:id',[':id'=>$pid])->one();
  222. if(!empty($data)){
  223. $manager_id = $data->manager_id;
  224. $manager = Manager::find()->where('id=:id',[':id'=>$manager_id])->one();
  225. if(!empty($manager)){
  226. $companyid = $manager->company;
  227. $company = UserCompany::find()->where('uid=:id',[':id'=>$companyid])->one();
  228. if(!empty($company)){
  229. $model = new Reply();
  230. $touserid = $company->uid;
  231. $model->from_userid = Yii::$app->user->id;
  232. $model->cid = $id;
  233. $model->to_userid = $touserid;
  234. $model->content = $content;
  235. $model->created_at = time();
  236. $model->status = Reply::STATUS_ACTIVE;
  237. if($model->validate() && $model->save()){
  238. $result=['sign'=>1,'msg'=>'已回复'];
  239. }else{
  240. $result=['sign'=>0,'msg'=>'回复失败'];
  241. }
  242. }else{
  243. $result=['sign'=>0,'msg'=>'公司不存在'];
  244. }
  245. }else{
  246. $result=['sign'=>0,'msg'=>'公司项目经理不存在'];
  247. }
  248. }else{
  249. $result=['sign'=>0,'msg'=>'房产pid数据有误'];
  250. }
  251. }
  252. }else{
  253. $result=['sign'=>0,'msg'=>'评价id不能为空'];
  254. }
  255. }else{
  256. $result=['sign'=>0,'msg'=>'内容不能为空'];
  257. }
  258. return json_encode($result);
  259. }
  260. //我的预约页面
  261. public function actionAppointment()
  262. {
  263. $cookie = Yii::$app->request->cookies;
  264. $latitude = "";
  265. $longitude = "";
  266. if($cookie->has('localtion'))
  267. {
  268. $lcookie = $cookie->getValue('localtion');
  269. $latitude=$lcookie['lat'];
  270. $longitude=$lcookie['lng'];
  271. }
  272. $userid = YII::$app->user->id;
  273. $datas = Appointment::find()->joinWith('building')->where('bd_appointment.uid=:id',[':id'=>$userid])->orderBy('bd_appointment.booking_time desc')->asArray()->all();
  274. //$datas = Appointment::find()->joinWith('building')->where('bd_appointment.uid=:id and bd_building.posted=:posted and bd_building.status=:status',[':id'=>$userid,':posted'=>Building::POSTED_YES,':status'=>Building::STATUS_SUCCESS])->orderBy('bd_appointment.booking_time desc')->asArray()->all();
  275. $geohash = new ZM_Geohash();
  276. for($i=0;$i<count($datas);$i++){
  277. $buildid = $datas[$i]['pid'];
  278. $images = ImageSource::find()->select('pic')->where('topid=:id and status=:status', [':id' => $buildid, ':status' => ImageSource::STATUS_YES])->andWhere(['type' => [20,30,40,50]])->asArray()->all();
  279. $datas[$i]['image'] = $images;
  280. if(!empty($latitude)&&!empty($longitude))
  281. {
  282. $distance = $geohash->getDistance($latitude,$longitude,$datas[$i]['building']['latitude'],$datas[$i]['building']['longitude']);
  283. $datas[$i]['distance'] = $distance;
  284. }
  285. }
  286. return $this->render('appointment',['datas'=>$datas]);
  287. }
  288. //用户电话绑定页面
  289. public function actionUsertel()
  290. {
  291. $id = Yii::$app->request->get('id');
  292. if(Yii::$app->request->post()){
  293. $userid = YII::$app->user->id;
  294. $tel = Yii::$app->request->post('tel');
  295. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  296. $telcheck = User::find()->where('tel=:tel and id=:id',[':tel'=>$tel,':id'=>$userid])->one();
  297. if(empty($telcheck)) {
  298. $code = Yii::$app->request->post('code');
  299. $mod = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
  300. if(!empty($mod)&&$code==$mod->code){
  301. $model = User::find()->where('id=:id',[':id'=>$userid])->one();
  302. $model->tel = $tel;
  303. $model->updated_at = time();
  304. if($model->validate() && $model->save()){
  305. if(empty($id)){
  306. $this->admin_alert("绑定成功",Url::toRoute(['user/userinfo']));
  307. }else{
  308. if($id=='index'){
  309. $this->admin_alert("绑定成功",Url::toRoute(['site/index']));
  310. }else{
  311. $this->admin_alert("绑定成功",Url::toRoute(['building/detail','id'=>$id]));
  312. }
  313. }
  314. }else{
  315. $this->admin_alert("绑定失败","");
  316. }
  317. }else{
  318. $this->admin_alert("请先获取验证码","");
  319. }
  320. }else{
  321. $this->admin_alert("您已绑定该手机号","");
  322. }
  323. }else{
  324. $this->admin_alert("手机号码格式不正确","");
  325. }
  326. }
  327. return $this->render('usertel',['id'=>$id]);
  328. }
  329. //弹窗提示
  330. function admin_alert($alert,$href=''){
  331. header('Content-type:text/html;charset=utf-8');
  332. if(empty($href)){
  333. exit("<script>alert('{$alert}');history.back();</script>");
  334. }else{
  335. exit("<script>alert('{$alert}');window.location.href='{$href}';</script>");
  336. }
  337. }
  338. //验证注册图片验证码
  339. public function actionYzm(){
  340. $code = Yii::$app->request->post('code');
  341. if(!empty($code)){
  342. //$session = Yii::$app->session;
  343. //$code1 = $session['code'];
  344. $result = $this->createAction('captcha')->validate($code, false);
  345. if($result){
  346. $result=['sign'=>1,'msg'=>'ok'];
  347. }else{
  348. $result=['sign'=>0,'msg'=>'验证码不一致'];
  349. }
  350. }else{
  351. $result=['sign'=>0,'msg'=>'验证码不能为空'];
  352. }
  353. return json_encode($result);
  354. }
  355. }