123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- var api = require('../../../config/api.js');
- var util = require('../../../utils/util.js');
- var app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- phone: '',
- smscode: '',
- second: '发送验证码',
- disabled: false,
- disabledUpdate: false,
- paramView: '',
- navUrl: '',
- isRegist: true
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- var that = this;
- if(options.view){
- that.setData({
- paramView: options.view
- });
- }
- console.log(that.data.paramView)
- if (wx.getStorageSync("navUrl")) {
- that.setData({
- navUrl: wx.getStorageSync("navUrl")
- })
- } else {
- that.setData({
- navUrl: '/pages/index/index'
- })
- }
- },
- /**
- * 页面渲染完成
- */
- onReady: function () {
- },
- /**
- * 页面显示
- */
- onShow: function () {
- },
- /**
- * 页面隐藏
- */
- onHide: function () {
- },
- /**
- * 页面关闭
- */
- onUnload: function () {
- console.log('登陆页面返回2,进入登陆页参数:' + this.data.paramView)
- wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
- if (this.data.paramView == 'hotGoods' || this.data.paramView == 'indexGoods' || this.data.paramView == 'cart') {
- wx.setStorageSync("isLocationIndex", "true");//购物车是否已跳转登录页,跳转成功设置为true已跳转
- wx.switchTab({
- url: '/pages/index/index'
- });
- }
- if (this.data.paramView == 'cateGoods') {
- wx.switchTab({
- url: '/pages/catalog/catalog'
- });
- }
- if (this.data.paramView == 'hotGoodsView') {
- wx.navigateBack({
- delta: 1
- })
- }
- },
- clearPhone() {
- this.setData({
- phone: ''
- });
- },
- clearCode() {
- this.setData({
- smscode: ''
- });
- },
- bindPhoneInput: function (e) {
- console.log(e.detail.value)
- this.setData({
- phone: e.detail.value
- });
- },
- bindSmscodeInput: function (e) {
- this.setData({
- smscode: e.detail.value
- });
- },
- /**
- * 用户登录,获取授权用户信息,查询授权用户openid是否与登录用户手机号绑定一致,
- * 不一致的提示是否更新手机绑定信息,一致则登录成功
- */
- loginUser: function () {
- let regular = /^1[3|4|5|7|8]\d{9}$/;
- if (!regular.test(this.data.phone)) {
- util.showErrorToast('手机格式不正确')
- return false;
- }
- if (this.data.smscode.length == 0) {
- util.showErrorToast('验证码不能为空')
- return false;
- }
- var that = this;
- wx.login({
- success: function (res) {
- if (res.code) {
- wx.getUserInfo({
- withCredentials: true,
- success: function (succRes) {
- util.request(api.getOpenId, {
- code: res.code
- }).then((openIdRes) => {
- if (openIdRes.errno === 0){
- //授权成功登录用户信息
- util.request(api.userLogin, {
- smscode: that.data.smscode,
- phone: that.data.phone,
- storeId: wx.getStorageSync('storeId'),
- merchSn: wx.getStorageSync('merchSn'),
- userInfo: succRes,
- openId: openIdRes.data
- }, 'POST').then(function (userLoginRes) {
- if (userLoginRes.errno === 0) {
- //存储用户信息
- that.setLoginUserInfo(userLoginRes);
- console.log("登录成功");
- util.showSuccessToast('登录成功');
- }else if (userLoginRes.errno === 2) {//openid的绑定的手机号与当前用户登录的手机号不一致
- wx.showModal({
- title: '提示',
- content: userLoginRes.errmsg,
- success: function (conRes) {
- if (conRes.confirm) {
- util.request(api.userLoginMobileUpdate, {
- phone: that.data.phone,
- openId: openIdRes.data,
- storeId: wx.getStorageSync('storeId'),
- merchSn: wx.getStorageSync('merchSn'),
- userInfo: succRes,
- }, 'POST').then(function (loginMobUpdRes) {
- if (loginMobUpdRes.errno === 0) {
- //存储用户信息
- that.setLoginUserInfo(loginMobUpdRes);
- console.log("登录成功");
- util.showSuccessToast('登录成功');
- } else {
- util.showErrorToast(loginMobUpdRes.errmsg);
- }
- })
- } else {
- console.log('弹框后点取消')
- }
- }
- })
- } else {
- if (userLoginRes.errmsg.length>8){
- wx.showModal({
- title: '提示',
- content: userLoginRes.errmsg,
- showCancel: false
- })
- } else {
- util.showErrorToast(userLoginRes.errmsg);
- }
- }
- });
- }
- });
-
- },
- fail: function (err) {
- wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
- // wx.switchTab({
- // url: '/pages/index/index'
- // });
- console.log("登录页重新认证");
- }
- });
- } else {
- console.log("failed");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- },
- /**
- * 存储用户信息
- */
- setLoginUserInfo: function (loginMobUpdRes){
- var that = this;
- if (loginMobUpdRes.data.userInfo) {
- wx.setStorageSync('userInfo', loginMobUpdRes.data.userInfo);
- }
- if (loginMobUpdRes.data.token) {
- wx.setStorageSync('token', loginMobUpdRes.data.token);
- }
- wx.setStorageSync('userId', loginMobUpdRes.data.userId);
- if (that.data.paramView == 'goodsView') {
- wx.navigateBack({
- delta: 1
- });
- } else {
- if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
- console.log(that.data.navUrl)
- wx.switchTab({
- url: that.data.navUrl,
- });
- } else if (that.data.navUrl) {
- wx.redirectTo({
- url: that.data.navUrl,
- });
- }
- }
- wx.setStorageSync('isRefusedLogin', 'false');//允许授权
- },
- /**
- * 发送短信
- */
- smscode: function () {
- var that = this;
- let regular = /^1[3|4|5|7|8]\d{9}$/;
- if (!regular.test(that.data.phone)) {
- util.showErrorToast('手机格式不正确')
- return false;
- }
- util.request(api.smscodeSend, {
- phone: that.data.phone
- }, 'POST').then(function (res) {
- console.log("发送验证码 => ", res);
- let n = 59;
- var timer = setInterval(function () {
- if (n == 0) {
- clearInterval(timer);
- that.setData({
- second: '发送验证码',
- disabled: false
- });
- } else {
- that.setData({
- second: n-- + 's后重新获取',
- disabled: true
- });
- }
- }, 1000);
- if (res.errno == 0) {
- wx.showToast({
- title: '短信发送成功'
- })
- } else {
- util.showErrorToast('发送失败');
- clearInterval(timer);
- that.setData({
- second: '发送验证码',
- disabled: false
- });
- return false;
- }
- });
- },
- backHome: function () {
- wx.reLaunch({
- url: '../index/index',
- })
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- // /**
- // * 用户点击右上角分享
- // */
- // onShareAppMessage: function () {
- // },
- bindCancelLogin: function () {
- wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
- wx.switchTab({
- url: '/pages/index/index'
- });
- },
- /**
- * 登录页直接触发微信授权,授权成功保存用户信息
- */
- bindGetUserInfo: function () {
- let that = this;
-
- wx.login({
- success: function (res) {
- if (res.code) {
- wx.getUserInfo({
- withCredentials: true,
- success: function (succRes) {
- //保存授权用户信息
- wx.request({
- url: api.AuthLoginByWeixin,
- data: {
- code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
- },
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (wxRes) {
- if (wxRes.data.errno === 0) {
- //存储用户信息到缓存
- if (wxRes.data.data.userInfo) {
- wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
- }
- wx.setStorageSync('token', wxRes.data.data.token);
- wx.setStorageSync('userId', wxRes.data.data.userId);
- if (that.data.paramView == 'goodsView') {
- wx.navigateBack({
- delta: 1
- });
- } else {
- if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
- console.log(that.data.navUrl)
- wx.switchTab({
- url: that.data.navUrl,
- });
- } else if (that.data.navUrl) {
- wx.redirectTo({
- url: that.data.navUrl,
- });
- }
- }
- wx.setStorageSync('isRefusedLogin', 'false');//允许授权
- // console.log("登录成功");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- },
- fail: function (err) {
- wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
- // wx.switchTab({
- // url: '/pages/index/index'
- // });
- console.log("登录页重新认证");
- }
- });
- } else {
- console.log("failed");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- },
- /*registUser: function () {
- var that = this;
- let regular = /^1[3|4|5|7|8]\d{9}$/;
- if (!regular.test(that.data.phone)) {
- util.showErrorToast('手机格式不正确')
- return false;
- }
- if (that.data.smscode.length == 0) {
- util.showErrorToast('验证码不能为空')
- return false;
- }
- wx.login({
- success: function (res) {
- if (res.code) {
- wx.getUserInfo({
- withCredentials: true,
- success: function (succRes) {
- util.request(api.getOpenId, {
- code: res.code
- }).then((openIdRes) => {
- if (openIdRes.errno === 0) {
- that.registUserUtil();
- }
- });
- },
- fail: function (err) {
- wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
- console.log("注册前进行授权重新认证");
- }
- });
- } else {
- console.log("faild");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- })
- },
- registUserUtil: function () {
- var that = this;
- util.request(api.registUser, {
- smscode: that.data.smscode,
- phone: that.data.phone
- }, 'POST').then(function (res) {
- if (res.errno === 0) {
- //存储用户信息
- wx.setStorageSync('userInfo', res.data.data.userInfo);
- wx.setStorageSync('token', res.data.data.token);
- wx.setStorageSync('userId', res.data.data.userId);
- if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
- wx.switchTab({
- url: that.data.navUrl,
- });
- } else if (that.data.navUrl) {
- wx.redirectTo({
- url: that.data.navUrl,
- });
- }
- wx.setStorageSync('isRefusedLogin', 'false');//允许授权
- // console.log("登录成功");
- } else {
- util.showErrorToast(res.errmsg);
- }
- });
- },
- showRegist: function(){
- var that = this;
- that.setData({
- isRegist: false
- });
- wx.setNavigationBarTitle({
- title: '注册'
- })
- }*/
- })
|