Connectwx.php 9.7 KB

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