AController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /*
  3. * 1,在postman中选择验证方式为no auth.
  4. *
  5. * 2,添加header,key为:Authrization,value为Bearer xxxxxxxxxxxxx.
  6. *
  7. * 注意:key的拼写必须一样(包括大小写),value中的Bearer和xxxxxxxx之间至少要有一个空格,xxxxxx为发送的access_token。
  8. */
  9. namespace app\common\controllers;
  10. use app\common\helpers\Identify;
  11. use app\modules\admin\models\Attachment;
  12. use app\modules\admin\models\AttachmentIndex;
  13. use app\modules\admin\models\Config;
  14. use app\modules\ad\models\AdData;
  15. use app\models\EmailCert;
  16. use app\models\MobileCert;
  17. use app\modules\ucenter\models\User;
  18. use yii\rest\ActiveController;
  19. use yii\filters\Cors;
  20. use yii\filters\auth\CompositeAuth;
  21. use yii\filters\auth\HttpBasicAuth;
  22. use yii\filters\auth\HttpBearerAuth;
  23. use yii\filters\auth\QueryParamAuth;
  24. use Yii;
  25. class AController extends ActiveController
  26. {
  27. public $modelClass = 'app\modules\ucenter\models\User';
  28. public $post;
  29. public $get;
  30. public $user;
  31. public $user_id;
  32. public $userInfo;
  33. public $attconfig;
  34. public $ossconfig;
  35. public $baseconfig;
  36. public $wapconfig;
  37. public $imageconfig;
  38. public $csrconfig;
  39. public $higherconfig;
  40. public $payconfig;
  41. public $coinconfig;
  42. public $pointconfig;
  43. public $baiduconfig;
  44. public $openauthconfig;
  45. public $smsconfig;
  46. public $mailerconfig;
  47. public $ueditorconfig;
  48. public $docconfig;
  49. public $mpconfig;
  50. public $wxappconfig;
  51. public $bdappconfig;
  52. public $transferconfig;
  53. public $withdrawconfig;
  54. public $commissionconfig;
  55. public $shoppingconfig;
  56. public $cmsconfig;
  57. public $badwordconfig;
  58. public $posterconfig;
  59. public $docname = '文档';
  60. public $colname = '合辑';
  61. public $optional = [
  62. 'sendverifycode',//发送验证码
  63. 'checkverifycode',//校验验证码
  64. 'register',//注册
  65. 'login',//登录
  66. 'fastlogin',//快速登录
  67. 'resetpwd',//重置密码
  68. 'docmd5check',
  69. 'doctitlecheck',
  70. 'sharedoc',
  71. 'gettag',
  72. 'error',
  73. ];
  74. public function behaviors()
  75. {
  76. $behaviors = parent::behaviors();
  77. // remove authentication filter
  78. unset($behaviors['authenticator']);
  79. // 这个是跨域配置
  80. $behaviors['corsFilter'] = [
  81. 'class' => Cors::class,
  82. 'cors' => [
  83. 'Origin' => CORS_DOMAIN,
  84. 'Access-Control-Request-Method' => ['GET','POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
  85. // Allow only POST and PUT methods
  86. 'Access-Control-Request-Headers' => ['*'],
  87. // Allow only headers 'X-Wsse'
  88. 'Access-Control-Allow-Credentials' => true,
  89. // Allow OPTIONS caching
  90. 'Access-Control-Max-Age' => 0,
  91. // Allow the X-Pagination-Current-Page header to be exposed to the browser.
  92. 'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
  93. ],
  94. ];
  95. $behaviors['authenticator'] = [
  96. 'class' => CompositeAuth::class,
  97. 'authMethods' => [
  98. //HttpBasicAuth::className(),
  99. HttpBearerAuth::class,
  100. //QueryParamAuth::className(),
  101. ],
  102. 'optional' =>$this->optional,
  103. 'except'=> ['options'] //认证排除OPTIONS请求
  104. ];
  105. return $behaviors;
  106. }
  107. public function init()
  108. {
  109. parent::init();
  110. //加入全局配置
  111. $configResultList = Config::find()->all();
  112. if(is_array($configResultList))foreach($configResultList as $configResult)
  113. {
  114. $configName = str_replace('config','',$configResult->name);
  115. $configName = str_replace('attachment','att',$configName).'config';
  116. $this->$configName = string2array($configResult->value);
  117. }
  118. $this->docname = $this->docconfig['docname']?$this->docconfig['docname']:'文档';
  119. $this->colname = $this->docconfig['colname']?$this->docconfig['colname']:'合辑';
  120. if(empty($this->cmsconfig['news_breadname']))$this->cmsconfig['news_breadname'] = '资讯';
  121. if(empty($this->cmsconfig['news_contentname']))$this->cmsconfig['news_contentname'] = '资讯';
  122. if(empty($this->docconfig['breadname']))$this->docconfig['breadname'] = '文档';
  123. if(empty($this->docconfig['docname']))$this->docconfig['docname'] = '文档';
  124. if(empty($this->docconfig['colname']))$this->docconfig['colname'] = '合辑';
  125. if(Yii::$app->request->headers->get('Authorization'))$this->user = User::findIdentityByAccessToken(Yii::$app->request->headers->get('Authorization'));
  126. }
  127. //验证码校验
  128. function checkCode($to,$code)
  129. {
  130. $patternEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
  131. $patternMobile = '/^1[3456789]{1}\d{9}$/';
  132. if(preg_match($patternEmail,$to)) $toType = 'email';
  133. if(preg_match($patternMobile,$to)) $toType='mobile';
  134. if($toType=='mobile')
  135. {
  136. $exist = \app\models\MobileCert::find()->where("mobile='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
  137. }
  138. else if($toType=='email')
  139. {
  140. $exist = \app\models\EmailCert::find()->where("email='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
  141. }
  142. //判断验证码有效期
  143. if($exist&&(TIMESTAMP-$exist->sent_time>$this->smsconfig['certValidTime']*60||$exist->cert==1))
  144. {
  145. return [
  146. 'error' => 1,
  147. 'msg' => '验证码无效',
  148. 'data' => [],
  149. 'code'=>200
  150. ];
  151. }
  152. //判断验证码正确行
  153. if($exist->cert_key!=$code)
  154. {
  155. return [
  156. 'error' => 1,
  157. 'msg' => '验证码错误',
  158. 'data' => [],
  159. 'code'=>200
  160. ];
  161. }
  162. $exist->cert = 1;
  163. $exist->cert_time = TIMESTAMP;
  164. $exist->save();
  165. }
  166. //当前用户是否有权限访问和操作所请求的资源
  167. public function checkRights($user_id,$model,$fild_name='user_id')
  168. {
  169. if ($model->$fild_name != $user_id){
  170. return [
  171. 'error' => 1,
  172. 'msg' => '你只能操作你自己创作的数据',
  173. 'data' => [],
  174. 'code'=>200
  175. ];
  176. }
  177. else
  178. {
  179. return true;
  180. }
  181. }
  182. //当前用户是否有权限访问和操作所请求的资源
  183. public function checkAccess($action, $model = null, $params = [])
  184. {
  185. /* // check if the user can access $action and $model
  186. // throw ForbiddenHttpException if access should be denied
  187. if ($action === 'update' || $action === 'delete') {
  188. if ($model->user_id !== \Yii::$app->user->id)
  189. throw new \yii\web\ForbiddenHttpException(sprintf('You can only %s articles that you\'ve created.', $action));
  190. }*/
  191. }
  192. //禁用一些操作
  193. public function actions()
  194. {
  195. $action= parent::actions(); // TODO: Change the autogenerated stub
  196. unset($action['index']);
  197. unset($action['create']);
  198. unset($action['update']);
  199. unset($action['delete']);
  200. }
  201. /**
  202. * @param $action
  203. * @return bool
  204. * @throws \yii\web\BadRequestHttpException
  205. */
  206. public function beforeAction($action)
  207. {
  208. parent::beforeAction($action);
  209. $this->post = Yii::$app->request->post();
  210. $this->get = Yii::$app->request->get();
  211. if(!empty(Yii::$app->user))
  212. {
  213. $this->user = Yii::$app->user->identity;
  214. $this->user_id = Yii::$app->user->id;
  215. $this->userInfo = Identify::getUserInfo(Yii::$app->user->id);
  216. }
  217. return $action;
  218. }
  219. /**
  220. * validateValue
  221. * @return bool
  222. */
  223. public function validateCaptcha($value)
  224. {
  225. $data = unserialize(Yii::$app->security->decryptByKey(base64_decode($value),Yii::$app->params['authKey']));
  226. if(empty($data['ip']) || $data['ip'] != Yii::$app->request->getUserIP()){
  227. return false;
  228. }
  229. if(empty($data['ua']) || $data['ua'] != crc32(Yii::$app->request->getUserAgent())){
  230. return false;
  231. }
  232. if(empty($data['ttl']) || time() > $data['ttl']) {
  233. return false;
  234. }
  235. return true;
  236. }
  237. //缩略图
  238. public function makeThumb($imagePath,$width,$height)
  239. {
  240. $hash = $imagePath?md5($imagePath):'noimage';
  241. return WEB_URL.'p/'.$width.'/'.$height.'/'.$hash.'.jpg';
  242. }
  243. //对接前端Url
  244. public static function urlList($page)
  245. {
  246. $urls['home'] = REQUEST_FROM==1? WEB_URL: WAP_URL;
  247. $urls['login'] = REQUEST_FROM==1? WEB_URL.'login/': WAP_URL.'login/';
  248. $urls['logout'] = REQUEST_FROM==1? WEB_URL.'logout/': WAP_URL.'logout/';
  249. $urls['ucenter'] = REQUEST_FROM==1? WEB_URL.'ucenter/': WAP_URL.'ucenter/';
  250. $urls['myupload'] = REQUEST_FROM==1? WEB_URL.'myupload/': WAP_URL.'myupload/';
  251. return $urls[$page];
  252. }
  253. }