MemberPcOfficeAccountService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\common\facades\EasyWeChat;
  10. use app\common\models\AccountWechats;
  11. use app\common\services\Session;
  12. use app\frontend\models\Member;
  13. use EasyWeChat\Foundation\Application;
  14. use Illuminate\Support\Facades\Redis;
  15. use app\common\helpers\Url;
  16. class MemberPcOfficeAccountService extends MemberService
  17. {
  18. const LOGIN_TYPE = 5;
  19. const IS_PC_QRCODE = 1;
  20. const WE_CHAT_SHOW_QR_CODE_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
  21. private $config;
  22. private $scene;
  23. public function __construct()
  24. {
  25. $this->config = '';
  26. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('wechat_qrcode_config'))) {
  27. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('wechat_qrcode_config'), 'class');
  28. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('wechat_qrcode_config'), 'function');
  29. $this->config = $class::$function();
  30. }
  31. return $this->config;
  32. }
  33. //验证是否能扫码登录
  34. public function checkLogin(){
  35. $arr = array('status' => 0);
  36. if(empty($this->config)){
  37. $arr = ['status'=>1,'msg'=>'不支持扫码登录'];
  38. }else if($this->config['is_open'] == 0){
  39. $arr = ['status'=>1,'msg'=>'未开启扫码登录'];
  40. }
  41. return $arr;
  42. }
  43. public function login()
  44. {
  45. $check = $this->checkLogin();
  46. if($check['status'] == 1) {
  47. exit("5001" . $check['msg']);
  48. }
  49. $pc_token = \YunShop::request()->pc_token;
  50. $yz_redirect = request()->yz_redirect;
  51. $uniacid = \YunShop::app()->uniacid;
  52. $mid = \app\common\models\Member::getMid();
  53. if($pc_token){
  54. if(Redis::get($pc_token)){
  55. return self::redirectUrl( Redis::get($pc_token.'member_id'),$uniacid, $mid, $yz_redirect); //登录成功 跳转会员中心
  56. }else{
  57. return show_json(10, '登录失败'); //todo status类型待优化
  58. }
  59. }else{
  60. return show_json(11, '生成二维码链接成功',array('account_url'=> $this->getQrCodeUrl(), 'pc_token' => $this->scene));
  61. }
  62. }
  63. private function getQrCodeUrl()
  64. {
  65. return static::WE_CHAT_SHOW_QR_CODE_URL . $this->getTicket();
  66. }
  67. /**
  68. * 生成公众号临时二维码,默认120s到期
  69. * @param $scene
  70. * @return mixed
  71. */
  72. private function createQR()
  73. {
  74. $account = AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid);
  75. $options = [
  76. 'app_id' => $account->key,
  77. 'secret' => $account->secret,
  78. ];
  79. $app = EasyWeChat::officialAccount($options);
  80. $qrcode = $app->qrcode;
  81. $result = $qrcode->temporary($this->getSceneValue(), 120);
  82. return $result;
  83. }
  84. public function checkLogged($login = null)
  85. {
  86. return MemberService::isLogged();
  87. }
  88. private function getTicket()
  89. {
  90. return self::createQR()['ticket'];
  91. }
  92. /**
  93. * 获取唯一场景值
  94. * @return string
  95. */
  96. private function getSceneValue()
  97. {
  98. $scene = sha1(rand(0,999999));
  99. $result = Redis::get($scene);
  100. if(!$result){
  101. Redis::setex($scene, 120, 0); //0 = 生成二维码未扫码
  102. $this->scene = $scene;
  103. return $scene;
  104. }else{
  105. $this->getSceneValue();
  106. }
  107. }
  108. /**
  109. * 扫码跳转到商城
  110. * @param $member_id
  111. * @param $uniacid
  112. * @param $mid
  113. */
  114. private function redirectUrl($member_id,$uniacid,$mid,$redirect_url){
  115. Session::set('member_id',$member_id);
  116. if($redirect_url){
  117. $url = base64_decode($redirect_url);
  118. }else{
  119. $url = Url::absoluteApp('member', ['i' => $uniacid, 'mid' => $mid]);//默认会员中心
  120. }
  121. $mobile = Member::where('uid', $member_id)->value('mobile');
  122. $pc_bind_mobile = 0;
  123. if(empty($mobile) && $this->config['pc_bind_mobile']){
  124. $pc_bind_mobile = 1;
  125. }
  126. return show_json(1,'登陆成功', ['url' => $url, 'pc_bind_mobile' => $pc_bind_mobile]);
  127. }
  128. }