index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="two">
  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="password">
  10. <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
  11. </uni-forms-item>
  12. <view class="btn">
  13. <button type="default" size="mini" @click="toSubmit('form')">登录</button>
  14. <button type="default" size="mini" @click="wxLogin()">微信信任登录</button>
  15. </view>
  16. </uni-forms>
  17. </view>
  18. </view>
  19. </mobile-frame>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. // 平台信息
  26. platform: {},
  27. openid: '',
  28. logoUrl: this.$config.logoUrl,
  29. form: {},
  30. rules: {
  31. phone: {
  32. rules: [{
  33. required: true,
  34. errorMessage: '请输入账号',
  35. },
  36. {
  37. minLength: 11,
  38. maxLength: 11,
  39. errorMessage: '账号长度在{maxLength}个字符',
  40. }
  41. ]
  42. },
  43. password: {
  44. rules: [{
  45. required: true,
  46. errorMessage: '请输入密码',
  47. }, ]
  48. },
  49. }
  50. };
  51. },
  52. onShow: function() {
  53. const that = this;
  54. that.search();
  55. },
  56. methods: {
  57. search() {
  58. const that = this;
  59. uni.getStorage({
  60. key: 'system',
  61. success: function(res) {
  62. if (res.data) that.$set(that, `platform`, res.data);
  63. // 获取openid
  64. that.searchOpenid()
  65. }
  66. })
  67. },
  68. // 查询openid
  69. searchOpenid() {
  70. const that = this;
  71. if (that.platform.uniPlatform == 'mp-weixin') {
  72. uni.login({
  73. provider: 'weixin',
  74. success: async function(res) {
  75. const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
  76. js_code: res.code,
  77. config: 'pointApp'
  78. })
  79. if (aee.errcode == '0') {
  80. that.$set(that, `openid`, aee.data.openid)
  81. }
  82. },
  83. fail: function(err) {
  84. console.log(err);
  85. }
  86. })
  87. } else if (that.platform.uniPlatform == 'app') {
  88. console.log('to do');
  89. }
  90. },
  91. // 账号登录
  92. toSubmit(ref) {
  93. const that = this;
  94. that.$refs[ref].validate().then(async params => {
  95. const res = await that.$api(`/user/login`, 'POST', {
  96. ...params
  97. })
  98. if (res.errcode == '0') {
  99. uni.showToast({
  100. title: '登录成功',
  101. icon: 'success'
  102. });
  103. uni.setStorage({
  104. key: 'token',
  105. data: res.data,
  106. success: function() {
  107. uni.navigateBack({
  108. delta: 1
  109. })
  110. }
  111. });
  112. } else {
  113. if (res.errcode == '-5') {
  114. // 账号无,注册账号
  115. that.createUser(params);
  116. } else {
  117. uni.showToast({
  118. title: res.errmsg,
  119. icon: 'none'
  120. });
  121. }
  122. }
  123. }).catch(err => {
  124. console.log('表单错误信息:', err);
  125. })
  126. },
  127. // 微信信任登录
  128. wxLogin() {
  129. const that = this;
  130. if (that.platform.uniPlatform == 'mp-weixin') {
  131. that.userLogin({
  132. openid: that.openid
  133. }, '2');
  134. } else if (that.platform.uniPlatform == 'app') {
  135. console.log('app');
  136. }
  137. },
  138. // 账号登录,无账号,注册账号
  139. async createUser(params) {
  140. const that = this;
  141. const openid = that.openid;
  142. if (that.platform.uniPlatform == 'mp-weixin') params.openid = openid;
  143. const res = await that.$api(`/user`, 'POST', {
  144. ...params
  145. })
  146. if (res.errcode == '0') {
  147. that.userLogin(params, '1')
  148. } else {
  149. uni.showToast({
  150. title: res.errmsg,
  151. });
  152. }
  153. },
  154. // 微信登录,无账号,微信注册账号
  155. async createwxUser() {
  156. const that = this;
  157. const res = await that.$api(`/user`, 'POST', {
  158. openid: that.openid
  159. })
  160. if (res.errcode == '0') {
  161. that.userLogin({
  162. openid: that.openid
  163. }, '2')
  164. } else {
  165. uni.showToast({
  166. title: res.errmsg,
  167. });
  168. }
  169. },
  170. // 用户登录
  171. async userLogin(params, type) {
  172. const that = this;
  173. let res;
  174. if (type == '1') {
  175. res = await that.$api(`/user/login`, 'POST', {
  176. ...params
  177. })
  178. } else if (type == '2') {
  179. res = await that.$api(`/user/wxLogin`, 'POST', {
  180. openid: params.openid
  181. })
  182. }
  183. if (res.errcode == '0') {
  184. uni.showToast({
  185. title: '登录成功',
  186. icon: 'success'
  187. });
  188. uni.setStorage({
  189. key: 'token',
  190. data: res.data,
  191. success: function() {
  192. uni.navigateBack({
  193. delta: 1
  194. })
  195. }
  196. });
  197. } else {
  198. if (res.errcode == '-5') {
  199. // 账号无,注册账号
  200. that.createwxUser(params);
  201. } else {
  202. uni.showToast({
  203. title: res.errmsg,
  204. icon: 'none'
  205. });
  206. }
  207. }
  208. },
  209. }
  210. }
  211. </script>
  212. <style lang="scss">
  213. .main {
  214. .two {
  215. padding: 2vw;
  216. .btn {
  217. text-align: center;
  218. margin: 0 0 2vw 0;
  219. button {
  220. margin: 0 2vw;
  221. background-color: var(--f35BColor);
  222. color: var(--fffColor);
  223. }
  224. }
  225. }
  226. }
  227. </style>