PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/applications/native/controller/NativeBaseController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 293 lines | 161 code | 29 blank | 103 comment | 19 complexity | ec3f2426594552a348679c25c79faf2e MD5 | raw file
  1. <?php
  2. /**
  3. * mobile应用基础类
  4. *
  5. * @fileName: NativeBaseController.php
  6. * @author: dongyong<dongyong.ydy@alibaba-inc.com><34214399@qq.com>
  7. * @license: http://www.phpwind.com
  8. * @version: $Id
  9. * @lastchange: 2014-12-17 15:53:58
  10. * @desc:
  11. **/
  12. defined('WEKIT_VERSION') || exit('Forbidden');
  13. abstract class NativeBaseController extends PwBaseController {
  14. protected $uid = 0;
  15. protected $_securityKey = null;
  16. public function beforeAction($handlerAdapter) {
  17. parent::beforeAction($handlerAdapter);
  18. $_config_securityKey = Wekit::C()->getConfigByName('site', 'securityKey');
  19. $this->_securityKey = $_config_securityKey['value'];
  20. //
  21. $this->authSessionKey();
  22. //
  23. //更新来往appKey&appSecret
  24. $this->_getLaiWangSerivce();
  25. }
  26. /**
  27. * 获得第三方平台的appid,用来app生成使用
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. protected function thirdPlatformAppid(){
  33. $config = Wekit::C()->getValues('thirdPlatform');
  34. //
  35. $apidata = array();
  36. if( count($config) ){
  37. foreach($config as $k=>$v){
  38. $_keys = explode('.',$k);
  39. $data[$_keys[0]][$_keys[1]] = $v;
  40. }
  41. foreach($data as $k=>$v){
  42. if( $v['status']==1 ){
  43. $data[$k] = $v['displayOrder'].'-'.$v['appId'];
  44. }else{
  45. unset($data[$k]);
  46. }
  47. }
  48. asort($data);
  49. foreach($data as $k=>$v){
  50. list($order,$appId) = explode('-',$v);
  51. $apidata[] = array(
  52. 'platformname'=>$k,
  53. 'order'=>$order,
  54. 'appId'=>$appId,
  55. );
  56. }
  57. }
  58. return $apidata;
  59. }
  60. /**
  61. * 获得消息助手的通知设置
  62. *
  63. * @access public
  64. * @return void
  65. */
  66. protected function notifierSetting()
  67. {
  68. Wind::import('APPS:native.service.PwLaiWangSerivce');
  69. $config = PwLaiWangSerivce::getNotifier();
  70. // 返回uid,nickname,avatar
  71. return array(
  72. 'uid' => $config['userid'],
  73. 'nickname' => $config['nickname'],
  74. 'avatar' => $config['avatar'],
  75. );
  76. }
  77. /**
  78. * 获得基本用户信息
  79. *
  80. * @param mixed $uid
  81. * @access private
  82. * @return void
  83. */
  84. protected function _getUserInfo($laiwangOK = true){
  85. //
  86. $_userInfo = $this->_getUserAllInfo(PwUser::FETCH_MAIN+PwUser::FETCH_INFO);
  87. //登录成功后,加密身份key
  88. $_idInfo = array(
  89. 'username'=>$_userInfo['username'],
  90. 'password'=>$_userInfo['password'],
  91. );
  92. $securityKey = Pw::encrypt( serialize($_idInfo), $this->_securityKey);
  93. //laiwang
  94. $wk_setting = PwLaiWangSerivce::$wk_setting;
  95. $wk_setting['openid'] = $_userInfo['uid'];
  96. $wk_setting['secretToken'] = PwLaiWangSerivce::getSecretToken($_userInfo['uid'], $_userInfo['password']);
  97. // 是否已经成功同步用户到来往
  98. $wk_setting['laiwangOK'] = $laiwangOK;
  99. //返回数据
  100. $_data = array(
  101. 'securityKey'=>$securityKey,
  102. 'userinfo' =>array(
  103. 'uid'=>$_userInfo['uid'],
  104. 'username'=>$_userInfo['username'],
  105. 'avatar'=>Pw::getAvatar($_userInfo['uid'],'big'),
  106. 'gender'=>$_userInfo['gender'],
  107. ),
  108. 'laiwangSetting'=>$wk_setting,
  109. );
  110. return $_data;
  111. }
  112. /**
  113. * 根据需求获得用户信息
  114. *
  115. * @access protected
  116. * @return void
  117. * @example
  118. * <pre>
  119. * args: PwUser::FETCH_MAIN | PwUser::FETCH_INFO | PwUser::FETCH_DATA | PwUser::FETCH_ALL
  120. * </pre>
  121. */
  122. protected function _getUserAllInfo($range=PwUser::FETCH_MAIN){
  123. return $this->uid?$this->_getUserDs()->getUserByUid($this->uid, $range):array();
  124. }
  125. /**
  126. * Enter description here ...
  127. *
  128. * @return PwCheckVerifyService
  129. */
  130. protected function _getVerifyService() {
  131. return Wekit::load("verify.srv.PwCheckVerifyService");
  132. }
  133. /**
  134. *
  135. * 校验登录用户的sessionid是否合法
  136. *
  137. * @access protected
  138. * @return void
  139. * @example
  140. <pre>
  141. post: session
  142. </pre>
  143. */
  144. protected function checkUserSessionValid(){
  145. if( $this->isLogin() ){
  146. return $this->uid;
  147. }else{
  148. $this->showError("NATIVE:error.sessionkey.error");
  149. }
  150. }
  151. /**
  152. * 认证sessionKey是否合法
  153. *
  154. * @access protected
  155. * @return integer
  156. */
  157. protected function authSessionKey(){
  158. $unsecurityKey = $this->getInput('securityKey');
  159. //
  160. // if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'],'multipart/form-data')!==false){
  161. // //$unsecurityKey = urldecode($unsecurityKey);
  162. // }
  163. if( $unsecurityKey ){
  164. $securityKey = unserialize(Pw::decrypt($unsecurityKey,$this->_securityKey));
  165. if( is_array($securityKey) && isset($securityKey['username']) && isset($securityKey['password']) ){
  166. $_userInfo = $this->_getUserDs()->getUserByName($securityKey['username'], PwUser::FETCH_MAIN);
  167. if( $_userInfo['username']==$securityKey['username'] && $_userInfo['password']==$securityKey['password'] ){
  168. $this->uid = $_userInfo['uid'];
  169. }
  170. }
  171. }
  172. return $this->uid;
  173. }
  174. /**
  175. * 判断是否登录
  176. *
  177. * @access protected
  178. * @return void
  179. */
  180. protected function isLogin(){
  181. return $this->uid;
  182. }
  183. /**
  184. * 关联帐号后,下载第三方平台的头像
  185. *
  186. * @access protected
  187. * @return void
  188. */
  189. protected function downloadThirdPlatformAvatar($uid,$avatar_url){
  190. Wind::import('WSRV:base.WindidUtility');
  191. $image_content = WindidUtility::buildRequest($avatar_url,array(),true,2,'get');
  192. if( $image_content ){
  193. // $temp_file = tempnam(sys_get_temp_dir(),'tmp_');
  194. $temp_file = tempnam(PUBLIC_PATH."data/tmp/",'tmp_');
  195. $handle = fopen($temp_file, "w");
  196. // $tmpdir = sys_get_temp_dir();
  197. // var_dump($temp_file,$handle);
  198. if( $handle ){
  199. $res = fwrite($handle, $image_content);
  200. fclose($handle);
  201. //
  202. Wind::import('WSRV:upload.action.WindidAvatarUpload');
  203. Wind::import('LIB:upload.PwUpload');
  204. $bhv = new WindidAvatarUpload($uid);
  205. $upload = new PwUpload($bhv);
  206. $value= array('name'=>'avatar.jpg','size'=>1024*1024*1,'tmp_name'=>$temp_file);
  207. $file = new PwUploadFile('_0', $value);
  208. $file->filename = $upload->filterFileName($bhv->getSaveName($file));
  209. $file->savedir = $bhv->getSaveDir($file);
  210. $file->store = Wind::getComponent($bhv->isLocal ? 'localStorage' : 'storage');
  211. $file->source = str_replace('attachment','windid/attachment',$file->store->getAbsolutePath($file->filename, $file->savedir) );
  212. if (PwUpload::moveUploadedFile($value['tmp_name'], $file->source)) {
  213. $image = new PwImage($file->source);
  214. if ($bhv->allowThumb()) {
  215. $thumbInfo = $bhv->getThumbInfo($file->filename, $file->savedir);
  216. foreach ($thumbInfo as $key => $value) {
  217. $thumburl = $file->store->getAbsolutePath($value[0], $value[1]);
  218. $thumburl = str_replace('attachment','windid/attachment',$thumburl);
  219. $result = $image->makeThumb($thumburl, $value[2], $value[3], $quality, $value[4], $value[5]);
  220. if ($result === true && $image->filename != $thumburl) {
  221. $ts = $image->getThumb();
  222. }
  223. }
  224. }
  225. }
  226. @unlink($temp_file);
  227. }
  228. }
  229. }
  230. /**
  231. * 第三方平台用户登录校验; 返回用户信息
  232. *
  233. * @access protected
  234. * @return array()
  235. * @example
  236. * <pre>
  237. * //post: access_token&platformname&native_name&oauth_uid(sina use) <br>
  238. * post: access_token&platformname&oauth_uid(sina use)(如果直接传递token就只需要二个参数)
  239. * </pre>
  240. */
  241. protected function authThirdPlatform(){
  242. $_oauth = Wekit::load("APPS:native.service.PwThirdOpenPlatformService");
  243. $_oauth->access_token = $this->getInput('access_token');
  244. $_oauth->third_platform_name = $this->getInput('platformname');
  245. $_oauth->oauth_uid = $this->getInput('oauth_uid');
  246. //
  247. $info = array();
  248. $_method_name = $_oauth->third_platform_name.'AuthInfo';
  249. if( method_exists($_oauth,$_method_name) ){
  250. $info = $_oauth->$_method_name();
  251. }
  252. if( empty($info) ){
  253. $this->showError('NATIVE:error.openaccount.noauth');
  254. }
  255. $info['type'] = $_oauth->third_platform_name;
  256. return $info;
  257. }
  258. protected function _getUserDs(){
  259. return Wekit::load('user.PwUser');
  260. }
  261. protected function _getLaiWangSerivce(){
  262. return Wekit::load("APPS:native.service.PwLaiWangSerivce");
  263. }
  264. }