register.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. pwdShow: false,
  126. agreeShow: false,
  127. usePhone: true,
  128. ruleFormPhone: {
  129. mobile: "",
  130. ver: "",
  131. nickname: "",
  132. pwd: ""
  133. },
  134. rulesPhone: {
  135. mobile: [
  136. { required: true, message: "请输入手机号", trigger: "blur" },
  137. { min: 7, max: 16, message: "请输入正确的手机号", trigger: "blur" }
  138. ],
  139. ver: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  140. nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
  141. pwd: [
  142. { required: true, message: "请输入密码", trigger: "blur" },
  143. { min: 6, message: "密码至少为6位字符", trigger: "blur" }
  144. ]
  145. },
  146. ruleFormEmail: {
  147. email: "",
  148. ver: "",
  149. nickname: "",
  150. pwd: ""
  151. },
  152. rulesEmail: {
  153. email: [
  154. {
  155. validator: validateEmail,
  156. message: "请输入正确的邮箱",
  157. trigger: "blur"
  158. }
  159. ],
  160. ver: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  161. nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
  162. pwd: [
  163. { required: true, message: "请输入密码", trigger: "blur" },
  164. { min: 6, message: "密码至少为6位字符", trigger: "blur" }
  165. ]
  166. }
  167. };
  168. },
  169. mounted() {
  170. if (this.$store.getters.isLogin) {
  171. this.$message("你已有注册的账号");
  172. setTimeout(() => {
  173. location.href = "/";
  174. }, 1000);
  175. }
  176. },
  177. methods: {
  178. chengeVerMobile(val) {
  179. this.ruleFormPhone.ver = val;
  180. },
  181. chengeVerEmail(val) {
  182. this.ruleFormEmail.ver = val;
  183. },
  184. chengeMobile(val) {
  185. this.ruleFormPhone.mobile = val;
  186. },
  187. /**
  188. * 点击注册
  189. */
  190. doRegist() {
  191. let mobile = this.ruleFormPhone.mobile;
  192. let register = async body => {
  193. let res = await this.$axios.$post("/api/passport/register", body);
  194. if (res && res.data) {
  195. this.$message.success(res.info);
  196. setTimeout(() => {
  197. location.href = "/user/success";
  198. }, 1000);
  199. }
  200. };
  201. if (this.usePhone) {
  202. this.$refs.ruleFormPhone.validate(valid => {
  203. if (valid) {
  204. let body = {
  205. auth_code: this.ruleFormPhone.ver,
  206. login_name: mobile,
  207. mobile,
  208. nickname: this.ruleFormPhone.nickname,
  209. password: this.ruleFormPhone.pwd
  210. };
  211. register(body);
  212. } else {
  213. return false;
  214. }
  215. });
  216. } else {
  217. this.$refs.ruleFormEmail.validate(valid => {
  218. if (valid) {
  219. let body = {
  220. captcha: this.ruleFormEmail.ver,
  221. email: this.ruleFormEmail.email,
  222. nickname: this.ruleFormEmail.nickname,
  223. password: this.ruleFormEmail.pwd
  224. };
  225. register(body);
  226. } else {
  227. return false;
  228. }
  229. });
  230. }
  231. },
  232. /**
  233. * 获取手机验证码
  234. * @params {function} run 闭包函数,用来启动验证码内部脚本
  235. */
  236. async getVerMobile(run) {
  237. // let checkRes = await this.$axios.$post('')
  238. let mobileSplit = this.ruleFormPhone.mobile.split("-");
  239. let area = mobileSplit[0] || "";
  240. let mobile = mobileSplit[1] || "";
  241. if (
  242. area.match(/\+86/) &&
  243. !mobile.match(
  244. /^[0-9]{11}$/
  245. )
  246. ) {
  247. this.$message({
  248. message: "请输入正确格式的手机号",
  249. type: "error"
  250. });
  251. return;
  252. }
  253. const that = this;
  254. const myCaptcha = _dx.Captcha(document.getElementById("getCode"), {
  255. appId: "2db960e4ca0eaeee12ef63db7e5b3918", //appId,在控制台中“应用管理”或“应用配置”模块获取
  256. style: "popup",
  257. success: async function(token) {
  258. let res = await that.$axios.$post("/api/user/sendMobileAuthCode", {
  259. mobile: area + "-" + mobile,
  260. token,
  261. type: 1 // 注册1,忘记密码2,提现3
  262. });
  263. if (res) {
  264. run();
  265. myCaptcha.hide();
  266. }
  267. }
  268. });
  269. myCaptcha.show();
  270. },
  271. /**
  272. * 点击切换注册模式
  273. */
  274. doSwitch() {
  275. this.usePhone = !this.usePhone;
  276. let clearValidate;
  277. if (!this.usePhone)
  278. clearValidate = this.$refs.ruleFormPhone.clearValidate;
  279. else clearValidate = this.$refs.ruleFormEmail.clearValidate;
  280. // setTimeout(clearValidate, 200)
  281. this.$nextTick(clearValidate);
  282. }
  283. }
  284. };
  285. </script>
  286. <style scoped>
  287. @import "@/assets/css/h1_line.css";
  288. .register {
  289. display: flex;
  290. flex-direction: column;
  291. align-items: center;
  292. width: 1000px;
  293. background: white;
  294. }
  295. .el-button,
  296. .el-input {
  297. width: 320px;
  298. }
  299. .remind {
  300. margin: 14px 0 22px;
  301. font-size: 12px;
  302. color: #919aa7;
  303. }
  304. .checks {
  305. color: #666;
  306. margin: 14px 0 30px;
  307. white-space: nowrap;
  308. }
  309. .checks span {
  310. padding: 0 20px;
  311. cursor: pointer;
  312. }
  313. .checks span:first-child {
  314. border-right: 1px solid #666;
  315. }
  316. .logos {
  317. display: flex;
  318. justify-content: space-between;
  319. align-items: center;
  320. width: 154px;
  321. }
  322. .logos img {
  323. width: 34px;
  324. height: 34px;
  325. margin-bottom: 50px;
  326. }
  327. .switch-passowrd {
  328. margin-right: 8px;
  329. cursor: pointer;
  330. }
  331. </style>