123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //app.js
- const api = require('./config.js');
- App({
- host:api.host,
- onLaunch: function () {
- let self = this;
- // 登录
- wx.login({
- success: res => {
- wx.request({
- url: self.host,
- data:{
- act:"login",
- op:"ministart",
- code: res.code,
- client_type:"ios"
- },
- // url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxfdaeb25e38c4c47e&secret=e5b5055cbc608d10c6de0d877c221270&js_code=${res.code}&grant_type=authorization_code`,
- success:function(res){
- self.globalData.userId.openid = res.data.datas.openid;
- self.globalData.userId.unionid = res.data.datas.unionid;
- // self.globalData.userId.openid = res.data.openid;
- // self.globalData.userId.unionid = res.data.unionid;
- // 获取用户信息
- wx.getSetting({
- success: res => {
- if (res.authSetting['scope.userInfo']) {
- wx.getUserInfo({
- success: res => {
- self.globalData.userInfo = res.userInfo;
- self.globalData.userInfo.nickname = res.userInfo.nickName;
- let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
- wx.request({
- url: self.host,
- method: 'GET',
- data: {
- user_info: userInfo,
- act: "login",
- op: "wxauthen",
- // op: "wxauthor",
- client_type: "ios"
- },
- success: function (res) {
- console.log(res.data.datas.HPHPSESSID);
- wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
- if (res.statusCode == 200) {
- let current_url = "/" + getCurrentPages()[0].route;
- wx.reLaunch({
- url: current_url
- });
- }
- else {
- }
- }
- })
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- if (this.userInfoReadyCallback) {
- this.userInfoReadyCallback(res)
- }
- }
- })
- }
- else {
- wx.authorize({
- scope: 'scope.userInfo',
- success: function (res) {
- wx.getUserInfo({
- success: res => {
- // 可以将 res 发送给后台解码出 unionId
- self.globalData.userInfo = res.userInfo;
- self.globalData.userInfo.nickname = res.userInfo.nickName;
- let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
- // console.log(userInfo);
- wx.request({
- url: self.host,
- method: 'GET',
- data: {
- user_info: userInfo,
- act: "login",
- op: "wxauthen",
- // op: "wxauthor",
- client_type: "ios"
- },
- success: function (res) {
- // console.log(res.data.datas.HPHPSESSID)
- wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
- if (res.statusCode == 200) {
- let current_url = "/" + getCurrentPages()[0].route;
- wx.reLaunch({
- url: current_url
- });
- }
- else {
- }
- }
- })
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- if (this.userInfoReadyCallback) {
- this.userInfoReadyCallback(res)
- }
- }
- })
- }
- });
- }
- }
- })
- }
- })
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- }
- })
- },
- globalData: {
- userId:{
- openid:'',
- unionid:''
- },
- userInfo: null,
- defaultAddress: null,
- fcodeErr: '',
- backLogin:false,
- arrayTree: []
- },
- checkDefaultAddress(address) {
- this.globalData.defaultAddress = address
- },
- clearAddress() {
- this.globalData.defaultAddress = null
- },
- setArrayTree(tree) {
- this.globalData.arrayTree = tree
- }
- })
|