12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\h5\controller;
- use app\hander\HelpHander;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- class WxLogin extends Controller {
- public function login() {
- $code = input('sn');
- $data = get_qrcode_arr($code);
- if (!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address') {
- return $this->fetch('h5/msg_error', ['msg' => '二维码错误']);
- }
- $this->redirect(url('index').'?w='.$code);
- }
- public function index() {
- $parameter = input('w');
- $openid = '';
- $currUrl = getSite().url("index").'?w='.$parameter;
- $state = input('state');
- $code = input('code');
- $address = get_qrcode_arr($parameter);
- $addressInfo = [
- 'org_id'=>0,
- ];
- if($address){
- $addressInfo = Db::name('dinner_address')
- ->where('id',$address['id'])
- ->find();
- }
- if (empty($code)) {
- $config =get_pay_wechat($addressInfo['org_id']);
- $app = \EasyWeChat\Factory::officialAccount($config);
- $response = $app->oauth->scopes(['snsapi_userinfo'])->redirect($currUrl);
- $response->send();
- exit();
- }
- else {
- try {
- $config =get_pay_wechat($addressInfo['org_id']);
- $userType = $config['user_type'];
- $app = \EasyWeChat\Factory::officialAccount($config);
- $user = $app->oauth->user();
- $openid = $user->id;
- $_data['curr_org_id'] = $addressInfo['org_id'];
- $_data['img'] = $user->avatar;
- $_data['nickname'] = $user->nickname;
- //查找数据库
- $user = Db::name('wxuser')
- ->where('openid', $openid)
- ->where('type', $userType)
- ->where('del',0)
- ->find();
- //不存在此用户
- if (empty($user)) {
- $_data['create_time'] = date('Y-m-d H:i:s');
- $_data['enable'] = 1;
- $_data['type'] = $userType;
- $_data['openid'] = $openid;
- Db::name('wxuser')->insertGetId($_data);
- }
- else { //存在此用户则更新信息
- if (trim($_data['img']) == trim($user['img'])) {
- unset($_data['img']);
- }
- if (trim($_data['nickname']) == trim($user['nickname'])) {
- unset($_data['nickname']);
- }
- if (!empty($_data)) {
- Db::name('wxuser')->where('id', $user['id'])->update($_data);
- }
- }
- //查找用户信息
- $user = Db::name('wxuser')
- ->where('openid', $openid)
- ->where('type', $userType)
- ->where('del',0)
- ->find();
- if (empty($user)) {
- return $this->fetch('h5/msg_error', ['msg' => '不存在此用户']);
- }
- session('userinfo', $user);
- Db::name('dinner_cart')->where('user_id', $user['id'])->delete();
- $this->redirect(url('WxHome/index').'?code='.$parameter);
- } catch (Exception $e) {
- return $this->fetch('h5/msg_error', ['msg' => '登录失败' . $e->getMessage()]);
- }
- }
- }
- }
|