index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. <uni-easyinput type="text" v-model="form.code" placeholder="请输入验证码" />
  11. <button type="default" size="mini">验证码</button>
  12. </uni-forms-item>
  13. <view class="btn">
  14. <button type="default" size="mini" @click="toSubmit('form')">提交登录</button>
  15. </view>
  16. </uni-forms>
  17. </view>
  18. <view class="two" v-else-if="platform.uniPlatform=='mp-weixin'">
  19. <view class="icon">
  20. <text class="iconfont icon-weixin"></text>
  21. </view>
  22. <view class="btn">
  23. <button type="default" size="mini" @tap="wxLogin()" :open-type="openType" @getphonenumber="getUserPhone">微信信任登录</button>
  24. </view>
  25. </view>
  26. <view class="thr">
  27. <view class="thr_1">
  28. <checkbox-group @change="changeAgree">
  29. <label>
  30. <checkbox :checked="agree" />
  31. <text @tap="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  32. </label>
  33. </checkbox-group>
  34. </view>
  35. </view>
  36. </view>
  37. </mobile-frame>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. // 平台信息
  44. platform: {},
  45. //openid
  46. openid: '',
  47. // 隐私协议
  48. agree: false,
  49. form: {},
  50. rules: {
  51. phone: {
  52. rules: [{
  53. required: true,
  54. errorMessage: '请输入手机号',
  55. },
  56. {
  57. minLength: 11,
  58. maxLength: 11,
  59. errorMessage: '账号长度在{maxLength}个字符',
  60. }
  61. ]
  62. },
  63. code: {
  64. rules: [{
  65. required: true,
  66. errorMessage: '请输入验证码',
  67. }, ]
  68. },
  69. },
  70. // 微信登陆
  71. // 无账号
  72. is_user: false,
  73. openType: '',
  74. };
  75. },
  76. onShow: function() {
  77. const that = this;
  78. that.search();
  79. },
  80. methods: {
  81. // 同意隐私协议
  82. changeAgree() {
  83. const that = this;
  84. if (that.agree) that.$set(that, `agree`, false)
  85. else that.$set(that, `agree`, true);
  86. },
  87. // 查看隐私协议
  88. toAgree() {
  89. uni.navigateTo({
  90. url: `/pages/other/agree`
  91. })
  92. },
  93. search() {
  94. const that = this;
  95. uni.getStorage({
  96. key: 'system',
  97. success: function(res) {
  98. if (res.data) that.$set(that, `platform`, res.data);
  99. // 获取openid
  100. that.searchOpenid()
  101. }
  102. })
  103. },
  104. // 查询openid
  105. searchOpenid() {
  106. const that = this;
  107. if (that.platform.uniPlatform == 'mp-weixin') {
  108. uni.login({
  109. provider: 'weixin',
  110. success: async function(res) {
  111. const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
  112. js_code: res.code,
  113. config: 'pointApp'
  114. })
  115. if (aee.errcode == '0') {
  116. that.$set(that, `openid`, aee.data.openid);
  117. // 微信登录
  118. that.openidLogin(aee.data.openid);
  119. }
  120. },
  121. fail: function(err) {
  122. console.log(err);
  123. }
  124. })
  125. } else if (that.platform.uniPlatform == 'app') {
  126. console.log('to do');
  127. }
  128. },
  129. // 微信登录
  130. async openidLogin(e) {
  131. const that = this;
  132. let res = await that.$api(`/user/wxLogin`, 'POST', {
  133. openid: e
  134. })
  135. if (res.errcode == '0') {
  136. uni.setStorage({
  137. data: res.data,
  138. key: 'token',
  139. success: function() {
  140. uni.navigateBack({
  141. delta: 1
  142. })
  143. }
  144. })
  145. } else {
  146. if (res.errcode == '-5') {
  147. console.log('无账号');
  148. that.$set(that, `is_user`, false)
  149. }
  150. }
  151. },
  152. // 微信信任登录
  153. async wxLogin() {
  154. const that = this;
  155. if (that.agree) {
  156. uni.getUserProfile({
  157. desc: '用于展示',
  158. success: async function(res) {
  159. let parmas = {
  160. openid: that.openid,
  161. icon: [{
  162. url: res.userInfo.avatarUrl
  163. }],
  164. name: res.userInfo.nickName,
  165. }
  166. const arr = await that.$api(`/user`, 'POST', parmas);
  167. if (arr.errcode == '0') {
  168. const agg = await that.$api(`/user/wxLogin`, 'POST', {
  169. openid: that.openid
  170. })
  171. if (agg.errcode == '0') {
  172. uni.setStorage({
  173. data: agg.data,
  174. key: 'token',
  175. success: function() {
  176. uni.navigateBack({
  177. delta: 1
  178. })
  179. }
  180. })
  181. } else {
  182. uni.showToast({
  183. title: agg.errmsg,
  184. icon: 'none'
  185. });
  186. }
  187. } else {
  188. uni.showToast({
  189. title: arr.errmsg,
  190. icon: 'none'
  191. });
  192. }
  193. },
  194. fail: function(err) {
  195. console.log(err);
  196. }
  197. })
  198. } else {
  199. uni.showToast({
  200. title: '请阅读并同意用户协议和隐私政策',
  201. icon: 'none'
  202. })
  203. }
  204. },
  205. // 获取手机号
  206. getUserPhone(e) {
  207. console.log('1');
  208. const that = this;
  209. },
  210. }
  211. // },
  212. // // 微信信任登录
  213. // wxLogin() {
  214. // const that = this;
  215. // let agree = that.agree;
  216. // if (agree) {
  217. // uni.getUserProfile({
  218. // desc: '用户展示',
  219. // success: function(res) {
  220. // uni.login({
  221. // success: function(resl) {
  222. // },
  223. // fail: function(errl) {
  224. // console.log(errl);
  225. // }
  226. // })
  227. // },
  228. // fail: function(err) {
  229. // console.log(err);
  230. // }
  231. // })
  232. // // uni.login({
  233. // // success: async function(event) {
  234. // // const res = await that.$api(`/wechat/api/login/app`, 'GET', {
  235. // // js_code: event.code,
  236. // // config: 'pointApp'
  237. // // })
  238. // // if (res.errcode == '0') {
  239. // // // 用openid登录
  240. // // // that.openidLogin(res.data.openid);
  241. // // let arr = await that.$api(`/user/wxLogin`, 'POST', {
  242. // // openid: res.data.openid
  243. // // })
  244. // // if (arr.errcode == '0') {
  245. // // console.log('登录成功');
  246. // // } else {
  247. // // if (arr.errcode == '-5') {
  248. // // // 去注册
  249. // // // that.registerWxuser(e);
  250. // // that.$set(that, `errcode`, arr.errcode)
  251. // // } else {
  252. // // uni.showToast({
  253. // // title: arr.errmsg,
  254. // // icon: 'none'
  255. // // })
  256. // // }
  257. // // }
  258. // // } else {
  259. // // console.log(res);
  260. // // }
  261. // // }
  262. // // })
  263. // // console.log(that.errcode);
  264. // // console.log('1');
  265. // // if (that.errcode == '-5') {
  266. // // console.log('2');
  267. // // uni.getUserProfile({
  268. // // desc: '用户展示',
  269. // // success: function(res) {
  270. // // console.log(res);
  271. // // console.log(e);
  272. // // },
  273. // // fail: function(err) {
  274. // // console.log(err);
  275. // // }
  276. // // })
  277. // // }
  278. // } else {
  279. // uni.showToast({
  280. // title: '请阅读并同意用户协议和隐私政策',
  281. // icon: 'none'
  282. // })
  283. // }
  284. // },
  285. // // openid登录
  286. // async openidLogin(e) {
  287. // const that = this;
  288. // let res = await that.$api(`/user/wxLogin`, 'POST', {
  289. // openid: e
  290. // })
  291. // if (res.errcode == '0') {
  292. // console.log('登录成功');
  293. // } else {
  294. // if (res.errcode == '-5') {
  295. // // 去注册
  296. // that.registerWxuser(e);
  297. // } else {
  298. // uni.showToast({
  299. // title: res.errmsg,
  300. // icon: 'none'
  301. // })
  302. // }
  303. // }
  304. // },
  305. // // wx注册
  306. // registerWxuser(e) {
  307. // const that = this;
  308. // console.log(e);
  309. // uni.getUserProfile({
  310. // desc: '用户展示',
  311. // success: function(res) {
  312. // console.log(res);
  313. // console.log(e);
  314. // },
  315. // fail: function(err) {
  316. // console.log(err);
  317. // }
  318. // })
  319. // },
  320. // // 账号登录
  321. // toSubmit(ref) {
  322. // const that = this;
  323. // that.$refs[ref].validate().then(async params => {
  324. // let agree = that.agree;
  325. // if (agree) {
  326. // console.log(params);
  327. // } else {
  328. // uni.showToast({
  329. // title: '请阅读并同意用户协议和隐私政策',
  330. // icon: 'none'
  331. // })
  332. // }
  333. // }).catch(err => {
  334. // console.log(err);
  335. // })
  336. // },
  337. }
  338. </script>
  339. <style lang="scss">
  340. .main {
  341. .one {
  342. padding: 2vw;
  343. .btn {
  344. text-align: center;
  345. margin: 0 0 2vw 0;
  346. button {
  347. margin: 0 2vw;
  348. background-color: var(--f35BColor);
  349. color: var(--fffColor);
  350. }
  351. }
  352. }
  353. .two {
  354. text-align: center;
  355. margin: 20vw 0;
  356. .icon {
  357. margin: 0 0 5vw 0;
  358. text {
  359. font-size: 60px;
  360. }
  361. }
  362. .btn {
  363. button {
  364. background-color: var(--f35BColor);
  365. color: var(--fffColor);
  366. }
  367. }
  368. }
  369. .thr {
  370. .thr_1 {
  371. padding: 0 2vw;
  372. font-size: 14px;
  373. text-align: center;
  374. position: absolute;
  375. width: 96vw;
  376. bottom: 25vw;
  377. }
  378. }
  379. }
  380. </style>