Base.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. namespace Passport\Lib;
  3. use Dever;
  4. use Upload\Src\Lib\Img;
  5. use Dever\Routing\Uri;
  6. use Dever\Session\Oper as Save;
  7. use Dever\String\Encrypt;
  8. class Base
  9. {
  10. const NAME = 'passportv10';
  11. const CODE = 'code';
  12. const MCODE = 'mcode';
  13. protected $save = false;
  14. protected $save_type = 'session';
  15. public function __construct()
  16. {
  17. //$this->save = new Save(false, 'cookie');
  18. /*
  19. $save_type = $this->save_type = Dever::config('base', 'project')->mobileCode['save'];
  20. if ($this->save_type == 'db') {
  21. $save_type = 'session';
  22. }
  23. */
  24. $save_type = 'session';
  25. $this->save = new Save(false, $save_type);
  26. }
  27. # 返回加密的用户信息
  28. public function getSign($uid, $vid = false, $mobile = false)
  29. {
  30. if ($vid) {
  31. $data['vid'] = $vid;
  32. }
  33. if ($mobile) {
  34. $data['mobile'] = $mobile;
  35. }
  36. $data['uid'] = $uid;
  37. $data['signature'] = Dever::login($uid);
  38. return $data;
  39. }
  40. # 注册用户
  41. public function reg($type, $system_source, $data)
  42. {
  43. $user = $this->getUserExtInfo($data);
  44. $user['source_type'] = $type;
  45. $user['system_source'] = $system_source;
  46. $user['system_id'] = isset($data['system']) ? $data['system'] : 1;
  47. if (isset($data['username'])) {
  48. $user['temp'] = 2;
  49. $user['username'] = $data['username'];
  50. } else {
  51. $user['temp'] = 1;
  52. $user['username'] = '临时用户';
  53. }
  54. if (isset($data['mobile'])) {
  55. $user['bind'] = 1;
  56. $user['mobile'] = $data['mobile'];
  57. } else {
  58. $user['bind'] = 2;
  59. $user['mobile'] = '';
  60. }
  61. $uid = Dever::db('passport/user')->insert($user);
  62. if ($user['system_id']) {
  63. $this->setSystem($uid, $user['system_id']);
  64. }
  65. $this->invite($uid);
  66. $this->createUsername($uid, $user['username'], true);
  67. if (Dever::config('base', 'project')->regAction) {
  68. $user['id'] = $uid;
  69. Dever::load(Dever::config('base', 'project')->regAction, $user);
  70. }
  71. return $uid;
  72. }
  73. # 更新用户
  74. public function updateUser($uid, $data)
  75. {
  76. $user = $this->getUserExtInfo($data);
  77. $user['username'] = $this->createUsername($uid, $data['username']);
  78. if (isset($user) && $user) {
  79. $user['where_id'] = $uid;
  80. Dever::db('passport/user')->update($user);
  81. }
  82. return $uid;
  83. }
  84. private function getUserExtInfo($data)
  85. {
  86. $user = array();
  87. if (isset($data['sex'])) {
  88. $user['sex'] = $data['sex'];
  89. }
  90. if (isset($data['avatar']) && $data['avatar']) {
  91. //$update['avatar'] = $this->saveAvatar($pic);
  92. $user['avatar'] = $data['avatar'];
  93. } else {
  94. # 获取默认头像
  95. $where = array();
  96. if (isset($user['sex']) && $user['sex']) {
  97. $where['sex'] = $user['sex'] . ',4';
  98. }
  99. $avatar = Dever::db('passport/avatar')->rand($where);
  100. if ($avatar) {
  101. $user['avatar_id'] = $avatar['id'];
  102. }
  103. }
  104. if (isset($data['city'])) {
  105. $user['city'] = $data['city'];
  106. }
  107. if (isset($data['province'])) {
  108. $user['province'] = $data['province'];
  109. }
  110. if (isset($data['country'])) {
  111. $user['country'] = $data['country'];
  112. }
  113. if (isset($user['country']) && isset($user['province']) && isset($user['city'])) {
  114. $user['set_area'] = $user['country'] .','. $user['province'] .','. $user['city'];
  115. }
  116. return $user;
  117. }
  118. public function wechat($data, $user = array(), $account, $system, $source_type, $system_source, $source = false, $invite = false, $uid = false)
  119. {
  120. if ($uid <= 0) {
  121. $uid = false;
  122. }
  123. if (!$data['openid']) {
  124. Dever::alert('错误的openid');
  125. }
  126. if (!isset($user['username'])) {
  127. Dever::alert('用户名错误');
  128. }
  129. if (isset($user['sex'])) {
  130. if ($user['sex'] != 1 && $user['sex'] != 2) {
  131. $user['sex'] = 3;
  132. }
  133. }
  134. if (isset($user['country']) && $user['country'] && isset($user['province']) && $user['province'] && isset($user['city']) && $user['city']) {
  135. $user['area'] = $user['country'] .','. $user['province'] .','. $user['city'];
  136. }
  137. $info = Dever::db('passport/wechat')->one(array('openid' => $data['openid']));
  138. if (!$info) {
  139. if (isset($data['unionid']) && $data['unionid']) {
  140. $info = Dever::db('passport/wechat')->one(array('unionid' => $data['unionid']));
  141. if ($info) {
  142. $uid = $info['uid'];
  143. }
  144. $wechat['unionid'] = $data['unionid'];
  145. }
  146. if (!$uid) {
  147. $user['source_type'] = 'service';//即将废弃
  148. $user['system_source'] = 6;
  149. if ($system) {
  150. $user['system_id'] = $system;
  151. }
  152. if ($source_type) {
  153. $user['source_type'] = $source_type;
  154. }
  155. if ($system_source) {
  156. $user['system_source'] = $system_source;
  157. }
  158. $uid = Dever::db('passport/user')->insert($user);
  159. } else {
  160. $user['where_id'] = $uid;
  161. Dever::db('passport/user')->update($user);
  162. }
  163. $wechat['access_token'] = $data['access_token'];
  164. $wechat['openid'] = $data['openid'];
  165. $wechat['expires_in'] = $data['expires_in'];
  166. $wechat['refresh_token'] = $data['refresh_token'];
  167. if ($account) {
  168. $wechat['account_id'] = $account;
  169. }
  170. if ($system) {
  171. $wechat['system_id'] = $system;
  172. }
  173. $wechat['uid'] = $uid;
  174. # 此处整个废弃
  175. if ($source_type) {
  176. # 默认为公众号
  177. $wechat['type'] = 2;//即将废弃,统一
  178. $wechat['system_source'] = 6;
  179. if ($source_type == 'applet') {
  180. $wechat['type'] = 1;//即将废弃,统一
  181. $wechat['system_source'] = 5;
  182. } elseif ($source_type == 'ios') {
  183. $wechat['type'] = 3;//即将废弃,统一
  184. $wechat['system_source'] = 3;
  185. } elseif ($source_type == 'android') {
  186. $wechat['type'] = 4;//即将废弃,统一
  187. $wechat['system_source'] = 2;
  188. }
  189. }
  190. if ($system_source) {
  191. $wechat['system_source'] = $system_source;
  192. }
  193. $id = Dever::db('passport/wechat')->insert($wechat);
  194. if (Dever::project('source') && isset($source) && $source && $source > 0) {
  195. Dever::load('source/lib/core')->saveUser($id, $uid, $source, 'oauth', $account);
  196. }
  197. } else {
  198. $uid = $info['uid'];
  199. if (isset($data['unionid']) && $data['unionid']) {
  200. $wechat['unionid'] = $data['unionid'];
  201. # 判断用户是否存在,是否需要合并
  202. //$wechat['uid'] = $this->combine($uid, $data['unionid']);
  203. }
  204. $wechat['access_token'] = $data['access_token'];
  205. $wechat['openid'] = $data['openid'];
  206. $wechat['expires_in'] = $data['expires_in'];
  207. $wechat['refresh_token'] = $data['refresh_token'];
  208. $wechat['where_id'] = $info['id'];
  209. if ($account) {
  210. $wechat['account_id'] = $account;
  211. }
  212. if ($system) {
  213. $wechat['system_id'] = $system;
  214. }
  215. $id = $info['id'];
  216. //Dever::db('passport/wechat')->update($wechat);
  217. $user['where_id'] = $uid;
  218. //Dever::load('passport/user-update', $user);
  219. }
  220. $this->createUsername($uid, $user['username']);
  221. $user = Dever::load('passport/user-one', $uid);
  222. if ($system) {
  223. $this->setSystem($uid, $system);
  224. }
  225. $this->save($user);
  226. $user['uid'] = $user['id'];
  227. $user['signature'] = Dever::login($user['id']);
  228. return $user;
  229. }
  230. public function setSystem($uid, $system)
  231. {
  232. $where['uid'] = $uid;
  233. $where['system_id'] = $system;
  234. $one = Dever::db('passport/user_system')->find($where);
  235. if (!$one) {
  236. Dever::db('passport/user_system')->insert($where);
  237. }
  238. }
  239. public function setProfession($uid, $profession)
  240. {
  241. $where['uid'] = $uid;
  242. $where['profession'] = $system;
  243. $one = Dever::db('passport/user_profession')->find($where);
  244. if (!$one) {
  245. Dever::db('passport/user_profession')->insert($where);
  246. }
  247. }
  248. /**
  249. * 更新用户信息 绑定用户手机号
  250. *
  251. * @return mixed
  252. */
  253. public function bind_mobile()
  254. {
  255. $uid = $this->check();
  256. //$code = Dever::input('mcode');
  257. $mobile = Dever::load('passport/reg')->checkMobileExists();
  258. //$mobile = Dever::input('mobile');
  259. if ($mobile && $uid) {
  260. $info = Dever::load('passport/user-one', array('mobile' => $mobile));
  261. if ($info && $info['bind'] == 1) {
  262. Dever::alert('该手机号已绑定');
  263. }
  264. $uid = $this->combine($uid, $mobile, 'mobile');
  265. $info = Dever::load('passport/user-one', $uid);
  266. $result['mobile'] = $mobile;
  267. if ($info) {
  268. $update['set_mobile'] = $mobile;
  269. $update['set_bind'] = 1;
  270. $update['where_id'] = $uid;
  271. Dever::load('passport/user-update', $update);
  272. $state = Dever::config('base', 'project')->regSendSms;
  273. if ($state) {
  274. Dever::setInput('skin', $state);
  275. $this->send($mobile, $uid);
  276. }
  277. } else {
  278. Dever::alert('无效的用户id,请重新登录');
  279. }
  280. }
  281. if (!$info['mobile']) {
  282. Dever::score($uid, 'bind_mobile', '绑定手机号');
  283. }
  284. $result['uid'] = $uid;
  285. $result['signature'] = Dever::login($uid);
  286. return $result;
  287. }
  288. public function createUsername($uid, $username, $update = false)
  289. {
  290. if (!$username || $username == '临时用户') {
  291. $username = $uid + 100000;
  292. $username = 'G' . $username;
  293. if ($update) {
  294. Dever::db('passport/user')->update(array('where_id' => $uid, 'username' => $username));
  295. }
  296. }
  297. return $username;
  298. }
  299. # 合并用户
  300. public function combine($uid, $unionid, $col = 'unionid')
  301. {
  302. $cur = $uid;
  303. if ($col == 'mobile' || $col == 'id') {
  304. $user_wechat = Dever::load('passport/user-all', array($col => $unionid));
  305. } else {
  306. $user_wechat = Dever::load('passport/wechat-getByUnionid', array($col => $unionid));
  307. }
  308. if ($user_wechat) {
  309. # 合并去
  310. $drop = array();
  311. $total = count($user_wechat);
  312. if ($total <= 1) {
  313. return $cur;
  314. }
  315. $new = false;
  316. foreach ($user_wechat as $k => $v) {
  317. if (!isset($v['uid'])) {
  318. $v['uid'] = $v['id'];
  319. $user = $v;
  320. } else {
  321. $user = Dever::db('passport/user')->one($v['uid']);
  322. $user['uid'] = $user['id'];
  323. }
  324. if (!$new) {
  325. if ($user['bind'] == 1 && !strstr($user['username'], '****')) {
  326. $new = $user;
  327. } elseif ($user['temp'] == 2 && $user['system_source'] == 5) {
  328. $new = $user;
  329. } elseif ($user['temp'] == 2 && $user['source_type'] == 'applet') {
  330. $new = $user;
  331. } elseif ($user['avatar']) {
  332. $new = $user;
  333. } elseif ($user['mobile']) {
  334. $new = $user;
  335. } else {
  336. $drop[$user['uid']] = $user;
  337. }
  338. } else {
  339. $drop[$user['uid']] = $user;
  340. }
  341. }
  342. $cur = $new;
  343. if (Dever::input('test') == 1) {
  344. print_r($user_wechat);
  345. print_r($cur);
  346. print_r($drop);die;
  347. }
  348. # 异步处理
  349. $this->updateCombine($cur, $drop);
  350. $cur = $new['uid'];
  351. }
  352. return $cur;
  353. }
  354. private function updateCombine($new, $drop)
  355. {
  356. # 队列
  357. //Dever::queue();
  358. if (isset($drop[$new['uid']])) {
  359. unset($drop[$new['uid']]);
  360. }
  361. $drops = array();
  362. if ($drop) {
  363. foreach ($drop as $k => $v) {
  364. $drops[] = $k;
  365. $update = array();
  366. if (!$new['username'] && $v['username']) {
  367. $update['username'] = $v['username'];
  368. } elseif (strstr($new['username'], '****') && $v['username']) {
  369. $update['username'] = $v['username'];
  370. }
  371. if (!$new['avatar'] && $v['avatar']) {
  372. $update['avatar'] = $v['avatar'];
  373. }
  374. if (!$new['mobile'] && $v['mobile']) {
  375. $update['mobile'] = $v['mobile'];
  376. }
  377. if (!$new['area'] && $v['area']) {
  378. $update['area'] = $v['area'];
  379. }
  380. if (isset($update) && $update) {
  381. $update['where_id'] = $new['uid'];
  382. Dever::load('passport/user-update', $update);
  383. }
  384. }
  385. if ($drops) {
  386. asort($drops);
  387. $drop_uid = implode(',', $drops);
  388. //Dever::load('passport/lib/base.handleCombine?new_uid=' . $new['uid'] . '&old_uid=' . $drop_uid);
  389. Dever::daemon('lib/base.handleCombine?new_uid=' . $new['uid'] . '&old_uid=' . $drop_uid, 'passport');
  390. }
  391. }
  392. }
  393. public function handleCombine()
  394. {
  395. $combine = array();
  396. $combine['status'] = 1;
  397. $combine['new_uid'] = Dever::input('new_uid');
  398. $combine['old_uid'] = Dever::input('old_uid');
  399. $info = Dever::db('passport/combine')->one($combine);
  400. if (!$info) {
  401. Dever::db('passport/combine')->insert($combine);
  402. }
  403. Dever::load('passport/user-updates', array('set_state' => 2, 'where_id' => $combine['old_uid']));
  404. Dever::load('passport/wechat-updates', array('set_uid' => $combine['new_uid'], 'where_uid' => $combine['old_uid']));
  405. }
  406. /**
  407. * 检测用户有效性
  408. *
  409. * @return mixed
  410. */
  411. public function check($state = true, $name = 'signature')
  412. {
  413. $signature = Dever::input($name);
  414. $user = Dever::checkLogin($signature, $state);
  415. if ($state && !isset($user['uid'])) {
  416. Dever::alert('user error');
  417. }
  418. if (isset($user['uid']) && $user['uid']) {
  419. return $user['uid'];
  420. }
  421. return -1;
  422. }
  423. public function getToken($request = array())
  424. {
  425. return http_build_query(Dever::token($request));
  426. }
  427. public function getUrl($method, $request = array())
  428. {
  429. return Dever::proxy($method, $this->getToken($request));
  430. }
  431. protected function info()
  432. {
  433. $data = $this->save->get(self::NAME);
  434. return $data;
  435. }
  436. public function code($code = false, $image = true)
  437. {
  438. if ($code) {
  439. $save = $this->save->get(self::CODE);
  440. $state = $code == $save;
  441. if ($state) {
  442. //$this->save->un(self::CODE);
  443. }
  444. return $state;
  445. }
  446. if ($image) {
  447. $code = new Code();
  448. $code->create();
  449. $code = $code->code;
  450. } else {
  451. $code = Dever::code();
  452. }
  453. $this->save->add(self::CODE, $code, 86400);
  454. return $code;
  455. }
  456. public function mcode($mobile, $code = false, $update = 1)
  457. {
  458. if (Dever::project('sms')) {
  459. $send['skin'] = 'code';
  460. $send['mobile'] = $mobile;
  461. if ($code) {
  462. $send['code'] = $code;
  463. $send['update'] = $update;
  464. return Dever::load('sms/api')->checkCode($send['skin'], $send['mobile'], $send['code'], $send['update']);
  465. } else {
  466. $send['state'] = true;
  467. return Dever::load('sms/api.sendCode', $send['skin'], $send['mobile'], $send['state']);
  468. }
  469. }
  470. $day = date('Ymd', time());
  471. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  472. $param['option_day'] = $day;
  473. $param['option_mobile'] = $mobile;
  474. if ($code) {
  475. if ($this->save_type == 'db') {
  476. $check = Dever::db('passport/code')->getNew($param);
  477. if ($check) {
  478. return $mobile . '_' . $code == $check['mobile'] . '_' . $check['code'];
  479. }
  480. return false;
  481. } else {
  482. $save = $this->save->get(self::MCODE);
  483. return $mobile . '_' . $code == $save;
  484. }
  485. }
  486. # 检测当前手机号今天已经发送多少验证码了
  487. $info = Dever::load('passport/code-total', $param);
  488. if ($info >= 1) {
  489. $check = Dever::db('passport/code')->getNew($param);
  490. if ($check) {
  491. if (time() - $check['cdate'] < Dever::config('base', 'project')->mobileCode['time']) {
  492. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  493. }
  494. }
  495. }
  496. $total = Dever::config('base', 'project')->mobileCode['total'];
  497. if ($info >= $total) {
  498. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  499. }
  500. $code = new Code();
  501. $code->createM();
  502. # 记录当前的验证码
  503. $insert['add_mobile'] = $mobile;
  504. $insert['add_day'] = $day;
  505. $insert['add_code'] = $code->mcode;
  506. $id = Dever::load('passport/code-insert', $insert);
  507. # 启动发送
  508. $this->send($mobile, $insert['add_code'], $id);
  509. if ($this->save_type != 'db') {
  510. $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, Dever::config('base', 'project')->mobileCode['timeout']);
  511. }
  512. return $code->mcode;
  513. }
  514. public function send($mobile, $code, $id = false)
  515. {
  516. $url = Dever::config('base', 'project')->mobileCode['url'];
  517. if (!$url) {
  518. return;
  519. }
  520. $content = Dever::config('base', 'project')->mobileCode['body'];
  521. $content = $this->replace($content, $mobile, $code);
  522. parse_str($content, $param);
  523. $type = Dever::config('base', 'project')->mobileCode['method'];
  524. $json = Dever::config('base', 'project')->mobileCode['json'];
  525. $header = Dever::config('base', 'project')->mobileCode['header'];
  526. return Dever::curl($url, $param, $type, $json, $header);
  527. }
  528. private function replace($content, $mobile = '', $code = '')
  529. {
  530. $skin = Dever::config('base', 'project')->mobileCode['skin'];
  531. $skin_key = Dever::input('skin', 'code');
  532. if (isset($skin[$skin_key])) {
  533. $skin = $skin[$skin_key];
  534. } else {
  535. $skin = array_shift($skin);
  536. }
  537. $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
  538. $replace = array($code, $mobile, Dever::config('base', 'project')->mobileCode['sign'], $skin);
  539. return str_replace($config, $replace, $content);
  540. }
  541. protected function refer($state = false)
  542. {
  543. $refer = Dever::input('refer');
  544. $project = 'main';
  545. if ($refer) {
  546. if (!strstr($refer, 'http://')) {
  547. $refer = Encrypt::decode($refer);
  548. }
  549. /*
  550. $url = parse_url(Encrypt::decode($refer));
  551. $url['path'] = preg_replace('/^\//', '', $url['path']);
  552. if (!isset($url['query'])) {
  553. $url['query'] = '';
  554. }
  555. if (Uri::$type == '?') {
  556. $refer = Dever::url(str_replace($url['path'] . Uri::$type, '', $url['query']), $project);
  557. } else {
  558. $refer = Dever::url($url['path'] . '?' . $url['query'], $project);
  559. }
  560. */
  561. } else {
  562. $refer = Dever::url('home', $project);
  563. }
  564. $param = Dever::input('param');
  565. if ($param) {
  566. $refer .= '&' . $param;
  567. }
  568. if ($state) {
  569. return $refer;
  570. }
  571. Dever::out($refer);
  572. }
  573. protected function save($user)
  574. {
  575. if (is_numeric($user)) {
  576. $user = Dever::load('passport/user-one', $user);
  577. }
  578. if ($user && is_array($user)) {
  579. if (Dever::mobile()) {
  580. $time = 30;
  581. } else {
  582. $time = 7;
  583. }
  584. $this->save->add(self::NAME, $user, 3600 * 24 * $time);
  585. $user['signature'] = Dever::login($user['id']);
  586. $this->invite($user['id']);
  587. if (Dever::config('base', 'project')->regAction) {
  588. Dever::load(Dever::config('base', 'project')->regAction, $user);
  589. }
  590. return $user;
  591. }
  592. }
  593. protected function invite($uid)
  594. {
  595. $invite = Dever::input('invite');
  596. if (Dever::project('invite') && $invite) {
  597. Dever::load('invite/api')->setRelation($uid, false, $invite);
  598. }
  599. }
  600. public function createRefer()
  601. {
  602. return 'refer=' . Encrypt::encode(Dever::url(false, 'main'));
  603. }
  604. protected function saveSex($sex)
  605. {
  606. if ($sex || $sex == 0) {
  607. $config_sex = Dever::config('base', 'project')->sex;
  608. if (isset($config_sex[$sex])) {
  609. $sex = $config_sex[$sex];
  610. } else {
  611. if ($sex == '男') {
  612. $sex = 1;
  613. } elseif ($sex == '女') {
  614. $sex = 2;
  615. } elseif ($sex == '未知') {
  616. $sex = 3;
  617. }
  618. }
  619. }
  620. return $sex;
  621. }
  622. protected function saveAvatar($value)
  623. {
  624. if (!$value) {
  625. return '';
  626. }
  627. $value = Dever::pic($value);
  628. $base = Dever::data();
  629. $date = explode('-', date("Y-m-d"));
  630. if (is_array($value)) {
  631. $user = $value[1];
  632. $value = $value[0];
  633. if (is_array($value)) {
  634. $name = md5($value['tmp_name'] . rand(0, 100) . microtime());
  635. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  636. $ext = '.jpg';
  637. $temp = Dever::path($base, $path . $name . $ext);
  638. move_uploaded_file($value['tmp_name'], $temp);
  639. $value = $temp;
  640. }
  641. }
  642. //$result = Dever::$global['host']['img'] . 'chead.jpg';
  643. if ($value) {
  644. # 裁图
  645. $handle = new Img();
  646. $size = '200_200_2';
  647. $user = isset($user) && $user ? $user : $this->init();
  648. if ($user && isset($user['id']) && $user['id']) {
  649. $name = md5($user['id']);
  650. } else {
  651. $name = md5($value . rand(0, 100) . microtime());
  652. }
  653. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  654. $ext = '.jpg';
  655. $file = Dever::path($base, $path . $name . $ext);
  656. $temp = $file . '.temp.jpg';
  657. //$value = Dever::curl($value);
  658. //file_put_contents($temp, file_get_contents($value));
  659. $file = $handle->thumb($value, $size, true, $file);
  660. if ($file) {
  661. $result = str_replace('/upload/', '/', Dever::config('host')->uploadRes . $path . $name . $ext);
  662. } else {
  663. $result = $value;
  664. }
  665. }
  666. return $result;
  667. }
  668. }