register.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="">
  3. <!-- 顶部导航 -->
  4. <fa-navbar :title="is_bind ? '创建或绑定账号' : '注册'"></fa-navbar>
  5. <view class="login">
  6. <view class="u-text-center" v-if="is_bind"><u-avatar :size="150" :src="vuex_third.avatar"></u-avatar></view>
  7. <view class="u-text-center u-p-t-20" v-if="is_bind">{{ vuex_third.nickname }}</view>
  8. <view class="u-m-t-30" v-if="!is_wx_phone || !is_bind">
  9. <u-form :model="form" ref="uForm">
  10. <block v-if="!is_bind">
  11. <u-form-item :label-position="labelPosition" label="用户名:" prop="username" label-width="120">
  12. <u-input :border="border" placeholder="请填写用户名" v-model="form.username" />
  13. </u-form-item>
  14. <u-form-item :label-position="labelPosition" label="密 码:" prop="password" label-width="120">
  15. <u-input :password-icon="true" :border="border" type="password" v-model="form.password" placeholder="请输入密码"></u-input>
  16. </u-form-item>
  17. </block>
  18. <u-form-item :label-position="labelPosition" label="手机号:" prop="mobile" label-width="120">
  19. <u-input :border="border" placeholder="请输入手机号" v-model="form.mobile" type="number"></u-input>
  20. </u-form-item>
  21. <u-form-item :label-position="labelPosition" label="验证码:" prop="code" label-width="120">
  22. <u-input :border="border" placeholder="请输入验证码" v-model="form.code" type="text"></u-input>
  23. <u-button
  24. hover-class="none"
  25. type="primary"
  26. slot="right"
  27. :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }"
  28. size="mini"
  29. @click="getCode"
  30. >
  31. {{ codeTips }}
  32. </u-button>
  33. </u-form-item>
  34. </u-form>
  35. </view>
  36. <view v-if="is_wx_phone && is_bind" class="" style="height: 150rpx;"></view>
  37. <view class="u-p-t-30 u-text-center u-flex">
  38. <u-checkbox :active-color="theme.bgColor" v-model="agreeChecked" name="agree">阅读并同意</u-checkbox>
  39. <text class="u-font-30 agree" @click="agreement" :style="[{ color: theme.bgColor }]">《用户协议》</text>
  40. </view>
  41. <view class="u-m-t-80" v-if="!is_wx_phone || !is_bind">
  42. <u-button hover-class="none" type="primary" :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }" shape="circle" @click="register">
  43. {{ is_bind ? '立即绑定' : '注册' }}
  44. </u-button>
  45. </view>
  46. <!-- #ifdef MP-WEIXIN -->
  47. <view class="u-m-t-60" v-if="is_bind && is_wx_phone">
  48. <u-button
  49. hover-class="none"
  50. open-type="getPhoneNumber"
  51. type="primary"
  52. :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }"
  53. shape="circle"
  54. @getphonenumber="getPhoneNumber"
  55. >
  56. 使用微信手机号码授权
  57. </u-button>
  58. </view>
  59. <view class="u-m-t-30 u-flex u-row-right" v-if="is_bind">
  60. <text v-text="is_wx_phone ? '使用其他手机号码权' : '使用微信手机号码授权'" @click="is_wx_phone = !is_wx_phone"></text>
  61. </view>
  62. <!-- #endif -->
  63. </view>
  64. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  65. <u-toast ref="uToast" />
  66. </view>
  67. </template>
  68. <script>
  69. import { loginfunc } from '@/common/fa.mixin.js';
  70. export default {
  71. mixins: [loginfunc],
  72. onLoad(e) {
  73. let query = this.$Route.query || e || {};
  74. this.is_bind = query.bind || '';
  75. if (!this.is_bing) {
  76. this.labelPosition = 'left';
  77. this.rules.username = [
  78. {
  79. required: true,
  80. message: '请输入用户名',
  81. trigger: ['change', 'blur']
  82. }
  83. ];
  84. this.rules.password = [
  85. {
  86. required: true,
  87. message: '请输入密码',
  88. trigger: 'change'
  89. }
  90. ];
  91. }
  92. },
  93. onReady() {
  94. if (!this.is_bind) {
  95. this.$refs.uForm.setRules(this.rules);
  96. }
  97. },
  98. watch: {
  99. is_wx_phone(newValue, oldValue) {
  100. if (!newValue) {
  101. this.$nextTick(() => {
  102. this.$refs.uForm.setRules(this.rules);
  103. });
  104. }
  105. }
  106. },
  107. data() {
  108. return {
  109. is_bind: '', //判断是绑定账号还是注册账号
  110. // #ifdef MP-WEIXIN
  111. is_wx_phone: true, //微信小程序默认为手机号码授权登录,不显示表单
  112. // #endif
  113. // #ifndef MP-WEIXIN
  114. is_wx_phone: false, //非微信小程序,需要显示表单
  115. // #endif
  116. agreeChecked: false,
  117. labelPosition: 'top',
  118. border: false,
  119. form: {
  120. username: '',
  121. password: '',
  122. mobile: '',
  123. code: ''
  124. },
  125. rules: {
  126. mobile: [
  127. {
  128. required: true,
  129. message: '请输入手机号码',
  130. trigger: 'change'
  131. },
  132. {
  133. // 自定义验证函数,见上说明
  134. validator: (rule, value, callback) => {
  135. return this.$u.test.mobile(value);
  136. },
  137. message: '手机号码不正确',
  138. trigger: ['change', 'blur']
  139. }
  140. ],
  141. code: [
  142. {
  143. required: true,
  144. message: '请输入短信验证码',
  145. trigger: 'change'
  146. }
  147. ]
  148. },
  149. codeTips: '',
  150. errorType: ['message']
  151. };
  152. },
  153. methods: {
  154. agreement() {
  155. this.$Router.push('/pages/my/agreement');
  156. },
  157. codeChange(text) {
  158. this.codeTips = text;
  159. },
  160. // 获取验证码
  161. getCode: async function() {
  162. if (!this.$u.test.mobile(this.form.mobile)) {
  163. this.$u.toast('手机号码格式不正确!');
  164. return;
  165. }
  166. if (this.$refs.uCode.canGetCode) {
  167. let res = await this.$api.getSmsSend({
  168. mobile: this.form.mobile,
  169. event: this.is_bind ? 'bind' : 'register'
  170. });
  171. this.$u.toast(res.msg);
  172. if (res.code) {
  173. this.$refs.uCode.start();
  174. }
  175. } else {
  176. this.$u.toast('倒计时结束后再发送');
  177. }
  178. },
  179. register() {
  180. if (!this.agreeChecked) {
  181. this.$refs.uToast.show({
  182. title: '请阅读并同意遵守《用户协议》',
  183. type: 'error'
  184. });
  185. return;
  186. }
  187. this.$refs.uForm.validate(valid => {
  188. if (valid) {
  189. this.is_bind ? this.goBind() : this.goReg();
  190. } else {
  191. this.$u.toast('验证失败');
  192. }
  193. });
  194. },
  195. // #ifdef MP-WEIXIN
  196. getPhoneNumber(e) {
  197. if (!this.agreeChecked) {
  198. this.$refs.uToast.show({
  199. title: '请阅读并同意遵守《用户协议》',
  200. type: 'error'
  201. });
  202. return;
  203. }
  204. let that = this;
  205. uni.login({
  206. provider: 'weixin',
  207. success: function(loginRes) {
  208. console.log(loginRes);
  209. that.$api.getWechatMobile({ encryptedData: e.detail.encryptedData, iv: e.detail.iv, code: loginRes.code }).then(function(res) {
  210. if (res.code == 1) {
  211. that.$u.vuex('vuex_token', res.data.token);
  212. that.success(3);
  213. } else {
  214. that.$u.toast(res.msg);
  215. }
  216. });
  217. },
  218. fail(e) {
  219. console.log(e);
  220. }
  221. });
  222. },
  223. // #endif
  224. //去注册
  225. goReg: async function() {
  226. if (this.vuex_wx_uid) {
  227. this.form.wx_user_id = this.vuex_wx_uid;
  228. }
  229. let res = await this.$api.goRegister(this.form);
  230. if (!res.code) {
  231. this.$u.toast(res.msg);
  232. return;
  233. }
  234. this.$u.vuex('vuex_token', res.data.token);
  235. this.success(3);
  236. },
  237. //绑定账号
  238. goBind: async function() {
  239. let res = await this.$api.goThirdAccount(this.form);
  240. if (!res.code) {
  241. this.$u.toast(res.msg);
  242. return;
  243. }
  244. let delta = 2;
  245. // #ifdef MP-WEIXIN
  246. delta = 3;
  247. // #endif
  248. if (res.data.userinfo) {
  249. this.$u.vuex('vuex_token', res.data.userinfo.token);
  250. this.success(delta);
  251. }
  252. }
  253. }
  254. };
  255. </script>
  256. <style>
  257. page {
  258. background-color: #ffffff;
  259. }
  260. .login {
  261. padding: 20% 10%;
  262. }
  263. .agree {
  264. margin-left: -25rpx;
  265. }
  266. </style>