Login.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Passport\Src;
  3. use Dever;
  4. use Content\Lib\Base;
  5. class Login extends Base
  6. {
  7. /**
  8. * 用户绑定 生成用户信息
  9. *
  10. * @return mixed
  11. */
  12. public function bind()
  13. {
  14. $appid = $this->data['info']['appid'];
  15. $secret = $this->data['info']['secret'];
  16. $url = Dever::config('base')->wechat;
  17. $code = Dever::input('code');
  18. $url .= '?appid=' . $appid;
  19. $url .= '&secret=' . $secret;
  20. $url .= '&js_code=' . $code;
  21. $url .= '&grant_type=authorization_code';
  22. $data = Dever::curl($url);
  23. if (strstr($data, 'errcode')) {
  24. Dever::alert('绑定失败');
  25. }
  26. $data = json_decode($data);
  27. //$data = array();
  28. //$data['openid'] = 2;
  29. //$data['unionid'] = 1;
  30. //$data['session_key'] = 1;
  31. $user = $this->create($data);
  32. return $user;
  33. }
  34. /**
  35. * 更新用户信息
  36. *
  37. * @return mixed
  38. */
  39. public function update()
  40. {
  41. $this->check_user();
  42. $uid = Dever::input('uid');
  43. $name = Dever::input('nickname');
  44. $pic = Dever::input('avatarurl');
  45. $sex = Dever::input('gender');
  46. $city = Dever::input('city');
  47. $mobile = Dever::input('mobile');
  48. $province = Dever::input('province');
  49. $country = Dever::input('country');
  50. $info = Dever::load('passport/user-one', $uid);
  51. if ($info) {
  52. $update['set_name'] = $name;
  53. $update['set_pic'] = $pic;
  54. $update['set_sex'] = $sex;
  55. $update['set_city'] = $city;
  56. $update['set_mobile'] = $mobile;
  57. $update['set_province'] = $province;
  58. $update['set_country'] = $country;
  59. $update['where_id'] = $uid;
  60. Dever::load('passport/user-update', $update);
  61. } else {
  62. Dever::alert('无效的用户id,请重新登录');
  63. }
  64. return $uid;
  65. }
  66. /**
  67. * 生成用户,返回uid
  68. *
  69. * @return int
  70. */
  71. private function create($data)
  72. {
  73. $uid = false;
  74. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  75. if (!$info) {
  76. if (isset($data['unionid']) && $data['unionid']) {
  77. $info = Dever::load('passport/wechat-one', array('option_unionid' => $data['unionid']));
  78. if (!$info) {
  79. $uid = false;
  80. } else {
  81. $uid = $info['uid'];
  82. }
  83. $wechat['add_unionid'] = $data['unionid'];
  84. }
  85. if (!$uid) {
  86. $user['add_name'] = '临时用户';
  87. $uid = Dever::load('passport/user-insert', $user);
  88. }
  89. $wechat['add_openid'] = $data['openid'];
  90. $wechat['add_session_key'] = $data['session_key'];
  91. $wechat['add_uid'] = $uid;
  92. $wechat['add_info_id'] = $this->data['info']['id'];
  93. Dever::load('passport/wechat-insert', $wechat);
  94. } else {
  95. $uid = $info['uid'];
  96. $wechat['set_session_key'] = $data['session_key'];
  97. $wechat['where_id'] = $info['id'];
  98. Dever::load('passport/wechat-update', $wechat);
  99. }
  100. $result['uid'] = $uid;
  101. $result['session'] = $this->session($uid);
  102. return $result;
  103. }
  104. }