register.vue 11 KB

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