<template>
  <view class="page" :style="themeColor">
    <!---flex布局flex布局开始-->
    <view class="flex flex-direction flex-wrap align-stretch benben-flex-layout codeLogin_flex_0">
      <view class='flex flex-wrap align-center codeLogin_fd0_0'>
        <text class='codeLogin_fd0_0_c0'>欢迎来到萬家商超</text>
      </view>
      <view class='flex flex-direction flex-wrap align-stretch codeLogin_fd0_1'>
        <text class='codeLogin_fd0_1_c0'>账号</text>
        <view class='flex flex-wrap align-center justify-between'>
          <input type="number" placeholder="请输入手机号" confirm-type="done" :maxlength="11"
            placeholder-style="color:rgba(191, 191, 191, 1);font-size:32rpx" v-model="mobile" />
        </view>
      </view>
      <view class='flex flex-direction flex-wrap align-stretch codeLogin_fd0_2'>
        <text class='codeLogin_fd0_2_c0'>验证码</text>
        <view class='flex flex-wrap align-center justify-between'>
          <input type="number" class="flex-sub" placeholder="请输入验证码" confirm-type="done" :maxlength="4"
            placeholder-style="color:rgba(191, 191, 191, 1);font-size:32rpx" v-model="code" />
          <benben-send-verification-code class='flex  codeLogin_fd0_2_c1_c1' after-text='后重新获取' before-text='获取验证码'
            type='3' :phone="mobile"></benben-send-verification-code>
        </view>
      </view>
      <view class='flex flex-direction-row-reverse flex-wrap align-center codeLogin_fd0_3'>
        <text class='codeLogin_fd0_3_c0' @tap.stop="handleJumpDiy" data-type="redirectTo"
          :data-url="`/pages/tabBar/passwordLogin/passwordLogin`">密码登录</text>
      </view>
      <view class='flex flex-wrap align-center codeLogin_fd0_4'>
        <button class='flex-sub codeLogin_fd0_4_c0' @tap.stop="codeLoginFunc()">立即登录</button>
      </view>
      <view class='flex flex-wrap align-center justify-center codeLogin_fd0_5'>
        <text class='codeLogin_fd0_5_c0' @tap.stop="handleJumpDiy" data-type="navigateTo"
          :data-url="`/pages/dl/registerLogin/registerLogin`">注册账号</text>

        <view class='flex  codeLogin_fd0_5_c1'></view>

        <text class='codeLogin_fd0_5_c2' @tap.stop="handleJumpDiy" data-type="navigateTo"
          :data-url="`/pages/dl/forgotPassword/forgotPassword`">忘记密码</text>
      </view>
    </view>

    <!---flex布局flex布局结束-->
    <view class="flex flex-direction align-center benben-position-layout flex codeLogin_flex_1" v-if="isPublish">
      <view class='flex flex-wrap align-center'>
        <view class='flex flex-wrap align-center codeLogin_fd1_0_c0'>
        </view>
        <text class='codeLogin_fd1_0_c1'>快捷登录</text>
        <view class='flex flex-wrap align-center codeLogin_fd1_0_c2'>
        </view>
      </view>
      <image class='codeLogin_fd1_1' :src='STATIC_URL+"116.png"' @tap.stop="wxLogin" data-type="navigateTo"
        :data-url="`/pages/dl/quickLogin/quickLogin`"></image>

    </view>
		<view class="text-center text-sm fixed flex align-center justify-center read">
		  <image v-if="!isRead" :src='STATIC_URL+"123.png"' mode="widthFix" @tap="handleToggle"></image>
		  <image v-else :src='STATIC_URL+"122.png"' mode="widthFix" @tap="handleToggle"></image>
		 已阅读并同意
		  <text class="alink margin-lr-xs" @tap="navto(8)">《用户注册协议》</text>
		 和
		  <text class="alink margin-lr-xs" @tap="navto(16)">《隐私政策》</text>
		</view>
    <view :style="{height: '300rpx'}"></view>


  </view>
</template>
<script>
  import {
    validate
  } from '@/common/utils/validate.js'
  export default {
    components: {},


    data() {
      return {
        "mobile": "",
        "code": "",
				isRead: false
      };
    },
    computed: {
      themeColor() {
        return this.$store.getters.themeColor
      },

    },
    watch: {},
    onLoad(options) {
			// #ifdef APP-PLUS
			this.getPublishStatus()
			// #endif
    },
    onUnload() {

    },
    onReady() {

    },
    onShow() {

    },
    onHide() {

    },
    onResize() {

    },
    onPullDownRefresh() {

    },
    onReachBottom(e) {

    },
    onPageScroll(e) {

    },
    methods: {
			handleToggle() {
			  this.isRead = !this.isRead;
			},
      /**
       * @event 微信登陆
       */
      wxLogin() {
        let _this = this;
        // if (!this.is_select_agree) return this.$message.info('请勾选用户协议');
        // #ifdef APP-PLUS
        // 如果是app环境打开微信
        if (
          plus.runtime.isApplicationExist({
            pname: "com.tencent.mm",
            action: "weixin://",
          })
        ) {
          uni.getProvider({
            service: "oauth",
            success: function(res) {
              console.log(res);
              // 如果支持微信等，就执行 wx_login_fn 方法
              if (~res.provider.indexOf("weixin")) {
                _this.wx_login_fn();
              }
            },
            fail: function(err) {
              // api错误是打印错误
              console.log(err);
            },
          });
        } else {
          // 调起失败时提醒用户
          this.$message.info("请安装微信后再使用此功能");
        }
        // #endif
      },
      /**
       * @function 微信授权登录
       */
      wx_login_fn() {
        let _this = this;
        /**
         * @desc uniapp的登陆api
         * @param {String} provider - 登录服务提供商
         * @param {login~success} loginRes - 接口调用成功时的回调函数
         */
        uni.login({
          provider: "weixin",
          /**
           * @callback login~success
           * @param {Object} loginRes - api返回来的信息
           */
          success: function(loginRes) {
            /**
             * @desc 获取用户信息
             * @param {String} provider - 登录服务提供商
             * @param {Funtion} success - 接口调用成功时的回调函数
             */
            uni.getUserInfo({
              provider: "weixin",
              /**
               * @callback success
               * @param {Object} infoRes - api返回来的信息
               */
              success: function(infoRes) {
                // 显示加载状态
                uni.showLoading({
                  title: "加载中...",
                });
                /**
                 * @let {String} nickName - 结构出来的变量昵称
                 * @let {String} avatarUrl - 解构出来的头像链接
                 * @let {String} gender - 结构出来的性别
                 */
                let {
                  nickName,
                  avatarUrl,
                  gender
                } = infoRes.userInfo;
                // #ifdef APP-PLUS
                let clientId = plus.push.getClientInfo().clientid || "";
                console.log("clientId", clientId);
                //  @const {Number} appSystem - 判断设备类型 1：ios；2：安卓；
                let appSystem = uni.getSystemInfoSync().platform == "ios" ? 1 : 2;
                // #endif
                console.log(infoRes, "app微信登录");
                console.log(infoRes.userInfo.unionId, "app微信登录");
                /**
                 * @description 第三方登录请求接口
                 */
                _this.$api
                  .post(global.apiUrls.postUserSocialLogin, {
                    third_type: 'wx_app',
                    extend_info: JSON.stringify({
                      unionid: infoRes.userInfo.unionId,
                      clientId: clientId || "",
                      appSystem: appSystem || "",
                      nickname: nickName,
                      avatar: avatarUrl,
                      gender: gender,
                      openid: infoRes.userInfo.openId,
                    })
                  })
                  .then((res) => {
                    console.log(res)
                    // 保存用户信息
                    global.tempUserLoginInfo = infoRes.userInfo;
                    // 关闭加载效果
                    uni.hideLoading();
                    if (res.data.code == 1) {
                      console.log(1)
                      if (res.data.data.userinfo && res.data.data.userinfo.user_token) {
                        _this.$store.commit('updateUserInfo', res.data.data.userinfo);
                        global.token=res.data.data.userinfo.user_token
                        global.userInfo=res.data.data.userinfo
                        uni.setStorageSync('USER_INFO', res.data.data.userinfo)
                        uni.setStorageSync('USER_TOKEN', res.data.data.userinfo.user_token)
                        _this.$message.info(res.data.msg);
                        uni.$emit("loginSuccess", {msg: 'loginSuccess'});
                        setTimeout(() => {
                          uni.navigateBack({
                            delta:1
                          });
                        }, 500);
                        // _this.$message.info(res.data.msg);
                      }
                      if (res.data.data.result && res.data.data.result.openid) {
                        uni.navigateTo({
                          url: `/pages/dl/bindPhone/bindPhone?unionId=${infoRes.userInfo.unionId}&openId=${infoRes.userInfo.openId}&sex=${infoRes.userInfo.gender}&nickName=${infoRes.userInfo.nickName}&avatarUrl=${infoRes.userInfo.avatarUrl}&authType=wx_unionid&bind_type=1`
                        })
                      }
      
                      // _this.$message.info(res.data.msg);
                      // /**
                      //  * @description 向Vux发送用户信息保存
                      //  * @param {String} updateUserInfo - 调用的方法名称
                      //  * @param {Object} res.data.data.userinfo - 用户信息
                      //  */
                      // _this.$store.commit(
                      //   "updateUserInfo",
                      //   res.data.data.userinfo
                      // );
                      // uni.$emit("loginSuccess", {msg: 'loginSuccess'});
                      // setTimeout(() => {
                      //   uni.navigateBack();
                      // }, 800);
                    } else if (res.data.code == 0) {
                      /**
                       * @description 清除当前页面，跳转到绑定手机页面
                       */
                      // _this.$urouter.redirectTo({
                      //   url: _this.PAGE_BASE_URL + "bind-phone/index",
                      //   params: {
                      //     unionId: infoRes.userInfo.unionId,
                      //     openId: infoRes.userInfo.openId,
                      //     sex: infoRes.userInfo.gender,
                      //     nickName: infoRes.userInfo.nickName,
                      //     avatarUrl: infoRes.userInfo.avatarUrl,
                      //     authType: "wx_unionid",
                      //     bind_type: 1
                      //   },
                      // });
                      // 点击手机号账号登录按钮
                    } else if (res.code == -1) {
                      _this.$message.info(res.data.msg);
                      return;
                    }
                  })
                  .catch((err) => {
                    uni.hideLoading();
                    console.log(err);
                  });
              },
            });
          },
          error: function(err) {
            uni.hideLoading();
            console.log(err);
          },
        });
      },
			Get5d8b062aefc08(userinfo){
				let clientId = '565656565656';
				let appSystem = 'appSystem';
				// #ifdef APP-PLUS
				clientId = plus.push.getClientInfo().clientid || '';
				console.log('getClientInfo', plus.push.getClientInfo());
				appSystem = uni.getSystemInfoSync().platform == 'ios' ? 1 : 2;
				// #endif
				this.$api.post(global.apiUrls.Get5d8b062aefc08,{
					client_id:clientId
				}).then(res=>{
					if(res.data.code==1){
						this.$store.commit("updateUserInfo", userinfo);
						global.token=userinfo.user_token
						global.userInfo=userinfo
						uni.setStorageSync('USER_INFO', userinfo)
						uni.setStorageSync('USER_TOKEN', userinfo.user_token)
            uni.$emit('loginSuccess')
						setTimeout(() => {
						  uni.navigateBack({
						    delta: 1
						  })
						}, 1000)
					}
				})
			},
      //验证码登录
      codeLoginFunc() {
        if (!validate(this.mobile, 'require')) {
          this.$message.info('手机号不能为空');
          return false;
        }
        if (!validate(this.mobile, 'phone')) {
          this.$message.info('请输入正确的手机号');
          return false;
        }
        if (!validate(this.code, 'require')) {
          this.$message.info('验证码不能为空');
          return false;
        }
        if (!validate(this.code, 'captcha')) {
          this.$message.info('请输入正确的验证码');
          return false;
        }
				if (!this.isRead) {
				  return this.$message.info('请先阅读并同意《用户协议》和 《隐私政策》');
				}
        this.$api.post(global.apiUrls.postCodeLogin, {
            mobile: this.mobile,
            code: this.code,
            type: 3
          })
          .then(res => {
            console.log(res.data.data.userinfo);
            this.$message.info(res.data.msg)
            if(res.data.code == 1){
							global.user_token=res.data.data.userinfo.user_token
							this.Get5d8b062aefc08(res.data.data.userinfo)
							
              // this.$store.commit("updateUserInfo", res.data.data.userinfo);
              // global.token=res.data.data.userinfo.user_token
              // global.userInfo=res.data.data.userinfo
              // uni.setStorageSync('USER_INFO', res.data.data.userinfo)
              // uni.setStorageSync('USER_TOKEN', res.data.data.userinfo.user_token)
              // setTimeout(() => {
              //   uni.navigateBack({
              //     delta: 1
              //   })
              // }, 1000)
            }
          });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .page {
    width: 100vw;
    overflow-x: hidden;
    min-height: calc(100vh - var(--window-bottom));
    background: url(https://lvtai-files.oss-cn-beijing.aliyuncs.com/mini/117.png) no-repeat, rgba(255, 255, 255, 1);
    background-size: 100% auto !important;
  }

  .codeLogin_flex_0 {
    padding: 246rpx 75rpx 0rpx 75rpx;
  }

  .codeLogin_fd0_5_c2 {
    color: #999999;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 34rpx;
  }

  .codeLogin_fd0_5_c1 {
    background: rgba(153, 153, 153, 1);
    width: 1rpx;
    height: 24rpx;
    margin: 0rpx 20rpx 0rpx 20rpx;
    transform: scaleX(0.5);
  }

  .codeLogin_fd0_5_c0 {
    color: #999999;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 34rpx;
  }

  .codeLogin_fd0_5 {
    margin: 40rpx 0rpx 0rpx 0rpx;
  }

  .codeLogin_fd0_4_c0 {
    background: rgba(255, 147, 0, 1);
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    font-size: 32rpx;
    color: #fff;
    height: 80rpx;
    line-height: 80rpx;
  }

  .codeLogin_fd0_4 {
    padding: 48rpx 0rpx 0rpx 0rpx;
  }

  .codeLogin_fd0_3_c0 {
    color: #333333;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 38rpx;
  }

  .codeLogin_fd0_3 {
    margin: 32rpx 0rpx 0rpx 0rpx;
  }

  .codeLogin_fd0_2_c1_c1 {
    line-height: 32rpx;
    text-align: center;
    font-size: 28rpx;
    color: #FF7F1E;
    font-weight: 400;
  }

  .codeLogin_fd0_2_c0 {
    margin: 0rpx 0rpx 24rpx 0rpx;
    color: #333333;
    font-size: 30rpx;
    font-weight: 600;
    line-height: 32rpx;
  }

  .codeLogin_fd0_2 {
    border-bottom: 1px solid #eee;
    padding: 40rpx 0rpx 20rpx 0rpx;
  }

  .codeLogin_fd0_1_c0 {
    margin: 0rpx 0rpx 24rpx 0rpx;
    color: #333333;
    font-size: 30rpx;
    font-weight: 600;
    line-height: 32rpx;
  }

  .codeLogin_fd0_1 {
    border-bottom: 1px solid #eee;
    padding: 40rpx 0rpx 20rpx 0rpx;
  }

  .codeLogin_fd0_0_c0 {
    color: #333333;
    font-size: 50rpx;
    font-weight: 600;
    line-height: 50rpx;
  }

  .codeLogin_fd0_0 {
    margin: 0rpx 0rpx 24rpx 0rpx;
  }

  .codeLogin_flex_1 {
    background: #fff;
    width: 750rpx;
    height: 300rpx;
    overflow: hidden;
    z-index: 10;
    bottom: calc(0rpx + var(--window-bottom));
    background-size: 100% auto !important;
  }

  .codeLogin_fd1_1 {
    width: 80rpx;
    height: 80rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    margin: 40rpx 0rpx 0rpx 0rpx;
  }

  .codeLogin_fd1_0_c2 {
    background: rgba(238, 238, 238, 1);
    background-size: 100% auto !important;
    height: 1rpx;
    width: 157rpx;
  }

  .codeLogin_fd1_0_c1 {
    line-height: 32rpx;
    font-size: 26rpx;
    font-weight: 400;
    color: #999999;
    padding: 0rpx 7rpx 0rpx 7rpx;
  }

  .codeLogin_fd1_0_c0 {
    background: rgba(238, 238, 238, 1);
    background-size: 100% auto !important;
    height: 1rpx;
    width: 157rpx;
  }
	
	.fixed {
	  width: 100%;
	  text-align: center;
	  position: fixed;
	  bottom: 56rpx;
	  color: #666;
	  image {
	    margin-right: 12rpx;
	    width: 28rpx;
	    height: 28rpx;
	  }
	}
	
	.read {
		bottom: 40rpx;
		z-index: 100;
	}
	.alink {
	  color: #59A6F4;
	}
</style>
