login.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <view class="warp">
  3. <view class="rect" @tap.stop>
  4. <view class="rectOne">Hi~欢迎来到{{config.title||'学吧'}}</view>
  5. <view class="rectTwo">
  6. <view class="icon">
  7. <view class="left">头像</view>
  8. <view class="right">
  9. <up-upload :fileList="form.icon" @afterRead="afterRead" @delete="deletePic" name="icon" multiple
  10. :maxCount="1"></up-upload>
  11. </view>
  12. </view>
  13. <view class="other">
  14. <view class="left">身份</view>
  15. <view class="right">
  16. <up-radio-group v-model="form.type" placement="row">
  17. <up-radio :customStyle="{marginRight: '16px'}" v-for="(item, index) in roleList"
  18. :key="index" :label="item.label" :name="item.value">
  19. </up-radio>
  20. </up-radio-group>
  21. </view>
  22. </view>
  23. <view class="other">
  24. <view class="left">昵称</view>
  25. <view class="right">
  26. <up-input v-model="form.nick_name" placeholder="请输入昵称" border="surround"
  27. shape="circle"></up-input>
  28. </view>
  29. </view>
  30. <view class="other">
  31. <view class="left">手机号</view>
  32. <view class="right">
  33. <up-input v-model="form.phone" placeholder="请输入手机号" border="surround" shape="circle"></up-input>
  34. </view>
  35. </view>
  36. <view class="agree">
  37. <checkbox-group @change="changeAgree">
  38. <label>
  39. <checkbox color="#3c9cff" :checked="agree" />
  40. <text @tap.stop="toAgree()">我同意使用我所提交的信息用于{{config.title||'学吧'}}程序使用<text
  41. style="color: #2979ff;">《软件使用许可协议》</text>和 <text
  42. style="color: #2979ff;">《隐私协议》</text></text>
  43. </label>
  44. </checkbox-group>
  45. </view>
  46. <view class="button">
  47. <button class="button_1" size="default" type="default" @click="toCancel">取消</button>
  48. <button class="button_2" size="default" type="default" @click="toLogin">注册并登录</button>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script lang="ts" setup>
  55. const emit = defineEmits(["showChange"])
  56. import moment from 'moment';
  57. import { inject, computed, ref } from 'vue';
  58. //该依赖已内置不需要单独安装
  59. import { onShow, onPullDownRefresh } from "@dcloudio/uni-app";
  60. // 请求接口
  61. const $api = inject('$api');
  62. const $apifile = inject('$apifile');
  63. // 遮罩层
  64. const showType = ref(false);
  65. // 表单
  66. const form = ref({});
  67. //字典表
  68. const roleList = ref([]);
  69. // 用户协议
  70. const agree = ref(false);
  71. // openid
  72. const openid = computed(() => {
  73. return uni.getStorageSync('openid');
  74. })
  75. // user
  76. const user = computed(() => {
  77. return uni.getStorageSync('user');
  78. })
  79. const config = computed(() => {
  80. return uni.getStorageSync('config');
  81. })
  82. onShow(async () => {
  83. await searchOther();
  84. })
  85. // 其他查询信息
  86. const searchOther = async () => {
  87. let res;
  88. // 角色
  89. res = await $api(`dictData`, 'GET', { code: 'role', is_use: '0' });
  90. if (res.errcode === 0) roleList.value = res.data;
  91. };
  92. // 删除图片
  93. const deletePic = (event) => {
  94. form.value.icon = []
  95. };
  96. // 新增图片
  97. const afterRead = async (event) => {
  98. const url = event.file[0].url
  99. const result = await $apifile(`/web/learn_user/upload`, 'file', url, 'file');
  100. if (result.errcode === 0) form.value.icon = [result]
  101. };
  102. // 选择身份
  103. const typeSelect = async (event) => {
  104. console.log(event);
  105. }
  106. // 取消
  107. const toCancel = async () => {
  108. form.value = { icon: [] }
  109. showType.value = false
  110. agree.value = false
  111. emit("showChange", false);
  112. }
  113. // 验证手机号
  114. const isValidPhoneNumber = (phoneNumber) => {
  115. const regex = /^1[3456789]\d{9}$/;
  116. return regex.test(phoneNumber);
  117. }
  118. // 登录
  119. const toLogin = async () => {
  120. if (agree.value) {
  121. if (openid.value) {
  122. if (form.value.type) {
  123. if (form.value.phone) {
  124. if (isValidPhoneNumber(form.value.phone)) {
  125. uni.getUserProfile({
  126. desc: '用于展示',
  127. success: async function (res) {
  128. const type = form.value.type
  129. delete form.value.type
  130. let parmas = {
  131. openid: openid.value,
  132. status: '1',
  133. }
  134. if (!form.value.nick_name) parmas.nick_name = res.userInfo.nickName + moment().valueOf()
  135. if (!form.value.icon || form.value.icon.length === 0) parmas.icon = config.value.icon
  136. let arr;
  137. if (type == '0') arr = await $api(`teacher`, 'POST', { ...form.value, ...parmas, is_show: '1' });
  138. else arr = await $api(`student`, 'POST', { ...form.value, ...parmas });
  139. if (arr.errcode == '0') {
  140. let role_type;
  141. if (type == '0') role_type = 'Teacher'
  142. else role_type = 'Student'
  143. uni.setStorageSync('user', { ...arr.data, role_type });
  144. uni.showToast({
  145. title: '登录成功',
  146. icon: 'success'
  147. });
  148. await toCancel()
  149. } else {
  150. uni.showToast({
  151. title: arr.errmsg,
  152. icon: 'error'
  153. });
  154. }
  155. },
  156. fail: function (err) {
  157. console.log(err);
  158. }
  159. })
  160. } else {
  161. uni.showToast({
  162. title: '请输入正确的手机号!',
  163. icon: 'none'
  164. })
  165. }
  166. } else {
  167. uni.showToast({
  168. title: '请输入手机号!',
  169. icon: 'none'
  170. })
  171. }
  172. } else {
  173. uni.showToast({
  174. title: '请选择用户身份!',
  175. icon: 'none'
  176. })
  177. }
  178. } else {
  179. uni.showToast({
  180. title: '系统更新中,请稍后再试!',
  181. icon: 'none'
  182. })
  183. }
  184. } else {
  185. uni.showToast({
  186. title: '请阅读并同意软件使用许可协议和隐私协议',
  187. icon: 'none'
  188. })
  189. }
  190. }
  191. // 查看隐私协议
  192. const toAgree = () => {
  193. uni.navigateTo({
  194. url: `/pagesHome/agree/index`
  195. })
  196. };
  197. // 同意隐私协议
  198. const changeAgree = () => {
  199. agree.value = !agree.value;
  200. };
  201. </script>
  202. <style lang="scss" scoped>
  203. .warp {
  204. display: flex;
  205. align-items: center;
  206. justify-content: center;
  207. height: 100%;
  208. }
  209. .rect {
  210. width: 90%;
  211. height: 380px;
  212. border-radius: 5px;
  213. padding: 3vw;
  214. background-color: #fff;
  215. .rectOne {
  216. padding: 2vw 0;
  217. text-align: center;
  218. font-size: var(--font16Size);
  219. }
  220. .rectTwo {
  221. .icon {
  222. width: 100%;
  223. display: flex;
  224. align-items: center;
  225. margin: 2vw 0;
  226. .left {
  227. width: 40%;
  228. font-size: var(--font14Size);
  229. }
  230. .right {
  231. width: 60%;
  232. }
  233. }
  234. .other {
  235. width: 100%;
  236. display: flex;
  237. align-items: center;
  238. margin: 3vw 0 0 0;
  239. .left {
  240. width: 15%;
  241. font-size: var(--font14Size);
  242. }
  243. .right {
  244. width: 85%;
  245. }
  246. }
  247. .agree {
  248. padding: 2vw;
  249. text-align: center;
  250. font-size: var(--font12Size);
  251. }
  252. .button {
  253. display: flex;
  254. justify-content: space-between;
  255. margin: 5vw 0 0 0;
  256. .button_1 {
  257. font-size: var(--font14Size);
  258. border-radius: 10vw;
  259. margin: 0 2vw;
  260. width: 35%;
  261. }
  262. .button_2 {
  263. font-size: var(--font14Size);
  264. color: var(--mainColor);
  265. background-color: var(--3c9Color);
  266. border-radius: 10vw;
  267. margin: 0 2vw;
  268. width: 55%;
  269. }
  270. }
  271. }
  272. }
  273. </style>