• 小程序登陆


    app.js

    /**
       * 当小程序启动,或从后台进入前台显示,会触发 onShow
       */
      onShow: function (options) {
        this.getUserInfo();
      },
    
      /**
       * 获取用户信息
       */
      getUserInfo: function (cb) {
    
        var that = this
    
        if (that.globalData.userInfo) {
    
          typeof cb == "function" && cb(that.globalData.userInfo)
    
        } else {
    
          wx.login({
            success: function (res) {
              var code = res.code
              wx.getUserInfo({
                success: function (res2) {
                  that.globalData.userInfo = res2.userInfo
                  var data = {encryptedData: res2.encryptedData, iv: res2.iv, code: code}
                  // 存储用户信息
                  util.commonAjax('/recentimgtext/saveuser', 'POST', data).then(function (resolve) {
                    if (resolve.data.status === 200) {
                      wx.setStorageSync('userInfo', resolve.data.data)
                      typeof cb == "function" && cb(that.globalData.userInfo)
                    } else {
                      console.log('用户信息存储失败');
                    }
                  })
                },
                fail: function (res2) {
                  wx.navigateTo({
                    url: '/pages/login/login',
                  })
                }
              })
            }
          })
        }
      },
    
      /**
       * 全局数据
       */
      globalData: {
        userInfo: null,
        url: 'http://ci.test.com',
        // url: 'http://ci.mag998.com',
      }

    utils.js

    // 访问数据
    const commonAjax = function (url, types, data) {
      var app = getApp();
      var promise = new Promise(function(resolve, reject, defaults) {
        wx.request({
          url: app.globalData.url + url,
          data: data,
          method: (types === 'GET') ? 'GET' : 'POST',
          header: (types === 'GET') ? {'content-type': 'application/json'} : {'content-type': 'application/x-www-form-urlencoded'},
          success: resolve,
          fail: reject,
          complete: defaults,
        })
      })
      return promise;
    }
    
    module.exports = {
      formatTime: formatTime,
      commonAjax: commonAjax
    }

    login.wxml

    <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo" type='primary' style='margin-top:50px;80%;'> 微信登陆 </button>

    login.js

    // pages/login/login.js
    const app = getApp()
    Page({
    
      data: {
        hasUserInfo: app.globalData.userInfo,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
      },
    
      getUserInfo (e) {
        // 防止getUserInfo数据未及时返回
        app.globalData.userInfo = e.detail.userInfo
        
        wx.navigateBack({
          delta: 1
        })
      }
    
    })

    saveuser控制器

    // 存储用户信息
        public function saveuser()
        {
            $encryptedData = $this->input->post('encryptedData');
            $iv = $this->input->post('iv');
    
            $code = $this->input->post('code');
            $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . RECENT_IMGTEXT_APPID . '&secret=' . RECENT_IMGTEXT_APPSECRET . '&js_code=' . $code . '&grant_type=authorization_code';
            $data = send_get($url);
            $sessionKey = json_decode($data)->session_key;
    
            $pc = new WXBizDataCrypt(RECENT_IMGTEXT_APPID, $sessionKey);
            $errCode = $pc->decryptData($encryptedData, $iv, $data); // $data为用户信息
    
            $message = $errCode;
            $status = 0;
    
            if ($errCode == 0) {
    
                $userinfo = json_decode($data, true);
                $openid = $userinfo['openId'];
                unset($userinfo['watermark']);
                $checkuser = $this->db->get_where($this->recent_user_table, ['openId' => $openid])->row_array();
                if (empty($checkuser)) {
                    $userinfo['created_at'] = date("Y-m-d H:i:s");
                    if ($this->db->insert($this->recent_user_table, $userinfo));
                    $message = '添加成功';
                    $status = 200;
                } else {
                    unset($userinfo['openId']);
                    $userinfo['updated_at'] = date("Y-m-d H:i:s");
                    if ($this->db->update($this->recent_user_table, $userinfo, 'openId = "' . $openid . '"'));
                    $message = '更新成功';
                    $status = 200;
                }
            }
            echo json_encode(['data' => $data, 'message' => $message, 'status' => $status]);
        }
  • 相关阅读:
    CSS资料:IE8 CSS hack
    作为前端制作师你必须知道的事情!
    TABLE的1PT边框
    IE6 png图片的支持
    开源公司IronTec将推动PHP进驻Android平台
    终于解决了FLASH 播放器的问题了
    Windows7 添加快速启动栏
    不能调试存储过程的解决方法
    给Asp.Net Forums的后台管理菜单做一个树形外衣
    Asp.net Forums与Cnforums研究文章集合
  • 原文地址:https://www.cnblogs.com/maoriaty/p/9948411.html
Copyright © 2020-2023  润新知