register.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <div class="register">
  3. <h1>注册新账号</h1>
  4. <div class="hr"></div>
  5. <el-form
  6. v-if="usePhone"
  7. :model="ruleFormPhone"
  8. :rules="rulesPhone"
  9. ref="ruleFormPhone"
  10. label-width="100px"
  11. class="demo-ruleForm"
  12. label-position="top"
  13. :hide-required-asterisk="true"
  14. >
  15. <el-form-item label="手机号" prop="mobile">
  16. <input-area @change="chengeMobile"></input-area>
  17. </el-form-item>
  18. <el-form-item label="验证码" prop="ver">
  19. <input-ver @click="getVerMobile" @change="chengeVerMobile"></input-ver>
  20. </el-form-item>
  21. <el-form-item label="昵称" prop="nickname">
  22. <el-input v-model="ruleFormPhone.nickname" placeholder="请输入昵称"></el-input>
  23. </el-form-item>
  24. <el-form-item label="密码" prop="pwd">
  25. <el-input
  26. :type="pwdShow ? 'text' : 'password'"
  27. v-model="ruleFormPhone.pwd"
  28. placeholder="请输入密码,至少包含6位字符"
  29. >
  30. <i class="el-icon-view switch-passowrd" @click="pwdShow = !pwdShow" slot="suffix"></i>
  31. </el-input>
  32. </el-form-item>
  33. </el-form>
  34. <el-form
  35. v-else
  36. :model="ruleFormEmail"
  37. :rules="rulesEmail"
  38. ref="ruleFormEmail"
  39. label-width="100px"
  40. class="demo-ruleForm"
  41. label-position="top"
  42. :hide-required-asterisk="true"
  43. >
  44. <el-form-item label="邮箱" prop="email">
  45. <el-input placeholder="请输入邮箱" v-model="ruleFormEmail.email"></el-input>
  46. </el-form-item>
  47. <el-form-item label="验证码" prop="ver">
  48. <input-ver-img @change="chengeVerEmail"></input-ver-img>
  49. </el-form-item>
  50. <el-form-item label="昵称" prop="nickname">
  51. <el-input placeholder="请输入昵称" v-model="ruleFormEmail.nickname"></el-input>
  52. </el-form-item>
  53. <el-form-item label="密码" prop="pwd">
  54. <el-input
  55. :type="pwdShow ? 'text' : 'password'"
  56. placeholder="请输入密码,至少包含6位字符"
  57. v-model="ruleFormEmail.pwd"
  58. >
  59. <i class="el-icon-view switch-passowrd" @click="pwdShow = !pwdShow" slot="suffix"></i>
  60. </el-input>
  61. </el-form-item>
  62. </el-form>
  63. <p class="remind">
  64. 点击注册表示你已阅读并同意
  65. <a href="/outsource/agreement?from=register" target="_blank">《程序员客栈服务协议》</a>
  66. </p>
  67. <el-button type="primary" @click="doRegist">注册</el-button>
  68. <div class="checks">
  69. <span @click="doSwitch">{{usePhone ? '邮箱' : '手机'}}注册</span>
  70. <span @click="goLogin($event, true)">直接登录</span>
  71. </div>
  72. <div class="logos">
  73. <a
  74. href="https://github.com/login/oauth/authorize?client_id=65396ecb78f8cb000768&redirect_uri=https%3A%2F%2Fwww.proginn.com%2Fuser%2Flogingithub&scope=user%3Aemail"
  75. >
  76. <img src="~@/assets/img/user/github_logo.png" alt="github" />
  77. </a>
  78. <a
  79. href="https://api.weibo.com/oauth2/authorize?client_id=2749593033&redirect_uri=https%3A%2F%2Fwww.proginn.com%2Fuser%2Floginweibo&response_type=code&forcelogin=true"
  80. >
  81. <img src="~@/assets/img/user/sina_logo.png" alt="sina" />
  82. </a>
  83. <a
  84. href="https://open.weixin.qq.com/connect/qrconnect?appid=wx0f62bbdc948d0f23&redirect_uri=https%3A%2F%2Fwww.proginn.com%2Fuser%2Floginweixin&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect"
  85. >
  86. <img src="~@/assets/img/user/wechat_logo.png" alt="wechat" />
  87. </a>
  88. </div>
  89. <agreement :visible.sync="agreeShow"></agreement>
  90. <div id="getCode"></div>
  91. </div>
  92. </template>
  93. <script>
  94. import InputVer from "@/components/input/ver";
  95. import InputVerImg from "@/components/input/ver_img";
  96. import InputArea from "@/components/input/area";
  97. import Agreement from "@/components/agreement";
  98. export default {
  99. components: {
  100. InputVer,
  101. InputVerImg,
  102. InputArea,
  103. Agreement
  104. },
  105. head() {
  106. return {
  107. script: [
  108. { src: "https://cdn.dingxiang-inc.com/ctu-group/captcha-ui/index.js" }
  109. ]
  110. };
  111. },
  112. data() {
  113. let validateEmail = (rule, value, callback) => {
  114. if (!value.trim()) {
  115. callback(new Error("请输入邮箱"));
  116. } else {
  117. if (value.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)) {
  118. callback();
  119. } else {
  120. callback(new Error("邮箱格式不正确!"));
  121. }
  122. }
  123. };
  124. return {
  125. isRegistering: false,
  126. pwdShow: false,
  127. agreeShow: false,
  128. usePhone: true,
  129. ruleFormPhone: {
  130. mobile: "",
  131. ver: "",
  132. nickname: "",
  133. pwd: ""
  134. },
  135. rulesPhone: {
  136. mobile: [
  137. { required: true, message: "请输入手机号", trigger: "blur" },
  138. { min: 7, max: 16, message: "请输入正确的手机号", trigger: "blur" }
  139. ],
  140. ver: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  141. nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
  142. pwd: [
  143. { required: true, message: "请输入密码", trigger: "blur" },
  144. { min: 6, message: "密码至少为6位字符", trigger: "blur" }
  145. ]
  146. },
  147. ruleFormEmail: {
  148. email: "",
  149. ver: "",
  150. nickname: "",
  151. pwd: ""
  152. },
  153. rulesEmail: {
  154. email: [
  155. {
  156. validator: validateEmail,
  157. message: "请输入正确的邮箱",
  158. trigger: "blur"
  159. }
  160. ],
  161. ver: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  162. nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
  163. pwd: [
  164. { required: true, message: "请输入密码", trigger: "blur" },
  165. { min: 6, message: "密码至少为6位字符", trigger: "blur" }
  166. ]
  167. }
  168. };
  169. },
  170. mounted() {
  171. if (this.$store.getters.isLogin) {
  172. this.$message("你已有注册的账号");
  173. setTimeout(() => {
  174. location.href = "/";
  175. }, 1000);
  176. }
  177. },
  178. methods: {
  179. chengeVerMobile(val) {
  180. this.ruleFormPhone.ver = val;
  181. },
  182. chengeVerEmail(val) {
  183. this.ruleFormEmail.ver = val;
  184. },
  185. chengeMobile(val) {
  186. this.ruleFormPhone.mobile = val;
  187. },
  188. /**
  189. * 点击注册
  190. */
  191. doRegist() {
  192. let mobile = this.ruleFormPhone.mobile;
  193. let register = async body => {
  194. if (this.isRegistering) {
  195. console.log('防抖中。。。')
  196. return
  197. }
  198. this.isRegistering = true
  199. setTimeout(()=>{
  200. this.isRegistering = false
  201. }, 1500)
  202. let res = await this.$axios.$post("/api/passport/register", body);
  203. if (res && res.data) {
  204. this.$message.success(res.info);
  205. setTimeout(() => {
  206. location.href = "/user/success";
  207. }, 1000);
  208. }
  209. };
  210. if (this.usePhone) {
  211. this.$refs.ruleFormPhone.validate(valid => {
  212. if (valid) {
  213. let body = {
  214. auth_code: this.ruleFormPhone.ver,
  215. login_name: mobile,
  216. mobile,
  217. nickname: this.ruleFormPhone.nickname,
  218. password: this.ruleFormPhone.pwd
  219. };
  220. register(body);
  221. } else {
  222. return false;
  223. }
  224. });
  225. } else {
  226. this.$refs.ruleFormEmail.validate(valid => {
  227. if (valid) {
  228. let body = {
  229. captcha: this.ruleFormEmail.ver,
  230. email: this.ruleFormEmail.email,
  231. nickname: this.ruleFormEmail.nickname,
  232. password: this.ruleFormEmail.pwd
  233. };
  234. register(body);
  235. } else {
  236. return false;
  237. }
  238. });
  239. }
  240. },
  241. /**
  242. * 获取手机验证码
  243. * @params {function} run 闭包函数,用来启动验证码内部脚本
  244. */
  245. async getVerMobile(run) {
  246. // let checkRes = await this.$axios.$post('')
  247. let mobileSplit = this.ruleFormPhone.mobile.split("-");
  248. let area = mobileSplit[0] || "";
  249. let mobile = mobileSplit[1] || "";
  250. if (
  251. area.match(/\+86/) &&
  252. !mobile.match(
  253. /^[0-9]{11}$/
  254. )
  255. ) {
  256. this.$message({
  257. message: "请输入正确格式的手机号",
  258. type: "error"
  259. });
  260. return;
  261. }
  262. const that = this;
  263. const myCaptcha = _dx.Captcha(document.getElementById("getCode"), {
  264. appId: "2db960e4ca0eaeee12ef63db7e5b3918", //appId,在控制台中“应用管理”或“应用配置”模块获取
  265. style: "popup",
  266. success: async function(token) {
  267. let res = await that.$axios.$post("/api/user/sendMobileAuthCode", {
  268. mobile: area + "-" + mobile,
  269. token,
  270. type: 1 // 注册1,忘记密码2,提现3
  271. });
  272. if (res) {
  273. run();
  274. myCaptcha.hide();
  275. }
  276. }
  277. });
  278. myCaptcha.show();
  279. },
  280. /**
  281. * 点击切换注册模式
  282. */
  283. doSwitch() {
  284. this.usePhone = !this.usePhone;
  285. let clearValidate;
  286. if (!this.usePhone)
  287. clearValidate = this.$refs.ruleFormPhone.clearValidate;
  288. else clearValidate = this.$refs.ruleFormEmail.clearValidate;
  289. // setTimeout(clearValidate, 200)
  290. this.$nextTick(clearValidate);
  291. }
  292. }
  293. };
  294. </script>
  295. <style scoped>
  296. @import "@/assets/css/h1_line.css";
  297. .register {
  298. display: flex;
  299. flex-direction: column;
  300. align-items: center;
  301. width: 1000px;
  302. background: white;
  303. }
  304. .el-button,
  305. .el-input {
  306. width: 320px;
  307. }
  308. .remind {
  309. margin: 14px 0 22px;
  310. font-size: 12px;
  311. color: #919aa7;
  312. }
  313. .checks {
  314. color: #666;
  315. margin: 14px 0 30px;
  316. white-space: nowrap;
  317. }
  318. .checks span {
  319. padding: 0 20px;
  320. cursor: pointer;
  321. }
  322. .checks span:first-child {
  323. border-right: 1px solid #666;
  324. }
  325. .logos {
  326. display: flex;
  327. justify-content: space-between;
  328. align-items: center;
  329. width: 154px;
  330. }
  331. .logos img {
  332. width: 34px;
  333. height: 34px;
  334. margin-bottom: 50px;
  335. }
  336. .switch-passowrd {
  337. margin-right: 8px;
  338. cursor: pointer;
  339. }
  340. </style>