index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one" v-if="platform.uniPlatform=='app'">
  5. <uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
  6. <uni-forms-item label="手机号" name="phone">
  7. <uni-easyinput type="text" v-model="form.phone" placeholder="请输入手机号" />
  8. </uni-forms-item>
  9. <uni-forms-item label="验证码" name="code">
  10. <view class="yzm">
  11. <view class="l">
  12. <uni-easyinput type="text" v-model="form.code" placeholder="请输入手机验证码" />
  13. </view>
  14. <view class="r">
  15. <button type="default" size="mini" @tap.stop="sendCount">{{time_count==0?'验证码':time_count}}</button>
  16. </view>
  17. </view>
  18. </uni-forms-item>
  19. <view class="btn">
  20. <button type="default" size="mini" @click="toSubmit('form')">提交登录</button>
  21. </view>
  22. </uni-forms>
  23. </view>
  24. <view class="two" v-else-if="platform.uniPlatform=='mp-weixin'">
  25. <view class="icon">
  26. <text class="iconfont icon-weixin"></text>
  27. </view>
  28. <view class="btn">
  29. <button type="default" size="mini" @tap="wxLogin()">微信信任登录</button>
  30. </view>
  31. </view>
  32. <view class="thr">
  33. <view class="thr_1">
  34. <checkbox-group @change="changeAgree">
  35. <label>
  36. <checkbox :checked="agree" />
  37. <text @tap="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  38. </label>
  39. </checkbox-group>
  40. </view>
  41. </view>
  42. </view>
  43. <uni-popup ref="dialogShow" type="dialog">
  44. <uni-popup-dialog :title="dialog.title" mode="base" :before-close="true" @confirm="diaSubmit(dialog)" @close="diaReset(dialog)">
  45. <view class="dialog dialog_1" v-if="dialog.type=='1'">
  46. <uni-forms ref="phoneForm" :model="phoneForm" label-width="auto">
  47. <uni-forms-item label="方式">
  48. <uni-data-checkbox v-model="phoneForm.type" :localdata="typeList" />
  49. </uni-forms-item>
  50. <uni-forms-item label="微信" v-if="phoneForm.type==0">
  51. <button type="default" size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">获取微信绑定手机</button>
  52. </uni-forms-item>
  53. <uni-forms-item label="手机号" v-if="phoneForm.type==0">
  54. <uni-easyinput type="number" v-model="phoneForm.phone" disabled />
  55. </uni-forms-item>
  56. <uni-forms-item label="手机号" v-if="phoneForm.type==1">
  57. <view class="yzm">
  58. <view class="l">
  59. <uni-easyinput type="number" v-model="phoneForm.phone" placeholder="请输入手机号" />
  60. </view>
  61. <view class="r">
  62. <button type="default" size="mini" @tap="sendCount">{{time_count==0?'验证码':time_count}}</button>
  63. </view>
  64. </view>
  65. </uni-forms-item>
  66. <uni-forms-item label="验证码" v-if="phoneForm.type==1">
  67. <uni-easyinput type="number" v-model="phoneForm.code" placeholder="请输入验证码" />
  68. </uni-forms-item>
  69. </uni-forms>
  70. </view>
  71. </uni-popup-dialog>
  72. </uni-popup>
  73. </mobile-frame>
  74. </template>
  75. <script>
  76. export default {
  77. data() {
  78. return {
  79. // 平台信息
  80. platform: {},
  81. //openid
  82. openid: '',
  83. // 隐私协议
  84. agree: false,
  85. form: {},
  86. rules: {
  87. phone: {
  88. rules: [{
  89. required: true,
  90. errorMessage: '请输入手机号',
  91. },
  92. {
  93. minLength: 11,
  94. maxLength: 11,
  95. errorMessage: '账号长度在{maxLength}个字符',
  96. }
  97. ]
  98. },
  99. code: {
  100. rules: [{
  101. required: true,
  102. errorMessage: '请输入验证码',
  103. }, ]
  104. },
  105. },
  106. // 微信登陆
  107. // 无账号
  108. is_user: false,
  109. user: {},
  110. // 弹框
  111. dialog: {
  112. title: '绑定手机号',
  113. type: '1'
  114. },
  115. // 绑定手机号
  116. phoneForm: {
  117. type: 0,
  118. },
  119. typeList: [ //
  120. {
  121. text: '微信',
  122. value: 0
  123. },
  124. {
  125. text: '短信验证',
  126. value: 1
  127. }
  128. ],
  129. // 倒计时
  130. time_count: 0
  131. };
  132. },
  133. onShow: function() {
  134. const that = this;
  135. that.search();
  136. },
  137. methods: {
  138. search() {
  139. const that = this;
  140. uni.getStorage({
  141. key: 'system',
  142. success: function(res) {
  143. if (res.data) that.$set(that, `platform`, res.data);
  144. // 获取openid
  145. that.searchOpenid()
  146. }
  147. })
  148. },
  149. // 查询openid
  150. searchOpenid() {
  151. const that = this;
  152. if (that.platform.uniPlatform == 'mp-weixin') {
  153. uni.login({
  154. provider: 'weixin',
  155. success: async function(res) {
  156. const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
  157. js_code: res.code,
  158. config: 'pointApp'
  159. })
  160. if (aee.errcode == '0') {
  161. that.$set(that, `openid`, aee.data.openid);
  162. // 微信登录
  163. that.openidLogin(aee.data.openid);
  164. }
  165. },
  166. fail: function(err) {
  167. console.log(err);
  168. }
  169. })
  170. } else if (that.platform.uniPlatform == 'app') {
  171. console.log('to do');
  172. }
  173. },
  174. // 有账号,微信登录,直接返回上一页
  175. async openidLogin(e) {
  176. const that = this;
  177. let res = await that.$api(`/user/wxLogin`, 'POST', {
  178. openid: e
  179. })
  180. if (res.errcode == '0') {
  181. uni.setStorage({
  182. data: res.data,
  183. key: 'token',
  184. success: function() {
  185. uni.navigateBack({
  186. delta: 1
  187. })
  188. }
  189. })
  190. } else {
  191. if (res.errcode == '-5') {
  192. console.log('无账号');
  193. that.$set(that, `is_user`, false)
  194. }
  195. }
  196. },
  197. // 微信信任登录
  198. async wxLogin() {
  199. const that = this;
  200. if (that.agree) {
  201. uni.getUserProfile({
  202. desc: '用于展示',
  203. success: async function(res) {
  204. let parmas = {
  205. openid: that.openid,
  206. icon: [{
  207. url: res.userInfo.avatarUrl
  208. }],
  209. name: res.userInfo.nickName,
  210. }
  211. const arr = await that.$api(`/user`, 'POST', parmas);
  212. if (arr.errcode == '0') {
  213. that.$set(that, `user`, arr.data);
  214. // 打开弹框
  215. that.diaShow();
  216. } else {
  217. uni.showToast({
  218. title: arr.errmsg,
  219. icon: 'none'
  220. });
  221. }
  222. },
  223. fail: function(err) {
  224. console.log(err);
  225. }
  226. })
  227. } else {
  228. uni.showToast({
  229. title: '请阅读并同意用户协议和隐私政策',
  230. icon: 'none'
  231. })
  232. }
  233. },
  234. // 微信信任登录-1
  235. async wxLogins() {
  236. const that = this;
  237. const agg = await that.$api(`/user/wxLogin`, 'POST', {
  238. openid: that.openid
  239. })
  240. if (agg.errcode == '0') {
  241. uni.setStorage({
  242. data: agg.data,
  243. key: 'token',
  244. success: function() {
  245. uni.navigateBack({
  246. delta: 1
  247. })
  248. }
  249. })
  250. } else {
  251. uni.showToast({
  252. title: agg.errmsg,
  253. icon: 'none'
  254. });
  255. }
  256. },
  257. // 打开弹框,绑定手机号
  258. diaShow() {
  259. const that = this;
  260. that.$refs.dialogShow.open();
  261. },
  262. // 获取手机号
  263. async getUserPhone(e) {
  264. const that = this;
  265. let user = that.user;
  266. let detail = e.detail;
  267. let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
  268. config: 'pointApp',
  269. code: detail.code
  270. })
  271. if (res.errcode == '0') {
  272. let phone = res.data && res.data.phone_info && res.data.phone_info.purePhoneNumber || '';
  273. that.$set(that.phoneForm, `phone`, phone);
  274. } else {
  275. uni.showToast({
  276. title: res.errmsg,
  277. icon: 'none'
  278. })
  279. }
  280. },
  281. // 短信验证
  282. // 获取验证码
  283. sendCount() {
  284. const that = this;
  285. let form = that.form && that.form.phone ? that.form : that.phoneForm;
  286. if (form && form.phone) {
  287. let time_count = 60;
  288. uni.showToast({
  289. title: '发送成功',
  290. icon: 'none'
  291. });
  292. that.$set(that, `time_count`, time_count);
  293. that.timeDown();
  294. } else {
  295. uni.showToast({
  296. title: '输入错误,请重新输入!',
  297. icon: 'none'
  298. })
  299. }
  300. },
  301. // 倒计时
  302. timeDown() {
  303. const that = this;
  304. var times = setInterval(() => {
  305. that.time_count--;
  306. if (that.time_count <= 0) {
  307. clearInterval(times);
  308. }
  309. }, 1000);
  310. },
  311. // 弹框确认(关闭)
  312. async diaSubmit(e) {
  313. const that = this;
  314. let user = this.user;
  315. if (e.type == '1') {
  316. const that = this;
  317. let form = that.phoneForm;
  318. if (form.type == '0') {
  319. if (form && form.phone) that.updatePhone(form.phone);
  320. } else if (form.type == '1') {
  321. let code = '1234';
  322. if (form.code == code) {
  323. if (form && form.phone) that.updatePhone(form.phone);
  324. } else {
  325. uni.showToast({
  326. title: '短信验证失败,请重新输入!',
  327. icon: 'none'
  328. })
  329. }
  330. }
  331. }
  332. },
  333. async updatePhone(e) {
  334. const that = this;
  335. let user = that.user;
  336. let arr = await that.$api(`/user/${user._id}`, 'POST', {
  337. phone: e
  338. })
  339. if (arr.errcode == '0') {
  340. that.wxLogins();
  341. } else {
  342. uni.showToast({
  343. title: res.errmsg,
  344. icon: 'none'
  345. })
  346. }
  347. },
  348. // 弹框取消(关闭)
  349. diaReset(e) {
  350. const that = this;
  351. if (e.type == '1') {
  352. that.wxLogins();
  353. }
  354. },
  355. // 账号登录
  356. toSubmit(ref) {
  357. const that = this;
  358. let agree = that.agree;
  359. that.$refs[ref].validate().then(async params => {
  360. let code = '1234';
  361. if (params.code == code) {
  362. if (agree) {
  363. console.log(params);
  364. } else {
  365. uni.showToast({
  366. title: '请阅读并同意用户协议和隐私政策',
  367. icon: 'none'
  368. })
  369. }
  370. } else {
  371. uni.showToast({
  372. title: '验证码错误',
  373. icon: 'none'
  374. })
  375. }
  376. }).catch(err => {
  377. console.log(err);
  378. })
  379. },
  380. // 同意隐私协议
  381. changeAgree() {
  382. const that = this;
  383. if (that.agree) that.$set(that, `agree`, false)
  384. else that.$set(that, `agree`, true);
  385. },
  386. // 查看隐私协议
  387. toAgree() {
  388. uni.navigateTo({
  389. url: `/pages/other/agree`
  390. })
  391. },
  392. }
  393. }
  394. </script>
  395. <style lang="scss">
  396. .main {
  397. .one {
  398. padding: 2vw;
  399. .btn {
  400. text-align: center;
  401. margin: 0 0 2vw 0;
  402. width: 96vw;
  403. button {
  404. width: 40%;
  405. padding: 1vw 2vw;
  406. background-color: var(--f35BColor);
  407. color: var(--fffColor);
  408. font-size: var(--font20Size);
  409. }
  410. }
  411. }
  412. .two {
  413. text-align: center;
  414. margin: 20vw 0;
  415. .icon {
  416. margin: 0 0 5vw 0;
  417. text {
  418. font-size: 60px;
  419. }
  420. }
  421. .btn {
  422. button {
  423. background-color: var(--f35BColor);
  424. color: var(--fffColor);
  425. }
  426. }
  427. }
  428. .thr {
  429. .thr_1 {
  430. padding: 0 2vw;
  431. font-size: 14px;
  432. text-align: center;
  433. position: absolute;
  434. width: 96vw;
  435. bottom: 25vw;
  436. }
  437. }
  438. }
  439. .dialog {
  440. width: 100vw;
  441. }
  442. .dialog_1 {
  443. .uni-forms {
  444. display: flex;
  445. form {
  446. width: 100%;
  447. }
  448. }
  449. .uni-data-checklist {
  450. border: 1px solid #E5E5E5;
  451. padding: 1vw;
  452. border-radius: 5px;
  453. }
  454. .uni-forms-item {
  455. margin-bottom: 2vw !important;
  456. }
  457. }
  458. .yzm {
  459. display: flex;
  460. flex-direction: row;
  461. justify-content: space-between;
  462. .l {
  463. flex-grow: 1;
  464. }
  465. .r {
  466. width: 18vw;
  467. button {
  468. width: 100%;
  469. line-height: 2.8;
  470. padding: 0 1vw;
  471. }
  472. }
  473. }
  474. </style>