index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!-- #ifdef H5 -->
  2. <template>
  3. <view v-if="bindPhone">
  4. <form @submit="editPwd" report-submit='true'>
  5. <view class="ChangePassword">
  6. <view class="list">
  7. <view class="item">
  8. <input type='number' placeholder='填写手机号码' placeholder-class='placeholder' v-model="phone"></input>
  9. </view>
  10. <view class="item acea-row row-between-wrapper">
  11. <input type='number' placeholder='填写验证码' placeholder-class='placeholder' class="codeIput" v-model="captcha"></input>
  12. <button class="code font-color" :class="disabled === true ? 'on' : ''" :disabled='disabled' @click="code">
  13. {{ text }}
  14. </button>
  15. </view>
  16. </view>
  17. <button form-type="submit" class="confirmBnt bg-color">确认绑定</button>
  18. </view>
  19. </form>
  20. </view>
  21. <view class="lottie-bg" v-else>
  22. <view id="lottie">
  23. <!-- <image src="/static/img/live-logo.gif" rel="preload" /> -->
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import wechat from "@/libs/wechat";
  29. import sendVerifyCode from "@/mixins/SendVerifyCode";
  30. import {
  31. getUserInfo
  32. } from "@/api/user";
  33. import {
  34. WX_AUTH,
  35. STATE_KEY,
  36. LOGINTYPE,
  37. BACK_URL
  38. } from '@/config/cache';
  39. import { silenceAuth } from '@/api/public';
  40. import {
  41. registerVerify,
  42. bindingPhone,
  43. verifyCode
  44. } from '@/api/api.js';
  45. export default {
  46. name: "Auth",
  47. mixins: [sendVerifyCode],
  48. data() {
  49. return {
  50. phone:'',
  51. captcha:'',
  52. key: '',
  53. authKey:'',
  54. scope: '',
  55. bindPhone: false,
  56. };
  57. },
  58. mounted() {
  59. },
  60. onLoad(options) {
  61. let that = this
  62. const {
  63. code,
  64. state,
  65. scope
  66. } = options;
  67. if (scope === 'snsapi_base') {
  68. this.url = options.url || options.back_url || '';
  69. let spid = this.$Cache.get("spid");
  70. let loginType = this.$Cache.get(LOGINTYPE);
  71. silenceAuth({ code : options.code || '',spread_spid : spid }).then( res => {
  72. if (res.data.key !== undefined) {
  73. this.bindPhone = true;
  74. this.authKey = res.data.key;
  75. } else {
  76. this.$store.commit("LOGIN", {
  77. token: res.data.token,
  78. time: parseInt(res.data.expires_time) - this.$Cache.time()
  79. });
  80. this.$Cache.set(WX_AUTH, options.code);
  81. this.$Cache.clear(STATE_KEY);
  82. loginType && this.$Cache.clear(LOGINTYPE);
  83. location.href = decodeURIComponent(
  84. decodeURIComponent(options.back_url)
  85. );
  86. }
  87. }).catch(err=>{
  88. return that.$util.Tips({
  89. title: err
  90. });
  91. })
  92. } else {
  93. wechat.auth(code, state).then(() => {
  94. location.href = decodeURIComponent(
  95. decodeURIComponent(options.back_url)
  96. );
  97. getUserInfo().then(res => {
  98. that.$store.commit("SETUID", res.data.uid);
  99. that.$store.commit("UPDATE_USERINFO", res.data);
  100. });
  101. }).catch(() => {
  102. location.replace("/");
  103. });
  104. }
  105. },
  106. methods: {
  107. editPwd: function() {
  108. let that = this;
  109. if (!that.phone) {
  110. return that.$util.Tips({
  111. title: '请填写手机号码!'
  112. });
  113. }
  114. if (!(/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.phone))) {
  115. return that.$util.Tips({
  116. title: '请输入正确的手机号码!'
  117. });
  118. }
  119. if (!that.captcha) {
  120. return that.$util.Tips({
  121. title: '请填写验证码'
  122. });
  123. }
  124. bindingPhone({
  125. phone: that.phone,
  126. captcha: that.captcha,
  127. key: this.authKey
  128. }).then(res => {
  129. let time = res.data.expires_time - this.$Cache.time();
  130. this.$store.commit('LOGIN', { token : res.data.token, time : time });
  131. if (this.url && this.url.indexOf('http') !== -1) {
  132. location.href = this.url;
  133. } else {
  134. return that.$util.Tips({
  135. title: '绑定成功!',
  136. icon: 'success'
  137. }, {
  138. tab: 4,
  139. url: '/pages/index/index'
  140. });
  141. }
  142. }).catch(err => {
  143. return that.$util.Tips({
  144. title: err
  145. });
  146. })
  147. },
  148. /**
  149. * 发送验证码
  150. *
  151. */
  152. code() {
  153. let that = this;
  154. if (!that.phone) return that.$util.Tips({
  155. title: '请填写手机号码!'
  156. });
  157. if (!(/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.phone))) return that.$util.Tips({
  158. title: '请输入正确的手机号码!'
  159. });
  160. verifyCode().then(res => {
  161. registerVerify(that.phone, 'reset', res.data.key, that.captcha).then(res => {
  162. that.$util.Tips({
  163. title: res.msg
  164. });
  165. that.sendCode();
  166. }).catch(err => {
  167. return that.$util.Tips({
  168. title: err
  169. });
  170. });
  171. });
  172. }
  173. }
  174. };
  175. </script>
  176. <style scoped lang="scss">
  177. page {
  178. background-color: #fff !important;
  179. }
  180. .ChangePassword .phone {
  181. font-size: 32rpx;
  182. font-weight: bold;
  183. text-align: center;
  184. margin-top: 55rpx;
  185. }
  186. .ChangePassword .list {
  187. width: 580rpx;
  188. margin: 53rpx auto 0 auto;
  189. }
  190. .ChangePassword .list .item {
  191. width: 100%;
  192. height: 110rpx;
  193. border-bottom: 2rpx solid #f0f0f0;
  194. }
  195. .ChangePassword .list .item input {
  196. width: 100%;
  197. height: 100%;
  198. font-size: 32rpx;
  199. }
  200. .ChangePassword .list .item .placeholder {
  201. color: #b9b9bc;
  202. }
  203. .ChangePassword .list .item input.codeIput {
  204. width: 340rpx;
  205. }
  206. .ChangePassword .list .item .code {
  207. font-size: 32rpx;
  208. background-color: #fff;
  209. }
  210. .ChangePassword .list .item .code.on {
  211. color: #b9b9bc !important;
  212. }
  213. .ChangePassword .confirmBnt {
  214. font-size: 32rpx;
  215. width: 580rpx;
  216. height: 90rpx;
  217. border-radius: 45rpx;
  218. color: #fff;
  219. margin: 92rpx auto 0 auto;
  220. text-align: center;
  221. line-height: 90rpx;
  222. }
  223. .lottie-bg {
  224. position: fixed;
  225. left: 0;
  226. top: 0;
  227. background-color: #fff;
  228. width: 100%;
  229. height: 100%;
  230. z-index: 999;
  231. display: flex;
  232. align-items: center;
  233. justify-content: center;
  234. }
  235. #lottie {
  236. display: block;
  237. width: 100%;
  238. height: 100%;
  239. display: flex;
  240. align-items: center;
  241. justify-content: center;
  242. overflow: hidden;
  243. transform: translate3d(0, 0, 0);
  244. margin: auto;
  245. image {
  246. width: 200rpx;
  247. height: 200rpx;
  248. }
  249. }
  250. </style>
  251. <!-- #endif -->