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