codelogin.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view>
  3. <!-- #ifndef MP-WEIXIN -->
  4. <form @submit="loginSub">
  5. <view class="cu-form-group margin-top">
  6. <view class="title">号码</view>
  7. <input placeholder="请输入手机号" name="phone" v-model="form.phone"></input>
  8. </view>
  9. <view class="cu-form-group">
  10. <view class="title">验证码</view>
  11. <input placeholder="请输入验证码" name="code" maxlength=4 v-model="form.code"></input>
  12. <span @click="getPhoneCode"
  13. class="cu-btn bg-gray"
  14. :class="'display:' + msgKey">{{msgText}}</span>
  15. </view>
  16. <view class="padding flex flex-direction">
  17. <button class="cu-btn margin-tb-sm lg round" :class="'bg-'+theme.backgroundColor" form-type="submit">立即登录</button>
  18. </view>
  19. <view class="margin-top flex justify-center text-sm" v-show="showPrivacyPolicy">
  20. <text>登录即代表您同意</text><navigator class="text-blue text-sm" :url="'/pages/public/webview/webview?title=用户协议&url='+protocolUrl">{{' 用户协议 '}}</navigator>和<navigator class="text-blue text-sm " :url="'/pages/public/webview/webview?title=隐私政策&url='+privacyPolicyUrl">{{' 隐私政策'}}</navigator>
  21. </view>
  22. </form>
  23. <!-- #endif -->
  24. <!-- #ifdef MP-WEIXIN -->
  25. <view class="padding flex flex-direction">
  26. <button class="cu-btn margin-tb-sm lg" :class="'bg-'+theme.backgroundColor" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键登录</button>
  27. </view>
  28. <view class="margin-top flex justify-center text-sm" v-show="showPrivacyPolicy">
  29. <text>登录即代表您同意</text><navigator class="text-blue text-sm" :url="'/pages/public/webview/webview?title=用户协议&url='+protocolUrl">{{' 用户协议 '}}</navigator>和<navigator class="text-blue text-sm " :url="'/pages/public/webview/webview?title=隐私政策&url='+privacyPolicyUrl">{{' 隐私政策'}}</navigator>
  30. </view>
  31. <!-- #endif -->
  32. </view>
  33. </template>
  34. <script>
  35. const app = getApp();
  36. import api from 'utils/api'
  37. import validate from 'utils/validate'
  38. import __config from '@/config/env';// 配置文件
  39. const MSGINIT = "发送验证码",
  40. MSGSCUCCESS = "${time}秒后可重发",
  41. MSGTIME = 60;
  42. export default {
  43. data() {
  44. return {
  45. theme: app.globalData.theme, //全局颜色变量
  46. CustomBar: this.CustomBar,
  47. form: {},
  48. msgText: MSGINIT,
  49. msgTime: MSGTIME,
  50. msgKey: false,
  51. showPrivacyPolicy: __config.showPrivacyPolicy,
  52. privacyPolicyUrl: __config.privacyPolicyUrl,
  53. protocolUrl: __config.protocolUrl,
  54. };
  55. },
  56. components: {
  57. },
  58. props: {
  59. reUrl: {//重定向页面
  60. type: String,
  61. default: '/pages/home/index'
  62. }
  63. },
  64. onLoad() {
  65. },
  66. onShow() {
  67. },
  68. methods: {
  69. getPhoneNumber(e) {
  70. if(e.detail.encryptedData){
  71. api.loginByPhoneMa(e.detail).then(res => {
  72. let userInfo = res.data;
  73. uni.setStorageSync('third_session', userInfo.thirdSession);
  74. uni.setStorageSync('user_info', userInfo);
  75. //登录完成跳到首页
  76. uni.reLaunch({
  77. url: this.reUrl?decodeURIComponent(this.reUrl):'/pages/home/index'
  78. });
  79. //获取购物车数量
  80. app.shoppingCartCount()
  81. });
  82. }
  83. },
  84. getPhoneCode(){
  85. if (this.msgKey) return
  86. if (!validate.validateMobile(this.form.phone)) {
  87. uni.showToast({
  88. title: '请输入正确的手机号码',
  89. icon: 'none',
  90. duration: 3000
  91. });
  92. return;
  93. }
  94. this.msgKey = true
  95. api.getPhoneCode({
  96. type: '1',
  97. phone: this.form.phone
  98. }).then(res => {
  99. this.msgKey = false
  100. if (res.code == '0') {
  101. uni.showToast({
  102. title: '验证码发送成功',
  103. icon: 'none',
  104. duration: 3000
  105. });
  106. this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
  107. this.msgKey = true
  108. const time = setInterval(() => {
  109. this.msgTime--
  110. this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
  111. if (this.msgTime == 0) {
  112. this.msgTime = MSGTIME
  113. this.msgText = MSGINIT
  114. this.msgKey = false
  115. clearInterval(time)
  116. }
  117. }, 1000)
  118. }else{
  119. }
  120. }).catch(() => {
  121. this.msgKey = false
  122. });
  123. },
  124. loginSub(e){
  125. if(!validate.validateMobile(this.form.phone)){
  126. uni.showToast({
  127. title: '请输入正确的手机号码',
  128. icon: 'none',
  129. duration: 3000
  130. });
  131. return;
  132. }
  133. if(!this.form.code){
  134. uni.showToast({
  135. title: '请输入验证码',
  136. icon: 'none',
  137. duration: 3000
  138. });
  139. return;
  140. }
  141. api.loginByPhone(this.form).then(res => {
  142. let userInfo = res.data;
  143. uni.setStorageSync('third_session', userInfo.thirdSession);
  144. uni.setStorageSync('user_info', userInfo);
  145. //登录完成跳到首页
  146. uni.reLaunch({
  147. url: this.reUrl?decodeURIComponent(this.reUrl):'/pages/home/index'
  148. });
  149. //获取购物车数量
  150. app.shoppingCartCount()
  151. });
  152. }
  153. }
  154. };
  155. </script>
  156. <style>
  157. </style>