123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- const getReq = require('../config.js').getReq
- const api = require('../config.js').host
- var ald = require('./ald-stat.js')
- export default class WxAuthor {
- fGetSetting = true
- constructor(app, getsetting) {
- this.target = app
- this.init(getsetting)
- }
- // 首先wx.login
- init(getsetting) {
- this.target.globalData.fMinistart = false
- this.fGetSetting = getsetting
- wx.login({
- success: res => {
- console.log('wx.login code:', res)
- this.ministart(res.code)
- }
- })
- }
- // 拿到code 请求act=login op=ministart
- ministart(code) {
- let params = {
- act: "login",
- op: "ministart",
- code
- }
- getReq(this.target, params, res => {
- this.target.globalData.fMinistart = true
- if (res.code == 200) {
- let { openid, unionid } = res.datas
- if (openid && unionid) {
- this.setId(openid, unionid)
- wx.aldstat.sendOpenid(openid)
- wx.aldPushSendOpenid(openid)
- if (this.fGetSetting) {
- this.getSetting(false)
- }
- } else {
- if (this.fGetSetting) {
- this.getSetting(true)
- }
- }
- }
- })
- }
- getSetting(flag) {
- wx.getSetting({
- success: res => {
- if (res.authSetting['scope.userInfo']) {
- console.log('wx getSetting:', res)
- this.getUserInfo(flag)
- }
- }
- })
- }
- getUserInfo(flag) {
- let withCredentials = flag
- wx.getUserInfo({
- withCredentials,
- success: res => {
- this.setUserInfo(res.userInfo, res.userInfo.nickName)
- let userInfo = this.target.globalData.userInfo
- if (flag) {
- let { encryptedData, signature, iv } = res
- userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
- }
- else {
- userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
- }
- this.wxauthen(userInfo)
- }
- })
- }
- wxauthen(userInfo) {
- let relay_id = this.target.globalData.relay_id
- let channel_num = this.target.globalData.channel_num
- let params = {
- user_info: userInfo,
- act: "login",
- op: "wxauthen",
- relay_id: relay_id,
- channel: channel_num
- }
- getReq(this.target, params, res => {
- if (res.code == 200) {
- this.target.globalData.member_id = res.datas.member_id
- this.target.globalData.hasmobile = res.datas.hasmobile
- let url = this.getUrl()
- console.log('url:',url)
- if (url != '/pages/index/index' && url != "pages/home/home") {
- wx.reLaunch({
- url
- })
- }
- }
- else {
- wx.showToast({
- icon: 'none',
- title: '登陆失败',
- duration: 2000
- })
- }
- })
- }
- getUrl() {
- let len = getCurrentPages().length
- if (len < 1) return ''
- let currentPage = getCurrentPages()[len - 1]
- let params = ''
- if (currentPage.options) {
- for (let k in currentPage.options) {
- params = params + k + '=' + currentPage.options[k] + '&'
- }
- params = params.slice(0, params.length - 1)
- }
- let current_url = "/" + currentPage['route']
- if (params) {
- current_url = current_url + '?' + params
- }
- return current_url
- }
- // 设置用户信息
- setUserInfo(userInfo, nickName) {
- this.target.globalData.userInfo = userInfo
- this.target.globalData.userInfo.nickname = nickName
- }
- //设置openid, unionid
- setId(openid, unionid) {
- this.target.globalData.userId.openid = openid
- this.target.globalData.userId.unionid = unionid
- var timeStamp = new Date().getTime();
- timeStamp = Math.floor(timeStamp);
- this.target.globalData.userId.timeStamp = timeStamp
- }
- }
|