bindweixin.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!-- @format -->
  2. <template>
  3. <view class="container">
  4. <u-form ref="uForm" :model="form" labelWidth="160" labelPosition="top">
  5. <u-form-item :required="true" label="用户名" prop="name">
  6. <u-input placeholder="请输入用户名" v-model="form.name"></u-input>
  7. </u-form-item>
  8. <u-form-item :required="true" label="密码" prop="password">
  9. <u-input placeholder="请输入密码" v-model="form.password"> </u-input>
  10. </u-form-item>
  11. <u-form-item>
  12. <u-button type="primary" @click="onSubmit">提交</u-button>
  13. </u-form-item>
  14. </u-form>
  15. <u-alert
  16. type="warning"
  17. :show-icon="true"
  18. title="第一次用微信登录需要绑定用户,如果没有账号请先到登录页进行注册。"
  19. ></u-alert>
  20. </view>
  21. </template>
  22. <script>
  23. import { wxBindUser } from "@/api/login";
  24. export default {
  25. data() {
  26. return {
  27. form: {
  28. openid: null,
  29. name: null,
  30. password: null,
  31. unionid: null,
  32. },
  33. rules: {
  34. name: [
  35. {
  36. required: true,
  37. message: "用户名不能为空",
  38. trigger: "blur",
  39. },
  40. ],
  41. password: [
  42. {
  43. required: true,
  44. message: "密码不能为空",
  45. trigger: "blur",
  46. },
  47. {
  48. // 自定义验证函数,见上说明
  49. validator: (rule, value, callback) => {
  50. if (value.trim().length < 8) {
  51. callback(new Error("密码长度不能低于8位"));
  52. } else {
  53. callback();
  54. }
  55. },
  56. // 触发器可以同时用blur和change
  57. trigger: ["change", "blur"],
  58. },
  59. ],
  60. },
  61. };
  62. },
  63. onReady() {
  64. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  65. this.$refs.uForm.setRules(this.rules);
  66. },
  67. onLoad(options) {
  68. this.form.openid = options.openid;
  69. this.form.unionid = options.unionId;
  70. },
  71. methods: {
  72. onSubmit() {
  73. this.$refs.uForm.validate().then(async (res) => {
  74. try {
  75. uni.showLoading({
  76. title: "提交中....",
  77. });
  78. await wxBindUser(this.form);
  79. uni.showToast({
  80. title: "绑定完成,请重新登录",
  81. //将值设置为 success 或者直接不用写icon这个参数
  82. icon: "success",
  83. //显示持续时间为 2秒
  84. duration: 2000,
  85. });
  86. uni.navigateBack({
  87. delta: 1,
  88. });
  89. } catch (error) {
  90. } finally {
  91. }
  92. });
  93. },
  94. },
  95. };
  96. </script>
  97. <style lang="scss">
  98. .container {
  99. height: 100vh;
  100. background: rgb(248, 249, 250);
  101. padding: 30rpx;
  102. }
  103. </style>