MemberWechatService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 17/2/23
  6. * Time: 上午11:21
  7. */
  8. namespace app\frontend\modules\member\services;
  9. use app\frontend\modules\member\services\MemberService;
  10. use app\frontend\modules\member\models\MemberWechatModel;
  11. use Illuminate\Session\Store;
  12. class MemberWechatService extends MemberService
  13. {
  14. const LOGIN_TYPE = 4;
  15. private $_app_id;
  16. private $_appSecret;
  17. public function __construct()
  18. {
  19. $this->_init();
  20. }
  21. private function _init()
  22. {
  23. $this->_app_id = '';
  24. $this->_appSecret = '';
  25. }
  26. public function login()
  27. {
  28. $uniacid = \YunShop::app()->uniacid;
  29. $callback = \YunShop::app()->siteroot . 'app/index.php?' . $_SERVER['QUERY_STRING'];
  30. //微信登录
  31. //-------生成唯一随机串防CSRF攻击
  32. $state = md5(uniqid(rand(), TRUE));
  33. session()->put("wx_state", $state);
  34. $callback = urlencode($callback);
  35. $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->_app_id."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
  36. if (!empty(\YunShop::request()->code)) {
  37. $user_info = $this->getUserInfo(\YunShop::request()->code);
  38. if (is_array($user_info) && !empty($user_info['unionid'])) {
  39. $UnionidInfo = MemberUniqueModel::getUnionidInfo($uniacid, $user_info['unionid']);
  40. if (!empty($UnionidInfo['unionid'])) {
  41. $types = explode('|',$UnionidInfo['type']);
  42. $member_id = $UnionidInfo['member_id'];
  43. if (!in_array(self::LOGIN_TYPE, $types)) {
  44. //更新ims_yz_member_unique表
  45. MemberUniqueModel::updateData(array(
  46. 'unique_id'=>$UnionidInfo['unique_id'],
  47. 'type' => $UnionidInfo['type'] . '|' . self::LOGIN_TYPE
  48. ));
  49. //添加yz_member_wechat表
  50. MemberWechatModel::insertData(array(
  51. 'uniacid' => $uniacid,
  52. 'member_id' => $UnionidInfo['member_id'],
  53. 'openid' => $user_info['openid'],
  54. 'nickname' => $user_info['nickname'],
  55. 'avatar' => $user_info['headimgurl'],
  56. 'gender' => $user_info['sex'],
  57. 'nationality' => $user_info['country'],
  58. 'resideprovince' => $user_info['province'] . '省',
  59. 'residecity' => $user_info['city'] . '市',
  60. 'created_at' => time()
  61. ));
  62. }
  63. } else {
  64. //添加ims_mc_member表
  65. $member_id = MemberModel::insertData(array(
  66. 'uniacid' => $uniacid,
  67. 'groupid' => $user_info['unionid'],
  68. 'createtime' => TIMESTAMP,
  69. 'nickname' => $user_info['nickname'],
  70. 'avatar' => $user_info['headimgurl'],
  71. 'gender' => $user_info['sex'],
  72. 'nationality' => $user_info['country'],
  73. 'resideprovince' => $user_info['province'] . '省',
  74. 'residecity' => $user_info['city'] . '市'
  75. ));
  76. //添加ims_yz_member_unique表
  77. MemberUniqueModel::insertData(array(
  78. 'uniacid' => $uniacid,
  79. 'unionid' => $user_info['unionid'],
  80. 'member_id' => $member_id,
  81. 'type' => self::LOGIN_TYPE
  82. ));
  83. //添加yz_member_wechat表
  84. MemberWechatModel::insertData(array(
  85. 'uniacid' => $uniacid,
  86. 'member_id' => $member_id,
  87. 'openid' => $user_info['openid'],
  88. 'nickname' => $user_info['nickname'],
  89. 'avatar' => $user_info['headimgurl'],
  90. 'gender' => $user_info['sex'],
  91. 'nationality' => $user_info['country'],
  92. 'resideprovince' => $user_info['province'] . '省',
  93. 'residecity' => $user_info['city'] . '市',
  94. 'created_at' => time()
  95. ));
  96. }
  97. session()->put('member_id',$member_id);
  98. } else {
  99. show_json(0, array('url'=> $wxurl));
  100. }
  101. } else {
  102. show_json(0, array('url'=> $wxurl));
  103. }
  104. }
  105. /**
  106. * pc端微信登录获取用户信息
  107. *
  108. * @return array|mixed|stdClass
  109. */
  110. public function getUserInfo($code)
  111. {
  112. if (\YunShop::request()->state != session("wx_state")) {
  113. exit("5001");
  114. }
  115. $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->_app_id . '&secret=' . $this->_appSecret . '&code=' . $code . '&grant_type=authorization_code';
  116. $resp = @\Curl::to($token_url)->get();
  117. $token = @json_decode($resp['content'], true);
  118. $userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token['access_token'] . '&openid=' . $token['openid'] . '&lang=zh_CN';
  119. $resp = @\Curl::to($userinfo_url)->get();
  120. $arr = @json_decode($resp['content'], true);
  121. return $arr;
  122. }
  123. /**
  124. * 验证登录状态
  125. *
  126. * @return bool
  127. */
  128. public function checkLogged($login = null)
  129. {
  130. return MemberService::isLogged();
  131. }
  132. }