SiteController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace api\controllers;
  3. use common\library\UrlHelper;
  4. use common\models\Information;
  5. use common\models\System;
  6. use common\models\User;
  7. use common\models\Wallet;
  8. use Yii;
  9. use yii\base\InvalidParamException;
  10. use yii\web\BadRequestHttpException;
  11. use yii\web\Controller;
  12. use yii\filters\VerbFilter;
  13. use yii\filters\AccessControl;
  14. use common\models\LoginForm;
  15. use frontend\models\PasswordResetRequestForm;
  16. use frontend\models\ResetPasswordForm;
  17. use frontend\models\SignupForm;
  18. use frontend\models\ContactForm;
  19. /**
  20. * Site controller
  21. */
  22. class SiteController extends Controller
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public function behaviors()
  28. {
  29. return [
  30. 'access' => [
  31. 'class' => AccessControl::className(),
  32. 'only' => ['logout', 'signup'],
  33. 'rules' => [
  34. [
  35. 'actions' => ['signup','sendcode','register'],
  36. 'allow' => true,
  37. 'roles' => ['?'],
  38. ],
  39. [
  40. 'actions' => ['logout'],
  41. 'allow' => true,
  42. 'roles' => ['@'],
  43. ],
  44. ],
  45. ],
  46. 'verbs' => [
  47. 'class' => VerbFilter::className(),
  48. 'actions' => [
  49. 'logout' => ['post'],
  50. 'sendcode'=>['post'],
  51. 'register'=>['post']
  52. ],
  53. ],
  54. ];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function actions()
  60. {
  61. return [
  62. 'error' => [
  63. 'class' => 'yii\web\ErrorAction',
  64. ],
  65. 'captcha' => [
  66. 'class' => 'yii\captcha\CaptchaAction',
  67. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  68. ],
  69. ];
  70. }
  71. /**
  72. * Displays homepage.
  73. *
  74. * @return mixed
  75. */
  76. public function actionIndex()
  77. {
  78. return $this->render('index');
  79. }
  80. /**
  81. * Logs in a user.
  82. *
  83. * @return mixed
  84. */
  85. public function actionLogin()
  86. {
  87. if (!Yii::$app->user->isGuest) {
  88. return $this->goHome();
  89. }
  90. $model = new LoginForm();
  91. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  92. return $this->goBack();
  93. } else {
  94. return $this->render('login', [
  95. 'model' => $model,
  96. ]);
  97. }
  98. }
  99. /**
  100. * banner跳转页面
  101. */
  102. public function actionHtmlpage(){
  103. $this->layout="main";
  104. $artid = intval(Yii::$app->request->get('id'));
  105. $info =Information::findOne($artid);
  106. return $this->render('myhtml',['content'=>$info]);
  107. }
  108. /**
  109. * 推荐有奖页面
  110. */
  111. public function actionAward(){
  112. $this->layout=false;
  113. return $this->render('register');
  114. }
  115. /**
  116. * 发送验证码
  117. */
  118. public function actionSendcode(){
  119. $tel = Yii::$app->request->post('tel');
  120. $str = 'salon'.$tel;
  121. $sige= md5($str);
  122. $post_data = array(
  123. 'username' => $tel,
  124. 'type'=>1,
  125. 'sign'=>$sige
  126. );
  127. $result =json_decode($this->send_post('http://api.jiehengtech.com/v1/telcode/get?response_code=json', $post_data),true);
  128. $code = $result['data']['code'];
  129. $msg=$result['data']['message'];
  130. if($code==1){
  131. return json_encode(['sign' => '0','msg'=>$msg]);
  132. }
  133. return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
  134. }
  135. /**
  136. * 注册验证
  137. */
  138. public function actionRegister(){
  139. $tel = Yii::$app->request->post('username');
  140. $code = Yii::$app->request->post('code');
  141. $pass = Yii::$app->request->post('password');
  142. $id = Yii::$app->request->post('id');//注册人的id
  143. $post_data = array(
  144. 'username' => $tel,
  145. 'code'=>$code,
  146. );
  147. //检验验证码是否正确
  148. $result =json_decode($this->send_post('http://api.jiehengtech.com/v1/user/register?response_code=json', $post_data),true);
  149. $code = $result['data']['code'];
  150. if($code==1){
  151. return json_encode(['sign' => '0','msg'=>'验证码错误!']);
  152. }
  153. else{
  154. //验证码验证通过
  155. //调用输入密码注册接口
  156. $post_data2 = array(
  157. 'username' => $tel,
  158. 'password'=>$pass,
  159. 'password2'=>$pass,
  160. 'recommend'=>$id
  161. );
  162. $result2 =json_decode($this->send_post('http://api.jiehengtech.com/v1/user/register2?response_code=json', $post_data2),true);
  163. $code = $result2['data']['code'];
  164. $message = $result2['data']['message'];
  165. if($code==1){
  166. return json_encode(['sign' => '0','msg'=>$message]);
  167. }
  168. //获取推荐人id
  169. if(!empty($id)){
  170. //录入推荐人奖励信息
  171. $set = System::find()->where(array('name'=>'earning'))->one();
  172. $award = $set->content ;//推荐每个人的奖励
  173. //查看推荐人是否存在
  174. $referals = User::find()->where(array('id'=>$id,'role'=>User::ROLE_CUSTOMER))->one();
  175. if(!empty($referals)){
  176. $wallet = Wallet::find()->where(array('uid'=>$referals->id))->one();
  177. if(empty($wallet)){
  178. //钱包不存在
  179. $wallet = new Wallet();
  180. $wallet->uid=Yii::$app->user->id;
  181. $wallet->amount=$award;
  182. $wallet->updated_at=time();
  183. $wallet->referrals=1;
  184. $wallet->allaward=$award;
  185. if(!$wallet->save()){
  186. //保存推荐人信息失败
  187. }
  188. }
  189. else{
  190. $wallet->updateCounters(['amount'=>$award,'referrals'=>1,'allaward'=>$award]);
  191. }
  192. }
  193. }
  194. return json_encode(['sign' => '1','msg'=>'注册成功!']);
  195. }
  196. }
  197. public function actionDownload(){
  198. return $this->render('download');
  199. }
  200. /**
  201. * 发送post请求
  202. * @param string $url 请求地址
  203. * @param array $post_data post键值对数据
  204. * @return string
  205. */
  206. function send_post($url, $post_data) {
  207. $postdata = http_build_query($post_data);
  208. $options = array(
  209. 'http' => array(
  210. 'method' => 'POST',
  211. 'header' => 'Content-type:application/x-www-form-urlencoded',
  212. 'content' => $postdata,
  213. 'timeout' => 15 * 60 // 超时时间(单位:s)
  214. )
  215. );
  216. $context = stream_context_create($options);
  217. $result = file_get_contents($url, false, $context);
  218. return $result;
  219. }
  220. /**
  221. * Logs out the current user.
  222. *
  223. * @return mixed
  224. */
  225. public function actionLogout()
  226. {
  227. Yii::$app->user->logout();
  228. return $this->goHome();
  229. }
  230. /**
  231. * Displays contact page.
  232. *
  233. * @return mixed
  234. */
  235. public function actionContact()
  236. {
  237. $model = new ContactForm();
  238. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  239. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  240. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  241. } else {
  242. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  243. }
  244. return $this->refresh();
  245. } else {
  246. return $this->render('contact', [
  247. 'model' => $model,
  248. ]);
  249. }
  250. }
  251. /**
  252. * Displays about page.
  253. *
  254. * @return mixed
  255. */
  256. public function actionAbout()
  257. {
  258. return $this->render('about');
  259. }
  260. /**
  261. * Signs user up.
  262. *
  263. * @return mixed
  264. */
  265. public function actionSignup()
  266. {
  267. $model = new SignupForm();
  268. if ($model->load(Yii::$app->request->post())) {
  269. if ($user = $model->signup()) {
  270. if (Yii::$app->getUser()->login($user)) {
  271. return $this->goHome();
  272. }
  273. }
  274. }
  275. return $this->render('signup', [
  276. 'model' => $model,
  277. ]);
  278. }
  279. /**
  280. * Requests password reset.
  281. *
  282. * @return mixed
  283. */
  284. public function actionRequestPasswordReset()
  285. {
  286. $model = new PasswordResetRequestForm();
  287. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  288. if ($model->sendEmail()) {
  289. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  290. return $this->goHome();
  291. } else {
  292. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  293. }
  294. }
  295. return $this->render('requestPasswordResetToken', [
  296. 'model' => $model,
  297. ]);
  298. }
  299. /**
  300. * Resets password.
  301. *
  302. * @param string $token
  303. * @return mixed
  304. * @throws BadRequestHttpException
  305. */
  306. public function actionResetPassword($token)
  307. {
  308. try {
  309. $model = new ResetPasswordForm($token);
  310. } catch (InvalidParamException $e) {
  311. throw new BadRequestHttpException($e->getMessage());
  312. }
  313. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  314. Yii::$app->session->setFlash('success', 'New password saved.');
  315. return $this->goHome();
  316. }
  317. return $this->render('resetPassword', [
  318. 'model' => $model,
  319. ]);
  320. }
  321. }