Connectwx.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Connectwx extends BaseMall
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/connectwx.lang.php');
  23. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/login-register.lang.php');
  24. }
  25. /**
  26. * 微信登录
  27. */
  28. public function index(){
  29. if(empty(input('get.code'))) {
  30. echo View::fetch($this->template_dir.'index');
  31. } else {
  32. $this->get_info();
  33. }
  34. }
  35. /**
  36. * 微信注册后修改密码
  37. */
  38. public function edit_info() {
  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. $code = input('get.code');
  99. if(!empty($code)) {
  100. $user_info = $this->get_user_info($code);
  101. if(!empty($user_info['unionid'])) {
  102. $unionid = $user_info['unionid'];
  103. $member_model = model('member');
  104. $member = $member_model->getMemberInfo(array('member_wxunionid'=> $unionid));
  105. if(!empty($member)) {//会员信息存在时自动登录
  106. if (!$member['member_state']) {//1为启用 0 为禁用
  107. $this->error(lang('login_index_account_stop'));
  108. }
  109. $member_model->createSession($member);
  110. //是否有卖家账户
  111. $seller_model = model('seller');
  112. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
  113. if ($seller_info) {
  114. // 更新卖家登陆时间
  115. $seller_model->editSeller(array('last_logintime' => TIMESTAMP), array('seller_id' => $seller_info['seller_id']));
  116. $sellergroup_model = model('sellergroup');
  117. $seller_group_info = $sellergroup_model->getSellergroupInfo(array('sellergroup_id' => $seller_info['sellergroup_id']));
  118. $store_model = model('store');
  119. $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);
  120. $seller_model->createSellerSession($member, $store_info, $seller_info, is_array($seller_group_info) ? $seller_group_info : array());
  121. }
  122. $this->success(lang('login_index_login_success'),'member/index');
  123. }
  124. if(session('member_id')) {//已登录时绑定微信
  125. $member_id = session('member_id');
  126. $member = array();
  127. $member['member_wxunionid'] = $unionid;
  128. $member['member_wxinfo'] = $user_info['member_wxinfo'];
  129. $member_model->editMember(array('member_id'=> $member_id),$member,$member_id);
  130. $this->success(lang('wechat_binding_was_successful'),'member/index');
  131. } else {
  132. if(config('ds_config.auto_register')){//如果开启了自动注册
  133. //自动注册会员并登录
  134. $logic_connect_api = model('connectapi', 'logic');
  135. $reg_info = array(
  136. // 'member_wxopenid'=>$user_info['openid'], #开发者帐号唯一标识,与公众号标识不同
  137. 'member_wxopenid'=>'',
  138. 'member_wxunionid'=>$user_info['unionid'],
  139. 'nickname'=> isset($user_info['nickname'])?$user_info['nickname']:'',
  140. 'headimgurl'=> isset($user_info['headimgurl'])?$user_info['headimgurl']:'',#提高体验暂时不对图片进行处理
  141. );
  142. $wx_member = $logic_connect_api->wx_register($reg_info,'wx');
  143. if (!empty($wx_member)) {
  144. if (!$wx_member['member_state']) {//1为启用 0 为禁用
  145. $this->error(lang('login_index_account_stop'));
  146. }
  147. $member_model->createSession($wx_member, true); //自动登录
  148. $success_message = lang('login_index_login_success');
  149. $this->success($success_message, HOME_SITE_URL);
  150. }else{
  151. $this->error(lang('login_usersave_regist_fail'), 'login/register'); //"会员注册失败"
  152. }
  153. }else{
  154. View::assign('wxuser_info', $user_info);
  155. View::assign('headimgurl', $user_info['headimgurl']);
  156. View::assign('password', '');
  157. echo View::fetch($this->template_dir . 'register');exit;
  158. }
  159. }
  160. }
  161. }
  162. $this->error(lang('wechat_binding_was_failed'),'login/login');
  163. }
  164. /**
  165. * 获取用户个人信息
  166. */
  167. public function get_user_info($code){
  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. }