Login.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. return $url;
  23. $data = Dever::curl($url);
  24. if (strstr($data, 'errcode')) {
  25. Dever::alert($data);
  26. }
  27. $data = json_decode($data);
  28. //$data = array();
  29. //$data['openid'] = 2;
  30. //$data['unionid'] = 1;
  31. //$data['session_key'] = 1;
  32. $user = $this->create($data);
  33. return $user;
  34. }
  35. /**
  36. * 更新用户信息
  37. *
  38. * @return mixed
  39. */
  40. public function update()
  41. {
  42. $this->check_user();
  43. $uid = Dever::input('uid');
  44. $name = Dever::input('nickname');
  45. $pic = Dever::input('avatarurl');
  46. $sex = Dever::input('gender');
  47. $city = Dever::input('city');
  48. $mobile = Dever::input('mobile');
  49. $province = Dever::input('province');
  50. $country = Dever::input('country');
  51. $info = Dever::load('passport/user-one', $uid);
  52. if ($info) {
  53. $update['set_name'] = $name;
  54. $update['set_pic'] = $pic;
  55. $update['set_sex'] = $sex;
  56. $update['set_city'] = $city;
  57. $update['set_mobile'] = $mobile;
  58. $update['set_province'] = $province;
  59. $update['set_country'] = $country;
  60. $update['where_id'] = $uid;
  61. Dever::load('passport/user-update', $update);
  62. } else {
  63. Dever::alert('无效的用户id,请重新登录');
  64. }
  65. return $uid;
  66. }
  67. /**
  68. * 生成用户,返回uid
  69. *
  70. * @return int
  71. */
  72. private function create($data)
  73. {
  74. $uid = false;
  75. $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
  76. if (!$info) {
  77. if (isset($data['unionid']) && $data['unionid']) {
  78. $info = Dever::load('passport/wechat-one', array('option_unionid' => $data['unionid']));
  79. if (!$info) {
  80. $uid = false;
  81. } else {
  82. $uid = $info['uid'];
  83. }
  84. $wechat['add_unionid'] = $data['unionid'];
  85. }
  86. if (!$uid) {
  87. $user['add_name'] = '临时用户';
  88. $uid = Dever::load('passport/user-insert', $user);
  89. }
  90. $wechat['add_openid'] = $data['openid'];
  91. $wechat['add_session_key'] = $data['session_key'];
  92. $wechat['add_uid'] = $uid;
  93. $wechat['add_info_id'] = $this->data['info']['id'];
  94. Dever::load('passport/wechat-insert', $wechat);
  95. } else {
  96. $uid = $info['uid'];
  97. $wechat['set_session_key'] = $data['session_key'];
  98. $wechat['where_id'] = $info['id'];
  99. Dever::load('passport/wechat-update', $wechat);
  100. }
  101. $result['uid'] = $uid;
  102. $result['session'] = $this->session($uid);
  103. return $result;
  104. }
  105. }