| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- // pages/user/login/login.js
- const app = getApp()
- const api = require('../../../request/api');
- const http = require('../../../request/http');
- const utils = require('../../../utils/util.js');
- const storeKeys = require('../../../utils/storageKeys.js');
- Page({
- data: {
- selected: false,
- code:'',
- callback:""
- },
- onLoad(options) {
- console.log(options)
- this.data.callback = options.callback || ""
- },
- logIn(){
- if(!this.data.selected){
- utils.toast("请先同意原味公明腊肠优选服务协议");
- }
- },
- changSelected(){
- let selected = this.data.selected;
- this.setData({
- selected:!selected
- })
- },
- toPage(e){
- let type = e.currentTarget.dataset.type;
- if(type==1){//服务协议
- wx.navigateTo({
- url: "/pages/public/pages/serviceAgreement/serviceAgreement"
- })
- }
- if(type==2){//隐私协议
- wx.navigateTo({
- url: "/pages/public/pages/privacy/privacy"
- })
- }
- },
- // 手机号登录/注册
- phoneLogin: utils.throttle(function (e) {
- wx.navigateTo({
- url: "/pages/tabBar/phoneLogin/phoneLogin"
- })
- }),
- getUserInfo: function() {
- http.request({
- url: api.URL + '/api/user/info',
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'GET',
- noLogin: true,
- success: function(res) {console.log(res)
- let userInfo = res.data.data.userInfo;
- app.globalData.userInfo = userInfo
- utils.setStorageSync(storeKeys.USERINFO, userInfo);
- },
- error: function(res) {},
- })
- },
- // 普通用户切换绑定关系
- promotion(){
- let sharerId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || "";
- console.log("sharerId",sharerId)
- if(!sharerId){
- return;
- }
- let data = {
- sharerId: sharerId,
- isScanCode: "1"
- }
- http.request({
- url: api.URL + '/api/promotion/ch_upper',
- data: data,
- method: 'POST',
- token: utils.getStorageSync(storeKeys.TOKEN),
- noLogin: true,
- noToast:true,
- success: function(res) {
- console.log("success")
- console.log(res)
- },
- error: function(res) {
- console.log("error")
- console.log(res)
- }
- })
- },
- getLoginCall(params){
- let that = this;
- console.log(params)
- http.request({
- url: api.URL + '/api/mp_wx/mobile',
- data: params,
- method: 'POST',
- success: function(res) {
- console.log(res)
- utils.toast('登录成功');
- utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
- that.promotion();
- that.getUserInfo()
- app.getCartNumber();
- setTimeout(() => {
- wx.navigateBack({
- delta: 1
- })
- }, 1000)
- },
- error: function(res) {
- console.log("新用户登陆失败")
- console.log(res)
- }
- })
- },
- getPhoneNumber: function(e) {
- let that = this;
- let errMsg = e.detail.errMsg;
- if (errMsg.indexOf('user cancel') != -1 || errMsg.indexOf('用户未绑定手机,请先在微信客户端进行绑定后重试') != -1) {
- console.log('取消');
- return;
- }
- if (errMsg.indexOf('user deny') != -1) {
- console.log('拒绝');
- return;
- }
-
- let pages = getCurrentPages();
- let prevPage = pages[pages.length - 2];
- let actSourceId = 0;
- let pageRoute = prevPage.route;
- let actSource = 0
- //来源拼团活动
- let isGroupShopping = pageRoute.indexOf("activity/pages/groupShopping")!= -1
- let isfission = prevPage.options.isfission
- // 来源砍价活动
- let isBargain = pageRoute.indexOf("activity/pages/bargain")!= -1
- // 来源商品详情
- let isGoodsDetail = pageRoute.indexOf("cart/pages/goodsDetail")!= -1
- if(isGroupShopping){
- actSource = 2
- if(isfission){
- actSource = 4
- }
- actSourceId = prevPage.data.activity_id || 0;
- }
- if(isBargain){
- actSource = 3
- actSourceId = prevPage.data.activity_id || 0;
- }
- if(isGoodsDetail){
- try{
- prevPage.countRecord()
- }catch(e){}
- }
- //存在则是来源渠道推广
- if(utils.getStorageSync(storeKeys.SOURECID)){
- actSourceId = utils.getStorageSync(storeKeys.SOURECID);
- actSource = 1
- let isCount = false;
- if(isGroupShopping){
- try{
- prevPage.changeCountRecord()
- }catch(e){}
- isCount = true;
- }
- if(isBargain){
- try{
- prevPage.changeCountRecord()
- }catch(e){}
- isCount = true;
- }
- !isCount && app.countRecord()
- }
- let params = {
- openid: utils.getStorageSync("openid"),
- iv: e.detail.iv,
- encryptedData: e.detail.encryptedData,
- actSource, // 活动来源 0-其他 1-渠道推广 2拼团活动 3砍价活动 4拼团裂变
- actSourceId, // 活动来源详情id 如:渠道推广id
- }
- console.log(params)
- if(that.data.code){
- http.request({
- url: api.URL + '/api/mp_wx/login',
- data: {
- // "code":res.code,
- "code": that.data.code,
- },
- method:'POST',
- success: function(res) {
- utils.setStorageSync(storeKeys.OPENID, res.data.data.openid);
- if(res.data.data.token){
- utils.toast('登录成功');
- utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
- that.getUserInfo()
- that.promotion();
- app.getCartNumber();
- setTimeout(() => {
- wx.navigateBack({
- delta: 1,
- success: () => {
- if(!that.data.callback){
- return;
- }
- try{
- prevPage.loginCallback()
- }catch(e){}
- }
- })
- }, 1000)
- }else{
- params.openid = res.data.data.openid
- that.getLoginCall(params)
- }
- },
- error: function(res) {
- }
- })
- }else{
- wx.login({
- success: function(res) {
- if (res.code) {
- http.request({
- url: api.URL + '/api/mp_wx/login',
- data: {
- "code": that.data.code,
- },
- method:'POST',
- success: function(res) {
- utils.setStorageSync(storeKeys.OPENID, res.data.data.openid);
- if(res.data.data.token){
- // clearInterval(that.timer1);
- // console.log('清除定时器老用户')
- utils.toast('登录成功');
- utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
- that.getUserInfo()
- that.promotion();
- app.getCartNumber();
- setTimeout(() => {
- wx.navigateBack({
- delta: 1,
- success: () => {
- if(!that.data.callback){
- return;
- }
- try{
- prevPage.loginCallback()
- }catch(e){}
- }
- })
-
- }, 1000)
- }else{
- params.openid = res.data.data.openid
- that.getLoginCall(params)
- }
- },
- error: function(res) {
- console.log(res)
- }
- })
- }
- }
- })
- }
- },
- onShow: function () {
- let that = this
- this.getCode()
- },
- getCode(){
- let that = this
- wx.login({
- success: function(res) {
- that.data.code = res.code
- console.log(that.data.code)
- }
- })
- },
- })
|