bindweixin.vue 2.8 KB

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