index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view v-if="isUp">
  3. <view class="mobile-bg" v-if="isShow" @click="close"></view>
  4. <view class="mobile-mask animated" :class="{slideInUp:isUp}" :style="{position:isPos?'fixed':'static'}">
  5. <view class="input-item">
  6. <input type="text" v-model="account" placeholder="输入手机号" />
  7. </view>
  8. <view class="input-item">
  9. <input type="text" v-model="codeNum" placeholder="输入验证码" />
  10. <button class="code" :disabled="disabled" @click="code">{{text}}</button>
  11. </view>
  12. <view class="sub_btn" @click="loginBtn">{{(!userInfo.phone && isLogin) || (userInfo.phone && isLogin)?'立即绑定':'立即登录'}}</view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. const app = getApp();
  18. import sendVerifyCode from "@/mixins/SendVerifyCode";
  19. import Routine from '@/libs/routine';
  20. import {mapGetters} from "vuex";
  21. import {
  22. loginMobile,
  23. registerVerify,
  24. getCodeApi,
  25. getUserInfo,
  26. phoneSilenceAuth,
  27. phoneWxSilenceAuth
  28. } from "@/api/user";
  29. import {
  30. bindingPhone
  31. } from '@/api/api.js'
  32. import {
  33. getUserPhone,
  34. iosBinding
  35. } from '@/api/public';
  36. const BACK_URL = "login_back_url";
  37. export default {
  38. name: 'login_mobile',
  39. computed: mapGetters(['userInfo','isLogin']),
  40. props: {
  41. isUp: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. authKey: {
  46. type: String,
  47. default: '',
  48. },
  49. isShow: {
  50. type: Boolean,
  51. default: true
  52. },
  53. isPos: {
  54. type: Boolean,
  55. default: true
  56. },
  57. appleShow: {
  58. type: String,
  59. default: ''
  60. },
  61. platform: {
  62. type: String,
  63. default: '',
  64. }
  65. },
  66. data() {
  67. return {
  68. keyCode: '',
  69. account: '',
  70. codeNum: '',
  71. isApp: 0
  72. }
  73. },
  74. mixins: [sendVerifyCode],
  75. mounted() {
  76. //this.getCode();
  77. },
  78. onLoad() {
  79. },
  80. methods: {
  81. // 获取验证码
  82. async code() {
  83. let that = this;
  84. if (!that.account) return that.$util.Tips({
  85. title: '请填写手机号码'
  86. });
  87. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  88. title: '请输入正确的手机号码'
  89. });
  90. await registerVerify(that.account).then(res => {
  91. that.$util.Tips({
  92. title: res.msg
  93. });
  94. that.sendCode();
  95. }).catch(err => {
  96. return that.$util.Tips({
  97. title: err
  98. })
  99. })
  100. },
  101. // 获取验证码api
  102. getCode() {
  103. let that = this
  104. getCodeApi().then(res => {
  105. that.keyCode = res.data.key;
  106. }).catch(res => {
  107. that.$util.Tips({
  108. title: res
  109. });
  110. });
  111. },
  112. close() {
  113. this.$emit('close', false)
  114. },
  115. // 登录
  116. loginBtn() {
  117. let that = this
  118. if (!that.account) return that.$util.Tips({
  119. title: '请填写手机号码'
  120. });
  121. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  122. title: '请输入正确的手机号码'
  123. });
  124. if (!that.codeNum) return that.$util.Tips({
  125. title: '请填写验证码'
  126. });
  127. if (!/^[\w\d]+$/i.test(that.codeNum)) return that.$util.Tips({
  128. title: '请输入正确的验证码'
  129. });
  130. uni.showLoading({
  131. title: !this.userInfo.phone && this.isLogin?'正在绑定中':'正在登录中'
  132. });
  133. if (!this.userInfo.phone && this.isLogin) {
  134. iosBinding({
  135. captcha: that.codeNum,
  136. phone: that.account
  137. }).then(res => {
  138. that.$util.Tips({
  139. title: '绑定手机号成功',
  140. icon: 'success'
  141. }, {
  142. tab: 3
  143. })
  144. that.isApp = 1;
  145. that.getUserInfo();
  146. }).catch(error => {
  147. uni.hideLoading()
  148. that.$util.Tips({
  149. title: error
  150. })
  151. })
  152. } else {
  153. getUserPhone({
  154. captcha: that.codeNum,
  155. phone: that.account,
  156. // #ifdef H5
  157. type: 'public',
  158. // #endif
  159. key: that.authKey
  160. }).then(res => {
  161. that.$store.commit('LOGIN', {
  162. token: res.data.token
  163. });
  164. that.$store.commit("SETUID", res.data.uid);
  165. that.getUserInfo();
  166. }).catch(error => {
  167. uni.hideLoading()
  168. that.$util.Tips({
  169. title: error
  170. })
  171. })
  172. }
  173. },
  174. // #ifdef MP
  175. phoneSilenceAuth(code) {
  176. let self = this
  177. phoneSilenceAuth({
  178. code: code,
  179. spid: app.globalData.spid,
  180. spread: app.globalData.code,
  181. phone: this.account,
  182. captcha: this.codeNum
  183. }).then(res => {
  184. this.$store.commit('LOGIN', res.data.token);
  185. this.$store.commit("SETUID", res.data.uid);
  186. this.getUserInfo();
  187. }).catch(error => {
  188. self.$util.Tips({
  189. title: error
  190. })
  191. })
  192. },
  193. // #endif
  194. /**
  195. * 获取个人用户信息
  196. */
  197. getUserInfo: function() {
  198. let that = this;
  199. getUserInfo().then(res => {
  200. uni.hideLoading();
  201. that.$store.commit("UPDATE_USERINFO", res.data);
  202. // #ifdef MP
  203. that.$util.Tips({
  204. title: '登录成功',
  205. icon: 'success'
  206. }, {
  207. tab: 3
  208. })
  209. that.close()
  210. // #endif
  211. // #ifdef H5
  212. that.$emit('wechatPhone', true)
  213. // #endif
  214. });
  215. },
  216. }
  217. }
  218. </script>
  219. <style lang="stylus" scoped>
  220. .mobile-bg {
  221. position: fixed;
  222. left: 0;
  223. top: 0;
  224. width: 100%;
  225. height: 100%;
  226. background: rgba(0, 0, 0, 0.5);
  227. }
  228. .isPos {
  229. position: static;
  230. }
  231. .mobile-mask {
  232. z-index: 20;
  233. // position: fixed;
  234. left: 0;
  235. bottom: 0;
  236. width: 100%;
  237. padding: 67rpx 30rpx;
  238. background: #fff;
  239. .input-item {
  240. display: flex;
  241. justify-content: space-between;
  242. width: 100%;
  243. height: 86rpx;
  244. margin-bottom: 38rpx;
  245. input {
  246. flex: 1;
  247. display: block;
  248. height: 100%;
  249. padding-left: 40rpx;
  250. border-radius: 43rpx;
  251. border: 1px solid #DCDCDC;
  252. }
  253. .code {
  254. display: flex;
  255. align-items: center;
  256. justify-content: center;
  257. width: 220rpx;
  258. height: 86rpx;
  259. margin-left: 30rpx;
  260. background: rgba(233, 51, 35, 0.05);
  261. font-size: 28rpx;
  262. color: $theme-color;
  263. border-radius: 43rpx;
  264. &[disabled] {
  265. background: rgba(0, 0, 0, 0.05);
  266. color: #999;
  267. }
  268. }
  269. }
  270. .sub_btn {
  271. width: 690rpx;
  272. height: 86rpx;
  273. line-height: 86rpx;
  274. margin-top: 60rpx;
  275. background: #E93323;
  276. border-radius: 43rpx;
  277. color: #fff;
  278. font-size: 28rpx;
  279. text-align: center;
  280. }
  281. }
  282. .animated {
  283. animation-duration: .4s
  284. }
  285. </style>