Connectwx.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Connectwx extends BaseMall
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/connectwx.lang.php');
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/login-register.lang.php');
  19. }
  20. /**
  21. * 微信登录
  22. */
  23. public function index()
  24. {
  25. if (empty(input('get.code'))) {
  26. echo View::fetch($this->template_dir . 'index');
  27. } else {
  28. $this->get_info();
  29. }
  30. }
  31. /**
  32. * 微信注册后修改密码
  33. */
  34. public function edit_info()
  35. {
  36. $member_model = model('member');
  37. $type = input('param.type');
  38. $user = input('param.user');
  39. $password = input('param.password');
  40. $password2 = input('param.password2');
  41. $reg_info = array(
  42. // 'member_wxopenid'=>$user_info['openid'], #开发者帐号唯一标识,与公众号标识不同
  43. 'member_wxopenid' => '',
  44. 'member_wxunionid' => input('param.unionid'),
  45. 'nickname' => input('param.nickname'),
  46. 'headimgurl' => input('param.headimgurl'), #提高体验暂时不对图片进行处理
  47. );
  48. $data = array(
  49. 'member_name' => $user,
  50. 'member_password' => $password,
  51. 'member_wxopenid' => $reg_info['member_wxopenid'],
  52. 'member_wxunionid' => $reg_info['member_wxunionid'],
  53. 'member_wxinfo' => serialize($reg_info),
  54. 'member_nickname' => $reg_info['nickname'],
  55. );
  56. if ($type == 1) { //注册
  57. $login_validate = ds_validate('member');
  58. if (!$login_validate->scene('register')->check($data)) {
  59. $this->error($login_validate->getError());
  60. }
  61. $member_info = $member_model->register($data);
  62. if (!isset($member_info['error'])) {
  63. $member_model->createSession($member_info, true);
  64. // $headimgurl = $reg_info['headimgurl'];
  65. // $avatar = @copy($headimgurl, BASE_UPLOAD_PATH . '/' . ATTACH_AVATAR . "/avatar_" . $member_info['member_id'] . ".jpg");
  66. // if ($avatar) {
  67. // $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_avatar' => "avatar_" . $member_info['member_id'] . ".jpg"),$member_info['member_id']);
  68. // }
  69. } else {
  70. $this->error($member_info['error']);
  71. }
  72. } else { //绑定
  73. $login_validate = ds_validate('member');
  74. if (!$login_validate->scene('login')->check($data)) {
  75. ds_json_encode(10001, $login_validate->getError());
  76. }
  77. $map = array(
  78. 'member_name' => $data['member_name'],
  79. 'member_password' => md5($data['member_password']),
  80. );
  81. $member_info = $member_model->getMemberInfo($map);
  82. if ($member_info) {
  83. $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_wxunionid' => $data['member_wxunionid'], 'member_wxinfo' => $data['member_wxinfo']), $member_info['member_id']);
  84. } else {
  85. $this->error(lang('login_register_bind_fail'));
  86. }
  87. $member_model->createSession($member_info, true);
  88. }
  89. $this->success(lang('ds_common_save_succ'), HOME_SITE_URL);
  90. }
  91. /**
  92. * 回调获取信息
  93. */
  94. public function get_info()
  95. {
  96. $code = input('get.code');
  97. if (!empty($code)) {
  98. $user_info = $this->get_user_info($code);
  99. if (!empty($user_info['unionid'])) {
  100. $unionid = $user_info['unionid'];
  101. $member_model = model('member');
  102. $member = $member_model->getMemberInfo(array('member_wxunionid' => $unionid));
  103. if (!empty($member)) { //会员信息存在时自动登录
  104. if (!$member['member_state']) { //1为启用 0 为禁用
  105. $this->error(lang('login_index_account_stop'));
  106. }
  107. $member_model->createSession($member);
  108. //是否有卖家账户
  109. $seller_model = model('seller');
  110. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
  111. if ($seller_info) {
  112. // 更新卖家登陆时间
  113. $seller_model->editSeller(array('last_logintime' => TIMESTAMP), array('seller_id' => $seller_info['seller_id']));
  114. $sellergroup_model = model('sellergroup');
  115. $seller_group_info = $sellergroup_model->getSellergroupInfo(array('sellergroup_id' => $seller_info['sellergroup_id']));
  116. $store_model = model('store');
  117. $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);
  118. $seller_model->createSellerSession($member, $store_info, $seller_info, is_array($seller_group_info) ? $seller_group_info : array());
  119. }
  120. $this->success(lang('login_index_login_success'), 'member/index');
  121. }
  122. if (session('member_id')) { //已登录时绑定微信
  123. $member_id = session('member_id');
  124. $member = array();
  125. $member['member_wxunionid'] = $unionid;
  126. $member['member_wxinfo'] = $user_info['member_wxinfo'];
  127. $member_model->editMember(array('member_id' => $member_id), $member, $member_id);
  128. $this->success(lang('wechat_binding_was_successful'), 'member/index');
  129. } else {
  130. if (config('ds_config.auto_register')) { //如果开启了自动注册
  131. //自动注册会员并登录
  132. $logic_connect_api = model('connectapi', 'logic');
  133. $reg_info = array(
  134. // 'member_wxopenid'=>$user_info['openid'], #开发者帐号唯一标识,与公众号标识不同
  135. 'member_wxopenid' => '',
  136. 'member_wxunionid' => $user_info['unionid'],
  137. 'nickname' => isset($user_info['nickname']) ? $user_info['nickname'] : '',
  138. 'headimgurl' => isset($user_info['headimgurl']) ? $user_info['headimgurl'] : '', #提高体验暂时不对图片进行处理
  139. );
  140. $wx_member = $logic_connect_api->wx_register($reg_info, 'wx');
  141. if (!empty($wx_member)) {
  142. if (!$wx_member['member_state']) { //1为启用 0 为禁用
  143. $this->error(lang('login_index_account_stop'));
  144. }
  145. $member_model->createSession($wx_member, true); //自动登录
  146. $success_message = lang('login_index_login_success');
  147. $this->success($success_message, HOME_SITE_URL);
  148. } else {
  149. $this->error(lang('login_usersave_regist_fail'), 'login/register'); //"会员注册失败"
  150. }
  151. } else {
  152. View::assign('wxuser_info', $user_info);
  153. View::assign('headimgurl', $user_info['headimgurl']);
  154. View::assign('password', '');
  155. echo View::fetch($this->template_dir . 'register');
  156. exit;
  157. }
  158. }
  159. }
  160. }
  161. $this->error(lang('wechat_binding_was_failed'), 'login/login');
  162. }
  163. /**
  164. * 获取用户个人信息
  165. */
  166. public function get_user_info($code)
  167. {
  168. $weixin_appid = config('ds_config.weixin_appid');
  169. $weixin_secret = config('ds_config.weixin_secret');
  170. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $weixin_appid . '&secret=' . $weixin_secret . '&code=' . $code . '&grant_type=authorization_code';
  171. $access_token = http_request($url); //通过code获取access_token
  172. $code_info = json_decode($access_token, true);
  173. $user_info = array();
  174. if (!empty($code_info['access_token'])) {
  175. $token = $code_info['access_token'];
  176. $openid = $code_info['openid'];
  177. $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token . '&openid=' . $openid;
  178. $result = http_request($url); //获取用户个人信息
  179. $user_info = json_decode($result, true);
  180. $member_wxinfo = array();
  181. $member_wxinfo['member_wxunionid'] = $user_info['unionid'];
  182. $member_wxinfo['nickname'] = $user_info['nickname'];
  183. $member_wxinfo['member_wxopenid'] = $user_info['openid']; //普通用户的标识,对当前开发者帐号唯一 不是公众号的 openid
  184. $user_info['member_wxinfo'] = serialize($member_wxinfo);
  185. }
  186. return $user_info;
  187. }
  188. }